def get(self): """Method to handle user profile retrieval""" exact_user = UserModel.query.filter_by( id=get_jwt_identity().get('id')).first() if not exact_user: return error_response(message="No associated account " + "with this email. 😩", status=404) user_data = UserSchema(exclude=['password']).dump(exact_user) return success_response(data=user_data)
def post(self): diary_data = request.get_json(force=True) diary_data = DiarySchema(exclude=['id', 'created_on', 'updated_on']).\ load_object_into_schema(diary_data) diary_data['user_id'] = get_jwt_identity().get('id') new_diary = DiaryModel(**diary_data).save() return success_response(data=DiarySchema().dump(new_diary), status=201, message='Your diary was successfully created')
def delete(self, diary_id): validate_uuid(diary_id) user_diary = DiaryModel.query.filter_by( id=diary_id, deleted=False, user_id=get_jwt_identity().get('id')).first() if not user_diary: return error_response(message='Diary not found', status=404) user_diary.delete() return success_response(message='Diary deleted successfully')
def get(self, diary_id): validate_uuid(diary_id) user_diary = DiaryModel.query.filter_by( id=diary_id, deleted=False, user_id=get_jwt_identity().get('id')).first() if not user_diary: return error_response(message='Diary not found', status=404) return success_response(data=DiarySchema().dump(user_diary), status=200, message='Diary retrieved successfully')
def post(self): user_info = request.get_json(force=True) existing_user = UserModel.query.filter_by( email=user_info['email']).first() if existing_user: return error_response(message="This email address" + " has been taken", status=409) new_user = UserModel(**user_info) new_user.save() return success_response( message="Your account has been created successfully", data=UserSchema(exclude=['password']).dump(new_user), status=201)
def put(self): """Method to handle user data update""" user_update_data = request.get_json(force=True) user_data = UserSchema( dump_only=['id']).load_object_into_schema(user_update_data) if user_data.get('password'): hashed_password = BCrypt.generate_password_hash( user_data.get('password')).decode('utf-8') user_data.update(password=hashed_password) exact_user = UserModel.query.filter_by( id=get_jwt_identity().get('id')).first() updated_user_object = exact_user.update(**user_data) return success_response( data=UserSchema(exclude=['password']).dump( updated_user_object), message='Profile Updated Successfully')
def put(self, diary_id): validate_uuid(diary_id) diary_update_data = request.get_json(force=True) diary_data = DiarySchema(exclude=['id', 'created_on', 'updated_on']).\ load_object_into_schema( diary_update_data, partial=True) user_diary = DiaryModel.query.filter_by( id=diary_id, deleted=False, user_id=get_jwt_identity().get('id')).first() if not user_diary: return error_response(message='Diary not found', status=404) if user_diary.updated_on and (user_diary.updated_on - user_diary.created_on).days > 0: return error_response( message='Diary cannot be modified after 24hrs', status=400) user_diary = user_diary.update(**diary_data) return success_response(data=DiarySchema().dump(user_diary), status=200, message='Diary updated successfully')
def post(self): user_info = request.get_json(force=True) exact_user = UserModel.query.filter_by( email=user_info['email']).first() if not exact_user: return error_response(message="No associated account " + "with this email. 😩", status=404) is_valid_password = BCrypt.check_password_hash(exact_user.password, user_info['password']) if not is_valid_password: return error_response(message="Email or Password " + "is not correct 😕", status=401) user_schema = UserSchema(exclude=['password', 'push_sub']) user_info = user_schema.dump(exact_user) token = create_access_token(identity={"id": exact_user.id}) return success_response(message='Logged in successfuly', data=dict(token=token, user=user_info), status=200)
def get(self): user_diaries = DiaryModel.query.filter_by( user_id=get_jwt_identity().get('id'), deleted=False).all() return success_response(data=DiarySchema(many=True).dump(user_diaries), status=200, message='Diaries retrieved successfully')