示例#1
0
    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
0
    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")