models.py 3.05 KB
Newer Older
Skia's avatar
Skia committed
1 2 3
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
4
from django.utils.translation import ugettext_lazy as _
Skia's avatar
Skia committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

from core.models import SithFile, User

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

    @property
    def is_in_sas(self):
        sas = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
        return sas in self.get_parent_list()

    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
21
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
Skia's avatar
Skia committed
22 23 24 25 26
                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
27 28 29 30 31 32
    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
33 34 35 36 37 38 39 40 41 42 43 44 45
    def rotate(self, degree):
        from PIL import Image
        from io import BytesIO
        from django.core.files.base import ContentFile
        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()

46 47 48 49 50 51
    def get_next(self):
        return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__gt=self.id).order_by('id').first()

    def get_previous(self):
        return self.parent.children.exclude(is_moderated=False, asked_for_removal=True).filter(id__lt=self.id).order_by('id').last()

Skia's avatar
Skia committed
52 53 54 55 56 57 58 59 60 61 62 63 64
class Album(SithFile):
    class Meta:
        proxy = True

    @property
    def is_in_sas(self):
        sas = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
        return sas in self.get_parent_list()

    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
65
        return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
Skia's avatar
Skia committed
66 67 68 69 70
                user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP))

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

71 72 73 74 75 76 77 78 79 80
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']