def post(self): parser = reqparse.RequestParser() parser.add_argument('license_plate', type=str, required=True, help="This field cannot be left blank") parser.add_argument('model_generic', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() matricula = data["license_plate"].upper() model_generic = data["model_generic"] if not MotoModel.is_license_plate(matricula): return { "message": "ERROR: The format of the license plate is not valid." }, 400 if model_generic not in ("basic", "premium"): return { "message": "ERROR: model_generic should be either [basic] or [premium]." }, 400 m = MotoModel.query.filter( MotoModel.matricula == data["license_plate"]).first() if m is not None: return { "message": "There is already a motorbike with license plate [{}].".format( matricula) }, 409 else: try: date_format = "%d/%m/%Y" today = datetime.now().strftime(date_format) str_today = today # str(today.day) + str(today.month) + str(today.year) # Esto de aquí genera unas coordenadas en BCN con ruido gausiano. latt, long = MotoModel.get_random_coordinates() moto = MotoModel('AVAILABLE', matricula, str_today, data['model_generic'], latt, long, 50, 0, str_today, 0) moto.save_to_db() return {"message": "Moto added successfully"}, 200 except: return {"message": "Internal Error Post Moto"}, 500
def put(self, client_email, moto_id): try: moto = MotoModel.query.filter(MotoModel.id == moto_id).first() client = ClientModel.query.filter(ClientModel.email == client_email).first() if moto is not None and client is not None: r = ReservedRunningModel.query.filter(and_( ReservedRunningModel.motoId == moto_id, ReservedRunningModel.clientId == client.client_id)).first() if r is not None and moto.state == "ACTIVE": # En caso de que exista y de que este ACTIVE # Aquí simulamos un par de cosas para aumentar el realismo de la versión de prueba old_coord = moto.last_coordinate_latitude, moto.last_coordinate_longitude # Unas nuevas coordenadas (Aleatorias) new_coord = MotoModel.get_random_coordinates(coord=old_coord, std_deviation = 0.0015 * moto.km_restantes) # Calculamos los km_recorridos basándonos en las coordenadas # (y poniendo un extra random, suponiendo trayectorias no rectas). km_recorridos = moto.compute_km_recorridos(new_coord) # Actualizamos todó lo necesario moto.km_totales += km_recorridos moto.km_restantes -= km_recorridos if moto.hasLowBattery(): moto.state = 'LOW_BATTERY_FUEL' else: moto.state = "AVAILABLE" moto.updateCoordAndAddress(new_coord) moto.save_to_db() r.delete_from_db() return {'message_status': 'Ok', 'message': 'Motorbike stoped successfully'},200 return {'message_status': "Not Found", 'message': "Client with email {} is not riding motorbike with id [{}]. " .format(client_email, moto_id)}, 404 except: return {'message_status': "Internal Error", 'message': "Internal Server Error. "}, 500