Merge branch 'merge/ronald' into 'master'

Merge/ronald

See merge request m3f_usm/admin_transporte/backend!2
francisco/photos
Francisco Sandoval 2024-01-29 23:54:34 +00:00
commit a9721c6733
7 changed files with 158 additions and 13 deletions

View File

@ -405,3 +405,32 @@ class RolLinea(models.Model):
managed = True managed = True
db_table = 'rol_linea' db_table = 'rol_linea'
unique_together = (('id_rol', 'id_linea'),) unique_together = (('id_rol', 'id_linea'),)
class RedTransporte(models.Model):
id_red = models.CharField(primary_key=True, max_length=10)
nombre_red = models.CharField(max_length=100, blank=True, null=True)
descripcion = models.CharField(max_length=1000, blank=True, null=True)
url_gtfs_rt = models.CharField(max_length=100, blank=True, null=True)
api_key = models.CharField(max_length=100, blank=True, null=True)
vigente = models.BooleanField(blank=True, null=True)
class Meta:
managed = False
db_table = 'red_transporte'
db_table_comment = 'Tabla que contiene las diferentes tipos de red de transporte, como son los de transporte publico, trenes, colectivos, etc.'
class GtfsArchivo(models.Model):
id_gtfs = models.AutoField(primary_key=True)
id_red = models.ForeignKey('RedTransporte', models.DO_NOTHING, db_column='id_red', blank=True, null=True)
archivo = models.CharField(max_length=200, blank=True, null=True)
ruta_archivo = models.CharField(max_length=200, blank=True, null=True)
valid_from = models.DateField(blank=True, null=True)
created = models.DateTimeField(blank=True, null=True)
usuario = models.CharField(max_length=100, blank=True, null=True)
vigente = models.BooleanField(blank=True, null=True)
status = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'gtfs_archivo'
db_table_comment = 'Registro de los archivos GTFS que se cargan en el sistema'

View File

@ -138,4 +138,14 @@ class RolLineaSerializer(serializers.ModelSerializer):
class LetreroLUR_Serializer(serializers.ModelSerializer): class LetreroLUR_Serializer(serializers.ModelSerializer):
class Meta: class Meta:
model = models.LetreroLUR model = models.LetreroLUR
fields = '__all__'
class RedTransporteSerializer(serializers.ModelSerializer):
class Meta:
model = models.RedTransporte
fields = '__all__'
class GtfsArchivoSerializer(serializers.ModelSerializer):
class Meta:
model = models.GtfsArchivo
fields = '__all__' fields = '__all__'

View File

@ -2,7 +2,7 @@ from django.urls import path, include
from rest_framework import routers from rest_framework import routers
# from api import views # from api import views
from api.views import usuario, auth, aplicacion, tipo, persona, comuna, region, rol, rolaplicacion from api.views import usuario, auth, aplicacion, tipo, persona, comuna, region, rol, rolaplicacion
from api.views import mapa, linea, letrero_lur, operador from api.views import mapa, linea, letrero_lur, operador,red_transporte,gtfs_archivo
from api.views import paradero, paradero_imagen, linea_paradero from api.views import paradero, paradero_imagen, linea_paradero
from api.views import dispositivo from api.views import dispositivo
from api.views import rol_linea from api.views import rol_linea
@ -26,6 +26,8 @@ router.register('lineas', linea.LineaViewSet)
router.register('lineas-paradero', linea_paradero.LineaParaderoViewSet) router.register('lineas-paradero', linea_paradero.LineaParaderoViewSet)
router.register('letreros-lur', letrero_lur.LetreroLUR_ViewSet) router.register('letreros-lur', letrero_lur.LetreroLUR_ViewSet)
router.register('operadores', operador.OperadorViewSet) router.register('operadores', operador.OperadorViewSet)
router.register('red-transporte', red_transporte.RedTransporteViewSet)
router.register('gtfs-archivo', gtfs_archivo.GtfsArchivoViewSet)
router.register('roles', rol.RolViewSet) router.register('roles', rol.RolViewSet)
router.register('rolyaplicacion', rolaplicacion.RolAplicacionViewSet, basename='rol_aplicacion') router.register('rolyaplicacion', rolaplicacion.RolAplicacionViewSet, basename='rol_aplicacion')
router.register('roles-lineas', rol_linea.RolLineaViewSet, basename='rol_linea') router.register('roles-lineas', rol_linea.RolLineaViewSet, basename='rol_linea')

