Skip to content

Commit

Permalink
Migrate albums and pictures to their own tables
Browse files Browse the repository at this point in the history
  • Loading branch information
imperosol committed Jan 26, 2025
1 parent b9482a6 commit 6d36f6e
Show file tree
Hide file tree
Showing 22 changed files with 676 additions and 289 deletions.
4 changes: 2 additions & 2 deletions core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class PageAdmin(admin.ModelAdmin):

@admin.register(SithFile)
class SithFileAdmin(admin.ModelAdmin):
list_display = ("name", "owner", "size", "date", "is_in_sas")
list_display = ("name", "owner", "size", "date")
autocomplete_fields = ("parent", "owner", "moderator")
search_fields = ("name", "parent__name")
search_fields = ("name",)


@admin.register(OperationLog)
Expand Down
2 changes: 1 addition & 1 deletion core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SithFileController(ControllerBase):
)
@paginate(PageNumberPaginationExtra, page_size=50)
def search_files(self, search: Annotated[str, annotated_types.MinLen(1)]):
return SithFile.objects.filter(is_in_sas=False).filter(name__icontains=search)
return SithFile.objects.filter(name__icontains=search)


@api_controller("/group")
Expand Down
16 changes: 1 addition & 15 deletions core/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def handle(self, *args, **options):
p.save(force_lock=True)

club_root = SithFile.objects.create(name="clubs", owner=root)
sas = SithFile.objects.create(name="SAS", owner=root)
main_club = Club.objects.create(
id=1,
name=settings.SITH_MAIN_CLUB["name"],
Expand Down Expand Up @@ -805,14 +804,7 @@ def handle(self, *args, **options):
# SAS
for f in self.SAS_FIXTURE_PATH.glob("*"):
if f.is_dir():
album = Album(
parent=sas,
name=f.name,
owner=root,
is_folder=True,
is_in_sas=True,
is_moderated=True,
)
album = Album(name=f.name, owner=root, is_moderated=True)
album.clean()
album.save()
for p in f.iterdir():
Expand All @@ -822,14 +814,8 @@ def handle(self, *args, **options):
name=p.name,
file=file,
owner=root,
is_folder=False,
is_in_sas=True,
is_moderated=True,
mime_type="image/webp",
size=file.size,
)
pict.file.name = p.name
pict.clean()
pict.generate_thumbnails()

img_skia = Picture.objects.get(name="skia.jpg")
Expand Down
17 changes: 17 additions & 0 deletions core/migrations/0045_remove_sithfile_is_in_sas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.17 on 2025-01-26 11:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("core", "0044_alter_userban_options"),
]

operations = [
migrations.RemoveField(
model_name="sithfile",
name="is_in_sas",
),
]
29 changes: 0 additions & 29 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,9 +898,6 @@ class SithFile(models.Model):
on_delete=models.CASCADE,
)
asked_for_removal = models.BooleanField(_("asked for removal"), default=False)
is_in_sas = models.BooleanField(
_("is in the SAS"), default=False, db_index=True
) # Allows to query this flag, updated at each call to save()

class Meta:
verbose_name = _("file")
Expand All @@ -909,24 +906,12 @@ def __str__(self):
return self.get_parent_path() + "/" + self.name

def save(self, *args, **kwargs):
sas = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
self.is_in_sas = sas in self.get_parent_list() or self == sas
copy_rights = False
if self.id is None:
copy_rights = True
super().save(*args, **kwargs)
if copy_rights:
self.copy_rights()
if self.is_in_sas:
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 is_owned_by(self, user: User) -> bool:
if user.is_anonymous:
Expand All @@ -939,8 +924,6 @@ def is_owned_by(self, user: User) -> bool:
return user.is_board_member
if user.is_com_admin:
return True
if self.is_in_sas and user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
return True
return user.id == self.owner_id

def can_be_viewed_by(self, user: User) -> bool:
Expand Down Expand Up @@ -1106,18 +1089,6 @@ def _check_fs(self):
def is_file(self):
return not self.is_folder

@cached_property
def as_picture(self):
from sas.models import Picture

return Picture.objects.filter(id=self.id).first()

@cached_property
def as_album(self):
from sas.models import Album

return Album.objects.filter(id=self.id).first()

