summaryrefslogtreecommitdiff
path: root/app/media/models.py
blob: e20b3975cf5b2b0e3c0fc8c94414fc2ef9b3231d (plain)
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
import os.path
import io
import datetime
from PIL import Image

from django.core.exceptions import ValidationError 
from django.db import models
from django.contrib.sitemaps import Sitemap
from django.utils.encoding import force_str
from django.utils.functional import cached_property
from django.urls import reverse
from django.apps import apps
from django.utils.html import format_html
from django.utils.text import slugify
from django.conf import settings
from django import forms

from taggit.managers import TaggableManager

from resizeimage.imageexceptions import ImageSizeError

from .utils import resize_image
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.db.models.signals import m2m_changed


def get_upload_path(self, filename):
    return "images/original/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


def get_vid_upload_path(self, filename):
    return "images/videos/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


def get_audio_upload_path(self, filename):
    return "audio/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


class LuxImageSize(models.Model):
    name = models.CharField(null=True, blank=True, max_length=30)
    width = models.IntegerField(null=True, blank=True)
    height = models.IntegerField(null=True, blank=True)
    quality = models.IntegerField()

    class Meta:
        ordering = ('-name', 'id')
        verbose_name_plural = 'Image Sizes'

    def __str__(self):
        if self.width:
            size = self.width
        if self.height:
            size = self.height
        return "%s - %s" %(self.name, str(size))


class LuxImage(models.Model):
    image = models.FileField(blank=True, null=True, upload_to=get_upload_path)
    title = models.CharField(null=True, blank=True, max_length=300)
    alt = models.CharField(null=True, blank=True, max_length=300)
    photo_credit_source = models.CharField(null=True, blank=True, max_length=300)
    photo_credit_url = models.CharField(null=True, blank=True, max_length=300)
    caption = models.TextField(blank=True, null=True)
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    height = models.CharField(max_length=6, blank=True, null=True)
    width = models.CharField(max_length=6, blank=True, null=True)
    is_public = models.BooleanField(default=True)
    sizes = models.ManyToManyField(LuxImageSize, blank=True)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Images'
        get_latest_by = 'pub_date'

    def __str__(self):
        if self.title:
            return "%s" % self.title
        else:
            return "%s" % self.pk

    def get_type(self):
        return str(self.__class__.__name__)

    def get_admin_image(self):
        for size in self.sizes.all():
            if size.width and size.width <= 820 or size.height and size.height <= 800:
                return self.get_image_by_size(size.name)

    def get_admin_insert(self):
        return "/media/images/%s/%s_tn.%s" % (self.pub_date.strftime("%Y"), self.get_image_name(), self.get_image_ext())

    def get_largest_image(self):
        t = []
        for size in self.sizes.all():
            t.append(size.width)
        t.sort(key=float)
        t.reverse()
        return self.get_image_path_by_size(t[0])

    def get_image_name(self):
        return self.image.url.split("original/")[1][5:-4]

    def get_image_ext(self):
        return self.image.url[-3:]

    @cached_property
    def get_featured_jrnl(self):
        ''' cached version of getting the primary image for archive page'''
        return "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.get_image_name(), 'featured_jrnl', self.get_image_ext())

    @cached_property
    def get_picwide_sm(self):
        ''' cached version of getting the second image for archive page'''
        return "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.get_image_name(), 'picwide-sm', self.get_image_ext())

    @cached_property
    def get_srcset(self):
        srcset = ""
        length = len(self.sizes.all())
        print(length)
        loopnum = 1
        for size in self.sizes.all():
            srcset += "%s%s/%s_%s.%s %sw" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.get_image_name(), size.name, self.get_image_ext(), size.width)
            if loopnum < length:
                srcset += ", "
                loopnum = loopnum+1
        return srcset

    @cached_property
    def get_src(self):
        src = ""
        if self.sizes.all().count() > 1:
            src += "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.get_image_name(), 'picwide-med', self.get_image_ext())
        else:
            src += "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.get_image_name(), [size.name for size in self.sizes.all()], self.get_image_ext())
        return src

    def get_image_by_size(self, size="original"):
        base = self.get_image_name()
        if size == "admin_insert":
            return "images/%s/%s.%s" % (self.pub_date.strftime("%Y"), base, self.get_image_ext())
        if size == "original":
            return "%soriginal/%s/%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), base, self.get_image_ext())
        else:
            if size != 'tn':
                s = LuxImageSize.objects.get(name=size)
                if s not in self.sizes.all():
                    print("new size is "+s.name)
                    self.sizes.add(s)
            return "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), base, size, self.get_image_ext())

    def get_image_path_by_size(self, size="original"):
        base = self.get_image_name()
        if size == "original":
            return "%s/original/%s/%s.%s" % (settings.IMAGES_ROOT, self.pub_date.strftime("%Y"), base, self.get_image_ext())
        else:
            return "%s/%s/%s_%s.%s" % (settings.IMAGES_ROOT, self.pub_date.strftime("%Y"), base, size, self.get_image_ext())

    def get_thumbnail_url(self):
        return self.get_image_by_size("tn")

    def admin_thumbnail(self):
        return format_html('<a href="%s"><img src="%s"></a>' % (self.get_image_by_size(), self.get_image_by_size("tn")))
    admin_thumbnail.short_description = 'Thumbnail'

    def get_sizes(self):
        return self.sizes.all()

    @property
    def get_previous_published(self):
        return self.get_previous_by_pub_date()

    @property
    def get_next_published(self):
        return self.get_next_by_pub_date()

    @property
    def get_previous_admin_url(self):
        n = self.get_previous_by_pub_date()
        return reverse('admin:%s_%s_change' %(self._meta.app_label,  self._meta.model_name),  args=[n.id] )

    @property
    def get_next_admin_url(self):
        model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name)
        try:
            return reverse('admin:%s_%s_change' %(self._meta.app_label,  self._meta.model_name),  args=[self.get_next_by_pub_date().pk] )
        except model.DoesNotExist:
            return ''

    @property
    def is_portait(self):
        if int(self.height) > int(self.width):
            return True
        else:
            return False 

    def save(self, *args, **kwargs):
        super(LuxImage, self).save()