View File

@ -6,7 +6,7 @@ ALTER TABLE rol_linea drop CONSTRAINT IF EXISTS rol_linea_id_linea_fkey;
ALTER TABLE linea_paradero drop CONSTRAINT IF EXISTS linea_paradero_id_linea_fkey; ALTER TABLE linea_paradero drop CONSTRAINT IF EXISTS linea_paradero_id_linea_fkey;
----- -----
/*
update operador update operador
set vigente =false set vigente =false
where id_operador not in (select agency_id from z_agency za); where id_operador not in (select agency_id from z_agency za);
@ -26,7 +26,7 @@ from z_agency za
where agency_id not in (select id_operador from operador za); where agency_id not in (select id_operador from operador za);
----- -----
*/
delete from gtfs_calendar ; delete from gtfs_calendar ;
----- -----
@ -36,11 +36,11 @@ select service_id, monday::bool , tuesday::bool , wednesday::bool , thursday::bo
from z_calendar zc; from z_calendar zc;
----- -----
/*
update paradero p update paradero p
set vigente = false set vigente = false
where id_paradero::text not in (select stop_id from z_stops ); where id_paradero::text not in (select stop_id from z_stops );
*/
----- -----
update paradero update paradero
@ -69,55 +69,92 @@ delete from linea_paradero;
----- -----
delete from linea ; /*delete from linea*/ ;
----- -----
update linea
set vigente = false
where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' )
and id_linea not in (select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar)
from z_routes zr
inner join z_trips zt
on zr.route_id =zt.route_id
);
update linea
set vigente = true
where id_red in (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' )
and id_linea in (select trim(zr.route_id)||'-'||trim(zt.direction_id::varchar)
from z_routes zr
inner join z_trips zt
on zr.route_id =zt.route_id
);
/*
update linea
route_short_name = (select substring(zt.trip_headsign FROM '(\S+) -') as route_short_name )
route_desc =
route_type =
route_url
route_color
route_text_color
route_long_name
vigente
id_red = (select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1)
id_linea
*/
insert into linea insert into linea
select distinct select distinct
trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id,
zr.agency_id , null,--zr.agency_id ,
substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , substring(zt.trip_headsign FROM '(\S+) -') as route_short_name ,
zr.route_desc , zr.route_desc ,
zr.route_type::numeric , zr.route_type::numeric ,
zr.route_url, zr.route_url,
zr.route_color , zr.route_color ,
zr.route_text_color , zr.route_text_color ,
zt.trip_headsign as route_long_name , true zt.trip_headsign as route_long_name , true ,
(select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red
from z_routes zr from z_routes zr
inner join z_trips zt inner join z_trips zt
on zr.route_id =zt.route_id on zr.route_id =zt.route_id
and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 1)||'-'||SPLIT_PART(route_long_name, '-', 2)),' ','') and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 1)||'-'||SPLIT_PART(route_long_name, '-', 2)),' ','')
where route_long_name not like '%'||route_short_name ||'%' where route_long_name not like '%'||route_short_name ||'%'
and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea )
union union
select distinct select distinct
trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id,
zr.agency_id , null,--zr.agency_id ,
substring(zt.trip_headsign FROM '(\S+) -') as route_short_name , substring(zt.trip_headsign FROM '(\S+) -') as route_short_name ,
zr.route_desc , zr.route_desc ,
zr.route_type::numeric , zr.route_type::numeric ,
zr.route_url, zr.route_url,
zr.route_color , zr.route_color ,
zr.route_text_color , zr.route_text_color ,
zt.trip_headsign as route_long_name , true zt.trip_headsign as route_long_name , true ,
(select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red
from z_routes zr from z_routes zr
inner join z_trips zt inner join z_trips zt
on zr.route_id =zt.route_id on zr.route_id =zt.route_id
and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 3)||'-'||SPLIT_PART(route_long_name, '-', 4)),' ','') and replace (zt.trip_headsign,' ','') =replace ((SPLIT_PART(route_long_name, '-', 3)||'-'||SPLIT_PART(route_long_name, '-', 4)),' ','')
where route_long_name not like '%'||route_short_name ||'%' where route_long_name not like '%'||route_short_name ||'%'
and (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea )
union union
select distinct select distinct
trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id, trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) as route_id,
zr.agency_id , null,--zr.agency_id ,
route_short_name , route_short_name ,
zr.route_desc , zr.route_desc ,
zr.route_type::numeric , zr.route_type::numeric ,
zr.route_url, zr.route_url,
zr.route_color , zr.route_color ,
zr.route_text_color , zr.route_text_color ,
zt.trip_headsign as route_long_name , true zt.trip_headsign as route_long_name , true ,
(select id_red from gtfs_archivo where trim(upper(status))='PROCESANDO' limit 1) as id_red
from z_routes zr from z_routes zr
inner join z_trips zt inner join z_trips zt
on zr.route_id =zt.route_id on zr.route_id =zt.route_id
@ -126,6 +163,7 @@ select distinct
or or
route_long_name like '%'||route_short_name ||'%' route_long_name like '%'||route_short_name ||'%'
) )
where (trim(zr.route_id)||'-'||trim(zt.direction_id::varchar) ) not in (select id_linea from linea )
order by 1; order by 1;
----- -----
@ -192,6 +230,10 @@ SET stop_name = REGEXP_REPLACE(
'gi' 'gi'
); );
-----
update paradero
set id_comuna = (select id_comuna from comuna_georeferencia as c where ST_Contains(c.geom, ST_SetSRID(ST_MakePoint(paradero.stop_lon, paradero.stop_lat), 4326)) limit 1);
----- -----
ALTER TABLE rol_linea ADD CONSTRAINT rol_linea_id_linea_fkey FOREIGN KEY (id_linea) REFERENCES linea(id_linea); ALTER TABLE rol_linea ADD CONSTRAINT rol_linea_id_linea_fkey FOREIGN KEY (id_linea) REFERENCES linea(id_linea);

View File

@ -0,0 +1,24 @@
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from .. import models , serializers
from django.db import models as dj_models
class GtfsArchivoViewSet(viewsets.ModelViewSet):
queryset = models.GtfsArchivo.objects.all()
serializer_class = serializers.GtfsArchivoSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['id_gtfs', 'id_red', 'archivo','valida_from','created','usuario','vigente','status']
def create(self, request, *args, **kwargs):
# se indica que si no se indico el id, entonces sea el maximo + 1
if not request.data.get('id_gtfs', None):
max_id = models.GtfsArchivo.objects.aggregate(dj_models.Max('id_gtfs'))['id_gtfs__max']
new_id = max_id + 1 if max_id is not None else 1
request.data['id_gtfs'] = new_id
return super().create(request, *args, **kwargs)

View File

@ -3,6 +3,7 @@ from rest_framework import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import action from rest_framework.decorators import action
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from .. import models, serializers from .. import models, serializers
class OperadorViewSet(viewsets.ModelViewSet): class OperadorViewSet(viewsets.ModelViewSet):
@ -10,3 +11,16 @@ class OperadorViewSet(viewsets.ModelViewSet):
serializer_class = serializers.OperadorSerializer serializer_class = serializers.OperadorSerializer
filter_backends = [DjangoFilterBackend] filter_backends = [DjangoFilterBackend]
filterset_fields = ['id_region', 'nombre_operador', 'vigente'] filterset_fields = ['id_region', 'nombre_operador', 'vigente']
def create(self, request, *args, **kwargs):
# Comprueba si 'id_operador' está presente en la petición y no está vacío
id_operador = request.data.get('id_operador')
if not id_operador:
# Si 'id_operador' no está presente o está vacío, retorna un error 400
return Response(
{"error": "No se puede crear el operador: 'id_operador' no proporcionado."},
status=status.HTTP_400_BAD_REQUEST
)
# Si 'id_operador' está presente, procede con la creación normal
return super().create(request, *args, **kwargs)

View File

@ -0,0 +1,24 @@
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from .. import models , serializers
from django.db import models as dj_models
class RedTransporteViewSet(viewsets.ModelViewSet):
queryset = models.RedTransporte.objects.all()
serializer_class = serializers.RedTransporteSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['id_red', 'nombre_red', 'descripcion','vigente']
def create(self, request, *args, **kwargs):
# se indica que si no se indico el id, entonces sea el maximo + 1
if not request.data.get('id_red', None):
max_id = models.RedTransporte.objects.aggregate(dj_models.Max('id_red'))['id_red__max']
new_id = max_id + 1 if max_id is not None else 1
request.data['id_red'] = new_id
return super().create(request, *args, **kwargs)