def add_subscription(cls, sub, device_key=""): current = Subscription.query(Subscription.model_key == sub.model_key, Subscription.model_type == sub.model_type, ancestor=ndb.Key(Account, sub.user_id)).get() if current is None: # Subscription doesn't exist, add it sub.put() # Send updates to user's other devices NotificationHelper.send_subscription_update( sub.user_id, device_key) return 200 else: if len( set(current.notification_types).symmetric_difference( set(sub.notification_types))) == 0: # Subscription already exists. Don't add it again return 304 else: # We're updating the settings current.notification_types = sub.notification_types current.put() # Send updates to user's other devices NotificationHelper.send_subscription_update( sub.user_id, device_key) return 200
def remove_subscription(cls, userId, modelKey, device_key=""): to_delete = Subscription.query(Subscription.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) # Send updates to user's other devices NotificationHelper.send_subscription_update(userId, device_key) return 200 else: # Subscription doesn't exist. Can't delete it return 404
def remove_subscription(cls, userId, modelKey, device_key=""): to_delete = Subscription.query(Subscription.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) if device_key: # Send updates to user's other devices NotificationHelper.send_subscription_update(userId, device_key) return 200 else: # Subscription doesn't exist. Can't delete it return 404
def add_subscription(cls, sub, device_key=""): current = Subscription.query(Subscription.model_key == sub.model_key, ancestor=ndb.Key(Account, sub.user_id)).get() if current is None: # Subscription doesn't exist, add it sub.put() # Send updates to user's other devices NotificationHelper.send_subscription_update(sub.user_id, device_key) return 200 else: if current.notification_types == sub.notification_types: # Subscription already exists. Don't add it again return 304 else: # We're updating the settings current.notification_types = sub.notification_types current.put() # Send updates to user's other devices NotificationHelper.send_subscription_update(sub.user_id, device_key) return 200