clubs: basic bulk mark as old

This commit is contained in:
Antoine Bartuccio 2019-04-24 18:17:03 +02:00
parent 80f1f9699c
commit 6964692556
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 37 additions and 7 deletions

View File

@ -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
)

View File

@ -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):