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
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)
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')
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)
def get_by_username(username) -> 'User': result = FirebaseService.find_equal(UserService._PATH, 'username', username) if result: return User.from_dict(result) else: return None
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
def edit(key, user: User) -> 'User': user_dict = user.to_dict() if 'password' in user_dict.keys(): del user_dict['password'] if 'id' in user_dict.keys(): del user_dict['id'] FirebaseService.edit(UserService._PATH + '/' + key, user_dict) return UserService.get_by_id(key)
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
def create(user: User) -> 'User': user.password = User.hash_password(user.password) return User.from_dict(FirebaseService.add(UserService._PATH, user.to_dict()))