Commit 1b3f5d9d authored by hupantingxue's avatar hupantingxue

Add audience payload;

parent 5fdb139b
...@@ -17,6 +17,8 @@ from .push import ( ...@@ -17,6 +17,8 @@ from .push import (
android, android,
winphone, winphone,
platform, platform,
audience,
options,
message, message,
) )
...@@ -36,6 +38,8 @@ __all__ = [ ...@@ -36,6 +38,8 @@ __all__ = [
winphone, winphone,
message, message,
platform, platform,
audience,
options,
] ]
# Silence urllib3 INFO logging by default # Silence urllib3 INFO logging by default
......
...@@ -14,6 +14,8 @@ from .payload import ( ...@@ -14,6 +14,8 @@ from .payload import (
platform, platform,
notification, notification,
message, message,
audience,
options,
) )
# Common selector for audience & platform # Common selector for audience & platform
...@@ -24,7 +26,6 @@ all_ = "all" ...@@ -24,7 +26,6 @@ all_ = "all"
Used in both ``audience`` and ``platform``. Used in both ``audience`` and ``platform``.
""" """
__all__ = [ __all__ = [
all_, all_,
Push, Push,
...@@ -35,4 +36,6 @@ __all__ = [ ...@@ -35,4 +36,6 @@ __all__ = [
notification, notification,
message, message,
platform, platform,
audience,
options,
] ]
...@@ -2,32 +2,22 @@ import re ...@@ -2,32 +2,22 @@ import re
# Value selectors; aliases, tags, etc. # Value selectors; aliases, tags, etc.
def tag(tag): def tag(*tags):
"""Select a single tag.""" """Select a single tag."""
return {"tag": tag} vtag = [t for t in tags]
return {"tag": vtag}
def tag_and(tag_and): def tag_and(*tag_ands):
"""Select a single tag_and.""" """Select a single tag_and."""
return {"tag_and": tag_and} vtag_and = [t for t in tag_ands]
return {"tag_and": vtag_and}
def alias(alias): def alias(*alias):
"""Select a single alias.""" """Select a single alias."""
return {"alias": alias} valias = [t for t in alias]
return {"alias": valias}
def registration_id(registration_id): def registration_id(*reg_ids):
"""Select a (list of) registration_id(s).""" """Select a (list of) registration_id(s)."""
return {"registration_id": registration_id} vregistration_id = [t for t in reg_ids]
return {"registration_id": vregistration_id}
def audience(*children):
"""Select audience that match all of the given selectors.
>>> audience(tag('sports'), tag_and('business'))
{'audience': {'tag':'sports', 'tag_and':'business'}}
"""
if len(types) == 1 and types[0] == 'all':
return {'audience':'all'}
for t in types:
if t not in ('tag', 'tag_and', 'alias', 'registration_id'):
raise ValueError("Invalid platform '%s'" % t)
return [t for t in types]
...@@ -84,7 +84,7 @@ def android(alert, title=None, builder_id=None, extras=None): ...@@ -84,7 +84,7 @@ def android(alert, title=None, builder_id=None, extras=None):
payload['title'] = title payload['title'] = title
if builder_id is not None: if builder_id is not None:
payload['builder_id'] = builder_id payload['builder_id'] = builder_id
if extra is not None: if extras is not None:
payload['extras'] = extras payload['extras'] = extras
return payload return payload
...@@ -149,3 +149,20 @@ def platform(*types): ...@@ -149,3 +149,20 @@ def platform(*types):
def options(options): def options(options):
"""Create options object.""" """Create options object."""
return {"options": options} return {"options": options}
def audience(*types):
"""Select audience that match all of the given selectors.
>>> audience(tag('sports'), tag_and('business'))
{'audience': {'tag':['sports'], 'tag_and':['business']}}
"""
if 1 == len(types) and 'all' == types[0]:
return "all"
audience = {}
for t in types:
for key in t:
if key not in ('tag', 'tag_and', 'alias', 'registration_id'):
raise ValueError("Invalid audience '%s'" % t)
audience[key] = t[key]
return audience
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment