def delete(self, email_client): parser = reqparse.RequestParser() parser.add_argument('password', type=str, required=True, help="Password cannot be left blanck") data = parser.parse_args() password = data['password'] c = ClientModel.find_by_email(email_client) if c: # Si existe el cliente if c.password == password: # Si la password coincide con la del cliente rr = ReservedRunningModel.find_by_client(c.client_id) if(rr is not None): # Si existe una reserva o start afiliada al cliente, indagamos moto = MotoModel.find_by_id(rr.moto.id) if(moto.state == "RESERVED"): # Si la moto esta reservada # Borramos la fila de rr rr.delete_from_db() # Actualizamos el estado de la moto moto.state = "AVAILABLE" moto.save_to_db() # Borramos el cliente c.delete_from_db() return {"message": "Client DELETED successfully"}, 200 if(moto.state == "ACTIVE"): # No podemos borrar el cliente porque antes tiene que finalizar el trayecto return {'message': 'The account cannot be deleted because you have a journey started'}, 401 else: # Si no existe reserva o start, podemos borrarlo directamente c.delete_from_db() return {"message": "Client DELETED successfully"}, 200 else: # Password erronea return {'message': 'ERROR: The password is not correct.'}, 400 else: # Si no existe el cliente return {'message': 'ERROR: There is no client with email [{}] .'.format(email_client)}, 404
def delete(self, id): try: moto = MotoModel.find_by_id(id) # Si no se encuentra... if moto is None: return { 'message_status': 'Not Found', 'message': 'Motorbike with id [{}] not found'.format(id) }, 404 if moto.state in ("ACTIVE", "RESERVED"): return { 'message_status': 'Conflict', 'message': 'Motorbike with id [{}] is {} and can not be deleted'. format(id, moto.state.lower()) }, 409 MotoModel.delete_from_db(moto) return { 'message_status': 'Ok', 'message': 'Motorbike with id [{}] deleted successfully.'.format(id) }, 200 except: return { 'message_status': 'Internal Error', 'message': 'Internal Server Error during Delete Moto.' }, 500
def delete(self, client_email, moto_id): try: moto = MotoModel.find_by_id(moto_id) client = ClientModel.find_by_email(client_email) # En caso de que tanto la moto y el cliente existan en sus respectivas tablas if moto is not None and client is not None: moto = MotoModel.find_by_id(moto_id) client = ClientModel.find_by_email(client_email) # Compruebo si existe el cliente segun el email recibido y si existe la moto segun el id recibido if moto is not None and client is not None: reserve = ReservedRunningModel.query.filter(and_( ReservedRunningModel.clientId == client.client_id, ReservedRunningModel.motoId == moto_id )).first() # Si no se encuentra la reserva con esas características... if reserve is None: return {"message": "DELETE ERROR. Moto reserved with id {} and client {} not found.".format( moto_id, client_email)}, 404 # En caso de que si que exista una fila en la tabla RR... else: # Comprobamos cual es el auténtico estado de la moto. # una reserva(state =="RESERVED"), if moto.state == 'RESERVED': # Borramos la fila de rr reserve.delete_from_db() # Actualizamos el estado de la moto moto.state = "AVAILABLE" moto.save_to_db() return {"message": "Reserved Moto DELETED successfully"}, 200 # una running (state =="ACTIVE") elif moto.state == 'ACTIVE': # No se puede cancelar la reserva si no está reservada, sinó running(ACTIVE) return {"message": "DELETE ERROR Moto Reserve: Moto is ACTIVE"}, 500 # un DESASTRE (state ==otra cosa, que no debería). else: # ¡¡¡¡ERROR FATAL!!!! # Si entramos en este else es que la moto no debería estar en esta tabla. # TODO Revisar si deberíamos borrarlo igualmente return {"message": "FATAL Error. DELETE Moto Reserve: Moto is {}.".format(moto.state)}, 500 # En caso de que o la moto o el cliente NO EXISTAN en sus respectivas tablas else: return {"message": "DELETE ERROR Moto Reserve. Moto with id [{}] or client with email [{}] not found." .format(moto_id, client_email)}, 404 except: return {"message": "DELETE ERROR Reserved Moto. Internal Failure."}, 500
def get(self, id): coord_mechanic = (23.44333, 23.4433) try: moto = MotoModel.find_by_id(id) moto_json = [moto.json_mechanicMoto()] result = MotoModel.compute_distance(moto_json, coord_mechanic, "distance") # los cambios de keyname de jsons es para coordinar con frontend return {'mechanic_moto': result['motos'][0]}, 200 except: return {"message": "Error Get Moto"}, 500
def put(self, id): parser = reqparse.RequestParser() parser.add_argument('matricula', type=str, required=True, help="This field cannot be left blank") parser.add_argument('km_restantes', type=float, required=True, help="This field cannot be left blank") parser.add_argument('state', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() moto = MotoModel.find_by_id(id) if (moto): moto_aux = MotoModel.find_by_matricula(data['matricula']) if (moto_aux is not None and moto.id != moto_aux.id): return { 'message': "Motorbike with license plate [{}] already exists".format( data['matricula']) }, 409 else: if ((data['km_restantes'] <= 5.0 and data['state'] == "AVAILABLE") or (data['km_restantes'] > 5.0 and data['state'] == "LOW_BATTERY_FUEL")): return { 'message': "State and the battery fields are not consistent" }, 400 try: moto.set_moto(data['matricula'], data['km_restantes'], data['state']) MotoModel.save_to_db(moto) return {"message": "Motorbike modified successfully"}, 200 except: return { "message": "Error while trying to modify motorbike with id [{}]". format(id) }, 500 else: return { 'message': "Motorbike with id [{}] Not Found".format(id) }, 404
def post(self, moto_id): try: moto = MotoModel.find_by_id(moto_id) if moto is not None: if moto.state == "RESERVED" or moto.state == "ACTIVE": rr = ReservedRunningModel.find_by_moto(moto.id) rr.delete_from_db() moto.set_state("ALERT") return {"message": "Correctly reported error"}, 201 else: moto.set_state("ALERT") return {"message": "Correctly reported error"}, 201 else: return {"message": "Moto not found"}, 404 except: return {"message": "Internal error when reporting the error"}, 500
def post(self): parser = reqparse.RequestParser() parser.add_argument('client_email', type=str, required=True, help="Client email cannot be left blank") parser.add_argument('moto_id', type=int, required=True, help="Moto id cannot be left blank") data = parser.parse_args() client_email = data['client_email'] moto_id = data['moto_id'] moto = MotoModel.find_by_id(moto_id) client = ClientModel.find_by_email(client_email) # Compruebo si existe el cliente segun el email recibido y si existe la moto segun el id recibido if moto is not None and client is not None: # Compruebo si el estado de la moto es el correcto (AVAILABLE) if moto.state == 'AVAILABLE': # Compruebo si el cliente no tiene motos reservadas if ReservedRunningModel.find_by_client(client.client_id) is None: rr = ReservedRunningModel(client, moto) ReservedRunningModel.save_to_db(rr) ReservedRunningModel.update_state_reserved(rr) remaining_time = rr.make_remaining_time() if remaining_time.minute < 10: time_min = "0" + str(remaining_time.minute) else: time_min = str(remaining_time.minute) if remaining_time.hour < 10: time_h = "0" + str(remaining_time.hour) else: time_h = str(remaining_time.hour) return {"message": "You have until {}:{}h to start the motorbike".format(time_h, time_min), "remaining_time": time_h + ":" + time_min}, 201 else: return {"message": "ERROR RESERVED MOTO. Customer [{}] already has a reserved motorcycle".format( client.client_id)}, 500 else: return {"message": "ERROR RESERVED MOTO. Moto state isn't AVAILABLE, moto state = [{}]".format( moto.state)}, 500 else: return {"message": "ERROR RESERVED MOTO. Motorcycle error or client not found for Reserved Moto post"}, 404
def get(self, id): parser = reqparse.RequestParser() parser.add_argument('client_coordinate_latitude', type=float, required=True, help="This field cannot be left blank") parser.add_argument('client_coordinate_longitude', type=float, required=True, help="This field cannot be left blank") data = parser.parse_args() coord_client = (data["client_coordinate_latitude"], data["client_coordinate_longitude"]) try: moto = MotoModel.find_by_id(id) moto_json = [moto.json_clientMoto()] result = MotoModel.compute_distance(moto_json, coord_client, "distance") # los cambios de keyname de jsons es para coordinar con frontend return {'client_moto': result['motos'][0]}, 200 except: return {"message": "Error Get Moto"}, 500
def post(self): try: parser = reqparse.RequestParser() parser.add_argument('client_email', type=str, required=True, help="Client email cannot be left blank") parser.add_argument('moto_id', type=int, required=True, help="Moto id cannot be left blank") data = parser.parse_args() client_email = data['client_email'] moto_id = data['moto_id'] moto = MotoModel.find_by_id(moto_id) client = ClientModel.find_by_email(client_email) rr = ReservedRunningModel.find_by_client_moto(client.client_id, moto_id) # Compruebo si existe el cliente segun el email recibido y si existe la moto segun el id recibido if moto is not None and client is not None: # Compruebo si el estado de la moto es el correcto (RESERVED) if moto.state == 'RESERVED': # Compruebo si ha encontrado la reserva del ciente y la moto if rr is not None: # Compruebo que no se ha superado el tiempo limite para el start moto desde la reserva if rr.check_remaining_time(): ReservedRunningModel.make_star_moto(rr) ReservedRunningModel.update_state_start(rr) return {"message": "Start successfully"}, 200 else: ReservedRunningModel.update_state_available(rr) ReservedRunningModel.delete_from_db(rr) return {"message": "The time limit for the start has expired"}, 500 else: return {"message": "Reservation error with that id_client and id_moto does not exist"}, 500 else: return {"message": "Error state moto isn't RESERVED"}, 500 else: return {"message": "Motorcycle or client not found error for POST Start Moto"}, 404 except: return {"message": "Error POST Start Moto"}, 500
def get(self, id): try: moto = MotoModel.find_by_id(id) return {'moto': moto.json()}, 200 except: return {"message": "Error Get Moto"}, 500