Skip to content

Commit

Permalink
Refactorise les vues avec queqlues mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Jun 13, 2021
1 parent 085183b commit 5befa37
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 60 deletions.
12 changes: 0 additions & 12 deletions zds/tutorialv2/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
76 changes: 28 additions & 48 deletions zds/tutorialv2/views/shareable_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit 5befa37

Please sign in to comment.