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

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

Issue 4883267715072000: Issue 2276 - Handle groups in notification.json requests (Closed)
Patch Set: Address comments Created June 12, 2015, 11 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
« no previous file with comments | « sitescripts/notifications/web/__init__.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 import copy
19 import json
20 import random
21 import time
22 import urlparse
23
24 from sitescripts.notifications.parser import load_notifications
25 from sitescripts.web import url_handler
26
27 def _assign_groups(notifications):
28 groups = []
29 selection = random.random()
30 start = 0
31 for notification in notifications:
32 if "variants" not in notification:
33 continue
34 group = {"id": notification["id"], "variant": 0}
35 groups.append(group)
36 for i, variant in enumerate(notification["variants"]):
37 sample_size = variant["sample"]
38 end = start + sample_size
39 selected = sample_size > 0 and start <= selection <= end
40 start = end
41 if selected:
42 group["variant"] = i + 1
43 break
44 return groups
45
46 def _get_active_variant(notifications, groups):
47 for group in groups:
48 group_id = group["id"]
49 variant = group["variant"]
50 if variant == 0:
51 continue
52 notification = next(x for x in notifications if x["id"] == group_id)
53 notification = copy.deepcopy(notification)
54 notification.update(notification["variants"][variant - 1])
55 for key_to_remove in ("sample", "variants"):
56 notification.pop(key_to_remove, None)
57 return notification
58
59 def _generate_version(groups):
60 version = time.strftime("%Y%m%d%H%M", time.gmtime())
61 for group in groups:
62 version += "-%s/%s" % (group["id"], group["variant"])
63 return version
64
65 def _create_response(notifications, groups):
66 active_variant = _get_active_variant(notifications, groups)
67 if active_variant:
68 notifications = [active_variant]
69 else:
70 notifications = [x for x in notifications if "variants" not in x]
71 response = {
72 "version": _generate_version(groups),
73 "notifications": notifications
74 }
75 return json.dumps(response, ensure_ascii=False, indent=2,
76 separators=(",", ": "), sort_keys=True)
77
78 @url_handler("/notification.json")
79 def notification(environ, start_response):
80 params = urlparse.parse_qs(environ.get("QUERY_STRING", ""))
81 version = params.get("lastVersion", [""])[0]
82 current_groups = dict(x.split("/") for x in version.split("-")[1:]
83 if x.count("/") == 1)
84 notifications = load_notifications()
85 groups = []
86 for notification in notifications:
87 if "variants" not in notification:
88 continue
89 group_id = notification["id"]
90 if group_id in current_groups:
91 groups.append({"id": group_id, "variant": int(current_groups[group_id])})
92 if not groups:
93 groups = _assign_groups(notifications)
94 response = _create_response(notifications, groups)
95 start_response("200 OK",
96 [("Content-Type", "application/json; charset=utf-8")])
97 return response.encode("utf-8")
OLDNEW
« no previous file with comments | « sitescripts/notifications/web/__init__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld