models.py 5.35 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
Sli's avatar
Sli committed
10
import os
Skia's avatar
Skia committed
11
12

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

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

Skia's avatar
Skia committed
19
20
    @property
    def is_vertical(self):
Sli's avatar
Sli committed
21
        with open(os.path.join(settings.MEDIA_ROOT, self.file.name).encode('utf-8'), 'rb') as f:
Skia's avatar
Skia committed
22
23
24
25
            im = Image.open(BytesIO(f.read()))
            (w, h) = im.size
            return (w / h) < 1
        return False
Skia's avatar
Skia committed
26

Skia's avatar
Skia committed
27
    def can_be_edited_by(self, user):
28
29
        # file = SithFile.objects.filter(id=self.id).first()
        return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)# or user.can_edit(file)
Skia's avatar
Skia committed
30
31

    def can_be_viewed_by(self, user):
32
        # file = SithFile.objects.filter(id=self.id).first()
Skia's avatar
Skia committed
33
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
34
                user.was_subscribed)# or user.can_view(file)
Skia's avatar
Skia committed
35
36
37
38

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

Skia's avatar
Skia committed
39
40
41
42
43
44
    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
45
46
47
    def get_absolute_url(self):
        return reverse('sas:picture', kwargs={'picture_id': self.id})

48
    def generate_thumbnails(self, overwrite=False):
Skia's avatar
Skia committed
49
50
51
52
53
54
        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])
55
56
57
58
59
        compressed = resize_image(im, 1200, self.mime_type.split('/')[-1])
        if overwrite:
            self.file.delete()
            self.thumbnail.delete()
            self.compressed.delete()
Skia's avatar
Skia committed
60
61
62
63
64
65
66
67
        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
68
69
    def rotate(self, degree):
        for attr in ['file', 'compressed', 'thumbnail']:
Skia's avatar
Skia committed
70
            name = self.__getattribute__(attr).name
Sli's avatar
Sli committed
71
            with open(os.path.join(settings.MEDIA_ROOT, name).encode('utf-8'), 'r+b') as file:
Skia's avatar
Skia committed
72
73
74
75
76
                if file:
                    im = Image.open(BytesIO(file.read()))
                    file.seek(0)
                    im = im.rotate(degree, expand=True)
                    im.save(fp=file, format=self.mime_type.split('/')[-1].upper(), quality=90, optimize=True, progressive=True)
Skia's avatar
Skia committed
77

78
    def get_next(self):
79
80
81
82
83
        if self.is_moderated:
            return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
                    id__gt=self.id).order_by('id').first()
        else:
            return Picture.objects.filter(id__gt=self.id, is_moderated=False, is_in_sas=True).order_by('id').first()
84
85

    def get_previous(self):
86
87
88
89
90
        if self.is_moderated:
            return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
                    id__lt=self.id).order_by('id').last()
        else:
            return Picture.objects.filter(id__lt=self.id, is_moderated=False, is_in_sas=True).order_by('-id').first()
91

Skia's avatar
Skia committed
92
93
94
95
class Album(SithFile):
    class Meta:
        proxy = True

96
97
98
99
100
101
102
103
    @property
    def children_pictures(self):
        return Picture.objects.filter(parent=self, is_folder=False)

    @property
    def children_albums(self):
        return Album.objects.filter(parent=self, is_folder=True)

Skia's avatar
Skia committed
104
    def can_be_edited_by(self, user):
105
106
        # file = SithFile.objects.filter(id=self.id).first()
        return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID)# or user.can_edit(file)
Skia's avatar
Skia committed
107
108

    def can_be_viewed_by(self, user):
109
        # file = SithFile.objects.filter(id=self.id).first()
Skia's avatar
Skia committed
110
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
111
                user.was_subscribed)# or user.can_view(file)
Skia's avatar
Skia committed
112
113
114
115

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

Skia's avatar
Skia committed
116
117
118
    def get_download_url(self):
        return reverse('sas:download', kwargs={'picture_id': self.id})

Skia's avatar
Skia committed
119
120
121
122
123
124
125
126
    def generate_thumbnail(self):
        p = self.children_pictures.order_by('?').first() or self.children_albums.exclude(file=None).exclude(file="").order_by('?').first()
        if p and p.file:
            im = Image.open(BytesIO(p.file.read()))
            self.file = resize_image(im, 200, "jpeg")
            self.file.name = self.name + '/thumb.jpg'
            self.save()

127
128
129
130
131
132
133
134
135
136
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
137
138
139

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