core: create a DetailFormView

This commit is contained in:
Antoine Bartuccio 2019-04-10 13:21:34 +02:00
parent 34459f83ec
commit 08d03087a4
Signed by: klmp200
GPG Key ID: E7245548C53F904B
3 changed files with 35 additions and 25 deletions

View File

@ -19,7 +19,7 @@
{{ user.tag() }} {{ user.tag() }}
</label> </label>
{% endfor %} {% endfor %}
<input type="submit" name="submit"> <input type="submit" name="submit" value="{% trans %}Delete{% endtrans %}">
</form> </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">

View File

@ -39,6 +39,9 @@ from django.core.exceptions import (
ImproperlyConfigured, ImproperlyConfigured,
) )
from django.views.generic.base import View 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 django.db.models import Count
from core.models import Group from core.models import Group
@ -275,6 +278,30 @@ class QuickNotifMixin:
return kwargs 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 .user import *
from .page import * from .page import *
from .files import * from .files import *

View File

@ -31,14 +31,13 @@ from django.views.generic import ListView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.shortcuts import get_object_or_404 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.utils.translation import ugettext_lazy as _
from django import forms from django import forms
from ajax_select.fields import AutoCompleteSelectMultipleField from ajax_select.fields import AutoCompleteSelectMultipleField
from core.models import RealGroup, User from core.models import RealGroup, User
from core.views import CanEditMixin from core.views import CanEditMixin, DetailFormView
# Forms # Forms
@ -117,7 +116,7 @@ class GroupCreateView(CanEditMixin, CreateView):
fields = ["name", "description"] fields = ["name", "description"]
class GroupTemplateView(CanEditMixin, FormView): class GroupTemplateView(CanEditMixin, DetailFormView):
""" """
Display all users in a given Group Display all users in a given Group
Allow adding and removing users from it Allow adding and removing users from it
@ -128,24 +127,11 @@ class GroupTemplateView(CanEditMixin, FormView):
pk_url_kwarg = "group_id" pk_url_kwarg = "group_id"
template_name = "core/group_detail.jinja" 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): def dispatch(self, request, *args, **kwargs):
self.group_id = kwargs[self.pk_url_kwarg]
self.users = self.get_object().users.all() 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): def form_valid(self, form):
resp = super(GroupTemplateView, self).form_valid(form) resp = super(GroupTemplateView, self).form_valid(form)
@ -161,18 +147,15 @@ class GroupTemplateView(CanEditMixin, FormView):
return resp 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.get_object().id}
)
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super(GroupTemplateView, self).get_form_kwargs() kwargs = super(GroupTemplateView, self).get_form_kwargs()
kwargs["users"] = self.users kwargs["users"] = self.users
return kwargs 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): class GroupDeleteView(CanEditMixin, DeleteView):
""" """