-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
136 lines (105 loc) · 3.7 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#coding=utf-8
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from django.utils.translation import ugettext_lazy as _
from libs.fields import AutoSlugField
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^timezones.fields.LocalizedDateTimeField"])
class Country(models.Model):
name = models.CharField(_(u"Nombre"), max_length = 200)
code = models.CharField(_(u"Codigo"), max_length = 2, db_index=True)
population = models.IntegerField(_(u"Habitantes"), blank=True)
continent = models.CharField(_(u"Continente"), max_length = 2)
tld = models.CharField(max_length = 5, unique=True)
objects = models.GeoManager()
def __unicode__(self):
return unicode(self.name)
@property
def hierarchy(self):
return [self]
class Meta:
ordering = ['name']
verbose_name = _(u"País")
verbose_name_plural = _(u"Países")
#------------------------------------------------------------------------------
class Region(models.Model):
name = models.CharField(_(u"Nombre"), max_length = 200, blank=True)
slug = AutoSlugField(populate_from=['name', 'country'], max_length = 50)
code = models.CharField(_(u"Codigo"), max_length = 10, db_index=True, blank=True)
country = models.ForeignKey(Country, verbose_name=_(u"País"),
related_name='regions')
objects = models.GeoManager()
def __unicode__(self):
return u"%s, %s" % (self.name, self.country)
@property
def hierarchy(self):
list = self.country.hierarchy
list.append(self)
return list
class Meta:
ordering = ['name']
order_with_respect_to = 'country'
verbose_name = _(u'Región')
verbose_name_plural = _(u'Regiones')
#------------------------------------------------------------------------------
class CityManager(models.GeoManager):
def nearest_to(self, lat, lon):
#Wrong x y order
#p = Point(float(lat), float(lon))
p = Point(float(lon), float(lat))
return self.nearest_to_point(p)
def nearest_to_point(self, point):
return self.distance(point).order_by('distance')[0]
class City(models.Model):
name = models.CharField(_(u"Nombre"), max_length = 200)
slug = AutoSlugField(populate_from=['name', 'region'], max_length = 50)
region = models.ForeignKey(Region, verbose_name=_(u"Región"),
related_name="cities", blank=True)
location = models.PointField(_(u"Localización"), blank=True, null=True)
population = models.IntegerField(_(u"Habitantes"), blank=True)
objects = CityManager()
def __unicode__(self):
return u"%s, %s" % (self.name, self.region)
@property
def hierarchy(self):
list = self.region.hierarchy
list.append(self)
return list
class Meta:
ordering = ['name']
order_with_respect_to = 'region'
verbose_name = _(u'Ciudad')
verbose_name_plural = _(u'Ciudades')
#===========================================================================
# Traduccion de modelos
#===========================================================================
from datatrans.utils import register
class RegionTranslation(object):
fields = ('name')
class CountryTranslation(object):
fields = ('name')
register(Country, CountryTranslation)
register(Region, RegionTranslation)
#class District(models.Model):
# name = models.CharField(max_length = 200)
# slug = models.CharField(max_length = 200, db_index=True)
# city = models.ForeignKey(City)
# location = models.PointField()
# population = models.IntegerField()
#
# objects = models.GeoManager()
#
# def __unicode__(self):
# return u"%s, %s" % (self.name, self.city)
#
# @property
# def hierarchy(self):
# list = self.city.hierarchy
# list.append(self)
# return list
#
# class Meta:
# ordering = ['name']
# order_with_respect_to = 'city'
# verbose_name = _(u'Distrito')
# verbose_name_plural = _(u'Distritos')