class LuxGallery(models.Model):
    title = models.CharField(blank=True, max_length=300)
    description = models.TextField(blank=True, null=True)
    slug = models.CharField(blank=True, max_length=300)
    thumb = models.ForeignKey(LuxImage, on_delete=models.CASCADE, related_name="gallery_thumb", null=True, blank=True)
    images = models.ManyToManyField(LuxImage)
    pub_date = models.DateTimeField(null=True)
    is_public = models.BooleanField(default=True)
    caption_style = models.CharField(blank=True, null=True, max_length=400)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Galleries'
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.title

    def get_main_image(self):
        return "%sgallery_thumbs/%s.jpg" % (settings.IMAGES_URL, self.id)

    def get_absolute_url(self):
        if self.is_public:
            return "/photos/galleries/%s" % (self.slug)
        else:
            return "/photos/galleries/private/%s" % (self.slug)

    def thumbs(self):
        lst = [x.image.name for x in self.images.all()]
        lst = ["<a href='/media/%s'>%s</a>" % (x, x.split('/')[-1]) for x in lst]
        return ', '.join(item for item in lst if item)
    thumbs.allow_tags = True


class LuxVideo(models.Model):
    video_mp4 = models.FileField(blank=True, null=True, upload_to=get_vid_upload_path)
    video_webm = models.FileField(blank=True, null=True, upload_to=get_vid_upload_path)
    video_poster = models.FileField(blank=True, null=True, upload_to=get_vid_upload_path)
    title = models.CharField(null=True, blank=True, max_length=300)
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    youtube_url = models.CharField(null=True, blank=True, max_length=80)
    vimeo_url = models.CharField(null=True, blank=True, max_length=300)

    def __str__(self):
        if self.title:
            return self.title
        else:
            return str(self.pk)

    def get_type(self):
        return str(self.__class__.__name__)

    class Meta:
        ordering = ('-pub_date', 'id')
        verbose_name_plural = 'Videos'
        verbose_name = 'Video'
        get_latest_by = 'pub_date'

    def save(self, *args, **kwargs):
        super(LuxVideo, self).save(*args, **kwargs)


