From 83ae21140d16cf56efcd175b4839a72a38ea777e Mon Sep 17 00:00:00 2001 From: imperosol Date: Thu, 10 Oct 2024 11:43:42 +0200 Subject: [PATCH] move SAS forms to their own file --- locale/fr/LC_MESSAGES/django.po | 45 ++++++++-------- sas/forms.py | 90 ++++++++++++++++++++++++++++++++ sas/views.py | 91 ++------------------------------- 3 files changed, 116 insertions(+), 110 deletions(-) create mode 100644 sas/forms.py diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 21c0a35c..67055c96 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -997,7 +997,7 @@ msgstr "Vous ne pouvez pas ajouter deux fois le même utilisateur" msgid "You should specify a role" msgstr "Vous devez choisir un rôle" -#: club/forms.py:283 sas/views.py:117 sas/views.py:241 +#: club/forms.py:283 sas/views.py:52 sas/views.py:176 msgid "You do not have the permission to do that" msgstr "Vous n'avez pas la permission de faire cela" @@ -2246,7 +2246,7 @@ msgstr "avoir une notification pour chaque click" msgid "get a notification for every refilling" msgstr "avoir une notification pour chaque rechargement" -#: core/models.py:914 sas/views.py:309 +#: core/models.py:914 sas/forms.py:86 msgid "file name" msgstr "nom du fichier" @@ -3496,12 +3496,12 @@ msgid "Error creating folder %(folder_name)s: %(msg)s" msgstr "Erreur de création du dossier %(folder_name)s : %(msg)s" #: core/views/files.py:153 core/views/forms.py:277 core/views/forms.py:284 -#: sas/views.py:81 +#: sas/forms.py:57 #, python-format msgid "Error uploading file %(file_name)s: %(msg)s" msgstr "Erreur d'envoi du fichier %(file_name)s : %(msg)s" -#: core/views/files.py:228 sas/views.py:313 +#: core/views/files.py:228 sas/forms.py:90 msgid "Apply rights recursively" msgstr "Appliquer les droits récursivement" @@ -5267,11 +5267,29 @@ msgstr "Utilisateur qui sera supprimé" msgid "User to be selected" msgstr "Utilisateur à sélectionner" +#: sas/forms.py:13 +msgid "Add a new album" +msgstr "Ajouter un nouvel album" + +#: sas/forms.py:16 +msgid "Upload images" +msgstr "Envoyer les images" + +#: sas/forms.py:34 +#, python-format +msgid "Error creating album %(album)s: %(msg)s" +msgstr "Erreur de création de l'album %(album)s : %(msg)s" + +#: sas/forms.py:69 trombi/templates/trombi/detail.jinja:15 +msgid "Add user" +msgstr "Ajouter une personne" + #: sas/models.py:282 msgid "picture" msgstr "photo" -#: sas/templates/sas/album.jinja:9 sas/templates/sas/main.jinja:8 +#: sas/templates/sas/album.jinja:9 +#: sas/templates/sas/ask_picture_removal.jinja:4 sas/templates/sas/main.jinja:8 #: sas/templates/sas/main.jinja:17 sas/templates/sas/picture.jinja:12 msgid "SAS" msgstr "SAS" @@ -5333,23 +5351,6 @@ msgstr "Image précédente" msgid "People" msgstr "Personne(s)" -#: sas/views.py:37 -msgid "Add a new album" -msgstr "Ajouter un nouvel album" - -#: sas/views.py:40 -msgid "Upload images" -msgstr "Envoyer les images" - -#: sas/views.py:58 -#, python-format -msgid "Error creating album %(album)s: %(msg)s" -msgstr "Erreur de création de l'album %(album)s : %(msg)s" - -#: sas/views.py:93 trombi/templates/trombi/detail.jinja:15 -msgid "Add user" -msgstr "Ajouter une personne" - #: sith/settings.py:255 sith/settings.py:474 msgid "English" msgstr "Anglais" diff --git a/sas/forms.py b/sas/forms.py new file mode 100644 index 00000000..4750dab9 --- /dev/null +++ b/sas/forms.py @@ -0,0 +1,90 @@ +from ajax_select import make_ajax_field +from ajax_select.fields import AutoCompleteSelectMultipleField +from django import forms +from django.utils.translation import gettext_lazy as _ + +from core.views import MultipleImageField +from core.views.forms import SelectDate +from sas.models import Album, PeoplePictureRelation, Picture + + +class SASForm(forms.Form): + album_name = forms.CharField( + label=_("Add a new album"), max_length=Album.NAME_MAX_LENGTH, required=False + ) + images = MultipleImageField( + label=_("Upload images"), + required=False, + ) + + def process(self, parent, owner, files, *, automodere=False): + try: + if self.cleaned_data["album_name"] != "": + album = Album( + parent=parent, + name=self.cleaned_data["album_name"], + owner=owner, + is_moderated=automodere, + ) + album.clean() + album.save() + except Exception as e: + self.add_error( + None, + _("Error creating album %(album)s: %(msg)s") + % {"album": self.cleaned_data["album_name"], "msg": repr(e)}, + ) + for f in files: + new_file = Picture( + parent=parent, + name=f.name, + file=f, + owner=owner, + mime_type=f.content_type, + size=f.size, + is_folder=False, + is_moderated=automodere, + ) + if automodere: + new_file.moderator = owner + try: + new_file.clean() + new_file.generate_thumbnails() + new_file.save() + except Exception as e: + self.add_error( + None, + _("Error uploading file %(file_name)s: %(msg)s") + % {"file_name": f, "msg": repr(e)}, + ) + + +class RelationForm(forms.ModelForm): + class Meta: + model = PeoplePictureRelation + fields = ["picture"] + widgets = {"picture": forms.HiddenInput} + + users = AutoCompleteSelectMultipleField( + "users", show_help_text=False, help_text="", label=_("Add user"), required=False + ) + + +class PictureEditForm(forms.ModelForm): + class Meta: + model = Picture + fields = ["name", "parent"] + + parent = make_ajax_field(Picture, "parent", "files", help_text="") + + +class AlbumEditForm(forms.ModelForm): + class Meta: + model = Album + fields = ["name", "date", "file", "parent", "edit_groups"] + + name = forms.CharField(max_length=Album.NAME_MAX_LENGTH, label=_("file name")) + date = forms.DateField(label=_("Date"), widget=SelectDate, required=True) + parent = make_ajax_field(Album, "parent", "files", help_text="") + edit_groups = make_ajax_field(Album, "edit_groups", "groups", help_text="") + recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False) diff --git a/sas/views.py b/sas/views.py index 1d4ae783..28381c04 100644 --- a/sas/views.py +++ b/sas/views.py @@ -13,9 +13,6 @@ # # -from ajax_select import make_ajax_field -from ajax_select.fields import AutoCompleteSelectMultipleField -from django import forms from django.conf import settings from django.core.exceptions import PermissionDenied from django.http import Http404, HttpResponse @@ -27,71 +24,9 @@ from django.views.generic.edit import FormMixin, FormView, UpdateView from core.models import SithFile, User from core.views import CanEditMixin, CanViewMixin -from core.views.files import FileView, MultipleImageField, send_file -from core.views.forms import SelectDate -from sas.models import Album, PeoplePictureRelation, Picture - - -class SASForm(forms.Form): - album_name = forms.CharField( - label=_("Add a new album"), max_length=Album.NAME_MAX_LENGTH, required=False - ) - images = MultipleImageField( - label=_("Upload images"), - required=False, - ) - - def process(self, parent, owner, files, *, automodere=False): - try: - if self.cleaned_data["album_name"] != "": - album = Album( - parent=parent, - name=self.cleaned_data["album_name"], - owner=owner, - is_moderated=automodere, - ) - album.clean() - album.save() - except Exception as e: - self.add_error( - None, - _("Error creating album %(album)s: %(msg)s") - % {"album": self.cleaned_data["album_name"], "msg": repr(e)}, - ) - for f in files: - new_file = Picture( - parent=parent, - name=f.name, - file=f, - owner=owner, - mime_type=f.content_type, - size=f.size, - is_folder=False, - is_moderated=automodere, - ) - if automodere: - new_file.moderator = owner - try: - new_file.clean() - new_file.generate_thumbnails() - new_file.save() - except Exception as e: - self.add_error( - None, - _("Error uploading file %(file_name)s: %(msg)s") - % {"file_name": f, "msg": repr(e)}, - ) - - -class RelationForm(forms.ModelForm): - class Meta: - model = PeoplePictureRelation - fields = ["picture"] - widgets = {"picture": forms.HiddenInput} - - users = AutoCompleteSelectMultipleField( - "users", show_help_text=False, help_text="", label=_("Add user"), required=False - ) +from core.views.files import FileView, send_file +from sas.forms import AlbumEditForm, PictureEditForm, SASForm +from sas.models import Album, Picture class SASMainView(FormView): @@ -293,26 +228,6 @@ class ModerationView(TemplateView): return kwargs -class PictureEditForm(forms.ModelForm): - class Meta: - model = Picture - fields = ["name", "parent"] - - parent = make_ajax_field(Picture, "parent", "files", help_text="") - - -class AlbumEditForm(forms.ModelForm): - class Meta: - model = Album - fields = ["name", "date", "file", "parent", "edit_groups"] - - name = forms.CharField(max_length=Album.NAME_MAX_LENGTH, label=_("file name")) - date = forms.DateField(label=_("Date"), widget=SelectDate, required=True) - parent = make_ajax_field(Album, "parent", "files", help_text="") - edit_groups = make_ajax_field(Album, "edit_groups", "groups", help_text="") - recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False) - - class PictureEditView(CanEditMixin, UpdateView): model = Picture form_class = PictureEditForm