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

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

Issue 29570562: Issue 5827 - Assign new groups even if prior groups are present (Closed)
Left Patch Set: Created Oct. 9, 2017, 8:56 a.m.
Right Patch Set: Removed return to make in-place editing more apparent Created Oct. 10, 2017, 2:46 p.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 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 22 matching lines...) Expand all
33 groups.append({'id': group_id, 'variant': int(version_groups[group_i d])}) 33 groups.append({'id': group_id, 'variant': int(version_groups[group_i d])})
34 return groups 34 return groups
35 35
36 36
37 def _assign_groups(groups, notifications): 37 def _assign_groups(groups, notifications):
38 selection = random.random() 38 selection = random.random()
39 start = 0 39 start = 0
40 for notification in notifications: 40 for notification in notifications:
41 if 'variants' not in notification: 41 if 'variants' not in notification:
42 continue 42 continue
43 if notification['id'] in [g['id'] for g in groups]: 43 if notification['id'] in [g['id'] for g in groups]:
Vasily Kuznetsov 2017/10/09 22:29:12 This should be a set and probably should be pre-ca
44 continue 44 continue
45 group = {'id': notification['id'], 'variant': 0} 45 group = {'id': notification['id'], 'variant': 0}
46 groups.append(group) 46 groups.append(group)
47 for i, variant in enumerate(notification['variants']): 47 for i, variant in enumerate(notification['variants']):
48 sample_size = variant['sample'] 48 sample_size = variant['sample']
49 end = start + sample_size 49 end = start + sample_size
50 selected = sample_size > 0 and start <= selection <= end 50 selected = sample_size > 0 and start <= selection <= end
51 start = end 51 start = end
52 if selected: 52 if selected:
53 group['variant'] = i + 1 53 group['variant'] = i + 1
54 break 54 break
55 return groups
56 55
57 56
58 def _get_active_variant(notifications, groups): 57 def _get_active_variant(notifications, groups):
59 for group in groups: 58 for group in groups:
60 group_id = group['id'] 59 group_id = group['id']
61 variant = group['variant'] 60 variant = group['variant']
62 if variant == 0: 61 if variant == 0:
63 continue 62 continue
64 notification = next((x for x in notifications if x['id'] == group_id), N one) 63 notification = next((x for x in notifications if x['id'] == group_id), N one)
65 if not notification: 64 if not notification:
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 } 104 }
106 105
107 106
108 @url_handler('/notification.json') 107 @url_handler('/notification.json')
109 def notification(environ, start_response): 108 def notification(environ, start_response):
110 params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) 109 params = urlparse.parse_qs(environ.get('QUERY_STRING', ''))
111 version = params.get('lastVersion', [''])[0] 110 version = params.get('lastVersion', [''])[0]
112 notifications = load_notifications() 111 notifications = load_notifications()
113 groups = _determine_groups(version, notifications) 112 groups = _determine_groups(version, notifications)
114 notifications = [x for x in notifications if not x.get('inactive', False)] 113 notifications = [x for x in notifications if not x.get('inactive', False)]
115 groups = _assign_groups(groups, notifications) 114 _assign_groups(groups, notifications)
wspee 2017/10/09 09:12:03 Ideally I would like to combine _determine_groups
Vasily Kuznetsov 2017/10/09 22:29:11 Since we remove inactive notifications in the midd
wspee 2017/10/10 09:03:53 Acknowledged.
116 response = _create_response(notifications, groups) 115 response = _create_response(notifications, groups)
117 response_headers = [('Content-Type', 'application/json; charset=utf-8'), 116 response_headers = [('Content-Type', 'application/json; charset=utf-8'),
118 ('ABP-Notification-Version', response['version'])] 117 ('ABP-Notification-Version', response['version'])]
119 response_body = json.dumps(response, ensure_ascii=False, indent=2, 118 response_body = json.dumps(response, ensure_ascii=False, indent=2,
120 separators=(',', ': '), 119 separators=(',', ': '),
121 sort_keys=True).encode('utf-8') 120 sort_keys=True).encode('utf-8')
122 start_response('200 OK', response_headers) 121 start_response('200 OK', response_headers)
123 return response_body 122 return response_body
LEFTRIGHT

Powered by Google App Engine
This is Rietveld