def put(self): user = current_user() data, _ = UserSchema().load(request.get_json(), instance=User.lookup(user.email), partial=True) db.session.commit() return {'status': 'success', 'msg': f'User {id} successfully updated'}
class ReviewSchema(ma.ModelSchema): class Meta: model = Review strict = True user = ma.Function(lambda obj: UserSchema(exclude=['password']).dump(User.query.get(obj.reviewer_id)).data)
def get(self): user = current_user() return { 'status': 'success', 'data': UserSchema(exclude=(['password'])).dump(user).data }
def post(self): try: data, _ = UserSchema().load(request.get_json()) id = User.add(data) except ValidationError as err: return {'status': 'error', 'errors': err.messages['_schema']}, 403 return {'status': 'success', 'msg': f'successfully created user {id}'}
def get(self, id): user = User.query.get(id) if user is None: return {'status': 'error', 'error': 'User not found'}, 404 return { 'status': 'success', 'data': UserSchema(exclude=['password']).dump(user).data }
def post(self): req = request.get_json() email = req.get("email", None) password = req.get("password", None) user = guard.authenticate(email, password) # use this when refreshing tokens automatically is added # return {'access_token': guard.encode_jwt_token(user)} return { 'access_token': guard.encode_eternal_jwt_token(user), 'user': UserSchema(exclude=['password']).dump(user).data }
def get(self): page = request.args.get('page', 1, type=int) users = User.query.paginate(page, int(os.getenv('PER_PAGE', 10)), False).items if not users: return {'status': 'error', 'error': 'No users found'}, 404 return { 'status': 'success', 'data': UserSchema(exclude=['password']).dump(users, many=True).data }
class ListingSchema(ma.ModelSchema): class Meta: model = Listing strict = True images = ma.Nested(ListingImageSchema, many=True) rooms = ma.Nested(RoomSchema, many=True) address = ma.Nested(AddressSchema) preferences = ma.Nested(PreferenceSchema, many=True) restrictions = ma.Nested(RestrictionSchema, many=True) user = ma.Function(lambda obj: UserSchema(exclude=['password']).dump( User.query.get(obj.user_id)).data) @validates_schema def validate_listing(self, data, **kwargs): errors = {} if 'name' in data: if data['name'] == "": errors['name'] = "Please provide a title" if 'num_housemates' in data: if data['num_housemates'] < 0: errors[ 'num_housemates'] = "Please provide the number of housemates" if 'num_vacancies' in data: if data['num_vacancies'] < 0: errors[ 'num_vacancies'] = "Please provide the number of vacancies" if 'num_bedrooms' in data: if data['num_bedrooms'] < 0: errors[ 'num_bedrooms'] = "Please provide the number of bedrooms" if 'landsize' in data: if data['landsize'] < 0: errors['landsize'] = "Please provide the landsize" if errors: raise ValidationError(errors)