Groups: allow bulk import of users on a group

This commit is contained in:
Antoine Bartuccio 2019-04-10 01:22:00 +02:00
parent f6553a8f52
commit 5cc0760e2c
Signed by: klmp200
GPG Key ID: E7245548C53F904B
3 changed files with 96 additions and 4 deletions

View File

@ -15,4 +15,14 @@
{% endfor %}
</ul>
{% endif %}
<form action="{{ url('core:group_detail', object.id) }}" method="post" id="add_users">
{% csrf_token %}
<p>
{{ form.users_added.errors }}
<label for="{{ form.users_added.id_for_label }}">{{ form.users_added.label }} :</label>
{{ form.users_added }}
<span class="helptext">{{ form.users_added.help_text }}</span>
</p>
<input type="submit" name="submit">
</form>
{% endblock content %}

View File

@ -66,7 +66,7 @@ urlpatterns = [
),
url(
r"^group/(?P<group_id>[0-9]+)/detail$",
GroupDetailView.as_view(),
GroupTemplateView.as_view(),
name="group_detail",
),
# User views

View File

@ -22,17 +22,46 @@
#
#
"""
This module contains views to manage Groups
"""
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django.views.generic import ListView, DetailView
from django.views.generic import ListView
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import get_object_or_404
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _
from django import forms
from ajax_select.fields import AutoCompleteSelectMultipleField
from core.models import RealGroup
from core.views import CanEditMixin
# Forms
class EditMembersForm(forms.Form):
"""
Add and remove members from a Group
"""
users_added = AutoCompleteSelectMultipleField(
"users",
label=_("Users to add"),
help_text=_("Search users to add (one or more)."),
required=False,
)
# Views
class GroupListView(CanEditMixin, ListView):
"""
Displays the group list
Displays the Group list
"""
model = RealGroup
@ -41,6 +70,10 @@ class GroupListView(CanEditMixin, ListView):
class GroupEditView(CanEditMixin, UpdateView):
"""
Edit infos of a Group
"""
model = RealGroup
pk_url_kwarg = "group_id"
template_name = "core/group_edit.jinja"
@ -48,18 +81,67 @@ class GroupEditView(CanEditMixin, UpdateView):
class GroupCreateView(CanEditMixin, CreateView):
"""
Add a new Group
"""
model = RealGroup
template_name = "core/group_edit.jinja"
fields = ["name", "description"]
class GroupDetailView(CanEditMixin, DetailView):
class GroupTemplateView(CanEditMixin, FormView):
"""
Display all users in a given Group
Allow adding and removing users from it
"""
model = RealGroup
form_class = EditMembersForm
pk_url_kwarg = "group_id"
template_name = "core/group_detail.jinja"
def get_object(self):
"""
Get current group from id in url
"""
return self.cached_object
@cached_property
def cached_object(self):
"""
Optimisation on group retrieval
"""
return get_object_or_404(self.model, pk=self.group_id)
def dispatch(self, request, *args, **kwargs):
self.group_id = kwargs[self.pk_url_kwarg]
return super(GroupTemplateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
data = form.clean()
group = self.get_object()
for user in data["users_added"]:
group.users.add(user)
group.save()
return super(GroupTemplateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("core:group_detail", kwargs={"group_id": self.group_id})
def get_context_data(self):
kwargs = super(GroupTemplateView, self).get_context_data()
kwargs["object"] = self.get_object()
return kwargs
class GroupDeleteView(CanEditMixin, DeleteView):
"""
Delete a Group
"""
model = RealGroup
pk_url_kwarg = "group_id"
template_name = "core/delete_confirm.jinja"