Index: sitescripts/notifications/web/notification.py |
=================================================================== |
--- a/sitescripts/notifications/web/notification.py |
+++ b/sitescripts/notifications/web/notification.py |
@@ -24,13 +24,13 @@ |
def _determine_groups(version, notifications): |
- version_groups = dict(x.split("/") for x in version.split("-")[1:] |
- if x.count("/") == 1) |
+ version_groups = dict(x.split('/') for x in version.split('-')[1:] |
+ if x.count('/') == 1) |
groups = [] |
for notification in notifications: |
- group_id = notification["id"] |
+ group_id = notification['id'] |
if group_id in version_groups: |
- groups.append({"id": group_id, "variant": int(version_groups[group_id])}) |
+ groups.append({'id': group_id, 'variant': int(version_groups[group_id])}) |
return groups |
@@ -39,45 +39,45 @@ |
selection = random.random() |
start = 0 |
for notification in notifications: |
- if "variants" not in notification: |
+ if 'variants' not in notification: |
continue |
- group = {"id": notification["id"], "variant": 0} |
+ group = {'id': notification['id'], 'variant': 0} |
groups.append(group) |
- for i, variant in enumerate(notification["variants"]): |
- sample_size = variant["sample"] |
+ for i, variant in enumerate(notification['variants']): |
+ sample_size = variant['sample'] |
end = start + sample_size |
selected = sample_size > 0 and start <= selection <= end |
start = end |
if selected: |
- group["variant"] = i + 1 |
+ group['variant'] = i + 1 |
break |
return groups |
def _get_active_variant(notifications, groups): |
for group in groups: |
- group_id = group["id"] |
- variant = group["variant"] |
+ group_id = group['id'] |
+ variant = group['variant'] |
if variant == 0: |
continue |
- notification = next((x for x in notifications if x["id"] == group_id), None) |
+ notification = next((x for x in notifications if x['id'] == group_id), None) |
if not notification: |
continue |
notification = copy.deepcopy(notification) |
- notification.update(notification["variants"][variant - 1]) |
- for key_to_remove in ("sample", "variants"): |
+ notification.update(notification['variants'][variant - 1]) |
+ for key_to_remove in ('sample', 'variants'): |
notification.pop(key_to_remove, None) |
return notification |
def _can_be_shown(notification): |
- return notification.get("title", None) and notification.get("message", None) |
+ return notification.get('title', None) and notification.get('message', None) |
def _generate_version(groups): |
- version = time.strftime("%Y%m%d%H%M", time.gmtime()) |
+ version = time.strftime('%Y%m%d%H%M', time.gmtime()) |
for group in groups: |
- version += "-%s/%s" % (group["id"], group["variant"]) |
+ version += '-%s/%s' % (group['id'], group['variant']) |
return version |
@@ -90,34 +90,34 @@ |
for notification in notifications: |
if not _can_be_shown(notification): |
continue |
- if "variants" in notification: |
+ if 'variants' in notification: |
notification = copy.deepcopy(notification) |
- del notification["variants"] |
+ del notification['variants'] |
notifications_to_send.append(notification) |
return notifications_to_send |
def _create_response(notifications, groups): |
return { |
- "version": _generate_version(groups), |
- "notifications": _get_notifications_to_send(notifications, groups) |
+ 'version': _generate_version(groups), |
+ 'notifications': _get_notifications_to_send(notifications, groups) |
} |
-@url_handler("/notification.json") |
+@url_handler('/notification.json') |
def notification(environ, start_response): |
- params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) |
- version = params.get("lastVersion", [""])[0] |
+ params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) |
+ version = params.get('lastVersion', [''])[0] |
notifications = load_notifications() |
groups = _determine_groups(version, notifications) |
- notifications = [x for x in notifications if not x.get("inactive", False)] |
+ notifications = [x for x in notifications if not x.get('inactive', False)] |
if not groups: |
groups = _assign_groups(notifications) |
response = _create_response(notifications, groups) |
- response_headers = [("Content-Type", "application/json; charset=utf-8"), |
- ("ABP-Notification-Version", response["version"])] |
+ response_headers = [('Content-Type', 'application/json; charset=utf-8'), |
+ ('ABP-Notification-Version', response['version'])] |
response_body = json.dumps(response, ensure_ascii=False, indent=2, |
- separators=(",", ": "), |
- sort_keys=True).encode("utf-8") |
- start_response("200 OK", response_headers) |
+ separators=(',', ': '), |
+ sort_keys=True).encode('utf-8') |
+ start_response('200 OK', response_headers) |
return response_body |