def put(self, _id=None): try: data = User.parser.parse_args() user = UserModel.find_by_id(_id) if user: hash_password = UserModel.hash_password(data['password']) user.roleId = data['roleId'] if (data['roleId'] is not None) else user.roleId user.email = data['email'] if (data['email'] is not None) else user.email user.password = hash_password if ( data['password'] is not None) else user.password user.fullName = data['fullName'] if ( data['fullName'] is not None) else user.fullName user.lastName = data['lastName'] if ( data['lastName'] is not None) else user.lastName user.phoneNumber = data['phoneNumber'] if ( data['phoneNumber'] is not None) else user.phoneNumber user.profilePicture = data['profilePicture'] if (data['profilePicture'] is not None) \ else user.profilePicture user.lastIPConnection = data['lastIPConnection'] if (data['lastIPConnection'] is not None) \ else user.lastIPConnection user.updatedOn = datetime.now().strftime('%Y-%m-%d %H:%M:%S') user.save_to_db() if user.lastIPConnection and DeviceModel.find_by_ip( user.lastIPConnection) is None: device = DeviceModel(user_id=user.id, ip=user.lastIPConnection, created_at=datetime.now().strftime( '%Y-%m-%d %H:%M:%S')) device.save_to_db() return BaseResponse.ok_response('User updated successfully.', user.json(is_long=True)) else: return BaseResponse.not_acceptable_response( 'User does not exists.', {}) except Exception as e: return BaseResponse.server_error_response(str(e))
def post(): try: data = User.parser.parse_args() if UserModel.find_by_email(data['email']): return BaseResponse.bad_request_response( 'This email already exists.', {}) elif UserModel.find_by_phone(data['phoneNumber']): return BaseResponse.bad_request_response( 'This phone number already exists.', {}) hash_password = UserModel.hash_password(data['password']) user = UserModel(role_id=data['roleId'], email=data['email'], password=hash_password, full_name=data['fullName'], last_name=data['lastName'], phone_number=data['phoneNumber'], profile_picture=data['profilePicture'], last_ip_connection=data['lastIPConnection'], created_at=None, updated_on=None, status=None) user.save_to_db() if user.lastIPConnection and DeviceModel.find_by_ip( user.lastIPConnection) is None: device = DeviceModel( user_id=user.id, ip=user.lastIPConnection, created_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) device.save_to_db() return BaseResponse.created_response('User created successfully.', user.json(is_long=True)) except Exception as e: return BaseResponse.server_error_response(str(e))
def post(): try: data = Login.parser.parse_args() if data['email']: user = UserModel.find_by_email(data['email']) else: user = UserModel.find_by_phone(data['phoneNumber']) pass if data['lastIPConnection'] and DeviceModel.find_by_ip(data['lastIPConnection']) is None \ and user is not None: user.lastIPConnection = data['lastIPConnection'] if (data['lastIPConnection'] is not None) \ else user.lastIPConnection user.save_to_db() device = DeviceModel( user_id=user.id, ip=data['lastIPConnection'], created_at=datetime.now().strftime('%Y-%m-%d %H:%M:%S')) device.save_to_db() if user and user.check_password(data['password']): access_token = create_access_token( identity=user.json(is_long=False), fresh=True) refresh_token = create_refresh_token(identity=user.json( is_long=False)) return BaseResponse.ok_response( 'Login successfully.', { 'accessToken': access_token, 'refreshToken': refresh_token, 'user': user.json(is_long=False) }) return BaseResponse.bad_request_response('Incorrect credentials.', {}) except Exception as e: return BaseResponse.server_error_response(str(e))