def post(self): # get the post data post_data = request.json try: user = UserDao.get_by_email(post_data['email']) if user and user.check_password(post_data['password']): auth_token = TokenUtil.encode_token(user.id) if auth_token: authdata = { 'firstname': user.firstname, 'lastname': user.lastname, 'email': user.email, 'is_admin': 0, 'id': user.id, 'create_date': user.create_date, 'updated_date': user.updated_date, 'last_login_date': user.last_login_date, 'token': auth_token.decode() } result = { 'status': 'success', 'message': 'Successful user login.' } response_object = { 'authdata': authdata, 'result': result, } UserDao.set_last_login_date(user.id) return response_object, 200 else: result = { 'status': 'fail', 'message': 'Email or password does not match.' } response_object = { 'authdata': None, 'result': result } return response_object, 401 except ApplicationException as e: result = { 'status': 'fail', 'message': 'Internal Server Error' } response_object = { 'authdata': None, 'result': result } return response_object, 500
def post(self): """Insert a user""" try: user_data = request.json if UserDao.get_by_email(user_data['email']) is not None: response_object = { 'status': 'fail', 'message': 'User cannot be created.' } return response_object, 409 if UserDao.get_by_email(user_data['email']) is not None: response_object = { 'status': 'fail', 'message': 'User cannot be created.' } return response_object, 409 new_user = UserModel() new_user.firstname = user_data['firstname'] new_user.lastname = user_data['lastname'] new_user.email = user_data['email'] new_user.set_password(user_data['password']) new_user = UserDao.save_user(new_user) response_object = { 'status': 'success', 'message': 'User successfully created.' } return response_object, 201 except Exception as e: response_object = { 'status': 'error', 'message': 'Internal Server Error' } return response_object, 500