models.py 3.79 KB
Newer Older
Skia's avatar
Skia committed
1
from django.db import models
Skia's avatar
Skia committed
2
from django.core.urlresolvers import reverse_lazy, reverse
Skia's avatar
Skia committed
3
4
from django.conf import settings
from django.core.urlresolvers import reverse
5
from django.utils.translation import ugettext_lazy as _
Skia's avatar
Skia committed
6
7
8
9
from django.core.files.base import ContentFile

from PIL import Image
from io import BytesIO
Skia's avatar
Skia committed
10
11

from core.models import SithFile, User
Skia's avatar
Skia committed
12
from core.utils import resize_image, exif_auto_rotate
Skia's avatar
Skia committed
13
14
15
16
17

class Picture(SithFile):
    class Meta:
        proxy = True

Skia's avatar
Skia committed
18
19
20
21
22
23
    @property
    def is_vertical(self):
        im = Image.open(BytesIO(self.file.read()))
        (w, h) = im.size
        return (w / h) < 1

Skia's avatar
Skia committed
24
25
26
27
    def can_be_edited_by(self, user):
        return user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_ID)

    def can_be_viewed_by(self, user):
Skia's avatar
Skia committed
28
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
Skia's avatar
Skia committed
29
30
31
32
33
                user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP))

    def get_download_url(self):
        return reverse('sas:download', kwargs={'picture_id': self.id})

Skia's avatar
Skia committed
34
35
36
37
38
39
    def get_download_compressed_url(self):
        return reverse('sas:download_compressed', kwargs={'picture_id': self.id})

    def get_download_thumb_url(self):
        return reverse('sas:download_thumb', kwargs={'picture_id': self.id})

Skia's avatar
Skia committed
40
41
42
    def get_absolute_url(self):
        return reverse('sas:picture', kwargs={'picture_id': self.id})

Skia's avatar
Skia committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    def generate_thumbnails(self):
        im = Image.open(BytesIO(self.file.read()))
        try:
            im = exif_auto_rotate(im)
        except: pass
        file = resize_image(im, max(im.size), self.mime_type.split('/')[-1])
        thumb = resize_image(im, 200, self.mime_type.split('/')[-1])
        compressed = resize_image(im, 600, self.mime_type.split('/')[-1])
        self.file = file
        self.file.name = self.name
        self.thumbnail = thumb
        self.thumbnail.name = self.name
        self.compressed = compressed
        self.compressed.name = self.name
        self.save()

Skia's avatar
Skia committed
59
60
61
62
63
64
65
66
67
68
    def rotate(self, degree):
        for attr in ['file', 'compressed', 'thumbnail']:
            if self.__getattribute__(attr):
                im = Image.open(BytesIO(self.__getattribute__(attr).read()))
                new_image = BytesIO()
                im = im.rotate(degree, expand=True)
                im.save(fp=new_image, format=self.mime_type.split('/')[-1].upper(), quality=90, optimize=True, progressive=True)
                self.__getattribute__(attr).save(self.name, ContentFile(new_image.getvalue()))
                self.save()

69
    def get_next(self):
Skia's avatar
Skia committed
70
71
        return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
                id__gt=self.id).order_by('id').first()
72
73

    def get_previous(self):
Skia's avatar
Skia committed
74
75
        return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
                id__lt=self.id).order_by('id').last()
76

Skia's avatar
Skia committed
77
78
79
80
81
82
83
84
class Album(SithFile):
    class Meta:
        proxy = True

    def can_be_edited_by(self, user):
        return user.is_in_group(settings.SITH_SAS_ADMIN_GROUP_ID)

    def can_be_viewed_by(self, user):
Skia's avatar
Skia committed
85
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
Skia's avatar
Skia committed
86
87
88
89
90
                user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP))

    def get_absolute_url(self):
        return reverse('sas:album', kwargs={'album_id': self.id})

91
92
93
94
95
96
97
98
99
100
class PeoplePictureRelation(models.Model):
    """
    The PeoplePictureRelation class makes the connection between User and Picture

    """
    user = models.ForeignKey(User, verbose_name=_('user'), related_name="pictures", null=False, blank=False)
    picture = models.ForeignKey(Picture, verbose_name=_('picture'), related_name="people", null=False, blank=False)

    class Meta:
        unique_together = ['user', 'picture']
Skia's avatar
Skia committed
101
102
103

    def __str__(self):
        return self.user.get_display_name() + " - " + str(self.picture)