-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmodels.py
287 lines (239 loc) · 10.2 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
import os
import tempfile
from datetime import datetime
import magic
from buckets.fields import S3FileField
from core.models import ID_FIELD_LENGTH, RandomIDModel
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db.models import GeometryCollectionField
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.dispatch import receiver
from django.utils.translation import ugettext as _
from django.utils.encoding import iri_to_uri
from jsonattrs.fields import JSONAttributeField
from simple_history.models import HistoricalRecords
from tutelary.decorators import permissioned_model
from . import messages
from .exceptions import InvalidGPXFile
from .managers import ResourceManager
from .processors.gpx import GPXProcessor
from .utils import io, thumbnail
from .validators import ACCEPTED_TYPES, validate_file_type
content_types = models.Q(app_label='organization', model='project')
GPX_MIME_TYPES = ('application/xml', 'text/xml', 'application/gpx+xml')
@permissioned_model
class Resource(RandomIDModel):
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
file = S3FileField(upload_to='resources', accepted_types=ACCEPTED_TYPES)
original_file = models.CharField(max_length=200)
file_versions = JSONField(null=True, blank=True)
mime_type = models.CharField(max_length=100,
validators=[validate_file_type])
archived = models.BooleanField(default=False)
last_updated = models.DateTimeField(auto_now=True)
contributor = models.ForeignKey('accounts.User')
project = models.ForeignKey('organization.Project')
objects = ResourceManager()
# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
class Meta:
ordering = ('name',)
class TutelaryMeta:
perm_type = 'resource'
path_fields = ('project', 'pk')
actions = (
('resource.list',
{'description': _("List resources"),
'permissions_object': 'project',
'error_message': messages.RESOURCE_LIST}),
('resource.add',
{'description': _("Add resources"),
'permissions_object': 'project',
'error_message': messages.RESOURCE_ADD}),
('resource.view',
{'description': _("View resource"),
'error_message': messages.RESOURCE_VIEW}),
('resource.edit',
{'description': _("Edit resource"),
'error_message': messages.RESOURCE_EDIT}),
('resource.archive',
{'description': _("Archive resource"),
'error_message': messages.RESOURCE_ARCHIVE}),
('resource.unarchive',
{'description': _("Unarchive resource"),
'error_message': messages.RESOURCE_UNARCHIVE}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_url = self.file.url
def __repr__(self):
repr_string = ('<Resource id={obj.id} name={obj.name}'
' file={obj.file.url} project={obj.project.slug}>')
return repr_string.format(obj=self)
@property
def file_name(self):
if not hasattr(self, '_file_name'):
self._file_name = self.file.url.split('/')[-1]
return self._file_name
@property
def file_type(self):
return self.file_name.lower().split('.')[-1]
@property
def thumbnail(self):
if not hasattr(self, '_thumbnail'):
icon = settings.ICON_LOOKUPS.get(self.mime_type, None)
if ('image' in self.mime_type and
all(img not in self.mime_type for img in ['tif', 'svg'])):
ext = self.file_name.split('.')[-1]
base_url = self.file.url[:self.file.url.rfind('.')]
self._thumbnail = base_url + '-128x128.' + ext
elif icon:
self._thumbnail = settings.ICON_URL.format(icon)
else:
self._thumbnail = ''
return self._thumbnail
@property
def num_entities(self):
if not hasattr(self, '_num_entities'):
self._num_entities = ContentObject.objects.filter(
resource=self).count()
return self._num_entities
def save(self, *args, **kwargs):
create_thumbnails(self, (not self.id))
super().save(*args, **kwargs)
@property
def ui_class_name(self):
return _("Resource")
def get_absolute_url(self):
return iri_to_uri(reverse(
'resources:project_detail',
kwargs={
'organization': self.project.organization.slug,
'project': self.project.slug,
'resource': self.id,
},
))
@receiver(models.signals.pre_save, sender=Resource)
def archive_file(sender, instance, **kwargs):
if instance._original_url and instance._original_url != instance.file.url:
now = str(datetime.now())
if not instance.file_versions:
instance.file_versions = {}
instance.file_versions[now] = instance._original_url
instance._original_url = instance.file.url
# Detach the resource when it is archived
if instance.archived:
ContentObject.objects.filter(resource=instance).delete()
def create_thumbnails(instance, created):
if created or instance._original_url != instance.file.url:
if 'image' in instance.mime_type and 'svg' not in instance.mime_type:
io.ensure_dirs()
file_name = instance.file.url.split('/')[-1]
name = file_name[:file_name.rfind('.')]
ext = file_name.split('.')[-1]
write_path = os.path.join(settings.MEDIA_ROOT,
'temp',
name + '-128x128.' + ext)
size = 128, 128
file = instance.file.open()
thumb = thumbnail.make(file, size)
thumb.save(write_path)
if instance.file.field.upload_to:
name = instance.file.field.upload_to + '/' + name
with open(write_path, 'rb') as src:
instance.file.storage.save(name + '-128x128.' + ext,
src.read())
file.close()
@receiver(models.signals.post_save, sender=Resource)
def create_spatial_resource(sender, instance, created, **kwargs):
if created or instance._original_url != instance.file.url:
if instance.mime_type in GPX_MIME_TYPES:
temp = io.ensure_dirs()
with tempfile.NamedTemporaryFile(mode='wb', dir=temp) as f:
instance_file = instance.file.open()
f.write(instance_file.read())
instance_file.close()
f.seek(0)
# need to double check the mime-type here as browser detection
# of gpx mime type is not reliable
mime_type = magic.from_file(f.name, mime=True)
if mime_type in GPX_MIME_TYPES:
processor = GPXProcessor(f.name)
layers = processor.get_layers()
for layer in layers.keys():
if len(layers[layer]) > 0:
SpatialResource.objects.create(
resource=instance, name=layer,
geom=layers[layer])
else:
raise InvalidGPXFile(
_("Invalid GPX mime type: {error}".format(
error=mime_type))
)
class ContentObject(RandomIDModel):
resource = models.ForeignKey(Resource, related_name='content_objects')
content_type = models.ForeignKey(ContentType,
on_delete=models.CASCADE,
null=True,
blank=True,
limit_choices_to=content_types)
object_id = models.CharField(max_length=ID_FIELD_LENGTH,
null=True,
blank=True)
content_object = GenericForeignKey('content_type', 'object_id')
history = HistoricalRecords()
# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
@permissioned_model
class SpatialResource(RandomIDModel):
class Meta:
ordering = ('name',)
class TutelaryMeta:
perm_type = 'resource'
path_fields = ('resource', 'pk')
actions = (
('resource.list',
{'description': _("List resources"),
'permissions_object': 'resource',
'error_message': messages.RESOURCE_LIST}),
('resource.view',
{'description': _("View resource"),
'error_message': messages.RESOURCE_VIEW}),
('resource.archive',
{'description': _("Archive resource"),
'error_message': messages.RESOURCE_ARCHIVE}),
('resource.unarchive',
{'description': _("Unarchive resource"),
'error_message': messages.RESOURCE_UNARCHIVE}),
)
name = models.CharField(max_length=256, null=True, blank=True)
time = models.DateTimeField(auto_now_add=True, null=True, blank=True)
resource = models.ForeignKey(
Resource,
on_delete=models.CASCADE, related_name='spatial_resources'
)
geom = GeometryCollectionField(
srid=4326, blank=True, null=True
)
attributes = JSONAttributeField(default={})
# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
def __repr__(self):
repr_string = ('<SpatialResource id={obj.id}'
' resource={obj.resource.id}>')
return repr_string.format(obj=self)
@property
def archived(self):
return self.resource.archived
@property
def project(self):
return self.resource.project