-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from datetime import datetime | ||
from sqlalchemy import ForeignKey, Date, func | ||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
from extensiones import db | ||
from .vehiculo import Vehiculo | ||
from .usuario import Usuario | ||
|
||
class BitacoraAsignaciones(db.Model): | ||
id: Mapped[int] = mapped_column(primary_key=True) | ||
id_vehiculo: Mapped[int] = mapped_column(ForeignKey('vehiculo.id')) | ||
id_usuario: Mapped[int] = mapped_column(ForeignKey('usuario.id')) | ||
fecha_hora_asignacion: Mapped[datetime] = mapped_column(default=func.now()) | ||
fecha_hora_desasignacion: Mapped[datetime] = mapped_column(nullable=True) | ||
|
||
vehiculo: Mapped['Vehiculo'] = relationship('Vehiculo', backref='bitacoras_asignaciones') | ||
usuario: Mapped['Usuario'] = relationship('Usuario', backref='bitacoras_asignaciones') | ||
|
||
def serialize(self): | ||
return { | ||
'id': self.id, | ||
'vehiculo': self.vehiculo.serialize() if self.vehiculo else None, | ||
'usuario': self.usuario.serialize() if self.usuario else None, | ||
'fecha_hora_asignacion': self.fecha_hora_asignacion, | ||
'fecha_hora_desasignacion': self.fecha_hora_desasignacion, | ||
} | ||
|
||
@staticmethod | ||
def agregar(bitacora): | ||
db.session.add(bitacora) | ||
db.session.commit() | ||
|
||
@staticmethod | ||
def listar(): | ||
return BitacoraAsignaciones.query.all() | ||
|
||
@staticmethod | ||
def listar_json(): | ||
return [bitacora.serialize() for bitacora in BitacoraAsignaciones.listar()] | ||
|
||
@staticmethod | ||
def encontrarPorId(id): | ||
return BitacoraAsignaciones.query.get(id) | ||
|
||
@staticmethod | ||
def agregarFechaHoraDesasignacion(id): | ||
bitacora = BitacoraAsignaciones.encontrarPorId(id) | ||
bitacora.fecha_hora_desasignacion = func.now() | ||
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
from flask import Blueprint | ||
from models.usuario import Usuario | ||
from models.vehiculo import Vehiculo | ||
from models.bitacora_asignaciones import BitacoraAsignaciones | ||
|
||
asig_operador_vehiculo = Blueprint('asig_operador_vehiculo', __name__) | ||
|
||
@asig_operador_vehiculo.route('/operadores/<string:nombre>/vehiculos/<int:id_vehiculo>/', methods=['PUT']) | ||
def asignar(id_vehiculo, nombre): | ||
@asig_operador_vehiculo.route('/asignacion/<int:id_usuario>/vehiculos/<int:id_vehiculo>/', methods=['PUT']) | ||
def asignar(id_vehiculo, id_usuario): | ||
vehiculo = Vehiculo.encontrarPorId(id_vehiculo) | ||
usuario = Usuario.buscar(nombre) | ||
if (vehiculo == None or usuario == None | ||
or vehiculo.estado != 'ACTIVO' | ||
or vehiculo.id_operador != None): | ||
return "Vehiculo u Operador no encontrado", 404 | ||
vehiculo.id_operador = usuario.id | ||
Vehiculo.actualizar() | ||
return 'OK', 202 | ||
usuario = Usuario.encontrarPorId(id_usuario) | ||
|
||
bitacoraAsignaciones = BitacoraAsignaciones( | ||
vehiculo=vehiculo, | ||
usuario=usuario | ||
) | ||
BitacoraAsignaciones.agregar(bitacoraAsignaciones) | ||
|
||
return bitacoraAsignaciones.serialize(), 201 | ||
|
||
|
||
|
||
|
||
|
||
@asig_operador_vehiculo.route('/operadores/<string:nombre>/vehiculos/desasign/<int:id_vehiculo>/', methods=['PUT']) | ||
def desasignar(id_vehiculo, nombre): | ||
vehiculo = Vehiculo.encontrarPorId(id_vehiculo) | ||
usuario = Usuario.buscar(nombre) | ||
print(vehiculo.id_operador) | ||
print(usuario.id) | ||
if (vehiculo == None or usuario == None | ||
or vehiculo.estado != 'ACTIVO' | ||
or vehiculo.id_operador != usuario.id): | ||
return "Vehiculo u Operador no encontrado", 404 | ||
vehiculo.id_operador = None | ||
Vehiculo.actualizar() | ||
return 'OK', 202 | ||
@asig_operador_vehiculo.route('/desasignacion/<int:id_asignacion>', methods=['PUT']) | ||
def desasignar(id_asignacion): | ||
bitacora = BitacoraAsignaciones.encontrarPorId(id_asignacion) | ||
BitacoraAsignaciones.agregarFechaHoraDesasignacion(id_asignacion) | ||
return bitacora.serialize(), 201 |