# Path: timeperstreet/models.py
# class Tramos15Min(models.Model):
#     eje_id = models.CharField(max_length=200, blank=True, null=True)
#     secuencia_eje_macro = models.IntegerField(blank=True, null=True)
#     tramo = models.CharField(max_length=200)
#     eje = models.CharField(max_length=200)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     calle_origen = models.CharField(max_length=200, blank=True, null=True)
#     calle_destino = models.CharField(max_length=200, blank=True, null=True)
#     dist_en_ruta = models.IntegerField()
#     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)
#     velocidad_tramo = models.FloatField(blank=True, null=True)
#     tiempo_viaje_tramo = models.IntegerField(blank=True, null=True)
#     tiempo_viaje_eje = models.IntegerField(blank=True, null=True)
#     velocidad_eje = models.FloatField(blank=True, null=True)
#     super_lento = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'tramos_15min'
#         unique_together = (('tramo', 'eje', 'dist_en_ruta'),)
# 
# class OrigenYDestinoEjes15Min(models.Model):
#     eje = models.CharField(max_length=200, blank=True, null=True)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     nombre = models.CharField(max_length=200, blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'origen_y_destino_ejes_15_min'
# 
# class EjesMacro(models.Model):
#     eje_id = models.CharField(primary_key=True, max_length=200)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     tiempo_viaje = models.IntegerField(blank=True, null=True)
#     velocidad = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ejes_macro'
# 
# class VelocidadGlobal(models.Model):                                                                                  
#     id = models.IntegerField(primary_key=True)
#     velocidad_sin_corredor = models.FloatField(blank=True, null=True)
#     velocidad_con_corredor = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'velocidad_global'


from django.shortcuts import render
from django.views.generic import View
from django.http import JsonResponse
from django.db import connection
from timeperstreet.models import Tramos15Min, OrigenYDestinoEjes15Min, EjesMacro, VelocidadGlobal
def getColor(velocity):
    """
    grey
    red
    orange
    yeloow
    green
    dark green
    blue
    """
    return '#c4c4c4' if velocity <  0  else \
           '#ff0000' if velocity <= 15 else \
           '#ff9000' if velocity <= 19 else \
           '#fff600' if velocity <= 21 else \
           '#19ff00' if velocity <= 25 else \
           '#0d8900' if velocity <= 30 else \
           '#2133f2'


class GetStreetTableData(View):
    '''This class requests to the database the street secction with travel time '''

    def __init__(self):
        """the contructor, context are the parameter given to the html template"""
        self.context={}

    def get(self, request):
        ''' '''
        axisId = request.GET.get('axisId')
        if axisId:

# Path: timeperstreet/models.py
# class Tramos15Min(models.Model):
#     eje_id = models.CharField(max_length=200, blank=True, null=True)
#     secuencia_eje_macro = models.IntegerField(blank=True, null=True)
#     tramo = models.CharField(max_length=200)
#     eje = models.CharField(max_length=200)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     calle_origen = models.CharField(max_length=200, blank=True, null=True)
#     calle_destino = models.CharField(max_length=200, blank=True, null=True)
#     dist_en_ruta = models.IntegerField()
#     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)
#     velocidad_tramo = models.FloatField(blank=True, null=True)
#     tiempo_viaje_tramo = models.IntegerField(blank=True, null=True)
#     tiempo_viaje_eje = models.IntegerField(blank=True, null=True)
#     velocidad_eje = models.FloatField(blank=True, null=True)
#     super_lento = models.IntegerField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'tramos_15min'
#         unique_together = (('tramo', 'eje', 'dist_en_ruta'),)
# 
# class OrigenYDestinoEjes15Min(models.Model):
#     eje = models.CharField(max_length=200, blank=True, null=True)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     nombre = models.CharField(max_length=200, blank=True, null=True)
#     latitud = models.FloatField(blank=True, null=True)
#     longitud = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'origen_y_destino_ejes_15_min'
# 
# class EjesMacro(models.Model):
#     eje_id = models.CharField(primary_key=True, max_length=200)
#     hito_origen = models.CharField(max_length=200, blank=True, null=True)
#     hito_destino = models.CharField(max_length=200, blank=True, null=True)
#     zona = models.CharField(max_length=200, blank=True, null=True)
#     destino = models.CharField(max_length=200, blank=True, null=True)
#     tiempo_viaje = models.IntegerField(blank=True, null=True)
#     velocidad = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'ejes_macro'
# 
# class VelocidadGlobal(models.Model):                                                                                  
#     id = models.IntegerField(primary_key=True)
#     velocidad_sin_corredor = models.FloatField(blank=True, null=True)
#     velocidad_con_corredor = models.FloatField(blank=True, null=True)
# 
#     class Meta:
#         managed = False
#         db_table = 'velocidad_global'


# Path: timeperstreet/views.py
from django.shortcuts import render
from django.views.generic import View
from django.http import JsonResponse
from django.db import connection
from timeperstreet.models import Tramos15Min, OrigenYDestinoEjes15Min, EjesMacro, VelocidadGlobal

def getColor(velocity):
    """
    grey
    red
    orange
    yeloow
    green
    dark green
    blue
    """
    return '#c4c4c4' if velocity <  0  else \
           '#ff0000' if velocity <= 15 else \
           '#ff9000' if velocity <= 19 else \
           '#fff600' if velocity <= 21 else \
           '#19ff00' if velocity <= 25 else \
           '#0d8900' if velocity <= 30 else \
           '#2133f2'


class GetStreetTableData(View):
    '''This class requests to the database the street secction with travel time '''

    def __init__(self):
        """the contructor, context are the parameter given to the html template"""
        self.context={}

    def get(self, request):
        ''' '''
        axisId = request.GET.get('axisId')
        if axisId:

            points = Tramos15Min.objects.filter(eje_id=axisId)