Пример #1
0
    def get(self, id):
        ret = [user for user in User.select().where(User.id == id)]

        if (len(ret) == 0):
            ret = utils.make_api_response(
                400, {"message": "User not found."})  # User not found
        else:
            ret = utils.make_api_response(201, jsonify(ret[0].to_dict()))

        return ret
Пример #2
0
    def delete(self, id):
        # retrieve the selected user if existing
        user = User.delete().where(User.id == id).execute()

        # user not found
        if (user == 0):
            ret = utils.make_api_response(
                400, {"message": "User not found."})  # User not found
        else:
            ret = utils.make_api_response(200, {"message": "User deleted"})

        return ret
Пример #3
0
    def put(self, id):
        parser = creatBasicUserParser()
        user = User.select().where(User.id == id)

        if (len(list(user)) == 0):
            # we are creating a new resource with the given id
            args = parser.parse_args()
            args.join_date = datetime.datetime.now()
            args.enabled = False
        else:
            # we are changing the entire existing resource, so request more required fields
            parser.add_argument('join_date',
                                type=str,
                                location='json',
                                required=True)
            parser.add_argument('enabled',
                                type=bool,
                                location='json',
                                required=True)
            # this is already returning a 400 status code with message, if the are parsing problems
            args = parser.parse_args()

        try:
            user = User.create(**args)
            user.save()
            user.password = ""
            ret = user.to_dict()
            code = 201
        except peewee.IntegrityError as error:
            ret = {"message": str(error)}
            code = 400

        return utils.make_api_response(code, ret)
Пример #4
0
    def get(self):
        users = User.select()
        ret = []

        for user in users:
            ret.append(user.to_dict())

        return utils.make_api_response(200, ret)
Пример #5
0
    def post(self):
        # creating a new user
        args = self.parser.parse_args()
        args.join_date = datetime.datetime.now()
        args.enabled = False

        try:
            user = User.create(**args)
            user.save()
            # remove password to returned object
            user.password = ""
            ret = user.to_dict()
            code = 201
        except peewee.IntegrityError as error:
            ret = {"message": str(error)}
            code = 400

        return utils.make_api_response(code, ret)