-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathmodels.py
328 lines (259 loc) · 9.89 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import logging
from django.core.exceptions import ValidationError
from django.db import models
try:
from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor
except ImportError:
from django.db.models.fields.related import (
ReverseSingleRelatedObjectDescriptor as ForwardManyToOneDescriptor,
)
logger = logging.getLogger(__name__)
__all__ = ["Country", "State", "Locality", "Address", "AddressField"]
class InconsistentDictError(Exception):
pass
def _to_python(value):
raw = value.get("raw", "")
country = value.get("country", "")
country_code = value.get("country_code", "")
state = value.get("state", "")
state_code = value.get("state_code", "")
locality = value.get("locality", "")
sublocality = value.get("sublocality", "")
postal_town = value.get("postal_town", "")
postal_code = value.get("postal_code", "")
street_number = value.get("street_number", "")
route = value.get("route", "")
formatted = value.get("formatted", "")
latitude = value.get("latitude", None)
longitude = value.get("longitude", None)
# If there is no value (empty raw) then return None.
if not raw:
return None
# Fix issue with NYC boroughs (https://code.google.com/p/gmaps-api-issues/issues/detail?id=635)
if not locality and sublocality:
locality = sublocality
# Fix issue with UK addresses with no locality
# (https://github.com/furious-luke/django-address/issues/114)
if not locality and postal_town:
locality = postal_town
# If we have an inconsistent set of value bail out now.
if (country or state or locality) and not (country and state and locality):
raise InconsistentDictError
# Handle the country.
try:
country_obj = Country.objects.get(name=country)
except Country.DoesNotExist:
if country:
if len(country_code) > Country._meta.get_field("code").max_length:
if country_code != country:
raise ValueError("Invalid country code (too long): %s" % country_code)
country_code = ""
country_obj = Country.objects.create(name=country, code=country_code)
else:
country_obj = None
# Handle the state.
try:
state_obj = State.objects.get(name=state, country=country_obj)
except State.DoesNotExist:
if state:
if len(state_code) > State._meta.get_field("code").max_length:
if state_code != state:
raise ValueError("Invalid state code (too long): %s" % state_code)
state_code = ""
state_obj = State.objects.create(name=state, code=state_code, country=country_obj)
else:
state_obj = None
# Handle the locality.
try:
locality_obj = Locality.objects.get(name=locality, postal_code=postal_code, state=state_obj)
except Locality.DoesNotExist:
if locality:
locality_obj = Locality.objects.create(name=locality, postal_code=postal_code, state=state_obj)
else:
locality_obj = None
# Handle the address.
try:
if not (street_number or route or locality):
address_obj = Address.objects.get(raw=raw)
else:
address_obj = Address.objects.get(street_number=street_number, route=route, locality=locality_obj)
except Address.DoesNotExist:
address_obj = Address(
street_number=street_number,
route=route,
raw=raw,
locality=locality_obj,
formatted=formatted,
latitude=latitude,
longitude=longitude,
)
# If "formatted" is empty try to construct it from other values.
if not address_obj.formatted:
address_obj.formatted = str(address_obj)
# Need to save.
address_obj.save()
# Done.
return address_obj
##
# Convert a dictionary to an address.
##
def to_python(value):
# Keep `None`s.
if value is None:
return None
# Is it already an address object?
if isinstance(value, Address):
return value
# If we have an integer, assume it is a model primary key.
elif isinstance(value, int):
return value
# A string is considered a raw value.
elif isinstance(value, str):
obj = Address(raw=value)
obj.save()
return obj
# A dictionary of named address components.
elif isinstance(value, dict):
# Attempt a conversion.
try:
return _to_python(value)
except InconsistentDictError:
return Address.objects.create(raw=value["raw"])
# Not in any of the formats I recognise.
raise ValidationError("Invalid address value.")
##
# A country.
##
class Country(models.Model):
name = models.CharField(max_length=40, unique=True, blank=True)
code = models.CharField(max_length=2, blank=True) # not unique as there are duplicates (IT)
class Meta:
verbose_name_plural = "Countries"
ordering = ("name",)
def __str__(self):
return "%s" % (self.name or self.code)
##
# A state. Google refers to this as `administration_level_1`.
##
class State(models.Model):
name = models.CharField(max_length=165, blank=True)
code = models.CharField(max_length=8, blank=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name="states")
class Meta:
unique_together = ("name", "country")
ordering = ("country", "name")
def __str__(self):
txt = self.to_str()
country = "%s" % self.country
if country and txt:
txt += ", "
txt += country
return txt
def to_str(self):
return "%s" % (self.name or self.code)
##
# A locality (suburb).
##
class Locality(models.Model):
name = models.CharField(max_length=165, blank=True)
postal_code = models.CharField(max_length=10, blank=True)
state = models.ForeignKey(State, on_delete=models.CASCADE, related_name="localities")
class Meta:
verbose_name_plural = "Localities"
unique_together = ("name", "postal_code", "state")
ordering = ("state", "name")
def __str__(self):
txt = "%s" % self.name
state = self.state.to_str() if self.state else ""
if txt and state:
txt += ", "
txt += state
if self.postal_code:
txt += " %s" % self.postal_code
cntry = "%s" % (self.state.country if self.state and self.state.country else "")
if cntry:
txt += ", %s" % cntry
return txt
##
# An address. If for any reason we are unable to find a matching
# decomposed address we will store the raw address string in `raw`.
##
class Address(models.Model):
street_number = models.CharField(max_length=20, blank=True)
route = models.CharField(max_length=100, blank=True)
locality = models.ForeignKey(
Locality,
on_delete=models.CASCADE,
related_name="addresses",
blank=True,
null=True,
)
raw = models.CharField(max_length=200)
formatted = models.CharField(max_length=200, blank=True)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
class Meta:
verbose_name_plural = "Addresses"
ordering = ("locality", "route", "street_number")
def __str__(self):
if self.formatted != "":
txt = "%s" % self.formatted
elif self.locality:
txt = ""
if self.street_number:
txt = "%s" % self.street_number
if self.route:
if txt:
txt += " %s" % self.route
locality = "%s" % self.locality
if txt and locality:
txt += ", "
txt += locality
else:
txt = "%s" % self.raw
return txt
def clean(self):
if not self.raw:
raise ValidationError("Addresses may not have a blank `raw` field.")
def as_dict(self):
ad = dict(
street_number=self.street_number,
route=self.route,
raw=self.raw,
formatted=self.formatted,
latitude=self.latitude if self.latitude else "",
longitude=self.longitude if self.longitude else "",
)
if self.locality:
ad["locality"] = self.locality.name
ad["postal_code"] = self.locality.postal_code
if self.locality.state:
ad["state"] = self.locality.state.name
ad["state_code"] = self.locality.state.code
if self.locality.state.country:
ad["country"] = self.locality.state.country.name
ad["country_code"] = self.locality.state.country.code
return ad
class AddressDescriptor(ForwardManyToOneDescriptor):
def __set__(self, inst, value):
super(AddressDescriptor, self).__set__(inst, to_python(value))
##
# A field for addresses in other models.
##
class AddressField(models.ForeignKey):
description = "An address"
def __init__(self, *args, **kwargs):
kwargs["to"] = "address.Address"
# The address should be set to null when deleted if the relationship could be null
default_on_delete = models.SET_NULL if kwargs.get("null", False) else models.CASCADE
kwargs["on_delete"] = kwargs.get("on_delete", default_on_delete)
super(AddressField, self).__init__(*args, **kwargs)
def contribute_to_class(self, cls, name, virtual_only=False):
from address.compat import compat_contribute_to_class
compat_contribute_to_class(self, cls, name, virtual_only)
setattr(cls, self.name, AddressDescriptor(self))
def formfield(self, **kwargs):
from .forms import AddressField as AddressFormField
defaults = dict(form_class=AddressFormField)
defaults.update(kwargs)
return super(AddressField, self).formfield(**defaults)