From e2b0668ee8659e3716958104d26e07f291688d9c Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 8 Feb 2016 17:09:52 +0100 Subject: [PATCH] Improve Club views --- club/views.py | 54 ++++++++++++++++++++++++++++++++---------- core/views/__init__.py | 3 +++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/club/views.py b/club/views.py index 739d8a80..b59e7d5c 100644 --- a/club/views.py +++ b/club/views.py @@ -11,21 +11,24 @@ from club.models import Club, Membership from subscription.views import SubscriberMixin class ClubListView(CanViewMixin, ListView): + """ + List the Clubs + """ model = Club template_name = 'club/club_list.jinja' class ClubView(CanViewMixin, DetailView): + """ + Front page of a Club + """ model = Club pk_url_kwarg = "club_id" template_name = 'club/club_detail.jinja' -class ClubEditView(CanEditMixin, UpdateView): - model = Club - pk_url_kwarg = "club_id" - fields = ['address'] - template_name = 'club/club_edit.jinja' - class ClubMemberForm(forms.ModelForm): + """ + Form handling the members of a club + """ error_css_class = 'error' required_css_class = 'required' class Meta: @@ -33,24 +36,37 @@ class ClubMemberForm(forms.ModelForm): fields = ['user', 'role'] def clean(self): + """ + Validates the permissions + e.g.: the president can add anyone anywhere, but a member can not make someone become president + """ ret = super(ClubMemberForm, self).clean() ms = self.instance.club.get_membership_for(self._user) if ms is not None and ms.role >= self.cleaned_data['role']: return ret raise ValidationError("You do not have the permission to do that") + def save(self, *args, **kwargs): + """ + Overloaded to return the club, and not to a Membership object that has no view + """ + ret = super(ClubMemberForm, self).save(*args, **kwargs) + return self.instance.club + class ClubMembersView(CanViewMixin, UpdateView): + """ + View of a club's members + """ model = Club pk_url_kwarg = "club_id" form_class = ClubMemberForm template_name = 'club/club_members.jinja' - def __init__(self, *args, **kwargs): - super(ClubMembersView, self).__init__(*args, **kwargs) - # TODO FIXME: error forbidden when adding new member to club, because self.object changes to the Membership object - # somewhere!!! - def get_form(self): + """ + Here we get a Membership object, but the view handles Club object. + That's why the save method of ClubMemberForm is overridden. + """ form = super(ClubMembersView, self).get_form() if 'user' in form.data and form.data.get('user') != '': # Load an existing membership if possible form.instance = Membership.objects.filter(club=self.object).filter(user=form.data.get('user')).filter(end_date=None).first() @@ -60,9 +76,21 @@ class ClubMembersView(CanViewMixin, UpdateView): form._user = self.request.user return form -class ClubEditPropView(CanEditPropMixin, UpdateView): +class ClubEditView(CanEditMixin, UpdateView): + """ + Edit a Club's main informations (for the club's members) + """ model = Club pk_url_kwarg = "club_id" - fields = ['name', 'address', 'parent'] + fields = ['address'] + template_name = 'club/club_edit.jinja' + +class ClubEditPropView(CanEditPropMixin, UpdateView): + """ + Edit the properties of a Club object (for the Sith admins) + """ + model = Club + pk_url_kwarg = "club_id" + fields = ['name', 'parent'] template_name = 'club/club_edit_prop.jinja' diff --git a/core/views/__init__.py b/core/views/__init__.py index ff19e750..20552eb8 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -72,6 +72,9 @@ class CanViewMixin(View): """ def dispatch(self, request, *arg, **kwargs): res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs) + import traceback + traceback.print_stack(limit=10) + print(self.__dict__) if hasattr(self, 'object'): obj = self.object elif hasattr(self, 'object_list'):