def get_parent_list(self):
parents = []
current = self.parent
Expand Down
2 changes: 1 addition & 1 deletion core/views/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def get_context_data(self, **kwargs):
class FileModerationView(AllowFragment, ListView):
model = SithFile
template_name = "core/file_moderation.jinja"
queryset = SithFile.objects.filter(is_moderated=False, is_in_sas=False)
queryset = SithFile.objects.filter(is_moderated=False)
paginate_by = 100

def dispatch(self, request: HttpRequest, *args, **kwargs):
Expand Down
16 changes: 3 additions & 13 deletions galaxy/management/commands/generate_galaxy_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
from datetime import timedelta
from typing import Final, Optional

from django.conf import settings
from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand
from django.utils import timezone

from club.models import Club, Membership
from core.models import Group, Page, SithFile, User
from core.models import Group, Page, User
from sas.models import Album, PeoplePictureRelation, Picture
from subscription.models import Subscription

Expand Down Expand Up @@ -98,13 +97,8 @@ def handle(self, *args, **options):
self.NB_CLUBS = options["club_count"]

root = User.objects.filter(username="root").first()
sas = SithFile.objects.get(id=settings.SITH_SAS_ROOT_DIR_ID)
self.galaxy_album = Album.objects.create(
name="galaxy-register-file",
owner=root,
is_moderated=True,
is_in_sas=True,
parent=sas,
name="galaxy-register-file", owner=root, is_moderated=True
)

self.make_clubs()
Expand Down Expand Up @@ -288,14 +282,10 @@ def make_pictures(self):
owner=u,
name=f"galaxy-picture {u} {i // self.NB_USERS}",
is_moderated=True,
is_folder=False,
parent=self.galaxy_album,
is_in_sas=True,
file=ContentFile(RED_PIXEL_PNG),
original=ContentFile(RED_PIXEL_PNG),
compressed=ContentFile(RED_PIXEL_PNG),
thumbnail=ContentFile(RED_PIXEL_PNG),
mime_type="image/png",
size=len(RED_PIXEL_PNG),
)
)
self.picts[i].file.name = self.picts[i].name
Expand Down
54 changes: 31 additions & 23 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-19 18:12+0100\n"
"POT-Creation-Date: 2025-01-26 12:47+0100\n"
"PO-Revision-Date: 2016-07-18\n"
"Last-Translator: Maréchal <[email protected]\n"
"Language-Team: AE info <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: accounting/models.py club/models.py com/models.py counter/models.py
#: forum/models.py launderette/models.py
#: forum/models.py launderette/models.py sas/models.py
msgid "name"
msgstr "nom"

Expand Down Expand Up @@ -935,10 +935,6 @@ msgstr "rôle"
msgid "description"
msgstr "description"

#: club/models.py
msgid "past member"
msgstr "ancien membre"

#: club/models.py
msgid "Email address"
msgstr "Adresse email"
Expand All @@ -948,7 +944,7 @@ msgid "Enter a valid address. Only the root of the address is needed."
msgstr ""
"Entrez une adresse valide. Seule la racine de l'adresse est nécessaire."

#: club/models.py com/models.py core/models.py
#: club/models.py com/models.py core/models.py sas/models.py
msgid "is moderated"
msgstr "est modéré"

Expand Down Expand Up @@ -2052,23 +2048,23 @@ msgstr "avoir une notification pour chaque click"
msgid "get a notification for every refilling"
msgstr "avoir une notification pour chaque rechargement"

#: core/models.py sas/forms.py
#: core/models.py sas/models.py
msgid "file name"
msgstr "nom du fichier"

#: core/models.py
#: core/models.py sas/models.py
msgid "parent"
msgstr "parent"

#: core/models.py
msgid "compressed file"
msgstr "version allégée"

#: core/models.py
#: core/models.py sas/models.py
msgid "thumbnail"
msgstr "miniature"

#: core/models.py
#: core/models.py sas/models.py
msgid "owner"
msgstr "propriétaire"

Expand All @@ -2092,14 +2088,10 @@ msgstr "type mime"
msgid "size"
msgstr "taille"

#: core/models.py
#: core/models.py sas/models.py
msgid "asked for removal"
msgstr "retrait demandé"

#: core/models.py
msgid "is in the SAS"
msgstr "est dans le SAS"

