Sith/sas/models.py

193 lines
7.0 KiB
Python
Raw Normal View History

# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
2016-10-26 17:21:19 +00:00
from django.db import models
from django.core.urlresolvers import reverse
2017-06-12 08:02:38 +00:00
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
2016-11-20 22:53:41 +00:00
from PIL import Image
from io import BytesIO
2017-03-30 17:13:47 +00:00
import os
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
2018-06-10 16:43:39 +00:00
class SASPictureManager(models.Manager):
def get_queryset(self):
return super(SASPictureManager, self).get_queryset().filter(is_in_sas=True,
2018-06-10 16:43:39 +00:00
is_folder=False)
class SASAlbumManager(models.Manager):
def get_queryset(self):
return super(SASAlbumManager, self).get_queryset().filter(is_in_sas=True,
2018-06-10 16:43:39 +00:00
is_folder=True)
2017-06-12 08:02:38 +00:00
2016-10-26 17:21:19 +00:00
class Picture(SithFile):
class Meta:
proxy = True
objects = SASPictureManager()
2016-11-20 22:53:41 +00:00
@property
def is_vertical(self):
2017-03-30 17:13:47 +00:00
with open(os.path.join(settings.MEDIA_ROOT, self.file.name).encode('utf-8'), 'rb') as f:
2016-11-29 10:35:31 +00:00
im = Image.open(BytesIO(f.read()))
(w, h) = im.size
return (w / h) < 1
return False
2016-11-20 22:53:41 +00:00
2016-10-26 17:21:19 +00:00
def can_be_edited_by(self, user):
# file = SithFile.objects.filter(id=self.id).first()
2017-06-12 08:02:38 +00:00
return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) # or user.can_edit(file)
2016-10-26 17:21:19 +00:00
def can_be_viewed_by(self, user):
# file = SithFile.objects.filter(id=self.id).first()
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
2017-06-12 08:02:38 +00:00
user.was_subscribed) # or user.can_view(file)
2016-10-26 17:21:19 +00:00
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})
def generate_thumbnails(self, overwrite=False):
2016-11-20 23:02:40 +00:00
im = Image.open(BytesIO(self.file.read()))
try:
im = exif_auto_rotate(im)
2017-06-12 08:02:38 +00:00
except:
pass
2016-11-20 23:02:40 +00:00
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, 1200, self.mime_type.split('/')[-1])
if overwrite:
self.file.delete()
self.thumbnail.delete()
self.compressed.delete()
2016-11-20 23:02:40 +00:00
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']:
2016-11-30 08:19:09 +00:00
name = self.__getattribute__(attr).name
2017-03-30 17:13:47 +00:00
with open(os.path.join(settings.MEDIA_ROOT, name).encode('utf-8'), 'r+b') as file:
2016-11-30 08:19:09 +00:00
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)
2016-11-20 12:39:04 +00:00
def get_next(self):
if self.is_moderated:
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
2017-06-12 08:02:38 +00:00
id__gt=self.id).order_by('id').first()
else:
return Picture.objects.filter(id__gt=self.id, is_moderated=False).order_by('id').first()
def get_previous(self):
if self.is_moderated:
return self.parent.children.filter(is_moderated=True, asked_for_removal=False, is_folder=False,
2017-06-12 08:02:38 +00:00
id__lt=self.id).order_by('id').last()
else:
return Picture.objects.filter(id__lt=self.id, is_moderated=False).order_by('-id').first()
2017-06-12 08:02:38 +00:00
2016-10-26 17:21:19 +00:00
class Album(SithFile):
class Meta:
proxy = True
objects = SASAlbumManager()
@property
def children_pictures(self):
return Picture.objects.filter(parent=self)
@property
def children_albums(self):
return Album.objects.filter(parent=self)
2016-10-26 17:21:19 +00:00
def can_be_edited_by(self, user):
# file = SithFile.objects.filter(id=self.id).first()
2017-06-12 08:02:38 +00:00
return user.is_in_group(settings.SITH_GROUP_SAS_ADMIN_ID) # or user.can_edit(file)
2016-10-26 17:21:19 +00:00
def can_be_viewed_by(self, user):
# file = SithFile.objects.filter(id=self.id).first()
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
2017-06-12 08:02:38 +00:00
user.was_subscribed) # or user.can_view(file)
2016-10-26 17:21:19 +00:00
def get_absolute_url(self):
return reverse('sas:album', kwargs={'album_id': self.id})
2016-11-30 08:19:09 +00:00
def get_download_url(self):
return reverse('sas:album_preview', kwargs={'album_id': self.id})
2016-11-30 08:19:09 +00:00
2016-12-18 17:34:48 +00:00
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()
2018-06-10 16:43:39 +00:00
def sas_notification_callback(notif):
count = Picture.objects.filter(is_moderated=False).count()
if count:
notif.viewed = False
else:
notif.viewed = True
notif.param = "%s" % count
notif.date = timezone.now()
2017-06-12 08:02:38 +00:00
2018-06-10 16:43:39 +00:00
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)