# Path: timepredictor/models.py
# class PrediccionTiempos(models.Model):
#     id = models.IntegerField(primary_key=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     codigo = models.CharField(max_length=6, blank=True, null=True)
#     tiempo_tstamp = models.DateTimeField(blank=True, null=True)
#     distancia = models.IntegerField(blank=True, null=True)
#     tiempo = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'prediccion_tiempos'
# 
# class UltimosGps(models.Model):
#     id = models.IntegerField(primary_key=True)
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     servicio_usuario = models.CharField(max_length=50, blank=True, null=True)
#     tiempo = models.DateTimeField(blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
#     x = models.IntegerField(blank=True, null=True)
#     y = models.IntegerField(blank=True, null=True)
#     dist_en_ruta = models.IntegerField(blank=True, null=True)
#     dist_a_ruta = models.IntegerField(blank=True, null=True)
#     velocidad_instantanea = models.IntegerField(blank=True, null=True)
#     velocidad_2gps = models.IntegerField(blank=True, null=True)
#     velocidad_4gps = models.IntegerField(blank=True, null=True)
#     operador = models.IntegerField(blank=True, null=True)
#     orientacion = models.IntegerField(blank=True, null=True)
#     tipo = models.CharField(max_length=10, blank=True, null=True)
#     capacidad = models.IntegerField(blank=True, null=True)
#     tiempo_sgt_bus = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ultimos_gps'


from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View
from timepredictor.models import PrediccionTiempos, UltimosGps
        positions = UltimosGps.objects.filter(patente = licencePlate)

	response = []
	for aPosition in positions:
	    response.append({
                'licencePlate': aPosition.patente, 
                'authRoute': aPosition.servicio, 
                'userRoute': aPosition.servicio_usuario, 
                'distOnroute': aPosition.dist_en_ruta, 
                'distToRoute': aPosition.dist_a_ruta, 
                'InstVelocity': aPosition.velocidad_instantanea, 
                'velocity2GPS': aPosition.velocidad_2gps, 
                'velocity4GPS': aPosition.velocidad_4gps, 
                'operator': aPosition.operador, 
                'latitude': aPosition.latitud, 
                'longitude': aPosition.longitud, 
                'time': aPosition.tiempo,
                'orientation': aPosition.orientacion,
                'type': aPosition.tipo,
                'capacity': aPosition.capacidad})

	return JsonResponse(response, safe=False)

class GetActiveBuses(View):
    '''This class requests to the database the buses are doing a trip '''

    def __init__(self):
	self.context={}

    def get(self, request):

# Path: timepredictor/models.py
# class PrediccionTiempos(models.Model):
#     id = models.IntegerField(primary_key=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     codigo = models.CharField(max_length=6, blank=True, null=True)
#     tiempo_tstamp = models.DateTimeField(blank=True, null=True)
#     distancia = models.IntegerField(blank=True, null=True)
#     tiempo = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'prediccion_tiempos'
# 
# class UltimosGps(models.Model):
#     id = models.IntegerField(primary_key=True)
#     patente = models.CharField(max_length=7, blank=True, null=True)
#     servicio = models.CharField(max_length=50, blank=True, null=True)
#     servicio_usuario = models.CharField(max_length=50, blank=True, null=True)
#     tiempo = models.DateTimeField(blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
#     x = models.IntegerField(blank=True, null=True)
#     y = models.IntegerField(blank=True, null=True)
#     dist_en_ruta = models.IntegerField(blank=True, null=True)
#     dist_a_ruta = models.IntegerField(blank=True, null=True)
#     velocidad_instantanea = models.IntegerField(blank=True, null=True)
#     velocidad_2gps = models.IntegerField(blank=True, null=True)
#     velocidad_4gps = models.IntegerField(blank=True, null=True)
#     operador = models.IntegerField(blank=True, null=True)
#     orientacion = models.IntegerField(blank=True, null=True)
#     tipo = models.CharField(max_length=10, blank=True, null=True)
#     capacidad = models.IntegerField(blank=True, null=True)
#     tiempo_sgt_bus = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ultimos_gps'


# Path: timepredictor/views.py
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View
from timepredictor.models import PrediccionTiempos, UltimosGps

        positions = UltimosGps.objects.filter(patente = licencePlate)

	response = []
	for aPosition in positions:
	    response.append({
                'licencePlate': aPosition.patente, 
                'authRoute': aPosition.servicio, 
                'userRoute': aPosition.servicio_usuario, 
                'distOnroute': aPosition.dist_en_ruta, 
                'distToRoute': aPosition.dist_a_ruta, 
                'InstVelocity': aPosition.velocidad_instantanea, 
                'velocity2GPS': aPosition.velocidad_2gps, 
                'velocity4GPS': aPosition.velocidad_4gps, 
                'operator': aPosition.operador, 
                'latitude': aPosition.latitud, 
                'longitude': aPosition.longitud, 
                'time': aPosition.tiempo,
                'orientation': aPosition.orientacion,
                'type': aPosition.tipo,
                'capacity': aPosition.capacidad})

	return JsonResponse(response, safe=False)

class GetActiveBuses(View):
    '''This class requests to the database the buses are doing a trip '''

    def __init__(self):
	self.context={}

    def get(self, request):

        activeBuses = UltimosGps.objects.order_by('patente', 'servicio').distinct('patente', 'servicio')