Exemplo n.º 1
0
    def get(self, id):
        """
        GET method
        Return: dict (moto, rentals and users)
        """
        data = parser.parse_args()

        if not data['num_rentals']:
            return {
                'message': {
                    "num_rentals": "Specify the number of rentals you want"
                }
            }, 400

        moto = MotosModel.find_by_id(id)

        associated_rentals = RentalsModel.find_by_moto_id(id)

        if not associated_rentals:
            return {
                'message':
                'Moto with id [{}] has no associated rentals'.format(id)
            }, 404

        if moto:
            return {
                'last_rentals':
                MotosModel.find_last_rentals_info(moto, data['num_rentals'],
                                                  associated_rentals)
            }, 200
        else:
            return {'message': 'Moto with id [{}] not found'.format(id)}, 404
Exemplo n.º 2
0
    def put(self, id):
        """
        PUT method
        Modifies a motos
        Param: id
        Return: dict (moto updated)
        """
        data = parser.parse_args()

        if not data['available']:
            return {'message': {"available": "Specify true or false"}}, 400

        moto = MotosModel.find_by_id(id=id)

        try:
            moto.available = str_to_bool(data['available'])
        except ValueError:
            return {
                'message': {
                    "available": "Specify a boolean value, true or false"
                }
            }, 400

        try:
            moto.save_to_db()
            return {'user': MotosModel.find_by_id(id).json()}, 200
        except:
            return {"message": "Error Description"}, 500
Exemplo n.º 3
0
    def post(self):
        """
        POST method
        Adds a new rental. This method is used for initialize a rental.
        Return: dict (rental created / message)
        """
        data = parser.parse_args()
        if not data['moto_id']:
            return {'message': {
                "moto_id": "Moto_id cant be empty"
            }}, 400
        if not data['user_id']:
            return {'message': {
                "user_id": "User_id cant be empty"
            }}, 400

        activerental = RentalsModel.find_active_rental_by_user_id(user_id=data['user_id'])
        if activerental:
            return {'message': "User with id [{}] already has an ongoing rental".format(data['user_id'])}, 409

        rental = RentalsModel(moto_id=data['moto_id'],
                              user_id=data['user_id'],
                              book_hour=datetime.now().isoformat())
        moto = MotosModel.find_by_id(data['moto_id'])
        try:
            rental.save_to_db()
            moto.set_available(False)
            return {'rental': RentalsModel.find_by_id(rental.id).json()}, 201

        except:
            return {'message': 'Internal server error'}, 500
Exemplo n.º 4
0
    def get(self, id):
        """
        GET method
        Gets an moto by id
        Param: int id
        Return: dict (moto ok / message)
        """

        moto = MotosModel.find_by_id(id=id)
        if moto:
            return {'moto': moto.json()}, 200
        else:
            return {'message': "Moto with id [{}] Not found.".format(id)}, 404
Exemplo n.º 5
0
 def get(self):
     """
     GET method
     Return: dict (accounts)
     """
     data = parser.parse_args()
     if data['available']:
         available = str_to_bool(data['available'])
         motos = MotosModel.get_available_motos(available=available)
         return {'motos': [moto.json() for moto in motos]}, 200
     if data['license_number']:
         moto = MotosModel.find_by_license_number(
             license_number=data['license_number'])
         if moto is None:
             return {
                 'motos': [],
                 'message':
                 "Moto with license number [{}] Not found".format(
                     data['license_number'])
             }, 404
     motos = MotosModel.all_motos()
     return {'motos': [moto.json() for moto in motos]}, 200
Exemplo n.º 6
0
 def delete(self, id):
     """
     DELETE method
     Removes a moto
     Param: int id
     Return: dict (message ok / message)
     """
     moto = MotosModel.find_by_id(id=id)
     if moto:
         try:
             moto.delete_from_db()
             return {
                 'message': "Moto with id [{}] deleted.".format(id)
             }, 200
         except:
             return {"message": "Something went wrong."}, 500
     else:
         return {'message': "Moto with id [{}] Not found".format(id)}, 404
Exemplo n.º 7
0
 def put(self, id):
     """
     PUT method
     Updates a rental. This method is used for finish a rental.
     :param id:
     :return: dict (rental updated / message)
     """
     data = parser.parse_args()
     rental = RentalsModel.find_by_id(id)
     moto = MotosModel.find_by_id(rental.moto_id)
     if rental is None:
         return {'message': 'This rental doesn t exist'}, 404
     if data['end_rental'] is not None:
         if str_to_bool(data['end_rental']):
             if rental.finish_rental_hour is not None:
                 return {'message': 'The rental is already finsished.'}, 409
             if not data['latitude'] or not data['longitude']:
                 return {'message': 'latitude and longitude parameters needed for updating moto coordinates'}, 409
             try:
                 rental.update_finish_rent_hour(datetime.now().isoformat())
                 moto.set_available(True)
                 moto.update_coords(data['latitude'], data['longitude'])
                 new_rental = RentalsModel.find_by_id(rental.id)
                 return {'rental': new_rental.json()}, 200
             except:
                 return {'message': 'Internal server error'}, 500
         else:
             if rental.finish_book_hour is not None:
                 return {'message': 'The book is already finsished.'}, 409
             try:
                 max_finish_book_hour = datetime.strptime(rental.book_hour, '%Y-%m-%dT%H:%M:%S.%f') + timedelta(minutes=15)
                 if max_finish_book_hour > datetime.now():
                     rental.update_finish_book_hour(datetime.now().isoformat())
                     new_rental = RentalsModel.find_by_id(rental.id)
                     return {'rental': new_rental.json()}, 200
                 else:
                     rental.update_finish_book_hour(max_finish_book_hour.isoformat())
                     new_rental = RentalsModel.find_by_id(rental.id)
                     return {'rental': new_rental.json()}, 200
             except:
                 return {'message': 'Internal server error'}, 500
     else:
         return {'message': 'Bad request, you must pass end_rental param'}, 400
Exemplo n.º 8
0
    def post(self):
        """
        POST method
        Adds a new account
        Return: dict (account created / message)
        """
        data = parser.parse_args()

        if not data['license_number']:
            return {'message': {"license_number": "Name cant be empty"}}, 400

        if not data['battery']:
            return {'message': {"battery": "Battery cant be empty"}}, 400

        if not data['longitude']:
            return {'message': {"longitude": "Longitude cant be empty"}}, 400

        if not data['latitude']:
            return {'message': {"latitude": "Latitude cant be empty"}}, 400

        if MotosModel.find_by_license_number(
                license_number=data['license_number']):
            return {
                'message':
                "Account with license number [{}] already exists".format(
                    data['license_number'])
            }, 409

        moto = MotosModel(license_number=data['license_number'],
                          battery=data['battery'],
                          latitude=data['latitude'],
                          longitude=data['longitude'])
        try:
            moto.save_to_db()
            return {'moto': MotosModel.find_by_id(moto.id).json()}, 201
        except:
            return {"message": "Something went wrong"}, 500
Exemplo n.º 9
0
 def init_motos(self):
     moto1 = MotosModel('1234AAA', 99, 42.1, 2.1)
     moto2 = MotosModel('4321AAA', 10, 42.0, 2.1)
     moto3 = MotosModel('5678AAA', 9, 42.1, 2.0)
     moto4 = MotosModel('8765AAA', 32, 42.0, 2.0)
     moto1.save_to_db()
     moto2.save_to_db()
     moto3.save_to_db()
     moto4.save_to_db()