From 34459f83ecaa3acc8e2be5fe8be1b59c808a8dcc Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 10 Apr 2019 12:32:04 +0200 Subject: [PATCH] Group: Groups: allow bulk removing of users from a group --- core/templates/core/group_detail.jinja | 19 ++++++++---- core/views/group.py | 43 ++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/core/templates/core/group_detail.jinja b/core/templates/core/group_detail.jinja index adcc562b..5ce41fef 100644 --- a/core/templates/core/group_detail.jinja +++ b/core/templates/core/group_detail.jinja @@ -5,15 +5,22 @@ {% endblock title %} {% block content %} +

{{ object }}

{% trans %}Back to list{% endtrans %}

- {% if not object.users.exists() %} + {% if form.users_removed | length <= 0 %}

{% trans %}No user in this group{% endtrans %}

{% else %} - +
+ {% csrf_token %} + {{ form.users_removed.errors }} + {% for user in form.users_removed %} + + {% endfor %} + +
{% endif %}
{% csrf_token %} diff --git a/core/views/group.py b/core/views/group.py index 76cf6d1e..b19a9bb3 100644 --- a/core/views/group.py +++ b/core/views/group.py @@ -37,7 +37,7 @@ from django import forms from ajax_select.fields import AutoCompleteSelectMultipleField -from core.models import RealGroup +from core.models import RealGroup, User from core.views import CanEditMixin # Forms @@ -48,6 +48,16 @@ class EditMembersForm(forms.Form): Add and remove members from a Group """ + def __init__(self, *args, **kwargs): + self.current_users = kwargs.pop("users", []) + super(EditMembersForm, self).__init__(*args, **kwargs) + self.fields["users_removed"] = forms.ModelMultipleChoiceField( + User.objects.filter(id__in=self.current_users).all(), + label=None, + required=False, + widget=forms.CheckboxSelectMultiple, + ) + users_added = AutoCompleteSelectMultipleField( "users", label=_("Users to add"), @@ -55,6 +65,23 @@ class EditMembersForm(forms.Form): required=False, ) + def clean_users_added(self): + cleaned_data = super(EditMembersForm, self).clean() + users_added = cleaned_data.get("users_added", None) + if not users_added: + return users_added + + current_users = [ + str(id_) for id_ in self.current_users.values_list("id", flat=True) + ] + for user in users_added: + if user in current_users: + raise forms.ValidationError( + _("You can not add the same user twice"), code="invalid" + ) + + return users_added + # Views @@ -117,21 +144,31 @@ class GroupTemplateView(CanEditMixin, FormView): def dispatch(self, request, *args, **kwargs): self.group_id = kwargs[self.pk_url_kwarg] + self.users = self.get_object().users.all() return super(GroupTemplateView, self).dispatch(request, *args, **kwargs) def form_valid(self, form): + resp = super(GroupTemplateView, self).form_valid(form) + data = form.clean() group = self.get_object() + for user in data["users_removed"]: + group.users.remove(user) for user in data["users_added"]: group.users.add(user) group.save() - return super(GroupTemplateView, self).form_valid(form) + return resp def get_success_url(self): return reverse_lazy("core:group_detail", kwargs={"group_id": self.group_id}) - def get_context_data(self): + def get_form_kwargs(self): + kwargs = super(GroupTemplateView, self).get_form_kwargs() + kwargs["users"] = self.users + return kwargs + + def get_context_data(self, *args, **kwargs): kwargs = super(GroupTemplateView, self).get_context_data() kwargs["object"] = self.get_object() return kwargs