def post(self): data = User.parser.parse_args() if UserModel.find_by_username(data['username']): return {'message': 'User with this email already exists.'}, 400 user = UserModel(data['first_name'], data['last_name'], data['username'], data['email'], data['password'], data['profile_picture']) try: user.save() except (): return {'message': 'An error occurred while saving the user.'}, 500 return user.json()
def get(self, car_id): car = CarModel.find_by_id(car_id) if car: user = UserModel.find_by_id(car.user_id) return {'car': car.json(), 'owner': user.json()} return {'message': 'Car not found'}, 404
def put(self, username): data = User.parser.parse_args() user = UserModel.find_by_username(username) if user is None: user = UserModel(data['first_name'], data['last_name'], data['username'], data['email'], data['password'], data['profile_picture']) else: user.first_name = data['first_name'] user.last_name = data['last_name'] user.email = data['email'] user.username = data['username'] user.password = encrypt(data['password']) user.profile_picture = data['profile_picture'] user.save() return user.json()
def delete(self, username): user = UserModel.find_by_username(username) if user: user.delete() return {'message': 'User has been deleted.'}
def get(self, username): user = UserModel.find_by_username(username) if user: return user.json() return {'message': 'User not found'}, 404
def authenticate(username, password): user = UserModel.find_by_username(username) if user and compare(user.password, password): return user return None
def identity(payload): user_id = payload['identity'] return UserModel.find_by_id(user_id)