Sith/sas/models.py

104 lines
3.8 KiB
Python
Raw Normal View History

2016-10-26 17:21:19 +00:00
from django.db import models
2016-11-25 12:47:09 +00:00
from django.core.urlresolvers import reverse_lazy, reverse
2016-10-26 17:21:19 +00:00
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
2016-11-20 22:53:41 +00:00
from django.core.files.base import ContentFile
from PIL import Image
from io import BytesIO
2016-10-26 17:21:19 +00:00
from core.models import SithFile, User
2016-11-20 23:02:40 +00:00
from core.utils import resize_image, exif_auto_rotate
2016-10-26 17:21:19 +00:00
class Picture(SithFile):
class Meta:
proxy = True
2016-11-20 22:53:41 +00:00
@property
def is_vertical(self):
im = Image.open(BytesIO(self.file.read()))
(w, h) = im.size
return (w / h) < 1
2016-10-26 17:21:19 +00:00
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):
2016-11-09 08:23:39 +00:00
return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
2016-10-26 17:21:19 +00:00
user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP))
def get_download_url(self):
return reverse('sas:download', kwargs={'picture_id': self.id})
2016-11-20 10:56:33 +00:00
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})
2016-11-25 12:47:09 +00:00
def get_absolute_url(self):
return reverse('sas:picture', kwargs={'picture_id': self.id})
2016-11-20 23:02:40 +00:00
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()
2016-11-20 12:39:04 +00:00
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()
def get_next(self):
2016-11-20 23:02:40 +00:00
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
id__gt=self.id).order_by('id').first()
def get_previous(self):
2016-11-20 23:02:40 +00:00
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
id__lt=self.id).order_by('id').last()
2016-10-26 17:21:19 +00:00
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):
2016-11-09 08:23:39 +00:00
return self.can_be_edited_by(user) or (self.is_in_sas and self.is_moderated and
2016-10-26 17:21:19 +00:00
user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP))
def get_absolute_url(self):
return reverse('sas:album', kwargs={'album_id': self.id})
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']
2016-11-25 12:47:09 +00:00
def __str__(self):
return self.user.get_display_name() + " - " + str(self.picture)