def get(self, bundle_id):
     tokens = [
         token.id
         for token in Token.query.filter_by(bundle_id=bundle_id).all()
     ]
     log_event("GET /tokens/{bundle_id} -> 200 Success".format(
         bundle_id=bundle_id))
     return tokens, 200
 def get(self, user_id):
     user_record = User.query.filter_by(id=user_id).first()
     if user_record:
         tokens = [token.id for token in user_record.tokens]
         log_event(
             "GET /user/{user}/tokens -> 200 Success".format(user=user_id))
         return tokens, 200
     else:
         return "User {user_id} does not exist".format(user_id=user_id), 404
 def get(self, bundle_id):
     all_users = list(
         dict.fromkeys([
             token.user_id
             for token in Token.query.filter_by(bundle_id=bundle_id)
             if token.bundle_id == bundle_id
         ]))
     log_event("GET /users/{bundle_id} -> 200 Success".format(
         bundle_id=bundle_id))
     return all_users, 200
 def delete(self, token_id):
     if Token.query.filter_by(id=token_id).first():
         Token.query.filter_by(id=token_id).delete()
         db_client.session.commit()
         log_event("DELETE /token/{token_id} -> 200 Success".format(
             token_id=token_id))
         return "{token_id} has been deleted".format(token_id=token_id), 200
     else:
         log_event("DELETE /token/{token_id} -> 404 NotFound".format(
             token_id=token_id))
         return "{token_id} not found".format(token_id=token_id), 404
 def get(self, token_id):
     token = Token.query.filter_by(id=token_id).first()
     if token:
         log_event("GET /token/{token_id} -> 200 Success".format(
             token_id=token_id))
         return {
             "id": token.id,
             "bundle-id": token.bundle_id,
             "user-id": token.user_id
         }, 200
     else:
         log_event("GET /token/{token_id} -> 404 NotFound".format(
             token_id=token_id))
         return "{token_id} not found".format(token_id=token_id), 404
    def delete(self, user_id):
        """
		Deletes given User and associated DeviceTokens
		"""
        user_record = User.query.filter_by(id=user_id).first()
        if user_record:
            for token in Token.query.filter_by(user_id=user_record.id).all():
                db_client.session.delete(token)
            db_client.session.delete(user_record)
            db_client.session.commit()
            log_event("DELETE /user/{request} -> 200 Success".format(
                request=user_id))
            return "{user} has been deleted".format(user=user_id), 200
        else:
            log_event("DELETE /user/{request} -> 404 NotFound".format(
                request=user_id))
            return "{user} not found".format(user=user_id), 404
    def get(self, user_id):
        """
		Gets info for given UserID
		"""
        user_record = User.query.filter_by(id=user_id).first()
        if user_record:
            user = {
                "device-tokens": [token.id for token in user_record.tokens],
                "user-id": user_record.id
            }
            log_event("GET /user/{request} -> 200 Success {result}".format(
                request=user_id, result=user))
            return user, 200
        else:
            log_event(
                "GET /user/{request} -> 404 NotFound".format(request=user_id))
            return "User {user_id} not found".format(user_id=user_id), 404
    def post(self, user_id):
        """
		Creates given UserID
		"""
        parser = reqparse.RequestParser()
        parser.add_argument("bundle-id", type=str)
        parser.add_argument("device-token", type=str)
        args = parser.parse_args()

        if valid_data(args):
            user_record = User.query.filter_by(id=user_id).first()
            if user_record:
                log_event("POST /user/{request} -> 409 AlreadyExists".format(
                    request=user_id))
                return "User {user_id} already exists".format(
                    user_id=user_id), 409
            else:
                db_client.session.add(User(id=user_id))
                db_client.session.add(
                    Token(id=args["device-token"],
                          user_id=user_id,
                          bundle_id=args["bundle-id"]))
                db_client.session.commit()
                log_event(
                    "POST /user/{request} -> 201 Created {result}".format(
                        request=user_id, result=args))
                return "Created user {user_id}".format(user_id=user_id), 201
        else:
            log_event(
                "POST /user/{request} -> 400 Bad Request {result}".format(
                    request=user_id, result=args))
            return args, 400
    def put(self, user_id):
        """
		Creates or completely replaces given UserID
		"""
        parser = reqparse.RequestParser()
        parser.add_argument("bundle-id", type=str)
        parser.add_argument("device-token", type=str)
        args = parser.parse_args()

        if valid_data(args):
            print(args)
            user_record = User.query.filter_by(id=user_id)
            if user_record.first():
                if args["device-token"] not in [
                        token.id for token in user_record.first().tokens
                ]:
                    db_client.session.add(
                        Token(id=args["device-token"],
                              bundle_id=args["bundle-id"],
                              user_id=user_id))
                else:
                    Token.query.filter_by(id=args["device-token"]).update(
                        dict(id=args["device-token"],
                             user_id=user_id,
                             bundle_id=args["bundle-id"]))
                db_client.session.commit()
                log_event("PUT /user/{request} -> 200 Success".format(
                    request=user_id))
                return "User {user_id} updated".format(user_id=user_id), 200
            else:
                db_client.session.add(User(id=user_id))
                db_client.session.add(
                    Token(id=args["device-token"],
                          user_id=user_id,
                          bundle_id=args["bundle-id"]))
                db_client.session.commit()
                log_event("PUT /user/{request} -> 201 Created".format(
                    request=user_id))
                return "Created user {user_id}".format(user_id=user_id), 201
        else:
            log_event("PUT /user/{request} -> 400 Bad Request {result}".format(
                request=user_id, result=args))
            return args, 400
 def get(self):
     tokens = [token.id for token in Token.query.all()]
     log_event("GET /tokens -> 200 Success")
     return tokens, 200
 def get(self):
     all_users = [entry.id for entry in User.query.all()]
     log_event("GET /users -> 200 Success")
     return all_users, 200