def post_user(request_args=None): try: user = User(**request_args.to_dict()) result = user.save() return result.to_json(), status.HTTP_200_OK except ValidationError as e: return str(e), status.HTTP_400_BAD_REQUEST
def change_password(): data = request.json try: valid_form = ChangePasswordForm(**data) except ValidationError as error: raise InvalidRequest().with_details({"errors": error.errors()}) user = User(**users_datastore.get_user_by_id(g.user["id"])) if user.password != valid_form.old_password: raise InvalidRequest().with_message("The old password is incorrect") user.password = valid_form.new_password users_datastore.update_user(user) return make_response()
def update_user(self, user: User): found_user = self.get_user_by_id(user.id) if found_user: found_user.update(**user.dict()) self.datastore_client.put(found_user) return True return "User not found"
async def create_user(request: Request) -> JSONResponse: try: payload = await request.json() user_data = asdict(User(**payload)) user_data["token"] = str(uuid4()) users.insert(user_data) except JSONDecodeError: raise HTTPException(status_code=400, detail=MALFORMED_JSON_MESSAGE) return JSONResponse(user_data, status_code=HTTP_201_CREATED)
def add_user(self, user: User): uniqueness_error = self.validate_user_uniqueness( id=user.id, user_name=user.user_name) if uniqueness_error: return uniqueness_error entity = datastore.Entity(key=datastore_client.key("user", user.id)) entity.update(**user.dict()) self.datastore_client.put(entity) return True
def register(): data = request.form image_file = list(request.files.values())[0] try: valid_form = UserRegistrationForm(**data, image=image_file) except ValidationError as error: raise InvalidRegistrationForm().with_details( {"errors": error.errors()}) image_url = image_storage.upload_image(image_file) new_user_result = users_datastore.add_user( User.parse_obj({ **valid_form.dict(), "image": image_url })) if new_user_result != True: raise new_user_result else: response = make_response() response.status_code = 201 return response
def get_users(request_args): result = User.objects(**request_args.to_dict()) return result.to_json()