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

Side by Side Diff: sitescripts/notifications/web/notification.py

Issue 29325919: Issue 2982 - Return all notifications that have title and message (Closed)
Patch Set: Created Sept. 4, 2015, 9:03 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 variant = group["variant"] 61 variant = group["variant"]
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):
72 return "title" in notification and "en-US" in notification["title"] and \
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
71 def _generate_version(groups): 75 def _generate_version(groups):
72 version = time.strftime("%Y%m%d%H%M", time.gmtime()) 76 version = time.strftime("%Y%m%d%H%M", time.gmtime())
73 for group in groups: 77 for group in groups:
74 version += "-%s/%s" % (group["id"], group["variant"]) 78 version += "-%s/%s" % (group["id"], group["variant"])
75 return version 79 return version
76 80
77 def _create_response(notifications, groups): 81 def _get_notifications_to_send(notifications, groups):
78 active_variant = _get_active_variant(notifications, groups) 82 active_variant = _get_active_variant(notifications, groups)
79 if active_variant: 83 if active_variant:
80 notifications = [active_variant] 84 return _can_be_shown(active_variant) and [active_variant] or []
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
81 else: 85
82 notifications = [x for x in notifications if "variants" not in x] 86 notifications_to_send = []
83 response = { 87 for notification in notifications:
88 if not _can_be_shown(notification):
89 continue
90 if "variants" in notification:
91 notification = copy.deepcopy(notification)
92 notification.pop("variants", None)
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)
94 return notifications_to_send
95
96 def _create_response(notifications, groups):
97 return {
84 "version": _generate_version(groups), 98 "version": _generate_version(groups),
85 "notifications": notifications 99 "notifications": _get_notifications_to_send(notifications, groups)
86 } 100 }
87 return response
88 101
89 @url_handler("/notification.json") 102 @url_handler("/notification.json")
90 def notification(environ, start_response): 103 def notification(environ, start_response):
91 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) 104 params = urlparse.parse_qs(environ.get("QUERY_STRING", ""))
92 version = params.get("lastVersion", [""])[0] 105 version = params.get("lastVersion", [""])[0]
93 notifications = load_notifications() 106 notifications = load_notifications()
94 groups = _determine_groups(version, notifications) 107 groups = _determine_groups(version, notifications)
95 if not groups: 108 if not groups:
96 groups = _assign_groups(notifications) 109 groups = _assign_groups(notifications)
97 response = _create_response(notifications, groups) 110 response = _create_response(notifications, groups)
98 response_headers = [("Content-Type", "application/json; charset=utf-8"), 111 response_headers = [("Content-Type", "application/json; charset=utf-8"),
99 ("ABP-Notification-Version", response["version"])] 112 ("ABP-Notification-Version", response["version"])]
100 response_body = json.dumps(response, ensure_ascii=False, indent=2, 113 response_body = json.dumps(response, ensure_ascii=False, indent=2,
101 separators=(",", ": "), 114 separators=(",", ": "),
102 sort_keys=True).encode("utf-8") 115 sort_keys=True).encode("utf-8")
103 start_response("200 OK", response_headers) 116 start_response("200 OK", response_headers)
104 return response_body 117 return response_body
OLDNEW

Powered by Google App Engine
This is Rietveld