mirror of
https://github.com/ae-utbm/sith.git
synced 2025-09-13 11:35:44 +00:00
Migrate albums and pictures to their own tables
This commit is contained in:
348
sas/models.py
348
sas/models.py
@@ -17,7 +17,6 @@ from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import ClassVar, Self
|
||||
|
||||
from django.conf import settings
|
||||
@@ -25,22 +24,39 @@ from django.core.cache import cache
|
||||
from django.db import models
|
||||
from django.db.models import Exists, OuterRef, Q
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from PIL import Image
|
||||
|
||||
from core.models import Notification, SithFile, User
|
||||
from core.models import Group, Notification, User
|
||||
from core.utils import exif_auto_rotate, resize_image
|
||||
|
||||
|
||||
class SasFile(SithFile):
|
||||
"""Proxy model for any file in the SAS.
|
||||
def get_directory(instance: SasFile, filename: str):
|
||||
return f"./{instance.parent_path}/{filename}"
|
||||
|
||||
|
||||
def get_compressed_directory(instance: SasFile, filename: str):
|
||||
return f"./.compressed/{instance.parent_path}/{filename}"
|
||||
|
||||
|
||||
def get_thumbnail_directory(instance: SasFile, filename: str):
|
||||
if isinstance(instance, Album):
|
||||
name, extension = filename.rsplit(".", 1)
|
||||
filename = f"{name}/thumb.{extension}"
|
||||
return f"./.thumbnails/{instance.parent_path}/{filename}"
|
||||
|
||||
|
||||
class SasFile(models.Model):
|
||||
"""Abstract model for SAS files
|
||||
|
||||
May be used to have logic that should be shared by both
|
||||
[Picture][sas.models.Picture] and [Album][sas.models.Album].
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
abstract = True
|
||||
permissions = [
|
||||
("moderate_sasfile", "Can moderate SAS files"),
|
||||
("view_unmoderated_sasfile", "Can view not moderated SAS files"),
|
||||
@@ -65,6 +81,121 @@ class SasFile(SithFile):
|
||||
def can_be_edited_by(self, user):
|
||||
return user.has_perm("sas.change_sasfile")
|
||||
|
||||
@cached_property
|
||||
def parent_path(self) -> str:
|
||||
return "/".join(["SAS", *[p.name for p in self.parent_list]])
|
||||
|
||||
@cached_property
|
||||
def parent_list(self) -> list[Self]:
|
||||
parents = []
|
||||
current = self.parent
|
||||
while current is not None:
|
||||
parents.append(current)
|
||||
current = current.parent
|
||||
return parents
|
||||
|
||||
|
||||
class AlbumQuerySet(models.QuerySet):
|
||||
def viewable_by(self, user: User) -> Self:
|
||||
"""Filter the albums that this user can view.
|
||||
|
||||
Warning:
|
||||
Calling this queryset method may add several additional requests.
|
||||
"""
|
||||
if user.is_root or user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
|
||||
return self.all()
|
||||
if user.was_subscribed:
|
||||
return self.filter(is_moderated=True)
|
||||
# known bug : if all children of an album are also albums
|
||||
# then this album is excluded, even if one of the sub-albums should be visible.
|
||||
# The fs-like navigation is likely to be half-broken for non-subscribers,
|
||||
# but that's ok, since non-subscribers are expected to see only the albums
|
||||
# containing pictures on which they have been identified (hence, very few).
|
||||
# Most, if not all, of their albums will be displayed on the
|
||||
# `latest albums` section of the SAS.
|
||||
# Moreover, they will still see all of their picture in their profile.
|
||||
return self.filter(
|
||||
Exists(Picture.objects.filter(parent_id=OuterRef("pk")).viewable_by(user))
|
||||
)
|
||||
|
||||
def annotate_is_moderated(self) -> Self:
|
||||
# an album is moderated if it has at least one moderated photo
|
||||
# if there is no photo at all, the album isn't considered as non-moderated
|
||||
# (it's just empty)
|
||||
return self.annotate(
|
||||
is_moderated=Exists(
|
||||
Picture.objects.filter(parent=OuterRef("pk"), is_moderated=True)
|
||||
)
|
||||
| ~Exists(Picture.objects.filter(parent=OuterRef("pk")))
|
||||
)
|
||||
|
||||
|
||||
class Album(SasFile):
|
||||
NAME_MAX_LENGTH: ClassVar[int] = 50
|
||||
|
||||
name = models.CharField(_("name"), max_length=100)
|
||||
parent = models.ForeignKey(
|
||||
"self",
|
||||
related_name="children",
|
||||
verbose_name=_("parent"),
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
thumbnail = models.FileField(
|
||||
upload_to=get_thumbnail_directory, verbose_name=_("thumbnail"), max_length=256
|
||||
)
|
||||
view_groups = models.ManyToManyField(
|
||||
Group, related_name="viewable_albums", verbose_name=_("view groups")
|
||||
)
|
||||
edit_groups = models.ManyToManyField(
|
||||
Group, related_name="editable_albums", verbose_name=_("edit groups")
|
||||
)
|
||||
event_date = models.DateField(
|
||||
_("event date"),
|
||||
help_text=_("The date on which the photos in this album were taken"),
|
||||
default=timezone.localdate,
|
||||
)
|
||||
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
||||
|
||||
objects = AlbumQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("album")
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["name", "parent"], name="unique_album_name_if_same_parent"
|
||||
)
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"Album {self.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
for user in User.objects.filter(
|
||||
groups__id__in=[settings.SITH_GROUP_SAS_ADMIN_ID]
|
||||
):
|
||||
Notification(
|
||||
user=user,
|
||||
url=reverse("sas:moderation"),
|
||||
type="SAS_MODERATION",
|
||||
param="1",
|
||||
).save()
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("sas:album", kwargs={"album_id": self.id})
|
||||
|
||||
def get_download_url(self):
|
||||
return reverse("sas:album_preview", kwargs={"album_id": self.id})
|
||||
|
||||
def generate_thumbnail(self):
|
||||
p = self.pictures.order_by("?").first() or self.children.order_by("?").first()
|
||||
if p and p.thumbnail:
|
||||
self.thumbnail = p.thumbnail
|
||||
self.thumbnail.name = f"{self.name}/thumb.webp"
|
||||
self.save()
|
||||
|
||||
|
||||
class PictureQuerySet(models.QuerySet):
|
||||
def viewable_by(self, user: User) -> Self:
|
||||
@@ -80,23 +211,62 @@ class PictureQuerySet(models.QuerySet):
|
||||
return self.filter(people__user_id=user.id, is_moderated=True)
|
||||
|
||||
|
||||
class SASPictureManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(is_in_sas=True, is_folder=False)
|
||||
|
||||
|
||||
class Picture(SasFile):
|
||||
class Meta:
|
||||
proxy = True
|
||||
name = models.CharField(_("file name"), max_length=256)
|
||||
parent = models.ForeignKey(
|
||||
Album,
|
||||
related_name="pictures",
|
||||
verbose_name=_("album"),
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
thumbnail = models.FileField(
|
||||
upload_to=get_thumbnail_directory,
|
||||
verbose_name=_("thumbnail"),
|
||||
unique=True,
|
||||
max_length=256,
|
||||
)
|
||||
original = models.FileField(
|
||||
upload_to=get_directory,
|
||||
verbose_name=_("original image"),
|
||||
max_length=256,
|
||||
unique=True,
|
||||
)
|
||||
compressed = models.FileField(
|
||||
upload_to=get_compressed_directory,
|
||||
verbose_name=_("compressed image"),
|
||||
max_length=256,
|
||||
unique=True,
|
||||
)
|
||||
created_at = models.DateTimeField(default=timezone.now)
|
||||
owner = models.ForeignKey(
|
||||
User,
|
||||
related_name="owned_pictures",
|
||||
verbose_name=_("owner"),
|
||||
on_delete=models.PROTECT,
|
||||
)
|
||||
|
||||
objects = SASPictureManager.from_queryset(PictureQuerySet)()
|
||||
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
||||
asked_for_removal = models.BooleanField(_("asked for removal"), default=False)
|
||||
moderator = models.ForeignKey(
|
||||
User,
|
||||
related_name="moderated_pictures",
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
)
|
||||
|
||||
@property
|
||||
def is_vertical(self):
|
||||
with open(settings.MEDIA_ROOT / self.file.name, "rb") as f:
|
||||
im = Image.open(BytesIO(f.read()))
|
||||
(w, h) = im.size
|
||||
return (w / h) < 1
|
||||
objects = PictureQuerySet.as_manager()
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self._state.adding:
|
||||
self.generate_thumbnails()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("sas:picture", kwargs={"picture_id": self.id})
|
||||
|
||||
def get_download_url(self):
|
||||
return reverse("sas:download", kwargs={"picture_id": self.id})
|
||||
@@ -107,41 +277,33 @@ class Picture(SasFile):
|
||||
def get_download_thumb_url(self):
|
||||
return reverse("sas:download_thumb", kwargs={"picture_id": self.id})
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("sas:picture", kwargs={"picture_id": self.id})
|
||||
@property
|
||||
def is_vertical(self):
|
||||
# original, compressed and thumbnail image have all three the same ratio,
|
||||
# so the smallest one is used to tell if the image is vertical
|
||||
im = Image.open(BytesIO(self.thumbnail.read()))
|
||||
(w, h) = im.size
|
||||
return w < h
|
||||
|
||||
def generate_thumbnails(self, *, overwrite=False):
|
||||
im = Image.open(BytesIO(self.file.read()))
|
||||
def generate_thumbnails(self):
|
||||
im = Image.open(self.original)
|
||||
with contextlib.suppress(Exception):
|
||||
im = exif_auto_rotate(im)
|
||||
# convert the compressed image and the thumbnail into webp
|
||||
# The original image keeps its original type, because it's not
|
||||
# meant to be shown on the website, but rather to keep the real image
|
||||
# for less frequent cases (like downloading the pictures of an user)
|
||||
extension = self.mime_type.split("/")[-1]
|
||||
# the HD version of the image doesn't need to be optimized, because :
|
||||
# - it isn't frequently queried
|
||||
# - optimizing large images takes a lot time, which greatly hinders the UX
|
||||
# - optimizing large images takes a lot of time, which greatly hinders the UX
|
||||
# - photographers usually already optimize their images
|
||||
file = resize_image(im, max(im.size), extension, optimize=False)
|
||||
thumb = resize_image(im, 200, "webp")
|
||||
compressed = resize_image(im, 1200, "webp")
|
||||
if overwrite:
|
||||
self.file.delete()
|
||||
self.thumbnail.delete()
|
||||
self.compressed.delete()
|
||||
new_extension_name = str(Path(self.name).with_suffix(".webp"))
|
||||
self.file = file
|
||||
self.file.name = self.name
|
||||
self.thumbnail = thumb
|
||||
self.thumbnail.name = new_extension_name
|
||||
self.thumbnail.name = self.name
|
||||
self.compressed = compressed
|
||||
self.compressed.name = new_extension_name
|
||||
self.compressed.name = self.name
|
||||
|
||||
def rotate(self, degree):
|
||||
for attr in ["file", "compressed", "thumbnail"]:
|
||||
name = self.__getattribute__(attr).name
|
||||
with open(settings.MEDIA_ROOT / name, "r+b") as file:
|
||||
for field in self.original, self.compressed, self.thumbnail:
|
||||
with open(field.file, "r+b") as file:
|
||||
if file:
|
||||
im = Image.open(BytesIO(file.read()))
|
||||
file.seek(0)
|
||||
@@ -154,110 +316,6 @@ class Picture(SasFile):
|
||||
progressive=True,
|
||||
)
|
||||
|
||||
def get_next(self):
|
||||
if self.is_moderated:
|
||||
pictures_qs = self.parent.children.filter(
|
||||
is_moderated=True,
|
||||
asked_for_removal=False,
|
||||
is_folder=False,
|
||||
id__gt=self.id,
|
||||
)
|
||||
else:
|
||||
pictures_qs = Picture.objects.filter(id__gt=self.id, is_moderated=False)
|
||||
return pictures_qs.order_by("id").first()
|
||||
|
||||
def get_previous(self):
|
||||
if self.is_moderated:
|
||||
pictures_qs = self.parent.children.filter(
|
||||
is_moderated=True,
|
||||
asked_for_removal=False,
|
||||
is_folder=False,
|
||||
id__lt=self.id,
|
||||
)
|
||||
else:
|
||||
pictures_qs = Picture.objects.filter(id__lt=self.id, is_moderated=False)
|
||||
return pictures_qs.order_by("-id").first()
|
||||
|
||||
|
||||
class AlbumQuerySet(models.QuerySet):
|
||||
def viewable_by(self, user: User) -> Self:
|
||||
"""Filter the albums that this user can view.
|
||||
|
||||
Warning:
|
||||
Calling this queryset method may add several additional requests.
|
||||
"""
|
||||
if user.has_perm("sas.moderate_sasfile"):
|
||||
return self.all()
|
||||
if user.was_subscribed:
|
||||
return self.filter(Q(is_moderated=True) | Q(owner=user))
|
||||
# known bug : if all children of an album are also albums
|
||||
# then this album is excluded, even if one of the sub-albums should be visible.
|
||||
# The fs-like navigation is likely to be half-broken for non-subscribers,
|
||||
# but that's ok, since non-subscribers are expected to see only the albums
|
||||
# containing pictures on which they have been identified (hence, very few).
|
||||
# Most, if not all, of their albums will be displayed on the
|
||||
# `latest albums` section of the SAS.
|
||||
# Moreover, they will still see all of their picture in their profile.
|
||||
return self.filter(
|
||||
Exists(Picture.objects.filter(parent_id=OuterRef("pk")).viewable_by(user))
|
||||
)
|
||||
|
||||
|
||||
class SASAlbumManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(is_in_sas=True, is_folder=True)
|
||||
|
||||
|
||||
class Album(SasFile):
|
||||
NAME_MAX_LENGTH: ClassVar[int] = 50
|
||||
"""Maximum length of an album's name.
|
||||
|
||||
[SithFile][core.models.SithFile] have a maximum length
|
||||
of 256 characters.
|
||||
However, this limit is too high for albums.
|
||||
Names longer than 50 characters are harder to read
|
||||
and harder to display on the SAS page.
|
||||
|
||||
It is to be noted, though, that this does not
|
||||
add or modify any db behaviour.
|
||||
It's just a constant to be used in views and forms.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
objects = SASAlbumManager.from_queryset(AlbumQuerySet)()
|
||||
|
||||
@property
|
||||
def children_pictures(self):
|
||||
return Picture.objects.filter(parent=self)
|
||||
|
||||
@property
|
||||
def children_albums(self):
|
||||
return Album.objects.filter(parent=self)
|
||||
|
||||
def get_absolute_url(self):
|
||||
if self.id == settings.SITH_SAS_ROOT_DIR_ID:
|
||||
return reverse("sas:main")
|
||||
return reverse("sas:album", kwargs={"album_id": self.id})
|
||||
|
||||
def get_download_url(self):
|
||||
return reverse("sas:album_preview", kwargs={"album_id": self.id})
|
||||
|
||||
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:
|
||||
image = resize_image(Image.open(BytesIO(p.file.read())), 200, "webp")
|
||||
self.file = image
|
||||
self.file.name = f"{self.name}/thumb.webp"
|
||||
self.save()
|
||||
|
||||
|
||||
def sas_notification_callback(notif: Notification):
|
||||
count = Picture.objects.filter(is_moderated=False).count()
|
||||
|
Reference in New Issue
Block a user