| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (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 from urlparse import parse_qs | |
| 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() | |
|
kzar
2015/06/03 11:09:50
Nit: `from random import random` and then we can j
Sebastian Noack
2015/06/03 12:17:15
I slightly prefer to import just the module, looki
Felix Dahlke
2015/06/12 11:02:40
Same here, not incredibly consistent about it howe
kzar
2015/06/15 15:17:38
Acknowledged.
| |
| 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 _create_response(notifications, groups): | |
| 47 response = { | |
| 48 "version": time.strftime("%Y%m%d%H%M", time.gmtime()) | |
| 49 } | |
| 50 for group in groups: | |
| 51 group_id = group["id"] | |
| 52 variant = group["variant"] | |
| 53 response["version"] += "-%s/%s" % (group_id, variant) | |
| 54 if variant > 0: | |
| 55 notification = next(x for x in notifications if x["id"] == group_id) | |
| 56 notification = copy.deepcopy(notification) | |
| 57 notification.update(notification["variants"][variant - 1]) | |
| 58 for key_to_remove in ("sample", "variants"): | |
| 59 notification.pop(key_to_remove, None) | |
| 60 response["notifications"] = [notification] | |
|
kzar
2015/06/03 11:09:50
Won't you overwrite a previous notification in the
Felix Dahlke
2015/06/12 11:02:40
That's intentional - if the user is in a test, the
kzar
2015/06/15 15:17:37
Acknowledged.
| |
| 61 if "notifications" not in response: | |
| 62 response["notifications"] = [x for x in notifications | |
| 63 if "variants" not in x] | |
| 64 return json.dumps(response, ensure_ascii=False, indent=2, | |
| 65 separators=(",", ": "), sort_keys=True) | |
| 66 | |
| 67 @url_handler("/notification.json") | |
| 68 def notification(environ, start_response): | |
| 69 params = parse_qs(environ.get("QUERY_STRING", "")) | |
|
kzar
2015/06/03 11:09:50
Nit: IMHO it's easier to do `params = dict(parse_q
Sebastian Noack
2015/06/03 12:17:15
Given that only one param is retrieved here, I thi
Felix Dahlke
2015/06/12 11:02:40
No particularly strong opinion, but I also think t
kzar
2015/06/15 15:17:37
Acknowledged.
| |
| 70 version = params.get("lastVersion", [""])[0] | |
| 71 current_groups = dict(x.split("/") for x in version.split("-")[1:] | |
| 72 if x.count("/") == 1) | |
| 73 notifications = load_notifications() | |
| 74 groups = [] | |
|
kzar
2015/06/03 11:09:50
Wouldn't it be better to merge current_groups with
Felix Dahlke
2015/06/12 11:02:40
I can see why you don't like this, but I'm not sur
Sebastian Noack
2015/06/12 11:15:09
I'd vote for this one.
Felix Dahlke
2015/06/12 12:12:52
I prefer that one too, went for it. Let me know wh
kzar
2015/06/15 15:17:37
Sounds good to me
| |
| 75 for notification in notifications: | |
| 76 if "variants" not in notification: | |
| 77 continue | |
| 78 group_id = notification["id"] | |
| 79 if group_id in current_groups: | |
| 80 groups.append({"id": group_id, "variant": int(current_groups[group_id])}) | |
| 81 if not groups: | |
| 82 groups = _assign_groups(notifications) | |
| 83 response = _create_response(notifications, groups) | |
| 84 start_response("200 OK", | |
| 85 [("Content-Type", "application/json; charset=utf-8")]) | |
| 86 return response.encode("utf-8") | |
| OLD | NEW |