From 69646925563f9d69e2cf87373daffcfe2cf2675a Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 24 Apr 2019 18:17:03 +0200 Subject: [PATCH] clubs: basic bulk mark as old --- club/models.py | 7 +++++-- club/views.py | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/club/models.py b/club/models.py index 250d584f..d446c080 100644 --- a/club/models.py +++ b/club/models.py @@ -292,12 +292,15 @@ class Membership(models.Model): """ return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) - def can_be_edited_by(self, user): + def can_be_edited_by(self, user, membership=None): """ Method to see if that object can be edited by the given user """ if user.memberships: - ms = user.memberships.filter(club=self.club, end_date=None).first() + if membership: # This is for optimisation purpose + ms = membership + else: + ms = user.memberships.filter(club=self.club, end_date=None).first() return (ms and ms.role >= self.role) or user.is_in_group( settings.SITH_MAIN_BOARD_GROUP ) diff --git a/club/views.py b/club/views.py index 64b5e301..82bd1ed2 100644 --- a/club/views.py +++ b/club/views.py @@ -318,12 +318,18 @@ class ClubMemberForm(forms.Form): "users", label=_("Users to add"), help_text=_("Search users to add (one or more)."), - required=True, + required=False, ) def __init__(self, *args, **kwargs): self.club = kwargs.pop("club") self.request_user = kwargs.pop("request_user") + self.club_members = kwargs.pop("club_members", None) + if not self.club_members: + self.club_members = ( + self.club.members.filter(end_date=None).order_by("-role").all() + ) + self.request_user_membership = self.club.get_membership_for(self.request_user) super(ClubMemberForm, self).__init__(*args, **kwargs) # Using a ModelForm binds too much the form with the model and we don't want that @@ -336,6 +342,20 @@ class ClubMemberForm(forms.Form): widgets={"start_date": SelectDate}, ) ) + self.fields["users_old"] = forms.ModelMultipleChoiceField( + User.objects.filter( + id__in=[ + ms.user.id + for ms in self.club_members + if ms.can_be_edited_by( + self.request_user, self.request_user_membership + ) + ] + ).all(), + label=_("Mark as old"), + required=False, + widget=forms.CheckboxSelectMultiple, + ) if not self.request_user.is_root: self.fields.pop("start_date") @@ -369,7 +389,7 @@ class ClubMemberForm(forms.Form): """ cleaned_data = super(ClubMemberForm, self).clean() request_user = self.request_user - membership = self.club.get_membership_for(request_user) + membership = self.request_user_membership if not ( cleaned_data["role"] <= SITH_MAXIMUM_FREE_ROLE or (membership is not None and membership.role >= cleaned_data["role"]) @@ -395,13 +415,12 @@ class ClubMembersView(ClubTabsMixin, CanViewMixin, DetailFormView): kwargs = super(ClubMembersView, self).get_form_kwargs() kwargs["request_user"] = self.request_user kwargs["club"] = self.get_object() + kwargs["club_members"] = self.members return kwargs def get_context_data(self, *args, **kwargs): kwargs = super(ClubMembersView, self).get_context_data(*args, **kwargs) - kwargs["members"] = ( - self.get_object().members.filter(end_date=None).order_by("-role").all() - ) + kwargs["members"] = self.members return kwargs def form_valid(self, form): @@ -412,12 +431,20 @@ class ClubMembersView(ClubTabsMixin, CanViewMixin, DetailFormView): data = form.clean() users = data.pop("users", []) + users_old = data.pop("users_old", []) for user in users: Membership(club=self.get_object(), user=user, **data).save() + for user in users_old: + membership = self.get_object().get_membership_for(user) + membership.end_date = timezone.now() + membership.save() return resp def dispatch(self, request, *args, **kwargs): self.request_user = request.user + self.members = ( + self.get_object().members.filter(end_date=None).order_by("-role").all() + ) return super(ClubMembersView, self).dispatch(request, *args, **kwargs) def get_success_url(self, **kwargs):