本文整理汇总了Python中helpers.push_helper.PushHelper类的典型用法代码示例。如果您正苦于以下问题:Python PushHelper类的具体用法?Python PushHelper怎么用?Python PushHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PushHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_subscription
def add_subscription(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to add subscription")
userId = PushHelper.user_email_to_id(current_user.email())
modelKey = request.model_key
sub = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).get()
if sub is None:
# Subscription doesn't exist, add it
Subscription( user_id = userId, model_key = modelKey, notification_types = PushHelper.notification_enums_from_string(request.notifications)).put()
if request.device_key:
# Send updates to user's other devices
GCMMessageHelper.send_subscription_update(userId, request.device_key)
return BaseResponse(code=200, message="Subscription added")
else:
if sub.notification_types == PushHelper.notification_enums_from_string(request.notifications):
# Subscription already exists. Don't add it again
return BaseResponse(code=304, message="Subscription already exists")
else:
# We're updating the settings
sub.notification_types = PushHelper.notification_enums_from_string(request.notifications)
sub.put()
if request.device_key:
# Send updates to user's other devices
GCMMessageHelper.send_subscription_update(userId, request.device_key)
return BaseResponse(code=200, message="Subscription updated")
示例2: send_match_score_update
def send_match_score_update(cls, match):
users = PushHelper.get_users_subscribed_to_match(match, NotificationType.MATCH_SCORE)
gcm_keys = PushHelper.get_client_ids_for_users(ClientType.names[ClientType.OS_ANDROID], users)
if len(gcm_keys) == 0:
return
notification = MatchScoreNotification(match)
message = notification.build(ClientType.OS_ANDROID, {ClientType.OS_ANDROID: gcm_keys})
gcm_connection = GCMConnection()
gcm_connection.notify_device(message)
示例3: list_subscriptions
def list_subscriptions(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return SubscriptionCollection(subscriptions=[])
user_id = PushHelper.user_email_to_id(current_user.email())
subscriptions = Subscription.query(ancestor=ndb.Key(Account, user_id)).fetch()
output = []
for subscription in subscriptions:
output.append(SubscriptionMessage(
model_key=subscription.model_key,
notifications=PushHelper.notification_string_from_enums(subscription.notification_types),
model_type=subscription.model_type))
return SubscriptionCollection(subscriptions=output)
示例4: send_match_video
def send_match_video(cls, match):
"""
Sends match_video and event_match_video notifications
If the match is current, MatchVideoNotification is sent.
Otherwise, EventMatchVideoNotification is sent
"""
match_users = set(PushHelper.get_users_subscribed_to_match(match, NotificationType.MATCH_VIDEO))
event_users = set(PushHelper.get_users_subscribed_to_event(match.event.get(), NotificationType.MATCH_VIDEO))
users = match_users.union(event_users)
if match.within_seconds(60*10):
user_keys = PushHelper.get_client_ids_for_users(users)
MatchVideoNotification(match).send(user_keys)
else:
user_keys = PushHelper.get_client_ids_for_users(users)
EventMatchVideoNotification(match).send(user_keys)
示例5: suggest_team_media
def suggest_team_media(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to make suggestions")
user_id = PushHelper.user_email_to_id(current_user.email())
# For now, only allow team media suggestions
if request.reference_type != "team":
# Trying to suggest a media for an invalid model type
return BaseResponse(code=400, message="Bad model type")
# Need to split deletehash out into its own private dict. Don't want that to be exposed via API...
private_details_json = None
if request.details_json:
incoming_details = json.loads(request.details_json)
private_details = None
if 'deletehash' in incoming_details:
private_details = {'deletehash': incoming_details.pop('deletehash')}
private_details_json = json.dumps(private_details) if private_details else None
status = SuggestionCreator.createTeamMediaSuggestion(
author_account_key=ndb.Key(Account, user_id),
media_url=request.media_url,
team_key=request.reference_key,
year_str=str(request.year),
private_details_json=private_details_json)
if status != 'bad_url':
if status == 'success':
return BaseResponse(code=200, message="Suggestion added")
else:
return BaseResponse(code=304, message="Suggestion already exists")
else:
return BaseResponse(code=400, message="Bad suggestion url")
示例6: ping_client
def ping_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to ping client")
user_id = PushHelper.user_email_to_id(current_user.email())
gcm_id = request.mobile_id
# Find a Client for the current user with the passed GCM ID
clients = MobileClient.query(MobileClient.messaging_id == gcm_id, ancestor=ndb.Key(Account, user_id)).fetch(1)
if len(clients) == 0:
# No Client for user with that push token - bailing
return BaseResponse(code=404, message="Invalid push token for user")
else:
client = clients[0]
response = NotificationHelper.send_ping(client)
# If we got a response from the send_ping method, it was sent via TBANS
# We'll bubble up any errors we got back
if response:
if response.code == 200:
return BaseResponse(code=200, message="Ping sent")
else:
return BaseResponse(code=response.code, message="Error pinging client - {}".format(response.message))
else:
return BaseResponse(code=200, message="Ping sent")
示例7: send_upcoming_match_notification
def send_upcoming_match_notification(cls, match, event):
users = PushHelper.get_users_subscribed_to_match(match, NotificationType.UPCOMING_MATCH)
keys = PushHelper.get_client_ids_for_users(users)
if match.set_number == 1 and match.match_number == 1:
# First match of a new type, send level starting notifications
start_users = PushHelper.get_users_subscribed_to_match(match, NotificationType.LEVEL_STARTING)
start_keys = PushHelper.get_client_ids_for_users(start_users)
level_start = CompLevelStartingNotification(match, event)
level_start.send(start_keys)
# Send upcoming match notification
notification = UpcomingMatchNotification(match, event)
notification.send(keys)
match.push_sent = True # Make sure we don't send updates for this match again
match.put()
示例8: list_favorites
def list_favorites(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return FavoriteCollection(favorites=[])
user_id = PushHelper.user_email_to_id(current_user.email())
favorites = Favorite.query(ancestor=ndb.Key(Account, user_id)).fetch()
output = []
for favorite in favorites:
output.append(FavoriteMessage(model_key=favorite.model_key, model_type=favorite.model_type))
return FavoriteCollection(favorites=output)
示例9: get
def get(self, type):
self._require_registration('/account/')
user_id = self.user_bundle.account.key.id()
logging.info("Sending for {}".format(type))
try:
type = int(type)
except ValueError:
# Not passed a valid int, just stop here
logging.info("Invalid number passed")
self.redirect('/apidocs/webhooks')
return
event = Event.get_by_id('2014necmp')
match = Match.get_by_id('2014necmp_f1m1')
district = District.get_by_id('2014ne')
if type == NotificationType.UPCOMING_MATCH:
notification = UpcomingMatchNotification(match, event)
elif type == NotificationType.MATCH_SCORE:
notification = MatchScoreNotification(match)
elif type == NotificationType.LEVEL_STARTING:
notification = CompLevelStartingNotification(match, event)
elif type == NotificationType.ALLIANCE_SELECTION:
notification = AllianceSelectionNotification(event)
elif type == NotificationType.AWARDS:
notification = AwardsUpdatedNotification(event)
elif type == NotificationType.MEDIA_POSTED:
# Not implemented yet
pass
elif type == NotificationType.DISTRICT_POINTS_UPDATED:
notification = DistrictPointsUpdatedNotification(district)
elif type == NotificationType.SCHEDULE_UPDATED:
notification = ScheduleUpdatedNotification(event, match)
elif type == NotificationType.FINAL_RESULTS:
# Not implemented yet
pass
elif type == NotificationType.MATCH_VIDEO:
notification = MatchVideoNotification(match)
elif type == NotificationType.EVENT_MATCH_VIDEO:
notification = EventMatchVideoNotification(match)
else:
# Not passed a valid int, return
self.redirect('/apidocs/webhooks')
return
keys = PushHelper.get_client_ids_for_users([user_id])
logging.info("Keys: {}".format(keys))
if notification:
# This page should not push notifications to the firebase queue
# Nor should its notifications be tracked in analytics
notification.send(keys, push_firebase=False, track_call=False)
self.redirect('/apidocs/webhooks')
示例10: send_subscription_update
def send_subscription_update(cls, user_id, sending_device_key):
clients = PushHelper.get_client_ids_for_users("android", [user_id])
if sending_device_key in clients:
clients.remove(sending_device_key)
if len(clients) == 0:
return
notification = UpdateSubscriptionsNotification(user_id)
message = notification.build(ClientType.OS_ANDROID, {ClientType.OS_ANDROID: clients})
gcm_connection = GCMConnection()
gcm_connection.notify_device(message)
示例11: list_subscriptions
def list_subscriptions(self, request):
user_id = get_current_user_id(self.headers)
if user_id is None:
return SubscriptionCollection(subscriptions=[])
subscriptions = Subscription.query(ancestor=ndb.Key(Account, user_id)).fetch()
output = []
for subscription in subscriptions:
output.append(SubscriptionMessage(
model_key=subscription.model_key,
notifications=PushHelper.notification_string_from_enums(subscription.notification_types),
model_type=subscription.model_type))
return SubscriptionCollection(subscriptions=output)
示例12: unregister_client
def unregister_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to unregister")
userID = PushHelper.user_email_to_id(current_user.email())
gcmId = request.mobile_id
query = MobileClient.query(MobileClient.messaging_id == gcmId, MobileClient.user_id == userID).fetch(keys_only=True)
if len(query) == 0:
# Record doesn't exist, so we can't remove it
return BaseResponse(code=404, message="User doesn't exist. Can't remove it")
else:
ndb.delete_multi(query)
return BaseResponse(code=200, message="User deleted")
示例13: get_current_user_id
def get_current_user_id(headers):
auth = headers.get('Authorization')
if not auth:
return None
id_token = auth.split(' ').pop()
try:
claims = google.oauth2.id_token.verify_firebase_token(id_token, HTTP_REQUEST)
except ValueError:
return None
if not claims:
return None
else:
return PushHelper.user_email_to_id(claims['email'])
示例14: register_client
def register_client(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to register")
userId = PushHelper.user_email_to_id(current_user.email())
gcmId = request.mobile_id
os = ClientType.enums[request.operating_system]
if MobileClient.query( MobileClient.messaging_id==gcmId ).count() == 0:
# Record doesn't exist yet, so add it
MobileClient( messaging_id = gcmId,
user_id = userId,
client_type = os ).put()
return BaseResponse(code=200, message="Registration successful")
else:
# Record already exists, don't bother updating it again
return BaseResponse(code=304, message="Client already exists")
示例15: remove_subscription
def remove_subscription(self, request):
current_user = endpoints.get_current_user()
if current_user is None:
return BaseResponse(code=401, message="Unauthorized to remove subscription")
userId = PushHelper.user_email_to_id(current_user.email())
modelKey = request.model_key
to_delete = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).fetch(keys_only=True)
if len(to_delete) > 0:
ndb.delete_multi(to_delete)
if request.device_key:
# Send updates to user's other devices
GCMMessageHelper.send_subscription_update(userId, request.device_key)
return BaseResponse(code=200, message="Subscriptions deleted")
else:
# Subscription doesn't exist. Can't delete it
return BaseResponse(code=404, message="Subscription not found")