forked from coderholic/django-cities
/
abstract_models.py
217 lines (165 loc) · 6.48 KB
/
abstract_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import re
import autoslug
import pytz
from django.db import models
from django.db.models import lookups
from django.utils.encoding import force_str
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from unidecode import unidecode
from .validators import timezone_validator
from .settings import INDEX_SEARCH_NAMES, CITIES_LIGHT_APP_NAME
__all__ = ['AbstractCountry', 'AbstractRegion',
'AbstractSubRegion', 'AbstractCity',
'CONTINENT_CHOICES']
CONTINENT_CHOICES = (
('OC', _('Oceania')),
('EU', _('Europe')),
('AF', _('Africa')),
('NA', _('North America')),
('AN', _('Antarctica')),
('SA', _('South America')),
('AS', _('Asia')),
)
ALPHA_REGEXP = re.compile('[\W_]+', re.UNICODE)
def to_ascii(value):
"""
Convert a unicode value to ASCII-only unicode string.
For example, 'République Françaisen' would become 'Republique Francaisen'
"""
return force_str(unidecode(value))
def to_search(value):
"""
Convert a string value into a string that is usable against
City.search_names.
For example, 'Paris Texas' would become 'paristexas'.
"""
return ALPHA_REGEXP.sub('', to_ascii(value)).lower()
class ToSearchIContainsLookup(lookups.IContains):
"""IContains lookup for ToSearchTextField."""
def get_prep_lookup(self):
"""Return the value passed through to_search()."""
value = super(ToSearchIContainsLookup, self).get_prep_lookup()
return to_search(value)
class ToSearchTextField(models.TextField):
"""
Trivial TextField subclass that passes values through to_search
automatically.
"""
ToSearchTextField.register_lookup(ToSearchIContainsLookup)
class Base(models.Model):
"""
Base model with boilerplate for all models.
"""
name = models.CharField(max_length=200, db_index=True)
name_ascii = models.CharField(max_length=200, blank=True, db_index=True)
slug = autoslug.AutoSlugField(populate_from='name_ascii')
geoname_id = models.IntegerField(null=True, blank=True, unique=True)
alternate_names = models.TextField(null=True, blank=True, default='')
class Meta:
abstract = True
ordering = ['name']
def __str__(self):
display_name = getattr(self, 'display_name', None)
if display_name:
return display_name
return self.name
class AbstractCountry(Base):
"""
Base Country model.
"""
code2 = models.CharField(max_length=2, null=True, blank=True, unique=True)
code3 = models.CharField(max_length=3, null=True, blank=True, unique=True)
continent = models.CharField(max_length=2, db_index=True,
choices=CONTINENT_CHOICES)
tld = models.CharField(max_length=5, blank=True, db_index=True)
phone = models.CharField(max_length=20, null=True, blank=True)
class Meta(Base.Meta):
verbose_name_plural = _('countries')
abstract = True
class AbstractRegion(Base):
"""
Base Region/State model.
"""
display_name = models.CharField(max_length=200)
geoname_code = models.CharField(max_length=50, null=True, blank=True,
db_index=True)
country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country',
on_delete=models.CASCADE)
class Meta(Base.Meta):
unique_together = (('country', 'name'), ('country', 'slug'))
verbose_name = _('region/state')
verbose_name_plural = _('regions/states')
abstract = True
def get_display_name(self):
return '%s, %s' % (self.name, self.country.name)
class AbstractSubRegion(Base):
"""
Base SubRegion model.
"""
display_name = models.CharField(max_length=200)
geoname_code = models.CharField(max_length=50, null=True, blank=True,
db_index=True)
country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country',
on_delete=models.CASCADE)
region = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Region',
null=True, blank=True,
on_delete=models.CASCADE)
class Meta(Base.Meta):
verbose_name = _('SubRegion')
verbose_name_plural = _('SubRegions')
abstract = True
def get_display_name(self):
return '%s, %s' % (self.name, self.country.name)
class AbstractCity(Base):
"""
Base City model.
"""
display_name = models.CharField(max_length=200)
search_names = ToSearchTextField(
max_length=4000,
db_index=INDEX_SEARCH_NAMES,
blank=True,
default='')
latitude = models.DecimalField(
max_digits=8,
decimal_places=5,
null=True,
blank=True)
longitude = models.DecimalField(
max_digits=8,
decimal_places=5,
null=True,
blank=True)
subregion = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.SubRegion',
blank=True, null=True,
on_delete=models.CASCADE)
region = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Region', blank=True,
null=True, on_delete=models.CASCADE)
country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country',
on_delete=models.CASCADE)
population = models.BigIntegerField(null=True, blank=True, db_index=True)
feature_code = models.CharField(max_length=10, null=True, blank=True,
db_index=True)
timezone = models.CharField(max_length=40, blank=True, null=True,
db_index=True, validators=[timezone_validator])
class Meta(Base.Meta):
unique_together = (('region', 'subregion', 'name'),
('region', 'subregion', 'slug'))
verbose_name_plural = _('cities')
abstract = True
def get_display_name(self):
if self.region_id:
return '%s, %s, %s' % (self.name, self.region.name,
self.country.name)
else:
return '%s, %s' % (self.name, self.country.name)
def get_timezone_info(self):
"""Return timezone info for self.timezone.
If self.timezone has wrong value, it returns timezone info
for value specified in settings.TIME_ZONE.
"""
try:
return pytz.timezone(self.timezone)
except (pytz.UnknownTimeZoneError, AttributeError):
return pytz.timezone(settings.TIME_ZONE)