Example #1
0
def create_users_with_list_input():

    if connexion.request.is_json:

        users = [User.from_dict(d) for d in connexion.request.get_json()]

        result = []

        for u in users:
            try:
                SchemaValidator.validate_create_user_schema(
                    u.to_json(show_id=False, show_password=True))
                other_user = UserService.get_by_username(u.username)

                if other_user is None:

                    new_user = UserService.create(u)

                    if result is not None:
                        result.append(
                            ApiResponse(status.HTTP_200_OK, 'Ok', new_user.id))
                    else:
                        result.append(
                            ApiResponse(status.HTTP_400_BAD_REQUEST,
                                        'BAD_REQUEST', 'Something went wrong'))
                else:
                    result.append(
                        ApiResponse(status.HTTP_400_BAD_REQUEST, 'BAD_REQUEST',
                                    'Username already taken'))
            except jsonschema.exceptions.ValidationError as e:
                result.append(
                    ApiResponse(status.HTTP_400_BAD_REQUEST, 'BAD_REQUEST',
                                e.message))

        return ResponseHelper.response_200(result)
Example #2
0
def update_user(id):

    result = UserService.get_by_id(id)

    if result is not None:

        try:

            if connexion.request.is_json:

                SchemaValidator.validate_edit_user_schema(
                    connexion.request.get_json())

                body = User.from_dict(connexion.request.get_json())

                other_user = UserService.get_by_username(body.username)

                if other_user is None or other_user.id == id:

                    result = UserService.edit(id, body)

                    if result is not None:
                        return ResponseHelper.response_200(result.to_json())
                    else:
                        return ResponseHelper.response_404('User not found')
                else:
                    return ResponseHelper.response_400(
                        'Username already taken')

        except jsonschema.exceptions.ValidationError as e:
            return ResponseHelper.response_400(e.message)

    else:
        return ResponseHelper.response_404('User not found')
Example #3
0
def create_user():
    try:

        if connexion.request.is_json:

            SchemaValidator.validate_create_user_schema(
                connexion.request.get_json())

            body = User.from_dict(connexion.request.get_json())

            other_user = UserService.get_by_username(body.username)

            if other_user is None:

                result = UserService.create(body)

                if result is not None:
                    return ResponseHelper.response_201(result.to_json())
                else:
                    return ResponseHelper.response_400('Something went wrong')
            else:
                return ResponseHelper.response_400('Username already taken')

    except jsonschema.exceptions.ValidationError as e:
        return ResponseHelper.response_400(e.message)
Example #4
0
    def get_by_username(username) -> 'User':

        result = FirebaseService.find_equal(UserService._PATH, 'username', username)

        if result:
            return User.from_dict(result)
        else:
            return None
Example #5
0
    def get_by_id(key) -> 'User':

        result = FirebaseService.get(UserService._PATH + '/' + key)
        if result:
            result['id'] = key
            return User.from_dict(result)
        else:
            return None
Example #6
0
    def change_password(key, new_pass) -> 'User':

        result: User = FirebaseService.get(UserService._PATH + '/' + key)

        if result:
            result['password'] = User.hash_password(new_pass)
            FirebaseService.edit(UserService._PATH + '/' + key, result)
            return User.from_dict(result)
        else:
            return None
Example #7
0
    def get_all() -> ['User']:

        result = FirebaseService.get(UserService._PATH)

        if result is None:
            return []
        else:

            obj_result: [User] = []

            for key, val in result.items():
                val['id'] = key
                obj_result.append(User.from_dict(val))

            return obj_result
Example #8
0
    def create(user: User) -> 'User':

        user.password = User.hash_password(user.password)

        return User.from_dict(FirebaseService.add(UserService._PATH, user.to_dict()))