class LocationModel(models.Model): """ Location object is the representation of physical station Fields: id -- This is the unique identifier for object instance. name -- This is the common identifier for a physical location. coordinates -- Latitude and Longuitude as string. example. "19.4094937,-99.1634261" geometry -- Similar to coordinate but using with postgis """ id = models.CharField(default=create_id("loc_"), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) # latitude = models.DecimalField(max_digits=19, decimal_places=16) # longitude = models.DecimalField(max_digits=19, decimal_places=16) coordinates = models.CharField(max_length=100, default="") # geometry = models.PointField() #TODO:Install GDAL Library def __str__(self): return self.name class Meta: ordering = ["-id"]
class LineModel(models.Model): id = models.CharField(default=create_id('line_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8)
class LocationModel(models.Model): """ Location object is the representation of physical station Fields: id -- This is the unique identifier for object instance. name -- This is the common identifier for a physical location. coordinates -- Latitude and Longuitude as string. example. "19.4094937,-99.1634261" geometry -- Similar to coordinate but using with postgis """ id = models.CharField(default=create_id('loc_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) latitude = models.DecimalField(max_digits=19, decimal_places=16) longitude = models.DecimalField(max_digits=19, decimal_places=16) def __str__(self): return self.name @property def geo(self): return "{},{}".format(self.latitude, self.longitude)
class LocationModel(UserCreate, TimeStamp, Active): """ Location object is the representation of physical station Fields: id -- This is the unique identifier for object instance. name -- This is the common identifier for a physical location. coordinates -- Latitude and Longuitude as string. example. "19.4094937,-99.1634261" geometry -- Similar to coordinate but using with postgis """ id = models.CharField(default=create_id('loc_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100, validators=[ RegexValidator(regex=REGEX_ONLY_LETTERS, message=ONLY_LETTERS_MESSAGE) ]) latitude = models.DecimalField(max_digits=19, decimal_places=16) longitude = models.DecimalField(max_digits=19, decimal_places=16) key = models.CharField(max_length=4, unique=True, help_text="The 3 fisrt letters") def __str__(self): return '%s-%s - longitude:%s - latitude:%s' % ( self.name, self.key, self.longitude, self.latitude)
class RouteModel(models.Model): """ Route object is the representation of physical Route Fields: id -- This is the unique identifier for object instance. line --Line of the Route. stations -- Stations of the route. direction -- Direction of the route. is_active -- If the route is active. created_at -- Date of creation updated_at -- Date of last modification """ id = models.CharField(default=create_id("route_"), primary_key=True, max_length=30, unique=True) line = models.ForeignKey(LineModel, on_delete=models.DO_NOTHING) stations = models.ManyToManyField(StationModel) direction = models.BooleanField(default=True) is_active = models.BooleanField(default=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-id"]
class StationModel(models.Model): id = models.CharField(default=create_id('sta_'), primary_key=True, max_length=30, unique=True) location = models.ForeignKey(LocationModel, on_delete=models.DO_NOTHING) order = models.IntegerField(default=0) is_active = models.BooleanField(default=True)
class RouteModel(models.Model): id = models.CharField(default=create_id('route_'), primary_key=True, max_length=30, unique=True) line = models.ForeignKey(LineModel, on_delete=models.DO_NOTHING) stations = models.ManyToManyField(StationModel) direction = models.BooleanField(default=True) is_active = models.BooleanField(default=True)
class LineModel(models.Model): # used to correct collisions due to unique IDs generated in migrations # id = models.AutoField(auto_created=True, primary_key=True, # max_length=30, unique=True) id = models.CharField(default=create_id('line_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8)
class LineModel(models.Model): id = models.CharField(default=create_id('line_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8) class Meta: verbose_name = 'Linea' def __str__(self): return self.name
class Line(models.Model): id = models.CharField(default=create_id('line_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8) def __str__(self): return self.name @property def prefix(self): prefix = self.__class__.__name__.lower()[0:3] + '_' return prefix
class RouteModel(models.Model): id = models.CharField(default=create_id('route_'), primary_key=True, max_length=30, unique=True) line = models.ForeignKey(LineModel, on_delete=models.DO_NOTHING) stations = models.ManyToManyField(StationModel) direction = models.BooleanField(default=True) is_active = models.BooleanField(default=True) class Meta: verbose_name = 'Ruta' def __str__(self): return self.line
class Route(models.Model): id = models.CharField(default=create_id('route_'), primary_key=True, max_length=30, unique=True) line = models.ForeignKey(Line, on_delete=models.DO_NOTHING) stations = models.ManyToManyField(Station) direction = models.BooleanField(default=True) is_active = models.BooleanField(default=True) def __str__(self): return self.line.name @property def prefix(self): prefix = self.__class__.__name__.lower()[0:3] + '_' return prefix
class LineModel(models.Model): """ LineModel: Django model for a Urbvan line """ class Meta: ordering = ['id'] verbose_name = 'line' verbose_name_plural = 'lines' id = models.CharField(default=create_id('line_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8) def __str__(self): return self.name
class RouteModel(models.Model): """ RouteModel: Django model for a Route of a line """ class Meta: ordering = ['id'] verbose_name = 'route' verbose_name_plural = 'routes' id = models.CharField(default=create_id('route_'), primary_key=True, max_length=30, unique=True) line = models.ForeignKey(LineModel, on_delete=models.DO_NOTHING) stations = models.ManyToManyField(LocationModel, through="stations.StationModel", blank=True) direction = models.BooleanField(default=True) is_active = models.BooleanField(default=True) # TODO: Change the direction boolean field for a more descriptive type like Char field with choices def __str__(self): return 'Route {} - {}'.format(self.line, self.direction)
class LineModel(models.Model): """ Line object is the representation of physical Line Fields: id -- This is the unique identifier for object instance. name -- Name of the Line. color -- Color of the Line. created_at -- Date of creation updated_at -- Date of last modification """ id = models.CharField(default=create_id("line_"), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) color = models.CharField(max_length=8) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-id"]
class StationModel(models.Model): """ Station object is the representation of physical station Fields: id -- This is the unique identifier for object instance. location -- Location of the station(coordinates). order -- Order of the station. is_active -- If the route is active. created_at -- Date of creation updated_at -- Date of last modification """ id = models.CharField( default=create_id("sta_"), primary_key=True, max_length=30, unique=True ) location = models.ForeignKey(LocationModel, on_delete=models.DO_NOTHING) order = models.IntegerField(default=0) is_active = models.BooleanField(default=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["-id"]
class StationModel(models.Model): """ Station object is the instance of a physical station to be included in a route Fields: id -- This is the unique identifier for object instance. location -- Foreign key to a physical station. order -- Position in the route as Integer is_active -- the station is active """ class Meta: ordering = ['route_id', '-is_active', 'order'] verbose_name = 'station' verbose_name_plural = 'stations' id = models.CharField(default=create_id('sta_'), primary_key=True, max_length=30, unique=True) location = models.ForeignKey(LocationModel, on_delete=models.CASCADE) route = models.ForeignKey(RouteModel, on_delete=models.CASCADE) order = models.IntegerField(default=0) is_active = models.BooleanField(default=True)
class LocationModel(models.Model): """ Location object is the representation of physical station Fields: id -- This is the unique identifier for object instance. name -- This is the common identifier for a physical location. latitude -- latitude coordinate as decimal longitude -- longitude coordinate as decimal """ class Meta: ordering = ['id'] verbose_name = 'location' verbose_name_plural = 'locations' id = models.CharField(default=create_id('loc_'), primary_key=True, max_length=30, unique=True) name = models.CharField(max_length=100) latitude = models.DecimalField(max_digits=19, decimal_places=16) longitude = models.DecimalField(max_digits=19, decimal_places=16) # TODO: Change database backend from SQLite to Postgres + PostGis # TODO: Change the fields latitude and longitude to a GeoDjango Point field def __str__(self): return self.name
def save(self, *args, **kwargs): exists = RouteModel.objects.filter(id=self.id).exists() if not exists: self.id = create_id('route_') super(RouteModel, self).save(*args, **kwargs)
def pre_save(sender, instance, *args, **kwargs): if not sender.objects.filter(pk=instance.id).exists(): instance.id = create_id(instance.prefix)
def pre_line_create_id(sender, instance, **kwargs): instance.id = create_id("line_")
def pre_route_create_id(sender, instance, **kwargs): instance.id = create_id("route_")
def save(self, *args, **kwargs): exists = LocationModel.objects.filter(id=self.id).exists() if not exists: self.id = create_id('loc_') super(LocationModel, self).save(*args, **kwargs)