From fcb3035b673985ea1b95cf7e5cf5a38916cb56d4 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 29 Sep 2019 12:09:03 +0200 Subject: [PATCH 1/2] trombi: fix some 500 errors when accessing page without being in a trombi --- trombi/templates/trombi/edit_profile.jinja | 2 - trombi/views.py | 52 ++++++++++++++-------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/trombi/templates/trombi/edit_profile.jinja b/trombi/templates/trombi/edit_profile.jinja index c0d60108..819ebe51 100644 --- a/trombi/templates/trombi/edit_profile.jinja +++ b/trombi/templates/trombi/edit_profile.jinja @@ -40,5 +40,3 @@ {% endblock %} - - diff --git a/trombi/views.py b/trombi/views.py index 86bc324e..9b3b786c 100644 --- a/trombi/views.py +++ b/trombi/views.py @@ -25,7 +25,7 @@ from django.http import Http404, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect from django.core.urlresolvers import reverse_lazy, reverse -from django.views.generic import DetailView, RedirectView, TemplateView +from django.views.generic import DetailView, RedirectView, TemplateView, View from django.views.generic.edit import UpdateView, CreateView, DeleteView from django.utils.translation import ugettext_lazy as _ from django import forms @@ -59,20 +59,21 @@ class TrombiTabsMixin(TabedViewMixin): tab_list.append( {"url": reverse("trombi:user_tools"), "slug": "tools", "name": _("Tools")} ) - tab_list.append( - { - "url": reverse("trombi:profile"), - "slug": "profile", - "name": _("My profile"), - } - ) - tab_list.append( - { - "url": reverse("trombi:pictures"), - "slug": "pictures", - "name": _("My pictures"), - } - ) + if hasattr(self.request.user, "trombi_user"): + tab_list.append( + { + "url": reverse("trombi:profile"), + "slug": "profile", + "name": _("My profile"), + } + ) + tab_list.append( + { + "url": reverse("trombi:pictures"), + "slug": "pictures", + "name": _("My pictures"), + } + ) try: trombi = self.request.user.trombi_user.trombi if self.request.user.is_owner(trombi): @@ -90,6 +91,19 @@ class TrombiTabsMixin(TabedViewMixin): return tab_list +class UserIsInATrombiMixin(View): + """ + This view check if the requested user has a trombi_user attribute + """ + + def dispatch(self, request, *args, **kwargs): + + if not hasattr(self.request.user, "trombi_user"): + raise Http404() + + return super(UserIsInATrombiMixin, self).dispatch(request, *args, **kwargs) + + class TrombiForm(forms.ModelForm): class Meta: model = Trombi @@ -317,7 +331,7 @@ class UserTrombiToolsView(QuickNotifMixin, TrombiTabsMixin, TemplateView): return kwargs -class UserTrombiEditPicturesView(TrombiTabsMixin, UpdateView): +class UserTrombiEditPicturesView(TrombiTabsMixin, UserIsInATrombiMixin, UpdateView): model = TrombiUser fields = ["profile_pict", "scrub_pict"] template_name = "core/edit.jinja" @@ -330,7 +344,9 @@ class UserTrombiEditPicturesView(TrombiTabsMixin, UpdateView): return reverse("trombi:user_tools") + "?qn_success" -class UserTrombiEditProfileView(QuickNotifMixin, TrombiTabsMixin, UpdateView): +class UserTrombiEditProfileView( + QuickNotifMixin, TrombiTabsMixin, UserIsInATrombiMixin, UpdateView +): model = User form_class = modelform_factory( User, @@ -358,7 +374,7 @@ class UserTrombiEditProfileView(QuickNotifMixin, TrombiTabsMixin, UpdateView): return reverse("trombi:user_tools") + "?qn_success" -class UserTrombiResetClubMembershipsView(RedirectView): +class UserTrombiResetClubMembershipsView(UserIsInATrombiMixin, RedirectView): permanent = False def get(self, request, *args, **kwargs): From 35d9c05abfe25c6cf583f9aad37c8b4d5e11280e Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Sun, 29 Sep 2019 11:52:48 +0200 Subject: [PATCH 2/2] trombi: fix trombi tools if user has a trombi profile but no trombi linked --- trombi/templates/trombi/user_tools.jinja | 3 ++- trombi/views.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/trombi/templates/trombi/user_tools.jinja b/trombi/templates/trombi/user_tools.jinja index 8cda2bc3..19056111 100644 --- a/trombi/templates/trombi/user_tools.jinja +++ b/trombi/templates/trombi/user_tools.jinja @@ -12,7 +12,8 @@ {{ subscribe_form.as_p() }}

-{% else %} +{% endif %} +{% if trombi %}

{% trans trombi = user.trombi_user.trombi %}You are subscribed to the Trombi {{ trombi }}{% endtrans %}


{% set can_comment = trombi.subscription_deadline < date.today() and diff --git a/trombi/views.py b/trombi/views.py index 9b3b786c..fa8a78e6 100644 --- a/trombi/views.py +++ b/trombi/views.py @@ -313,9 +313,13 @@ class UserTrombiToolsView(QuickNotifMixin, TrombiTabsMixin, TemplateView): def post(self, request, *args, **kwargs): self.form = UserTrombiForm(request.POST) if self.form.is_valid(): - trombi_user = TrombiUser( - user=request.user, trombi=self.form.cleaned_data["trombi"] - ) + if hasattr(request.user, "trombi_user"): + trombi_user = request.user.trombi_user + trombi_user.trombi = self.form.cleaned_data["trombi"] + else: + trombi_user = TrombiUser( + user=request.user, trombi=self.form.cleaned_data["trombi"] + ) trombi_user.save() self.quick_notif_list += ["qn_success"] return super(UserTrombiToolsView, self).get(request, *args, **kwargs) @@ -323,7 +327,10 @@ class UserTrombiToolsView(QuickNotifMixin, TrombiTabsMixin, TemplateView): def get_context_data(self, **kwargs): kwargs = super(UserTrombiToolsView, self).get_context_data(**kwargs) kwargs["user"] = self.request.user - if not hasattr(self.request.user, "trombi_user"): + if not ( + hasattr(self.request.user, "trombi_user") + and self.request.user.trombi_user.trombi + ): kwargs["subscribe_form"] = UserTrombiForm() else: kwargs["trombi"] = self.request.user.trombi_user.trombi