Example #1
0
    def post(self):
        """Book specific hotels"""
        try:
            params = self.parser.parse_args()

            offerId = params['offerId']
            guests: list = params['guests']
            payments = params['payments']
            booking_responce = amadeus.booking.hotel_bookings.post(offerId, guests, payments)
            booking_dict = booking_responce.data[0]
            booking: BookingModel = BookingModel()
            booking.booking_id = booking_dict['id']
            booking.providerConfirmationId = booking_dict['providerConfirmationId']
            booking.user_id = g.user_id
            booking.hotel_name = params['hotel_name']
            booking.checking_date = params['checking_date']
            booking.checkout_date = params['checkout_date']
            booking.number_of_guest = len(guests)
            booking.number_of_room = params['number_of_room']
            booking.city = params['city']
            booking.address = params['address']
            booking.save()

            return ApiResponse.success(booking_dict, 200)
        except ResponseError as error:
            print(f'Error is ----------{error}')
            return ApiResponse.error(error.response.body, 402)
Example #2
0
def sqlalchemy_error_handler(exc):
    error_type_name = type(exc).__name__
    error_message = str(exc.__dict__['orig'])
    return ApiResponse.error(error=error_message,
                             status_code=422,
                             error_type=error_type_name,
                             message=error_message)
Example #3
0
def handle_integrity_error(exc):
    db.session.rollback()
    msg = exc.args[0].split('Key')
    message = re.sub(u'[()\\n]', '', msg[-1])
    message = re.sub(u'[=]', ' ', message)
    return ApiResponse.error(exc.args,
                             409,
                             message,
                             error_type=exc.__class__.__name__)
Example #4
0
 def get(self):
     """All offer provided by a specific hotels"""
     try:
         params = self.parser.parse_args()
         params = cleanNullItems(params)
         hotel_offers = amadeus.shopping.hotel_offers_by_hotel.get(**params)
         return ApiResponse.success(hotel_offers.data, 200)
     except ResponseError as error:
         print(f'Error is ----------{error}')
         return ApiResponse.error(error.response.body, 402)
Example #5
0
 def post(self):
     """Login via Email"""
     json = self.parser.parse_args()
     userquery = UserModel.query.filter(UserModel.email == json['email'],
                                        UserModel.is_deleted == False)
     user = userquery.first()
     if user is not None:
         authorized = user.check_password(json['password'])
         if authorized:
             loggeding_data = perform_login(user)
             return ApiResponse.success(loggeding_data,
                                        200,
                                        message=KMessages.LOGIN_DONE)
         else:
             return ApiResponse.error(None,
                                      404,
                                      message=KMessages.INVALID_LOGIN_AUTH)
     else:
         return ApiResponse.error(None, 404, message=KMessages.NO_EMAIL_ID)
Example #6
0
    def get(self):
        """Get User data"""
        user_id = request.args.get('user_id', None, type=int)
        if user_id is None:
            user_id = g.user_id
        user = UserModel.get_by_id(user_id)

        if user is not None:
            return ApiResponse.success(UserSchema().dump(user), 200)
        return ApiResponse.error('User not found.', 402)
Example #7
0
 def post(self):
     """Get Token for Forget password"""
     arg_json = self.parser.parse_args()
     email = arg_json['email']
     user: UserModel = UserModel.query.filter(
         UserModel.email == email).first()
     if user:
         from api.helpers.email import send_password_reset_request_email
         send_password_reset_request_email(user)
         return ApiResponse.success(
             None, 200, message=KMessages.RESET_PASSWORD_TOKEN_SEND)
     return ApiResponse.error(None, 404, message=KMessages.NO_EMAIL_ID)
Example #8
0
        def wrapper(*args, **kwargs):
            response = False
            if roleTypes is not None:
                for roleType in roleTypes:
                    user_role_type = get_jwt_claims()['user_role']
                    if user_role_type == roleType.value:
                        response = True

            if response is False:
                return ApiResponse.error(
                    f'user is is not authorized to perform this access', 403)
            return fn(*args, **kwargs)
Example #9
0
 def wrapper(self, *args, **kwargs):
     verify_jwt_in_request()
     authorization = request.headers.get('Authorization')
     if not authorization:
         return ApiResponse.error('No authorization token provided', 403)
     auth_token = authorization.split(" ")[1]
     resp = UserToken.is_token_valid(auth_token)
     if not resp:
         return ApiResponse.error('Invalid authorization token', 403)
     # validate = JWT.validate_authorization(auth_token)
     #
     from flask_jwt_extended import get_jwt_identity
     user = get_jwt_identity()
     # clam = get_jwt_claims()
     g.user_id = user['user_id']
     #
     # from api.modules.user.role.model import UserRole
     # g.user_role: UserRole = UserRole.query.filter_by(user_id=g.user_id).first()
     #
     # if not validate:
     #     return ApiResponse.error('Unauthorized', 403)
     return fn(self, *args, **kwargs)
Example #10
0
 def get(self):
     try:
         params = self.parser.parse_args()
         if params['childAges'] is not None:
             params['childAges'] = list_to_csv(params['childAges'])
         if params['ratings'] is not None:
             params['ratings'] = list_to_csv(params['ratings'])
         params = cleanNullItems(params)
         response = amadeus.shopping.hotel_offers.get(**params)
         mf_data = format_muslim_friendly_data(response.data)
         return ApiResponse.success(mf_data, 200)
     except ResponseError as error:
         print(f'Error is ----------{error}')
         return ApiResponse.error(error.response.body, 402)
Example #11
0
 def post(self):
     """Update userpasword"""
     arg_json = self.parser.parse_args()
     user: UserModel = UserModel.get_by_id(g.user_id)
     authorized = user.check_password(arg_json['current_password'])
     if not authorized:
         return ApiResponse.error(None,
                                  404,
                                  message=KMessages.CURRENT_PASSWORD_DIFFER)
     new_password = arg_json['new_password']
     password_validation(new_password)
     user.password = generate_password_hash(new_password).decode('utf8')
     user.update()
     return ApiResponse.success(
         None, 200, message=KMessages.PASSWORD_CHANGE_SUCESSFULLY)
Example #12
0
 def post(self):
     """Reset your password with token"""
     arg_json = self.parser.parse_args()
     user: UserModel = verify_reset_password_token(arg_json['token'])
     if not user:
         return ApiResponse.error(None,
                                  404,
                                  message=KMessages.INVALID_TOKEN)
     arg_json = self.parser.parse_args()
     new_password = arg_json['new_password']
     password_validation(new_password)
     user.password = generate_password_hash(new_password).decode('utf8')
     user.update()
     from api.helpers.email import send_password_reset_confirmation_email
     send_password_reset_confirmation_email(user)
     return ApiResponse.success(
         None, 200, message=KMessages.PASSWORD_CHANGE_SUCESSFULLY)
Example #13
0
 def validationError(cls, error, status_code):
     print(status_code)
     error, code = ApiResponse.error(error=error,
                                     status_code=status_code,
                                     message='')
     return cls(errors=error, status_code=code)
Example #14
0
def schema_validation_error(exc):
    error_type_name = type(exc).__name__
    return ApiResponse.error(error=exc.messages,
                             status_code=422,
                             error_type=error_type_name)