diff --git a/core/templates/core/group_detail.jinja b/core/templates/core/group_detail.jinja index 58ec1f89..adcc562b 100644 --- a/core/templates/core/group_detail.jinja +++ b/core/templates/core/group_detail.jinja @@ -15,4 +15,14 @@ {% endfor %} {% endif %} +
+ {% csrf_token %} +

+ {{ form.users_added.errors }} + + {{ form.users_added }} + {{ form.users_added.help_text }} +

+ +
{% endblock content %} \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 21398841..6798a6bb 100644 --- a/core/urls.py +++ b/core/urls.py @@ -66,7 +66,7 @@ urlpatterns = [ ), url( r"^group/(?P[0-9]+)/detail$", - GroupDetailView.as_view(), + GroupTemplateView.as_view(), name="group_detail", ), # User views diff --git a/core/views/group.py b/core/views/group.py index fb1f7996..76cf6d1e 100644 --- a/core/views/group.py +++ b/core/views/group.py @@ -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"