Showing
12 changed files
with
155 additions
and
21 deletions
1 | from sqlalchemy import create_engine, MetaData, Table | 1 | from sqlalchemy import create_engine, MetaData, Table |
2 | -from sqlalchemy.orm import mapper | 2 | +from sqlalchemy.orm import mapper, sessionmaker |
3 | from sqlalchemy.sql import select | 3 | from sqlalchemy.sql import select |
4 | 4 | ||
5 | -from config import settings | 5 | +from project.config import settings |
6 | 6 | ||
7 | 7 | ||
8 | class Persona(object): | 8 | class Persona(object): |
@@ -11,6 +11,8 @@ class Persona(object): | @@ -11,6 +11,8 @@ class Persona(object): | ||
11 | 11 | ||
12 | engine = create_engine(settings.database_url) | 12 | engine = create_engine(settings.database_url) |
13 | metadata = MetaData(engine) | 13 | metadata = MetaData(engine) |
14 | +Session = sessionmaker(engine) | ||
15 | +session = Session() | ||
14 | 16 | ||
15 | tabla_persona = Table('tb_personas', metadata, autoload=True) | 17 | tabla_persona = Table('tb_personas', metadata, autoload=True) |
16 | mapper(Persona, tabla_persona) | 18 | mapper(Persona, tabla_persona) |
@@ -18,9 +20,10 @@ mapper(Persona, tabla_persona) | @@ -18,9 +20,10 @@ mapper(Persona, tabla_persona) | ||
18 | print(tabla_persona.columns) | 20 | print(tabla_persona.columns) |
19 | 21 | ||
20 | # Ejecutar una query | 22 | # Ejecutar una query |
21 | -conn = engine.connect() | ||
22 | -s = select([Persona.Apellido]).where(Persona.id == 802) | ||
23 | -res = conn.execute(s) | ||
24 | -row = res.fetchone() | ||
25 | -res = None if row is None else row['ColumnName'] | ||
26 | -print(res) | 23 | +if __name__ == '__main__': |
24 | + conn = engine.connect() | ||
25 | + s = select([Persona.Apellido]).where(Persona.id == 802) | ||
26 | + res = conn.execute(s) | ||
27 | + row = res.fetchone() | ||
28 | + res = None if row is None else row['ColumnName'] | ||
29 | + print(res) |
project/apps/agente/__init__.py
0 → 100644
project/apps/agente/models.py
0 → 100644
1 | +from sqlalchemy import Column, INTEGER, VARCHAR, DATE, ForeignKey | ||
2 | +from sqlalchemy.orm import relationship | ||
3 | + | ||
4 | +from database import ModelBase | ||
5 | +from persona.models import Persona | ||
6 | + | ||
7 | + | ||
8 | +class Agente(ModelBase): | ||
9 | + __tablename__ = 'tb_agentes' | ||
10 | + | ||
11 | + id = Column('idAgente', INTEGER(), primary_key=True, nullable=False) | ||
12 | + legajo = Column('Legajo', VARCHAR(length=10)) | ||
13 | + observaciones = Column('Observaciones', VARCHAR(length=250)) | ||
14 | + | ||
15 | + causa_baja = Column('CausaBaja', INTEGER()) | ||
16 | + fecha_alta = Column('FechaAlta', DATE(), nullable=False) | ||
17 | + fecha_baja = Column('FechaBaja', DATE()) | ||
18 | + | ||
19 | + # Relaciones | ||
20 | + persona_id = Column('Persona', INTEGER(), ForeignKey('tb_Personas.idPersona'), nullable=False) | ||
21 | + # persona = relationship(Persona, primaryjoin="Agente.Persona == Persona.idPersona", lazy='dynamic') | ||
22 | + | ||
23 | + def __str__(self): | ||
24 | + return f"{self.persona_id}" | ||
25 | + | ||
26 | + def __repr__(self): | ||
27 | + return f"<Agente: {self}>" |
project/apps/agente/routers.py
0 → 100644
1 | +from fastapi import APIRouter, Depends, HTTPException | ||
2 | +from sqlalchemy.orm import Session | ||
3 | + | ||
4 | +from agente.models import Agente | ||
5 | +from agente.schemas import AgenteSchema | ||
6 | +from database import get_db | ||
7 | + | ||
8 | +router = APIRouter() | ||
9 | + | ||
10 | + | ||
11 | +@router.get('/agentes/{persona_id}/', response_model=AgenteSchema) | ||
12 | +def obtener_agente_por_persona_id(persona_id: int, db: Session = Depends(get_db)): | ||
13 | + agente = db.query(Agente).filter(Agente.persona_id == persona_id).first() | ||
14 | + | ||
15 | + if agente is None: | ||
16 | + raise HTTPException(status_code=404, detail=f"No se encontró el Agente para la Persona con id {persona_id}") | ||
17 | + | ||
18 | + return agente |
project/apps/agente/schemas.py
0 → 100644
1 | +from datetime import date | ||
2 | +from typing import Optional | ||
3 | + | ||
4 | +import pydantic | ||
5 | + | ||
6 | +from persona.schemas import PersonaSchema | ||
7 | + | ||
8 | + | ||
9 | +class AgenteSchema(pydantic.BaseModel): | ||
10 | + id: int | ||
11 | + legajo: Optional[str] = None | ||
12 | + observaciones: Optional[str] = None | ||
13 | + causa_baja: Optional[int] = None | ||
14 | + fecha_alta: date | ||
15 | + fecha_baja: Optional[date] = None | ||
16 | + | ||
17 | + # persona: PersonaSchema | ||
18 | + | ||
19 | + class Config: | ||
20 | + orm_mode = True |
@@ -2,6 +2,7 @@ from sqlalchemy import Column, INTEGER, VARCHAR, ForeignKey, DATE | @@ -2,6 +2,7 @@ from sqlalchemy import Column, INTEGER, VARCHAR, ForeignKey, DATE | ||
2 | from sqlalchemy.orm import relationship | 2 | from sqlalchemy.orm import relationship |
3 | 3 | ||
4 | from database import ModelBase | 4 | from database import ModelBase |
5 | +from utils.models import Nacionalidad | ||
5 | 6 | ||
6 | 7 | ||
7 | class Sexo(ModelBase): | 8 | class Sexo(ModelBase): |
@@ -28,11 +29,21 @@ class Persona(ModelBase): | @@ -28,11 +29,21 @@ class Persona(ModelBase): | ||
28 | documento = Column('Documento', INTEGER(), nullable=False) | 29 | documento = Column('Documento', INTEGER(), nullable=False) |
29 | fecha_nacimiento = Column('FechaNacimiento', DATE(), nullable=False) | 30 | fecha_nacimiento = Column('FechaNacimiento', DATE(), nullable=False) |
30 | domicilio = Column('Domicilio', VARCHAR(length=100)) | 31 | domicilio = Column('Domicilio', VARCHAR(length=100)) |
32 | + codigo_postal = Column('CodPostal', VARCHAR(length=20)) | ||
33 | + telefono = Column('Telefono', VARCHAR(length=30)) | ||
31 | email = Column('Email', VARCHAR(length=50)) | 34 | email = Column('Email', VARCHAR(length=50)) |
35 | + | ||
36 | + es_discapacitado = Column('EsDiscapacitado', VARCHAR(length=1), nullable=False) | ||
37 | + vive = Column('Vive', VARCHAR(length=1)) | ||
38 | + | ||
39 | + # Relaciones. | ||
32 | sexo_id = Column('Sexo', INTEGER(), ForeignKey('tb_sexo.idSexo'), nullable=False) | 40 | sexo_id = Column('Sexo', INTEGER(), ForeignKey('tb_sexo.idSexo'), nullable=False) |
33 | sexo = relationship('Sexo') | 41 | sexo = relationship('Sexo') |
34 | - es_discapacitado = Column('EsDiscapacitado', VARCHAR(length=1), nullable=False) | ||
35 | - codigo_postal = Column('CodPostal', VARCHAR(length=20)) | 42 | + |
43 | + nacionalidad_id = Column('Nacionalidad', INTEGER(), ForeignKey('tb_Nacionalidad.idNacionalidad')) | ||
44 | + nacionalidad = relationship(Nacionalidad) | ||
45 | + | ||
46 | + # TODO: Agregar TipoDocumento, LocalidadNac | ||
36 | 47 | ||
37 | def __str__(self): | 48 | def __str__(self): |
38 | return f"{self.apellido}, {self.nombres}" | 49 | return f"{self.apellido}, {self.nombres}" |
@@ -5,15 +5,25 @@ from sqlalchemy.orm import Session | @@ -5,15 +5,25 @@ from sqlalchemy.orm import Session | ||
5 | 5 | ||
6 | from database import get_db | 6 | from database import get_db |
7 | from persona.models import Sexo, Persona | 7 | from persona.models import Sexo, Persona |
8 | -from persona.schemas import SexoSchema, PersonaSchema | 8 | +from persona.schemas import SexoSchema, PersonaSchema, SexoSchemaBase |
9 | 9 | ||
10 | router = APIRouter() | 10 | router = APIRouter() |
11 | 11 | ||
12 | 12 | ||
13 | -@router.get("/sexos/", response_model=List[SexoSchema]) | 13 | +@router.get('/sexos/', response_model=List[SexoSchema]) |
14 | def obtener_sexos(db: Session = Depends(get_db)): | 14 | def obtener_sexos(db: Session = Depends(get_db)): |
15 | - queryset = db.query(Sexo).all() | ||
16 | - return queryset | 15 | + lista_sexos = db.query(Sexo).all() |
16 | + return lista_sexos | ||
17 | + | ||
18 | + | ||
19 | +@router.post('/sexos/', response_model=SexoSchema) | ||
20 | +def crear_sexo(sexo: SexoSchemaBase, db: Session = Depends(get_db)): | ||
21 | + instancia = Sexo(**sexo.dict()) | ||
22 | + db.add(instancia) | ||
23 | + db.commit() | ||
24 | + db.refresh(instancia) | ||
25 | + | ||
26 | + return instancia | ||
17 | 27 | ||
18 | 28 | ||
19 | @router.get('/personas/', response_model=List[PersonaSchema]) | 29 | @router.get('/personas/', response_model=List[PersonaSchema]) |
@@ -23,10 +33,12 @@ def obtener_personas( | @@ -23,10 +33,12 @@ def obtener_personas( | ||
23 | search: Optional[str] = None, | 33 | search: Optional[str] = None, |
24 | db: Session = Depends(get_db)): | 34 | db: Session = Depends(get_db)): |
25 | 35 | ||
36 | + queryset = db.query(Persona) | ||
37 | + | ||
26 | if search: | 38 | if search: |
27 | - return db.query(Persona).filter(Persona.documento == search).all() | 39 | + queryset = queryset.filter(Persona.documento == search) |
28 | 40 | ||
29 | - return db.query(Persona).offset(skip).limit(limit).all() | 41 | + return queryset.offset(skip).limit(limit).all() |
30 | 42 | ||
31 | 43 | ||
32 | @router.get('/personas/{persona_id}/', response_model=PersonaSchema) | 44 | @router.get('/personas/{persona_id}/', response_model=PersonaSchema) |
@@ -3,12 +3,17 @@ from typing import Optional | @@ -3,12 +3,17 @@ from typing import Optional | ||
3 | 3 | ||
4 | import pydantic | 4 | import pydantic |
5 | 5 | ||
6 | +from utils.schemas import NacionalidadSchema | ||
6 | 7 | ||
7 | -class SexoSchema(pydantic.BaseModel): | ||
8 | - id: int | 8 | + |
9 | +class SexoSchemaBase(pydantic.BaseModel): | ||
9 | nombre: str | 10 | nombre: str |
10 | abreviacion: Optional[str] = None | 11 | abreviacion: Optional[str] = None |
11 | 12 | ||
13 | + | ||
14 | +class SexoSchema(SexoSchemaBase): | ||
15 | + id: int | ||
16 | + | ||
12 | class Config: | 17 | class Config: |
13 | orm_mode = True | 18 | orm_mode = True |
14 | 19 | ||
@@ -21,10 +26,15 @@ class PersonaSchema(pydantic.BaseModel): | @@ -21,10 +26,15 @@ class PersonaSchema(pydantic.BaseModel): | ||
21 | documento: int | 26 | documento: int |
22 | fecha_nacimiento: date | 27 | fecha_nacimiento: date |
23 | domicilio: Optional[str] = None | 28 | domicilio: Optional[str] = None |
29 | + codigo_postal: Optional[str] = None | ||
24 | email: Optional[str] = None | 30 | email: Optional[str] = None |
25 | - sexo: SexoSchema | 31 | + telefono: Optional[str] = None |
26 | es_discapacitado: str | 32 | es_discapacitado: str |
27 | - codigo_postal: Optional[str] = None | 33 | + vive: Optional[str] = None |
34 | + | ||
35 | + # Relaciones | ||
36 | + sexo: SexoSchema | ||
37 | + nacionalidad: Optional[NacionalidadSchema] = None | ||
28 | 38 | ||
29 | class Config: | 39 | class Config: |
30 | orm_mode = True | 40 | orm_mode = True |
project/apps/utils/__init__.py
0 → 100644
project/apps/utils/models.py
0 → 100644
1 | +from sqlalchemy import Column, INTEGER, VARCHAR | ||
2 | + | ||
3 | +from database import ModelBase | ||
4 | + | ||
5 | + | ||
6 | +class Nacionalidad(ModelBase): | ||
7 | + __tablename__ = 'tb_Nacionalidad' | ||
8 | + | ||
9 | + id = Column('idNacionalidad', INTEGER(), primary_key=True, nullable=False) | ||
10 | + nombre = Column('Descripcion', VARCHAR(length=50), nullable=False) | ||
11 | + nacionalidad_anses_id = Column('idNacionalidadAnses', INTEGER()) | ||
12 | + | ||
13 | + def __str__(self): | ||
14 | + return f"{self.nombre}" | ||
15 | + | ||
16 | + def __repr__(self): | ||
17 | + return f"<Nacionalidad: {self}>" |
project/apps/utils/schemas.py
0 → 100644
@@ -2,8 +2,9 @@ from fastapi import FastAPI | @@ -2,8 +2,9 @@ from fastapi import FastAPI | ||
2 | 2 | ||
3 | from config import settings | 3 | from config import settings |
4 | from persona.routers import router as persona_router | 4 | from persona.routers import router as persona_router |
5 | - | 5 | +from agente.routers import router as agente_router |
6 | 6 | ||
7 | app = FastAPI(debug=settings.debug) | 7 | app = FastAPI(debug=settings.debug) |
8 | 8 | ||
9 | app.include_router(persona_router) | 9 | app.include_router(persona_router) |
10 | +app.include_router(agente_router) |
-
Please register or login to post a comment