class LuxAudio(models.Model):
    title = models.CharField(max_length=200)
    subtitle = models.CharField(max_length=200, blank=True)
    slug = models.SlugField(unique_for_date='pub_date', blank=True)
    body_html = models.TextField(blank=True)
    body_markdown = models.TextField(blank=True)
    pub_date = models.DateTimeField(default=datetime.datetime.now)
    mp3 = models.FileField(blank=True, null=True, upload_to=get_audio_upload_path)
    ogg = models.FileField(blank=True, null=True, upload_to=get_audio_upload_path)

    class Meta:
        ordering = ('-pub_date',)
        verbose_name_plural = 'Audio'
        verbose_name = 'Audio'
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("prompt:detail", kwargs={"slug": self.slug})

    @property
    def get_previous_published(self):
        return self.get_previous_by_pub_date(status__exact=1)

    @property
    def get_previous_admin_url(self):
        n = self.get_previous_by_pub_date()
        return reverse('admin:%s_%s_change' % (self._meta.app_label,  self._meta.model_name),  args=[n.id])

    @property
    def get_next_published(self):
        return self.get_next_by_pub_date(status__exact=1)

    @property
    def get_next_admin_url(self):
        model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name)
        try:
            return reverse('admin:%s_%s_change' % (self._meta.app_label,  self._meta.model_name),  args=[self.get_next_by_pub_date().pk])
        except model.DoesNotExist:
            return ''

    def get_type(self):
        return str(self.__class__.__name__)

    def save(self, *args, **kwargs):
        md = render_images(self.body_markdown)
        self.body_html = markdown_to_html(md)
        super(LuxAudio, self).save(*args, **kwargs)


@receiver(post_save, sender=LuxImage)
def post_save_events(sender, update_fields, created, instance, **kwargs):
    filename, file_extension = os.path.splitext(instance.image.path)
    if file_extension != ".mp4":
        img = Image.open(instance.image.path)
        instance.height = img.height
        instance.width = img.width
        post_save.disconnect(post_save_events, sender=LuxImage)
        instance.save()
        post_save.connect(post_save_events, sender=LuxImage)


@receiver(m2m_changed, sender=LuxImage.sizes.through)
def update_photo_sizes(sender, instance, **kwargs):
    base_path = "%s/%s/" % (settings.IMAGES_ROOT, instance.pub_date.strftime("%Y"))
    filename, file_extension = os.path.splitext(instance.image.path)
    if file_extension != ".mp4":
        img = Image.open(instance.image.path)
        resize_image(img, 160, None, 78, base_path, "%s_tn.%s" % (instance.get_image_name(), instance.get_image_ext()))
        for size in instance.sizes.all():
            if size.width:
                print("Image width is:"+str(img.width))
                try:
                    if size.width <= img.width:
                        resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), slugify(size.name), instance.get_image_ext()))
                    else:
                        raise ValidationError({'items': ["Size is larger than source image"]})
                except ImageSizeError:
                    m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through)
                    instance.sizes.remove(size)
                    m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through)
            if size.height:
                try:
                    if size.height <= img.height:
                        resize_image(img, None, size.height, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), slugify(size.name), instance.get_image_ext()))

                    else:
                        pass
                except ImageSizeError:
                    m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through)
                    instance.sizes.remove(size)
                    m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through)