entity.py
6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import time
from typing import List, Any, Optional, Union
from fastapi import APIRouter, Depends, Request, HTTPException, Body, Response
from sqlalchemy.orm import Session
from sqlalchemy import and_, or_, select
from presupuestov1 import model, schema, constant, schemas, models, search
from presupuestov1.routers._decorators import not_found
router_entidad = APIRouter(
prefix='/entidad',
tags=['entidad'],
)
router_ubigeo = APIRouter(
prefix='/ubigeo',
tags=['ubigeo'],
)
router_da = APIRouter(
prefix='/direccion_administrativa',
tags=['direccion_administrativa'],
)
###############################################################################
# GET
###############################################################################
# clas
@router_entidad.get('/clasificador', response_model=List[schemas.classifier.ClasInstitucional])
@not_found
def fetch_entidad_classifier(
db: Session = Depends(model.get_db),
):
return db.query(models.classifier.ClasInstitucional).all()
@router_ubigeo.get('/clasificador', response_model=List[schemas.classifier.ClasGeografico])
@not_found
def fetch_ubigeo_classifier(
db: Session = Depends(model.get_db),
):
return db.query(models.classifier.ClasGeografico).all()
# base
@router_entidad.get('/{entity_id:str}', response_model=schemas.entity.ClasInstitucionalIngresosGastos)
@not_found
def fetch_entidad(
entity_id: str,
db: Session = Depends(model.get_db),
):
ClasInstitucional = models.classifier.ClasInstitucional
entidad = db.query(ClasInstitucional).filter(
ClasInstitucional.entidad == entity_id
).one()
gastos_ingresos = get_resumen(db, 'entidad', entity_id)
setattr(entidad, 'gastos_ingresos', gastos_ingresos)
return entidad
@router_ubigeo.get('/{ubigeo_id:str}', response_model=schemas.entity.ClasGeograficoIngresosGastos)
@not_found
def fetch_ubigeo(
ubigeo_id: str,
db: Session = Depends(model.get_db),
):
ClasGeografico = models.classifier.ClasGeografico
ubigeo = db.query(ClasGeografico).filter(
ClasGeografico.ubigeo == ubigeo_id
).one()
gastos_ingresos = get_resumen(db, 'municipio_ubigeo', ubigeo_id)
setattr(ubigeo, 'gastos_ingresos', gastos_ingresos)
return ubigeo
@router_da.get('/{da_id:str}', response_model=List[schemas.entity.EntidadResumen])
@not_found
def fetch_da(
da_id: str,
db: Session = Depends(model.get_db),
):
gastos_ingresos = get_resumen(db, 'entidad_da', da_id)
return gastos_ingresos
# objetos
@router_entidad.get('/{entity_id:str}/objetos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_entidad_objetos(
entity_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad', entity_id, 'objeto', query)
@router_ubigeo.get('/{ubigeo_id:str}/objetos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_ubigeo_objetos(
ubigeo_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'municipio_ubigeo', ubigeo_id, 'objeto', query)
@router_da.get('/{da_id:str}/objetos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_da_objetos(
da_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad_da', da_id, 'objeto', query)
@router_entidad.get('/{entity_id:str}/finfuns', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_entidad_finfuns(
entity_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad', entity_id, 'finfun', query)
@router_ubigeo.get('/{ubigeo_id:str}/finfuns', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_ubigeo_finfuns(
ubigeo_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'municipio_ubigeo', ubigeo_id, 'finfun', query)
@router_da.get('/{da_id:str}/finfuns', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_da_finfuns(
da_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad_da', da_id, 'finfun', query)
@router_entidad.get('/{entity_id:str}/actecos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_entidad_actecos(
entity_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad', entity_id, 'acteco', query)
@router_ubigeo.get('/{ubigeo_id:str}/actecos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_ubigeo_actecos(
ubigeo_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'municipio_ubigeo', ubigeo_id, 'acteco', query)
@router_da.get('/{da_id:str}/actecos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_da_actecos(
da_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad_da', da_id, 'acteco', query)
@router_entidad.get('/{entity_id:str}/rubros', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_entidad_rubros(
entity_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad', entity_id, 'rubro', query)
@router_entidad.get('/{entity_id:str}/organismos', response_model=List[schemas.entity.EntidadDistribucion])
def fetch_entidad_organismos(
entity_id: str,
db: Session = Depends(model.get_db),
query: schema.QueryParams = Depends(),
):
return get_distribuciones(db, 'entidad', entity_id, 'organismo', query)
###############################################################################
# private
###############################################################################
@not_found
def get_distribuciones(db, tipo_codigo, entity_id, dimension, query):
EntidadDistribucionesSchema = schemas.entity.EntidadDistribucion
EntidadDistribuciones = models.entity.EntidadDistribuciones
schema_fields = EntidadDistribucionesSchema.model_fields.keys()
cols = [getattr(EntidadDistribuciones, name) for name in schema_fields]
filters = []
if query.gestion is not None:
filters.append(EntidadDistribuciones.gestion == query.gestion)
filters.extend([
EntidadDistribuciones.tipo_codigo == tipo_codigo,
EntidadDistribuciones.codigo == entity_id,
EntidadDistribuciones.dimension == dimension,
])
return db.execute(
select(*cols).where(*filters)
).mappings().all()
def get_resumen(db, tipo_codigo, codigo):
EntidadResumenes = models.entity.EntidadResumenes
EntidadResumenSchema = schemas.entity.EntidadResumen
schema_fields = EntidadResumenSchema.model_fields.keys()
cols = [getattr(EntidadResumenes, name) for name in schema_fields]
return db.execute(
select(*cols).where(
EntidadResumenes.tipo_codigo == tipo_codigo,
EntidadResumenes.codigo == codigo,
)
).all()