Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/notifications/web/notification.py

Issue 29325919: Issue 2982 - Return all notifications that have title and message (Closed)
Left Patch Set: Created Sept. 4, 2015, 9:03 a.m.
Right Patch Set: Address comments Created Sept. 7, 2015, 7:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/notifications/test/notification.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 if variant == 0: 62 if variant == 0:
63 continue 63 continue
64 notification = next(x for x in notifications if x["id"] == group_id) 64 notification = next(x for x in notifications if x["id"] == group_id)
65 notification = copy.deepcopy(notification) 65 notification = copy.deepcopy(notification)
66 notification.update(notification["variants"][variant - 1]) 66 notification.update(notification["variants"][variant - 1])
67 for key_to_remove in ("sample", "variants"): 67 for key_to_remove in ("sample", "variants"):
68 notification.pop(key_to_remove, None) 68 notification.pop(key_to_remove, None)
69 return notification 69 return notification
70 70
71 def _can_be_shown(notification): 71 def _can_be_shown(notification):
72 return "title" in notification and "en-US" in notification["title"] and \ 72 return "title" in notification and "message" in notification
Sebastian Noack 2015/09/04 11:38:12 Please don't repeat yourself. How about about:
Sebastian Noack 2015/09/04 11:47:36 I just realized: What is if we show a notification
Felix Dahlke 2015/09/07 07:54:36 Well, we don't support that yet, so I can't really
73 "message" in notification and "en-US" in notification["message"]
74 73
75 def _generate_version(groups): 74 def _generate_version(groups):
76 version = time.strftime("%Y%m%d%H%M", time.gmtime()) 75 version = time.strftime("%Y%m%d%H%M", time.gmtime())
77 for group in groups: 76 for group in groups:
78 version += "-%s/%s" % (group["id"], group["variant"]) 77 version += "-%s/%s" % (group["id"], group["variant"])
79 return version 78 return version
80 79
81 def _get_notifications_to_send(notifications, groups): 80 def _get_notifications_to_send(notifications, groups):
82 active_variant = _get_active_variant(notifications, groups) 81 active_variant = _get_active_variant(notifications, groups)
83 if active_variant: 82 if active_variant:
84 return _can_be_shown(active_variant) and [active_variant] or [] 83 return [active_variant] if _can_be_shown(active_variant) else []
Wladimir Palant 2015/09/04 11:17:04 Yay on obfuscation! return [active_variant] if
Felix Dahlke 2015/09/07 07:54:36 Well, it was the old way to do it, but I really ha
85 84
86 notifications_to_send = [] 85 notifications_to_send = []
87 for notification in notifications: 86 for notification in notifications:
88 if not _can_be_shown(notification): 87 if not _can_be_shown(notification):
89 continue 88 continue
90 if "variants" in notification: 89 if "variants" in notification:
91 notification = copy.deepcopy(notification) 90 notification = copy.deepcopy(notification)
92 notification.pop("variants", None) 91 del notification["variants"]
Wladimir Palant 2015/09/04 11:17:04 Second parameter is unnecessary here, you already
Sebastian Noack 2015/09/04 11:38:12 If this is true, the del statement would be more a
Felix Dahlke 2015/09/07 07:54:36 Done.
93 notifications_to_send.append(notification) 92 notifications_to_send.append(notification)
94 return notifications_to_send 93 return notifications_to_send
95 94
96 def _create_response(notifications, groups): 95 def _create_response(notifications, groups):
97 return { 96 return {
98 "version": _generate_version(groups), 97 "version": _generate_version(groups),
99 "notifications": _get_notifications_to_send(notifications, groups) 98 "notifications": _get_notifications_to_send(notifications, groups)
100 } 99 }
101 100
102 @url_handler("/notification.json") 101 @url_handler("/notification.json")
103 def notification(environ, start_response): 102 def notification(environ, start_response):
104 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) 103 params = urlparse.parse_qs(environ.get("QUERY_STRING", ""))
105 version = params.get("lastVersion", [""])[0] 104 version = params.get("lastVersion", [""])[0]
106 notifications = load_notifications() 105 notifications = load_notifications()
107 groups = _determine_groups(version, notifications) 106 groups = _determine_groups(version, notifications)
108 if not groups: 107 if not groups:
109 groups = _assign_groups(notifications) 108 groups = _assign_groups(notifications)
110 response = _create_response(notifications, groups) 109 response = _create_response(notifications, groups)
111 response_headers = [("Content-Type", "application/json; charset=utf-8"), 110 response_headers = [("Content-Type", "application/json; charset=utf-8"),
112 ("ABP-Notification-Version", response["version"])] 111 ("ABP-Notification-Version", response["version"])]
113 response_body = json.dumps(response, ensure_ascii=False, indent=2, 112 response_body = json.dumps(response, ensure_ascii=False, indent=2,
114 separators=(",", ": "), 113 separators=(",", ": "),
115 sort_keys=True).encode("utf-8") 114 sort_keys=True).encode("utf-8")
116 start_response("200 OK", response_headers) 115 start_response("200 OK", response_headers)
117 return response_body 116 return response_body
LEFTRIGHT

Powered by Google App Engine
This is Rietveld