# Path: runner/models.py
# class User(UserAbstract):
#     def __str__(self):
#         return self.name ;
#     class Meta:
#         managed = True
#         db_table = 'User'


from django.utils.encoding import python_2_unicode_compatible
from django.db.models import Q
from django.db import models, transaction
from django.db.utils import OperationalError
from runner.models import User
from datetime import datetime, timedelta
from django.utils import timezone
import urllib, urllib2
import json
    source = models.ForeignKey(Stop, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    dest = models.ForeignKey(Stop, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    driver = models.ForeignKey(Employee, models.DO_NOTHING,  limit_choices_to=get_drivers, related_name='+')  # Field name made lowercase.
    conductor = models.ForeignKey(Employee, models.DO_NOTHING,  limit_choices_to=get_conductors, related_name='+')  # Field name made lowercase.
    bus = models.ForeignKey(Bus, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    start_time = models.DateTimeField()  # Field name made lowercase.
    seats_avail = models.IntegerField(default='56')  # Field name made lowercase.
    seats_config = models.CharField(max_length=56, default=def_bus_config)
    journey_time = models.DateTimeField(null=True, blank=True)
    fair = models.IntegerField( null=True, default='0')
    def __str__(self):
        return str(self.source.name)+"("+str(self.source.city)+")"+"-"+str(self.dest.name)+"("+str(self.dest.city)+")"

    def save(self, *args, **kwargs):
        if(self.journey_time and self.fair):
            super(Route, self).save(*args, **kwargs)
        else:
            datadict = gmaps(source=self.source.get_location(), dest=self.dest.get_location())
            self.fair = self.bus.fair_ratio*datadict['distance']/1000
            self.journey_time = self.start_time + timedelta(seconds=datadict['time'])
            super(Route, self).save(*args, **kwargs)

    class Meta:
        managed = True 



@python_2_unicode_compatible
class Ticket(models.Model):
    id = models.AutoField( primary_key=True)  # Field name made lowercase.

# Path: runner/models.py
# class User(UserAbstract):
#     def __str__(self):
#         return self.name ;
#     class Meta:
#         managed = True
#         db_table = 'User'


# Path: bus/models.py
from django.utils.encoding import python_2_unicode_compatible
from django.db.models import Q
from django.db import models, transaction
from django.db.utils import OperationalError
from runner.models import User
from datetime import datetime, timedelta
from django.utils import timezone
import urllib, urllib2
import json

    source = models.ForeignKey(Stop, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    dest = models.ForeignKey(Stop, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    driver = models.ForeignKey(Employee, models.DO_NOTHING,  limit_choices_to=get_drivers, related_name='+')  # Field name made lowercase.
    conductor = models.ForeignKey(Employee, models.DO_NOTHING,  limit_choices_to=get_conductors, related_name='+')  # Field name made lowercase.
    bus = models.ForeignKey(Bus, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.
    start_time = models.DateTimeField()  # Field name made lowercase.
    seats_avail = models.IntegerField(default='56')  # Field name made lowercase.
    seats_config = models.CharField(max_length=56, default=def_bus_config)
    journey_time = models.DateTimeField(null=True, blank=True)
    fair = models.IntegerField( null=True, default='0')
    def __str__(self):
        return str(self.source.name)+"("+str(self.source.city)+")"+"-"+str(self.dest.name)+"("+str(self.dest.city)+")"

    def save(self, *args, **kwargs):
        if(self.journey_time and self.fair):
            super(Route, self).save(*args, **kwargs)
        else:
            datadict = gmaps(source=self.source.get_location(), dest=self.dest.get_location())
            self.fair = self.bus.fair_ratio*datadict['distance']/1000
            self.journey_time = self.start_time + timedelta(seconds=datadict['time'])
            super(Route, self).save(*args, **kwargs)

    class Meta:
        managed = True 



@python_2_unicode_compatible
class Ticket(models.Model):
    id = models.AutoField( primary_key=True)  # Field name made lowercase.

    user = models.ForeignKey(User, models.DO_NOTHING,  related_name='+')  # Field name made lowercase.