#: core/models.py
msgid "Character '/' not authorized in name"
msgstr "Le caractère '/' n'est pas autorisé dans les noms de fichier"
Expand Down Expand Up @@ -3299,8 +3291,8 @@ msgstr "Nom d'utilisateur, email, ou numéro de compte AE"

#: core/views/forms.py
msgid ""
"Profile: you need to be visible on the picture, in order to be recognized "
"(e.g. by the barmen)"
"Profile: you need to be visible on the picture, in order to be recognized (e."
"g. by the barmen)"
msgstr ""
"Photo de profil: vous devez être visible sur la photo afin d'être reconnu "
"(par exemple par les barmen)"
Expand Down Expand Up @@ -3642,7 +3634,7 @@ msgstr "élément de relevé de caisse"
msgid "banner"
msgstr "bannière"

#: counter/models.py
#: counter/models.py sas/models.py
msgid "event date"
msgstr "date de l'événement"

Expand Down Expand Up @@ -3906,8 +3898,8 @@ msgstr ""
#: counter/templates/counter/mails/account_dump.jinja
msgid "If you think this was a mistake, please mail us at [email protected]."
msgstr ""
"Si vous pensez qu'il s'agit d'une erreur, veuillez envoyer un mail à "
"ae@utbm.fr."
"Si vous pensez qu'il s'agit d'une erreur, veuillez envoyer un mail à ae@utbm."
"fr."

#: counter/templates/counter/mails/account_dump.jinja
msgid ""
Expand Down Expand Up @@ -4324,11 +4316,11 @@ msgstr "début des candidatures"
msgid "end candidature"
msgstr "fin des candidatures"

#: election/models.py
#: election/models.py sas/models.py
msgid "edit groups"
msgstr "groupe d'édition"

#: election/models.py
#: election/models.py sas/models.py
msgid "view groups"
msgstr "groupe de vue"

Expand Down Expand Up @@ -5143,6 +5135,22 @@ msgstr "Erreur de création de l'album %(album)s : %(msg)s"
msgid "You already requested moderation for this picture."
msgstr "Vous avez déjà déposé une demande de retrait pour cette photo."

#: sas/models.py
msgid "The date on which the photos in this album were taken"
msgstr "La date à laquelle les photos de cet album ont été prises"

#: sas/models.py
msgid "album"
msgstr "album"

#: sas/models.py
msgid "original image"
msgstr "image originale"

#: sas/models.py
msgid "compressed image"
msgstr "version compressée"

#: sas/models.py
msgid "picture"
msgstr "photo"
Expand Down
8 changes: 4 additions & 4 deletions sas/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

@admin.register(Picture)
class PictureAdmin(admin.ModelAdmin):
list_display = ("name", "parent", "date", "size", "is_moderated")
list_display = ("name", "parent", "is_moderated")
search_fields = ("name",)
autocomplete_fields = ("owner", "parent", "edit_groups", "view_groups", "moderator")
autocomplete_fields = ("owner", "parent", "moderator")


@admin.register(PeoplePictureRelation)
Expand All @@ -33,9 +33,9 @@ class PeoplePictureRelationAdmin(admin.ModelAdmin):

@admin.register(Album)
class AlbumAdmin(admin.ModelAdmin):
list_display = ("name", "parent", "date", "owner", "is_moderated")
list_display = ("name", "parent")
search_fields = ("name",)
autocomplete_fields = ("owner", "parent", "edit_groups", "view_groups")
autocomplete_fields = ("parent", "edit_groups", "view_groups")


@admin.register(PictureModerationRequest)
Expand Down
2 changes: 1 addition & 1 deletion sas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def fetch_pictures(self, filters: Query[PictureFilterSchema]):
return (
filters.filter(Picture.objects.viewable_by(user))
.distinct()
.order_by("-parent__date", "date")
.order_by("-parent__event_date", "created_at")
.select_related("owner")
.annotate(album=F("parent__name"))
)
Expand Down
8 changes: 1 addition & 7 deletions sas/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

from sas.models import Picture

picture_recipe = Recipe(
Picture,
is_in_sas=True,
is_folder=False,
is_moderated=True,
name=seq("Picture "),
)
picture_recipe = Recipe(Picture, is_moderated=True, name=seq("Picture "))
"""A SAS Picture fixture.
Warnings:
Expand Down
Loading

0 comments on commit 6d36f6e

Please sign in to comment.