2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith3
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
|
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
import os
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.cache import cache
|
2016-10-26 17:21:19 +00:00
|
|
|
from django.db import models
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2017-09-25 10:35:56 +00:00
|
|
|
from django.utils import timezone
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-11-20 22:53:41 +00:00
|
|
|
from PIL import Image
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
from core.models import SithFile, User
|
2024-06-24 11:07:36 +00:00
|
|
|
from core.utils import exif_auto_rotate, resize_image
|
2016-10-26 17:21:19 +00:00
|
|
|
|
2018-06-10 16:43:39 +00:00
|
|
|
|
2017-09-25 10:34:05 +00:00
|
|
|
class SASPictureManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().get_queryset().filter(is_in_sas=True, is_folder=False)
|
2018-06-10 16:43:39 +00:00
|
|
|
|
2017-09-25 10:34:05 +00:00
|
|
|
|
|
|
|
class SASAlbumManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2024-06-27 12:46:43 +00:00
|
|
|
return super().get_queryset().filter(is_in_sas=True, is_folder=True)
|
2018-06-10 16:43:39 +00:00
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-10-26 17:21:19 +00:00
|
|
|
class Picture(SithFile):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
2017-09-25 10:34:05 +00:00
|
|
|
objects = SASPictureManager()
|
|
|
|
|
2016-11-20 22:53:41 +00:00
|
|
|
@property
|
|
|
|
def is_vertical(self):
|
2018-10-04 19:29:19 +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
|
2016-11-20 22:53:41 +00:00
|
|
|
|
2016-10-26 17:21:19 +00:00
|
|
|
def can_be_edited_by(self, user):
|
2023-04-05 12:32:32 +00:00
|
|
|
perm = cache.get("%d_can_edit_pictures" % (user.id), None)
|
|
|
|
if perm is None:
|
2023-05-02 10:36:59 +00:00
|
|
|
perm = user.is_root or user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID)
|
2023-04-05 12:32:32 +00:00
|
|
|
|
|
|
|
cache.set("%d_can_edit_pictures" % (user.id), perm, timeout=4)
|
|
|
|
return perm
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
def can_be_viewed_by(self, user):
|
2019-09-15 14:43:17 +00:00
|
|
|
# SAS pictures are visible to old subscribers
|
2019-09-16 00:14:57 +00:00
|
|
|
# Result is cached 4s for this user
|
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
2023-03-30 12:38:40 +00:00
|
|
|
|
2019-09-16 00:14:57 +00:00
|
|
|
perm = cache.get("%d_can_view_pictures" % (user.id), False)
|
2023-04-05 12:32:32 +00:00
|
|
|
if not perm:
|
|
|
|
perm = user.was_subscribed
|
2023-03-30 12:38:40 +00:00
|
|
|
|
2019-09-16 00:14:57 +00:00
|
|
|
cache.set("%d_can_view_pictures" % (user.id), perm, timeout=4)
|
2023-04-05 12:32:32 +00:00
|
|
|
return (perm and self.is_moderated and self.is_in_sas) or self.can_be_edited_by(
|
|
|
|
user
|
|
|
|
)
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
def get_download_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:download", kwargs={"picture_id": self.id})
|
2016-10-26 17:21:19 +00:00
|
|
|
|
2016-11-20 10:56:33 +00:00
|
|
|
def get_download_compressed_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:download_compressed", kwargs={"picture_id": self.id})
|
2016-11-20 10:56:33 +00:00
|
|
|
|
|
|
|
def get_download_thumb_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:download_thumb", kwargs={"picture_id": self.id})
|
2016-11-20 10:56:33 +00:00
|
|
|
|
2016-11-25 12:47:09 +00:00
|
|
|
def get_absolute_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:picture", kwargs={"picture_id": self.id})
|
2016-11-25 12:47:09 +00:00
|
|
|
|
2024-06-27 12:57:40 +00:00
|
|
|
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
|
2018-10-04 19:29:19 +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])
|
2016-12-19 15:20:50 +00:00
|
|
|
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):
|
2018-10-04 19:29:19 +00:00
|
|
|
for attr in ["file", "compressed", "thumbnail"]:
|
2016-11-30 08:19:09 +00:00
|
|
|
name = self.__getattribute__(attr).name
|
2018-10-04 19:29:19 +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)
|
2018-10-04 19:29:19 +00:00
|
|
|
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
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
def get_next(self):
|
2016-12-09 18:35:23 +00:00
|
|
|
if self.is_moderated:
|
2018-10-04 19:29:19 +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()
|
|
|
|
)
|
2016-12-09 18:35:23 +00:00
|
|
|
else:
|
2018-10-04 19:29:19 +00:00
|
|
|
return (
|
|
|
|
Picture.objects.filter(id__gt=self.id, is_moderated=False)
|
|
|
|
.order_by("id")
|
|
|
|
.first()
|
|
|
|
)
|
2016-11-19 16:19:00 +00:00
|
|
|
|
|
|
|
def get_previous(self):
|
2016-12-09 18:35:23 +00:00
|
|
|
if self.is_moderated:
|
2018-10-04 19:29:19 +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-12-09 18:35:23 +00:00
|
|
|
else:
|
2018-10-04 19:29:19 +00:00
|
|
|
return (
|
|
|
|
Picture.objects.filter(id__lt=self.id, is_moderated=False)
|
|
|
|
.order_by("-id")
|
|
|
|
.first()
|
|
|
|
)
|
2016-11-19 16:19:00 +00:00
|
|
|
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2016-10-26 17:21:19 +00:00
|
|
|
class Album(SithFile):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
2017-09-25 10:34:05 +00:00
|
|
|
objects = SASAlbumManager()
|
|
|
|
|
2016-12-18 11:35:23 +00:00
|
|
|
@property
|
|
|
|
def children_pictures(self):
|
2017-09-25 10:34:05 +00:00
|
|
|
return Picture.objects.filter(parent=self)
|
2016-12-18 11:35:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def children_albums(self):
|
2017-09-25 10:34:05 +00:00
|
|
|
return Album.objects.filter(parent=self)
|
2016-12-18 11:35:23 +00:00
|
|
|
|
2016-10-26 17:21:19 +00:00
|
|
|
def can_be_edited_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
return user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID)
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
def can_be_viewed_by(self, user):
|
2016-12-18 11:35:23 +00:00
|
|
|
# file = SithFile.objects.filter(id=self.id).first()
|
2018-10-04 19:29:19 +00:00
|
|
|
return self.can_be_edited_by(user) or (
|
|
|
|
self.is_in_sas and self.is_moderated and user.was_subscribed
|
|
|
|
) # or user.can_view(file)
|
2016-10-26 17:21:19 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("sas:album", kwargs={"album_id": self.id})
|
2016-10-26 17:21:19 +00:00
|
|
|
|
2016-11-30 08:19:09 +00:00
|
|
|
def get_download_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
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):
|
2018-10-04 19:29:19 +00:00
|
|
|
p = (
|
|
|
|
self.children_pictures.order_by("?").first()
|
|
|
|
or self.children_albums.exclude(file=None)
|
|
|
|
.exclude(file="")
|
|
|
|
.order_by("?")
|
|
|
|
.first()
|
|
|
|
)
|
2016-12-18 17:34:48 +00:00
|
|
|
if p and p.file:
|
|
|
|
im = Image.open(BytesIO(p.file.read()))
|
|
|
|
self.file = resize_image(im, 200, "jpeg")
|
2018-10-04 19:29:19 +00:00
|
|
|
self.file.name = self.name + "/thumb.jpg"
|
2016-12-18 17:34:48 +00:00
|
|
|
self.save()
|
|
|
|
|
2018-06-10 16:43:39 +00:00
|
|
|
|
2017-09-25 10:35:56 +00:00
|
|
|
def sas_notification_callback(notif):
|
|
|
|
count = Picture.objects.filter(is_moderated=False).count()
|
|
|
|
if count:
|
|
|
|
notif.viewed = False
|
|
|
|
else:
|
|
|
|
notif.viewed = True
|
2017-10-15 09:59:54 +00:00
|
|
|
notif.param = "%s" % count
|
|
|
|
notif.date = timezone.now()
|
2017-06-12 08:02:38 +00:00
|
|
|
|
2018-06-10 16:43:39 +00:00
|
|
|
|
2016-11-19 16:19:00 +00:00
|
|
|
class PeoplePictureRelation(models.Model):
|
2024-07-12 07:34:16 +00:00
|
|
|
"""The PeoplePictureRelation class makes the connection between User and Picture."""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
verbose_name=_("user"),
|
|
|
|
related_name="pictures",
|
|
|
|
null=False,
|
|
|
|
blank=False,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
picture = models.ForeignKey(
|
|
|
|
Picture,
|
|
|
|
verbose_name=_("picture"),
|
|
|
|
related_name="people",
|
|
|
|
null=False,
|
|
|
|
blank=False,
|
2019-10-05 17:05:56 +00:00
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-11-19 16:19:00 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-10-04 19:29:19 +00:00
|
|
|
unique_together = ["user", "picture"]
|
2016-11-25 12:47:09 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.user.get_display_name() + " - " + str(self.picture)
|