def add(self): if self.__addition_json != "": self.__model_instance = sensor_schema.load(self.__addition_json, session=db.session) else: user_repo = UserRepository() user_repo.set_db_session(db.session) sensor = SensorModel( name=''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(3)), ip=".".join(map(str, (random.randint(0, 255) for _ in range(4)))), port=''.join(map(str, (random.randint(0, 9) for _ in range(4)))), status=bool(random.getrandbits(1)), active=bool(random.getrandbits(1)), user_id=int(random.choice(user_repo.get_users_id_list())), ) self.__model_instance = sensor db.session.add(self.__model_instance) try: db.session.commit() return sensor_schema.dump(self.__model_instance), 201 except Exception as error: db.session.rollback() print("\nSensor addition error: ", error) return "Error in HTTP Method", 409
def post(self): sensor = SensorModel.from_json(request.get_json()) try: db.session.add(sensor) db.session.commit() except Exception as error: return str(error), 400 return sensor.to_json(), 201
def post(self): # Traemos la coleccion de sensores de la db y la alojamos en la variable "sensors". sensor = SensorModel.from_json(request.get_json()) # Realiza la operacion try: # Añadimos un sensor a la coleccion db.session.add(sensor) # Actualiza la db y nos devuelve el mensaje de creado con un codigo "201 (Nuevo recurso Creado)". db.session.commit() return sensor.to_json(), 201 # Si algo pasa o ocurre mal... except Exception as error: # Nos retorna el error "400 (Solicitud incorrecta)" return str(error), 400
def post(self): sensor = SensorModel.from_json(request.get_json()) db.session.add(sensor) db.session.commit() return sensor.to_json(), 201