-
Notifications
You must be signed in to change notification settings - Fork 76
/
mixins.py
405 lines (336 loc) · 13.9 KB
/
mixins.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
import os
import logging
import shutil
import datetime
from django.conf import settings
from django.db.models import Manager as DefaultManager
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import slugify
from easy_thumbnails.alias import aliases
from easy_thumbnails.exceptions import InvalidImageFormatError
from easy_thumbnails.files import get_thumbnailer
from embed_video.backends import detect_backend, VideoDoesntExistException
from geotrek.common.utils import classproperty
logger = logging.getLogger(__name__)
class TimeStampedModelMixin(models.Model):
# Computed values (managed at DB-level with triggers)
date_insert = models.DateTimeField(auto_now_add=True, editable=False, verbose_name=_(u"Insertion date"), db_column='date_insert')
date_update = models.DateTimeField(auto_now=True, editable=False, verbose_name=_(u"Update date"), db_column='date_update')
class Meta:
abstract = True
def reload(self, fromdb=None):
"""Reload fields computed at DB-level (triggers)
"""
if fromdb is None:
fromdb = self.__class__.objects.get(pk=self.pk)
self.date_insert = fromdb.date_insert
self.date_update = fromdb.date_update
return self
class NoDeleteMixin(models.Model):
deleted = models.BooleanField(editable=False, default=False, db_column='supprime', verbose_name=_(u"Deleted"))
def delete(self, force=False, using=None, **kwargs):
if force:
super(NoDeleteMixin, self).delete(using, **kwargs)
else:
self.deleted = True
self.save(using=using)
class Meta:
abstract = True
def reload(self, fromdb=None):
"""Reload fields computed at DB-level (triggers)
"""
if fromdb is None:
fromdb = self.__class__.objects.get(pk=self.pk)
self.deleted = fromdb.deleted
return self
@classmethod
def get_manager_cls(cls, parent_mgr_cls=DefaultManager):
class NoDeleteManager(parent_mgr_cls):
# Use this manager when walking through FK/M2M relationships
use_for_related_fields = True
# Filter out deleted objects
def existing(self):
return self.get_queryset().filter(deleted=False)
return NoDeleteManager
class PicturesMixin(object):
"""A common class to share code between Trek and POI regarding
attached pictures"""
@property
def pictures(self):
"""
Find first image among attachments.
Since we allow screenshot to be overriden by attachments
named 'mapimage', filter it from object pictures.
"""
if hasattr(self, '_pictures'):
return self._pictures
all_attachments = self.attachments.order_by('-starred', 'attachment_file').all()
return [a for a in all_attachments if a.is_image and a.title != 'mapimage']
@pictures.setter
def pictures(self, values):
self._pictures = values
@property
def serializable_pictures(self):
serialized = []
for picture, thdetail in self.resized_pictures:
serialized.append({
'author': picture.author,
'title': picture.title,
'legend': picture.legend,
'url': os.path.join(settings.MEDIA_URL, thdetail.name),
})
return serialized
@property
def resized_pictures(self):
resized = []
for picture in self.pictures:
thumbnailer = get_thumbnailer(picture.attachment_file)
try:
thdetail = thumbnailer.get_thumbnail(aliases.get('medium'))
except InvalidImageFormatError:
logger.info(_("Image %s invalid or missing from disk.") % picture.attachment_file)
else:
resized.append((picture, thdetail))
return resized
@property
def picture_print(self):
for picture in self.pictures:
thumbnailer = get_thumbnailer(picture.attachment_file)
try:
thumbnail = thumbnailer.get_thumbnail(aliases.get('print'))
except InvalidImageFormatError:
logger.info(_("Image %s invalid or missing from disk.") % picture.attachment_file)
continue
thumbnail.author = picture.author
thumbnail.legend = picture.legend
return thumbnail
return None
@property
def thumbnail(self):
for picture in self.pictures:
thumbnailer = get_thumbnailer(picture.attachment_file)
try:
thumbnail = thumbnailer.get_thumbnail(aliases.get('small-square'))
except InvalidImageFormatError:
logger.info(_("Image %s invalid or missing from disk.") % picture.attachment_file)
continue
thumbnail.author = picture.author
thumbnail.legend = picture.legend
return thumbnail
return None
@classproperty
def thumbnail_verbose_name(cls):
return _("Thumbnail")
@property
def thumbnail_display(self):
thumbnail = self.thumbnail
if thumbnail is None:
return _("None")
return '<img height="20" width="20" src="%s"/>' % os.path.join(settings.MEDIA_URL, thumbnail.name)
@property
def thumbnail_csv_display(self):
return '' if self.thumbnail is None else os.path.join(settings.MEDIA_URL, self.thumbnail.name)
@property
def serializable_thumbnail(self):
th = self.thumbnail
if not th:
return None
return os.path.join(settings.MEDIA_URL, th.name)
@property
def videos(self):
all_attachments = self.attachments.order_by('-starred')
return all_attachments.exclude(attachment_video='')
@property
def serializable_videos(self):
serialized = []
for att in self.videos:
video = detect_backend(att.attachment_video)
try:
serialized.append({
'author': att.author,
'title': att.title,
'legend': att.legend,
'backend': type(video).__name__.replace('Backend', ''),
'url': video.get_url(),
'code': video.code,
})
except VideoDoesntExistException:
pass
return serialized
@property
def files(self):
all_attachments = self.attachments.order_by('-starred')
all_attachments = all_attachments.exclude(attachment_file='')
return [a for a in all_attachments if not a.is_image]
@property
def serializable_files(self):
serialized = []
for att in self.files:
serialized.append({
'author': att.author,
'title': att.title,
'legend': att.legend,
'url': att.attachment_file.url,
})
return serialized
class BasePublishableMixin(models.Model):
""" Basic fields to control publication of objects.
It is used for flat pages and publishable entities.
"""
published = models.BooleanField(verbose_name=_(u"Published"), default=False,
help_text=_(u"Online"), db_column='public')
publication_date = models.DateField(verbose_name=_(u"Publication date"),
null=True, blank=True, editable=False,
db_column='date_publication')
class Meta:
abstract = True
def save(self, *args, **kwargs):
if self.publication_date is None and self.any_published:
self.publication_date = datetime.date.today()
if self.publication_date is not None and not self.any_published:
self.publication_date = None
super(BasePublishableMixin, self).save(*args, **kwargs)
@property
def any_published(self):
"""Returns True if the object is published in at least one of the language
"""
if not settings.PUBLISHED_BY_LANG:
return self.published
for l in settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES']:
if getattr(self, 'published_%s' % l[0], False):
return True
return False
@property
def published_status(self):
"""Returns the publication status by language.
"""
status = []
for l in settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES']:
if settings.PUBLISHED_BY_LANG:
published = getattr(self, 'published_%s' % l[0], None) or False
else:
published = self.published
status.append({
'lang': l[0],
'language': l[1],
'status': published
})
return status
@property
def published_langs(self):
"""Returns languages in which the object is published.
"""
langs = [l[0] for l in settings.MAPENTITY_CONFIG['TRANSLATED_LANGUAGES']]
if settings.PUBLISHED_BY_LANG:
return [l for l in langs if getattr(self, 'published_%s' % l, None)]
elif self.published:
return langs
else:
return []
class PublishableMixin(BasePublishableMixin):
"""A mixin that contains all necessary stuff to publish objects
(e.g. on Geotrek-rando).
It will only work with MapEntity models.
Initially, it was part of the ``trekking.Trek`` class. But now, all kinds of information
can be published (c.f. PN Cevennes project).
"""
name = models.CharField(verbose_name=_(u"Name"), max_length=128,
help_text=_(u"Public name (Change carefully)"), db_column='nom')
review = models.BooleanField(verbose_name=_(u"Waiting for publication"),
default=False, db_column='relecture')
class Meta:
abstract = True
@property
def slug(self):
return slugify(self.name.lower().replace(u"œ", u"oe")) or str(self.pk)
@property
def name_display(self):
s = u'<a data-pk="%s" href="%s" title="%s">%s</a>' % (self.pk,
self.get_detail_url(),
self.name,
self.name)
if self.published:
s = u'<span class="badge badge-success" title="%s">☆</span> ' % _("Published") + s
elif self.review:
s = u'<span class="badge badge-warning" title="%s">☆</span> ' % _("Waiting for publication") + s
return s
@property
def name_csv_display(self):
return unicode(self.name)
@models.permalink
def get_document_public_url(self):
raise NotImplementedError
def is_complete(self):
"""It should also have a description, etc.
"""
modelname = self.__class__._meta.object_name.lower()
mandatory = settings.COMPLETENESS_FIELDS.get(modelname, [])
for f in mandatory:
if not getattr(self, f):
return False
return True
def is_publishable(self):
return self.is_complete() and self.has_geom_valid()
def has_geom_valid(self):
return self.geom is not None
def prepare_map_image(self, rooturl):
"""
We override the default behaviour of map image preparation :
if the object has a attached picture file with *title* ``mapimage``, we use it
as a screenshot.
TODO: remove this when screenshots are bullet-proof ?
"""
attached = None
for picture in [a for a in self.attachments.all() if a.is_image]:
if picture.title == 'mapimage':
attached = picture.attachment_file
break
if attached is None:
super(PublishableMixin, self).prepare_map_image(rooturl)
else:
# Copy it along other screenshots
src = os.path.join(settings.MEDIA_ROOT, attached.name)
dst = self.get_map_image_path()
shutil.copyfile(src, dst)
def get_geom_aspect_ratio(self):
""" Force object aspect ratio to fit height and width of
image in public document.
"""
modelname = self.__class__._meta.object_name.lower()
s = settings.EXPORT_MAP_IMAGE_SIZE[modelname]
return float(s[0]) / s[1]
def get_attachment_print(self):
"""
Look in attachment if there is document to be used as print version
"""
overriden = self.attachments.filter(title="docprint").get()
# Must have OpenOffice document mimetype
if overriden.mimetype != ['application', 'vnd.oasis.opendocument.text']:
raise overriden.DoesNotExist()
return os.path.join(settings.MEDIA_ROOT, overriden.attachment_file.name)
def is_public(self):
return self.any_published
class PictogramMixin(models.Model):
pictogram = models.FileField(verbose_name=_(u"Pictogram"), upload_to=settings.UPLOAD_DIR,
db_column='picto', max_length=512, null=True)
class Meta:
abstract = True
def pictogram_img(self):
return u'<img src="%s" />' % (self.pictogram.url if self.pictogram else "")
pictogram_img.short_description = _("Pictogram")
pictogram_img.allow_tags = True
def get_pictogram_url(self):
return self.pictogram.url if self.pictogram else None
class OptionalPictogramMixin(PictogramMixin):
class Meta:
abstract = True
OptionalPictogramMixin._meta.get_field('pictogram').blank = True
class AddPropertyMixin(object):
@classmethod
def add_property(cls, name, func, verbose_name):
if hasattr(cls, name):
raise AttributeError("%s has already an attribute %s" % (cls, name))
setattr(cls, name, property(func))
setattr(cls, '%s_verbose_name' % name, verbose_name)