From f5a99525655162ff255c694ecc67212370faebe3 Mon Sep 17 00:00:00 2001 From: Arnaud-D <35631001+Arnaud-D@users.noreply.github.com> Date: Mon, 14 Jun 2021 00:01:38 +0200 Subject: [PATCH] Refactorise les vues avec quelques mixins --- zds/tutorialv2/models/database.py | 12 ---- zds/tutorialv2/views/shareable_links.py | 76 +++++++++---------------- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/zds/tutorialv2/models/database.py b/zds/tutorialv2/models/database.py index c79a141a11..47c04720e5 100644 --- a/zds/tutorialv2/models/database.py +++ b/zds/tutorialv2/models/database.py @@ -1556,18 +1556,6 @@ def full_url(self): def expired(self): return self.expiration < datetime.now() - def can_edit(self, user): - return user in self.content.authors.all() - - def can_deactivate(self, user): - return user in self.content.authors.all() - - def can_reactivate(self, user): - return user in self.content.authors.all() - - def can_delete(self, user): - return user in self.content.authors.all() - def deactivate(self): self.active = False self.save() diff --git a/zds/tutorialv2/views/shareable_links.py b/zds/tutorialv2/views/shareable_links.py index 374e4a9cf6..6c57d1d0c6 100644 --- a/zds/tutorialv2/views/shareable_links.py +++ b/zds/tutorialv2/views/shareable_links.py @@ -52,19 +52,35 @@ class ShareableLinkForm(forms.Form): type = forms.ChoiceField(choices=SHAREABLE_LINK_TYPES) -class CreateShareableLink(ModalFormView): +class BaseMixin: http_method_names = ["post"] - form_class = ShareableLinkForm - modal_form = True @method_decorator(login_required) def dispatch(self, *args, **kwargs): - self.content = get_object_or_404(PublishableContent, pk=self.kwargs["pk"]) if self.request.user not in self.content.authors.all(): raise PermissionDenied self.redirect_url = reverse("content:list-shareable-links", kwargs={"pk": self.content.pk}) return super().dispatch(*args, *kwargs) + +class ContentMixin(BaseMixin): + def dispatch(self, *args, **kwargs): + self.content = get_object_or_404(PublishableContent, pk=self.kwargs["pk"]) + return super().dispatch(*args, *kwargs) + + +class LinkMixin(BaseMixin): + def dispatch(self, *args, **kwargs): + self.link = get_object_or_404(ShareableLink, id=self.kwargs["id"]) + self.content = self.link.content + return super().dispatch(*args, *kwargs) + + +class CreateShareableLink(ContentMixin, ModalFormView): + http_method_names = ["post"] + form_class = ShareableLinkForm + modal_form = True + def form_invalid(self, form): form.previous_page_url = self.redirect_url return super().form_invalid(form) @@ -81,19 +97,10 @@ def form_valid(self, form): return super().form_valid(form) -class EditShareableLink(ModalFormView): - http_method_names = ["post"] +class EditShareableLink(LinkMixin, ModalFormView): form_class = ShareableLinkForm modal_form = True - @method_decorator(login_required) - def dispatch(self, *args, **kwargs): - self.link = get_object_or_404(ShareableLink, id=self.kwargs["id"]) - if not self.link.can_edit(self.request.user): - raise PermissionDenied - self.redirect_url = reverse("content:list-shareable-links", kwargs={"pk": self.link.content.pk}) - return super().dispatch(*args, *kwargs) - def form_invalid(self, form): form.previous_page_url = self.redirect_url return super().form_invalid(form) @@ -108,49 +115,22 @@ def form_valid(self, form): return super().form_valid(form) -class DeactivateShareableLink(View): - http_method_names = ["post"] - - @method_decorator(login_required) - def dispatch(self, *args, **kwargs): - self.link = get_object_or_404(ShareableLink, id=self.kwargs["id"]) - if not self.link.can_deactivate(self.request.user): - raise PermissionDenied - return super().dispatch(*args, *kwargs) - +class DeactivateShareableLink(LinkMixin, View): def post(self, *args, **kwargs): self.link.deactivate() messages.success(self.request, "Le lien a été désactivé.") - return redirect("content:list-shareable-links", pk=self.link.content.pk) - + return redirect(self.redirect_url) -class ReactivateShareableLink(View): - http_method_names = ["post"] - - @method_decorator(login_required) - def dispatch(self, *args, **kwargs): - self.link = get_object_or_404(ShareableLink, id=self.kwargs["id"]) - if not self.link.can_reactivate(self.request.user): - raise PermissionDenied - return super().dispatch(*args, *kwargs) +class ReactivateShareableLink(LinkMixin, View): def post(self, *args, **kwargs): self.link.reactivate() messages.success(self.request, "Le lien a été réactivé.") - return redirect("content:list-shareable-links", pk=self.link.content.pk) - - -class DeleteShareableLink(View): - http_method_names = ["post"] + return redirect(self.redirect_url) - @method_decorator(login_required) - def dispatch(self, *args, **kwargs): - self.link = get_object_or_404(ShareableLink, id=self.kwargs["id"]) - if not self.link.can_delete(self.request.user): - raise PermissionDenied - return super().dispatch(*args, *kwargs) - def post(self, *arg, **kwargs): +class DeleteShareableLink(LinkMixin, View): + def post(self, *args, **kwargs): self.link.delete() messages.success(self.request, "Le lien a été supprimé.") - return redirect("content:list-shareable-links", pk=self.link.content.pk) + return redirect(self.redirect_url)