Group: Groups: allow bulk removing of users from a group

This commit is contained in:
Antoine Bartuccio 2019-04-10 12:32:04 +02:00
parent 5cc0760e2c
commit 34459f83ec
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 53 additions and 9 deletions

View File

@ -5,15 +5,22 @@
{% endblock title %} {% endblock title %}
{% block content %} {% block content %}
<h1>{{ object }}</h1>
<p><a href="{{ url('core:group_list') }}">{% trans %}Back to list{% endtrans %}</a></p> <p><a href="{{ url('core:group_list') }}">{% trans %}Back to list{% endtrans %}</a></p>
{% if not object.users.exists() %} {% if form.users_removed | length <= 0 %}
<p>{% trans %}No user in this group{% endtrans %}</p> <p>{% trans %}No user in this group{% endtrans %}</p>
{% else %} {% else %}
<ul> <form action="{{ url('core:group_detail', object.id) }}" method="post" id="add_users">
{% for user in object.users.all() %} {% csrf_token %}
<li><a href="{{ url('core:user_profile', user.id) }}">{{ user }}</a></li> {{ form.users_removed.errors }}
{% for user in form.users_removed %}
<label for="{{ user.id_for_label }}">
<a href="{{ url('core:user_profile', user.data.value) }}">{{ user.choice_label }}</a>
{{ user.tag() }}
</label>
{% endfor %} {% endfor %}
</ul> <input type="submit" name="submit">
</form>
{% endif %} {% endif %}
<form action="{{ url('core:group_detail', object.id) }}" method="post" id="add_users"> <form action="{{ url('core:group_detail', object.id) }}" method="post" id="add_users">
{% csrf_token %} {% csrf_token %}

View File

@ -37,7 +37,7 @@ from django import forms
from ajax_select.fields import AutoCompleteSelectMultipleField from ajax_select.fields import AutoCompleteSelectMultipleField
from core.models import RealGroup from core.models import RealGroup, User
from core.views import CanEditMixin from core.views import CanEditMixin
# Forms # Forms
@ -48,6 +48,16 @@ class EditMembersForm(forms.Form):
Add and remove members from a Group 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_added = AutoCompleteSelectMultipleField(
"users", "users",
label=_("Users to add"), label=_("Users to add"),
@ -55,6 +65,23 @@ class EditMembersForm(forms.Form):
required=False, 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 # Views
@ -117,21 +144,31 @@ class GroupTemplateView(CanEditMixin, FormView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.group_id = kwargs[self.pk_url_kwarg] self.group_id = kwargs[self.pk_url_kwarg]
self.users = self.get_object().users.all()
return super(GroupTemplateView, self).dispatch(request, *args, **kwargs) return super(GroupTemplateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form): def form_valid(self, form):
resp = super(GroupTemplateView, self).form_valid(form)
data = form.clean() data = form.clean()
group = self.get_object() group = self.get_object()
for user in data["users_removed"]:
group.users.remove(user)
for user in data["users_added"]: for user in data["users_added"]:
group.users.add(user) group.users.add(user)
group.save() group.save()
return super(GroupTemplateView, self).form_valid(form) return resp
def get_success_url(self): def get_success_url(self):
return reverse_lazy("core:group_detail", kwargs={"group_id": self.group_id}) 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 = super(GroupTemplateView, self).get_context_data()
kwargs["object"] = self.get_object() kwargs["object"] = self.get_object()
return kwargs return kwargs