def create_test_moto(): new_moto = MotoModel.query.filter(MotoModel.matricula == matricula).first() # Si ya existe retornamos directamente if new_moto is not None: return new_moto # Si no existe la creamos new_moto = MotoModel(state="AVAILABLE", matricula=matricula, date_estreno="28/10/2020", model_generic="premium", last_coordinate_latitude=23.4434, last_coordinate_longitude=23.4433, km_restantes=120.0, km_totales=500.0, date_last_check="18/10/2020", km_last_check=0.0) new_moto.save_to_db() new_moto = MotoModel.query.filter(MotoModel.matricula == matricula).first() if new_moto is None: # No se ha añadido a la base de datos!!!! # Si esto no funciona los tests no tienen sentido. assert False return new_moto
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, 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