Verified Commit 08d03087 authored by Sli's avatar Sli

core: create a DetailFormView

parent 34459f83
......@@ -19,7 +19,7 @@
{{ user.tag() }}
</label>
{% endfor %}
<input type="submit" name="submit">
<input type="submit" name="submit" value="{% trans %}Delete{% endtrans %}">
</form>
{% endif %}
<form action="{{ url('core:group_detail', object.id) }}" method="post" id="add_users">
......
......@@ -39,6 +39,9 @@ from django.core.exceptions import (
ImproperlyConfigured,
)
from django.views.generic.base import View
from django.views.generic.edit import FormView
from django.views.generic.detail import SingleObjectMixin
from django.utils.functional import cached_property
from django.db.models import Count
from core.models import Group
......@@ -275,6 +278,30 @@ class QuickNotifMixin:
return kwargs
class DetailFormView(SingleObjectMixin, FormView):
"""
Class that allow both a detail view and a form view
"""
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 super(DetailFormView, self).get_object()
def get_context_data(self, *args, **kwargs):
kwargs = super(DetailFormView, self).get_context_data()
kwargs["object"] = self.get_object()
return kwargs
from .user import *
from .page import *
from .files import *
......
......@@ -31,14 +31,13 @@ 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, User
from core.views import CanEditMixin
from core.views import CanEditMixin, DetailFormView
# Forms
......@@ -117,7 +116,7 @@ class GroupCreateView(CanEditMixin, CreateView):
fields = ["name", "description"]
class GroupTemplateView(CanEditMixin, FormView):
class GroupTemplateView(CanEditMixin, DetailFormView):
"""
Display all users in a given Group
Allow adding and removing users from it
......@@ -128,24 +127,11 @@ class GroupTemplateView(CanEditMixin, FormView):
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]
self.users = self.get_object().users.all()
return super(GroupTemplateView, self).dispatch(request, *args, **kwargs)
resp = super(GroupTemplateView, self).dispatch(request, *args, **kwargs)
return resp
def form_valid(self, form):
resp = super(GroupTemplateView, self).form_valid(form)
......@@ -161,18 +147,15 @@ class GroupTemplateView(CanEditMixin, FormView):
return resp
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.get_object().id}
)
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
class GroupDeleteView(CanEditMixin, DeleteView):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment