forked from TDTP/admin_transporte_backend
117 lines
3.8 KiB
Python
117 lines
3.8 KiB
Python
![]() |
|
||
|
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 django.db import connection
|
||
|
from .. import models, serializers
|
||
|
from django.http import JsonResponse
|
||
|
import logging
|
||
|
|
||
|
class LineaViewSet(viewsets.ModelViewSet):
|
||
|
queryset = models.Linea.objects.all()
|
||
|
serializer_class = serializers.LineaSerializer
|
||
|
filter_backends = [DjangoFilterBackend]
|
||
|
filterset_fields = ['id_operador', 'route_short_name', 'route_long_name', 'vigente']
|
||
|
|
||
|
|
||
|
@action(detail=False, methods=['get'])
|
||
|
def proto(self, request, pk=None):
|
||
|
id_paradero = request.GET['id_paradero']
|
||
|
|
||
|
query = "SELECT json_data \
|
||
|
from gtfs_posiciones_json \
|
||
|
where stop_id = %s"
|
||
|
|
||
|
with connection.cursor() as cursor:
|
||
|
cursor.execute(query, [ id_paradero ])
|
||
|
row = cursor.fetchone()
|
||
|
|
||
|
return JsonResponse(row[0], safe=False)
|
||
|
|
||
|
|
||
|
@action(detail=False, methods=['get'])
|
||
|
def paraderos(self, request, pk=None):
|
||
|
pk = request.GET['id_linea']
|
||
|
|
||
|
paraderos = models.Paradero.objects \
|
||
|
.filter(vigente=True, lineaparadero__id_linea=pk) \
|
||
|
.values('id_paradero','stop_lat','stop_lon','stop_name') \
|
||
|
.all()
|
||
|
return JsonResponse(list(paraderos), safe=False)
|
||
|
|
||
|
|
||
|
@action(detail=False, methods=['get'])
|
||
|
def buses(self, request, pk=None):
|
||
|
pk = request.GET['id_linea']
|
||
|
|
||
|
query = "SELECT distinct \
|
||
|
l.route_color, \
|
||
|
l.route_text_color, \
|
||
|
gp.vehicle_license_plate as Patente_vehiculo, \
|
||
|
speed::numeric(5,2) as speed, \
|
||
|
gp.longitude::numeric, \
|
||
|
gp.latitude::numeric \
|
||
|
from gtfs_posiciones gp \
|
||
|
inner join linea l on (trim(gp.route_id)||'-'||trim(gp.direction_id::varchar)) = l.id_linea \
|
||
|
where l.id_linea = %s"
|
||
|
|
||
|
with connection.cursor() as cursor:
|
||
|
cursor.execute(query, [ pk ])
|
||
|
rows = cursor.fetchall()
|
||
|
|
||
|
buses = []
|
||
|
for row in rows:
|
||
|
buses.append({
|
||
|
'route_color': row[0],
|
||
|
'route_text_color': row[1],
|
||
|
'Patente_vehiculo': row[2],
|
||
|
'speed': row[3],
|
||
|
'longitude': row[4],
|
||
|
'latitude': row[5],
|
||
|
})
|
||
|
|
||
|
return JsonResponse(buses, safe=False)
|
||
|
|
||
|
|
||
|
|
||
|
@action(detail=False, methods=['get'])
|
||
|
def buses_proto(self, request, pk=None):
|
||
|
pk = request.GET['id_linea']
|
||
|
|
||
|
linea = models.Linea.objects \
|
||
|
.filter(id_linea=pk) \
|
||
|
.first()
|
||
|
|
||
|
logging.error(linea)
|
||
|
|
||
|
detalle_buses = []
|
||
|
paraderos = models.Paradero.objects \
|
||
|
.filter(vigente=True, lineaparadero__id_linea=pk) \
|
||
|
.values('id_paradero') \
|
||
|
.all()
|
||
|
|
||
|
query = "SELECT json_data \
|
||
|
from gtfs_posiciones_json \
|
||
|
where stop_id = %s"
|
||
|
|
||
|
for p in paraderos:
|
||
|
id_paradero = p['id_paradero']
|
||
|
|
||
|
with connection.cursor() as cursor:
|
||
|
cursor.execute(query, [ id_paradero ])
|
||
|
datajson = cursor.fetchone()
|
||
|
|
||
|
if datajson != None:
|
||
|
buses = list(filter(lambda rowjson: rowjson['Descripcion'] == linea.route_short_name, datajson[0]))
|
||
|
for bus in buses:
|
||
|
for llegada in bus['Llegadas']:
|
||
|
data_bus = {
|
||
|
'patente': llegada['patente'],
|
||
|
'estimada_gps': llegada['EstimadaGPS'],
|
||
|
'distancia_gps': llegada['DistanciaGPS'],
|
||
|
}
|
||
|
detalle_buses.append(data_bus)
|
||
|
|
||
|
return JsonResponse(detalle_buses, safe=False)
|