-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmodels.py
300 lines (254 loc) · 11 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
from core.models import RandomIDModel
from django.utils.functional import cached_property
from django.core.urlresolvers import reverse
from django.contrib.gis.db.models import GeometryField
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.utils.translation import ugettext as _
from django.utils.encoding import iri_to_uri
from django.dispatch import receiver
from django.utils.translation import get_language
from organization.models import Project
from tutelary.decorators import permissioned_model
from simple_history.models import HistoricalRecords
from shapely.geometry import Point, Polygon, LineString
from shapely.wkt import dumps
from . import messages, managers
from .choices import TYPE_CHOICES
from resources.mixins import ResourceModelMixin
from jsonattrs.fields import JSONAttributeField
from jsonattrs.decorators import fix_model_for_attributes
from questionnaires.models import Questionnaire, QuestionOption
@fix_model_for_attributes
@permissioned_model
class SpatialUnit(ResourceModelMixin, RandomIDModel):
"""A single spatial unit: has a type, an optional geometry, a
type-dependent set of attributes, and a set of relationships to
other spatial units.
"""
# All spatial units are associated with a single project.
project = models.ForeignKey(Project, on_delete=models.CASCADE,
related_name='spatial_units')
# Spatial unit type: used to manage range of allowed attributes.
type = models.CharField(max_length=100)
# Spatial unit geometry is optional: some spatial units may only
# have a textual description of their location.
geometry = GeometryField(null=True, geography=True)
# Area, auto-calculated via trigger (see spatial/migrations/#0005)
area = models.FloatField(default=0)
# JSON attributes field with management of allowed members.
attributes = JSONAttributeField(default={})
# Spatial unit-spatial unit relationships: includes spatial
# containment and split/merge relationships.
relationships = models.ManyToManyField(
'self',
through='SpatialRelationship',
through_fields=('su1', 'su2'),
symmetrical=False,
related_name='relationships_set',
)
# Denormalized duplication of label from the QuestionOption related to the
# SpatialUnit
label = JSONField(null=True)
# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
class Meta:
ordering = ('type',)
class TutelaryMeta:
perm_type = 'spatial'
path_fields = ('project', 'id')
actions = (
('spatial.list',
{'description': _("List existing spatial units of a project"),
'error_message': messages.SPATIAL_LIST,
'permissions_object': 'project'}),
('spatial.create',
{'description': _("Add a spatial unit to a project"),
'error_message': messages.SPATIAL_CREATE,
'permissions_object': 'project'}),
('spatial.view',
{'description': _("View an existing spatial unit"),
'error_message': messages.SPATIAL_VIEW}),
('spatial.update',
{'description': _("Update an existing spatial unit"),
'error_message': messages.SPATIAL_UPDATE}),
('spatial.delete',
{'description': _("Delete an existing spatial unit"),
'error_message': messages.SPATIAL_DELETE}),
('spatial.resources.add',
{'description': _("Add resources to this spatial unit"),
'error_message': messages.SPATIAL_ADD_RESOURCE})
)
def __str__(self):
return "<SpatialUnit: {}>".format(self.name)
def __repr__(self):
repr_string = ('<SpatialUnit id={obj.id}'
' project={obj.project.slug}'
' type={obj.type}>')
return repr_string.format(obj=self)
@property
def name(self):
return self.location_type_label
@property
def ui_class_name(self):
return _("Location")
def get_absolute_url(self):
return iri_to_uri(reverse(
'locations:detail',
kwargs={
'organization': self.project.organization.slug,
'project': self.project.slug,
'location': self.id,
},
))
@cached_property
def location_type_label(self):
# Handle no questionnaire
if (not self.project.current_questionnaire) or (self.label is None):
return dict(TYPE_CHOICES)[self.type]
# Handle non-translatable label
if isinstance(self.label, str):
return self.label
# Handle translated label
translated_label = self.label.get(get_language())
if translated_label:
return translated_label
# If label failed to translate, fallback to default language
rel_questionnaire = Questionnaire.objects.get(
id=self.project.current_questionnaire)
return self.label.get(rel_questionnaire.default_language)
def reassign_spatial_geometry(instance):
coords = list(instance.geometry.coords)
if type(coords[0]) == float:
coords = [coords]
else:
while (type(coords[0][0]) != float):
coords = coords[0]
coords = [list(x) for x in coords]
for point in coords:
if point[0] >= -180 and point[0] <= 180:
return
while coords[0][0] < -180:
for point in coords:
point[0] += 360
while coords[0][0] > 180:
for point in coords:
point[0] -= 360
geometry = []
for point in coords:
latlng = [point[0], point[1]]
geometry.append(tuple(latlng))
if len(geometry) > 1:
if geometry[0] == geometry[-1]:
instance.geometry = dumps(Polygon(geometry))
else:
instance.geometry = dumps(LineString(geometry))
else:
instance.geometry = dumps(Point(geometry))
@receiver(models.signals.pre_save, sender=SpatialUnit)
def check_extent(sender, instance, **kwargs):
geom = instance.geometry
# Store 'POLYGON EMPTY' data as null to avoid libgeos bug
# (https://trac.osgeo.org/geos/ticket/680)
# TODO: Rm this check when we're using Django 1.11+ or libgeos 3.6.1+
# https://github.com/django/django/commit/b90d72facf1e4294df1c2e6b51b26f6879bf2992#diff-181a3ea304dfaf57f1e1d680b32d2b76R248
from django.contrib.gis.geos import Polygon
if isinstance(geom, Polygon) and geom.empty:
instance.geometry = None
if geom and not geom.empty:
reassign_spatial_geometry(instance)
@receiver(models.signals.post_save, sender=SpatialUnit)
def refresh_area(sender, instance, **kwargs):
""" Ensure DB-generated area is set on instance """
from django.contrib.gis.geos import MultiPolygon, Polygon
geom = instance.geometry
if not isinstance(geom, (MultiPolygon, Polygon)):
return
qs = type(instance)._default_manager.filter(id=instance.id)
instance.area = qs.values_list('area', flat=True)[0]
@fix_model_for_attributes
@permissioned_model
class SpatialRelationship(RandomIDModel):
"""A relationship between spatial units: encodes simple logical terms
like ``su1 is-contained-in su2`` or ``su1 is-split-of su2``. May
have additional requirements.
"""
# Possible spatial unit relationships types: TYPE_CHOICES is the
# well-known name used by the JSONAttributesField field type to
# manage the range of allowed attribute fields.
TYPE_CHOICES = (('C', 'is-contained-in'),
('S', 'is-split-of'),
('M', 'is-merge-of'))
# All spatial unit relationships are associated with a single project.
project = models.ForeignKey(Project, on_delete=models.CASCADE,
related_name='spatial_relationships')
# Spatial units are in the relationships.
su1 = models.ForeignKey(SpatialUnit, on_delete=models.CASCADE,
related_name='spatial_unit_one')
su2 = models.ForeignKey(SpatialUnit, on_delete=models.CASCADE,
related_name='spatial_unit_two')
# Spatial unit relationship type: used to manage range of allowed
# attributes
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
# JSON attributes field with management of allowed members.
attributes = JSONAttributeField(default={})
objects = managers.SpatialRelationshipManager()
# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
class TutelaryMeta:
perm_type = 'spatial_rel'
path_fields = ('project', 'id')
actions = (
('spatial_rel.list',
{'description': _("List existing spatial relationships"
" of a project"),
'error_message': messages.SPATIAL_REL_LIST,
'permissions_object': 'project'}),
('spatial_rel.create',
{'description': _("Add a spatial relationship to a project"),
'error_message': messages.SPATIAL_REL_CREATE,
'permissions_object': 'project'}),
('spatial_rel.view',
{'description': _("View an existing spatial relationship"),
'error_message': messages.SPATIAL_REL_VIEW}),
('spatial_rel.update',
{'description': _("Update an existing spatial relationship"),
'error_message': messages.SPATIAL_REL_UPDATE}),
('spatial_rel.delete',
{'description': _("Delete an existing spatial relationship"),
'error_message': messages.SPATIAL_REL_DELETE}),
)
def __str__(self):
return "<SpatialRelationship: <{su1}> {type} <{su2}>>".format(
su1=self.su1.name, su2=self.su2.name,
type=dict(self.TYPE_CHOICES).get(self.type))
def __repr__(self):
repr_string = ('<SpatialRelationship id={obj.id}'
' project={obj.project.slug}'
' su1={obj.su1_id}'
' su2={obj.su2_id}'
' type={obj.type}>')
return repr_string.format(obj=self)
@receiver(models.signals.pre_save, sender=SpatialUnit)
def set_label_on_spatialunits(sender, instance, **kwargs):
"""
Set label of related QuestionOption onto new SpatialUnit instances
"""
# Ignore if instance is not new and has not changed its 'type' property
if not instance._state.adding:
unchanged = SpatialUnit.objects.filter(
id=instance.id, type=instance.type).exists()
if unchanged:
return
try:
rel_questionoption = QuestionOption.objects.get(
name=instance.type,
question__name='location_type',
question__questionnaire__id=instance.project.current_questionnaire)
except QuestionOption.DoesNotExist:
return
instance.label = rel_questionoption.label_xlat