Backport common mixin and generics improvements
* Rename get_object_list to get_source_queryset. * Add uniqueness validation to SingleObjectCreateView. * Remove MultipleInstanceActionMixin. * Backport MultipleObjectMixin improvements. * Remove ObjectListPermissionFilterMixin. * Add and improve tests. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
+163
-46
@@ -1,7 +1,7 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.contrib import messages
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.exceptions import ImproperlyConfigured, ValidationError
|
||||
from django.db import transaction
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
@@ -36,7 +36,7 @@ from .literals import (
|
||||
from .mixins import (
|
||||
DeleteExtraDataMixin, DynamicFormViewMixin, ExternalObjectMixin,
|
||||
ExtraContextMixin, FormExtraKwargsMixin, MultipleObjectMixin,
|
||||
ObjectActionMixin, ObjectListPermissionFilterMixin, ObjectNameMixin,
|
||||
ObjectActionMixin, ObjectNameMixin,
|
||||
ObjectPermissionCheckMixin, RedirectionMixin, RestrictedQuerysetMixin,
|
||||
ViewPermissionCheckMixin
|
||||
)
|
||||
@@ -138,7 +138,10 @@ class MultiFormView(DjangoFormView):
|
||||
return self.forms_invalid(forms=self.forms)
|
||||
|
||||
|
||||
class AddRemoveView(ExternalObjectMixin, ExtraContextMixin, ViewPermissionCheckMixin, RestrictedQuerysetMixin, MultiFormView):
|
||||
class AddRemoveView(
|
||||
ExternalObjectMixin, ExtraContextMixin, ViewPermissionCheckMixin,
|
||||
RestrictedQuerysetMixin, MultiFormView
|
||||
):
|
||||
form_classes = {'form_available': ChoiceForm, 'form_added': ChoiceForm}
|
||||
list_added_help_text = _(
|
||||
'Select entries to be removed. Hold Control to select multiple '
|
||||
@@ -384,15 +387,21 @@ class AddRemoveView(ExternalObjectMixin, ExtraContextMixin, ViewPermissionCheckM
|
||||
)
|
||||
|
||||
|
||||
class ConfirmView(ObjectListPermissionFilterMixin, ObjectPermissionCheckMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, TemplateView):
|
||||
class ConfirmView(
|
||||
RestrictedQuerysetMixin, ViewPermissionCheckMixin, ExtraContextMixin,
|
||||
RedirectionMixin, TemplateView
|
||||
):
|
||||
template_name = 'appearance/generic_confirm.html'
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.view_action()
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
return HttpResponseRedirect(redirect_to=self.get_success_url())
|
||||
|
||||
|
||||
class FormView(ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, FormExtraKwargsMixin, DjangoFormView):
|
||||
class FormView(
|
||||
ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin,
|
||||
FormExtraKwargsMixin, DjangoFormView
|
||||
):
|
||||
template_name = 'appearance/generic_form.html'
|
||||
|
||||
|
||||
@@ -400,7 +409,11 @@ class DynamicFormView(DynamicFormViewMixin, FormView):
|
||||
pass
|
||||
|
||||
|
||||
class MultipleObjectFormActionView(ObjectActionMixin, MultipleObjectMixin, FormExtraKwargsMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, DjangoFormView):
|
||||
class MultipleObjectFormActionView(
|
||||
ExtraContextMixin, ObjectActionMixin, ViewPermissionCheckMixin,
|
||||
RestrictedQuerysetMixin, MultipleObjectMixin, FormExtraKwargsMixin,
|
||||
RedirectionMixin, DjangoFormView
|
||||
):
|
||||
"""
|
||||
This view will present a form and upon receiving a POST request will
|
||||
perform an action on an object or queryset
|
||||
@@ -413,7 +426,7 @@ class MultipleObjectFormActionView(ObjectActionMixin, MultipleObjectMixin, FormE
|
||||
if self.__class__.mro()[0].get_queryset != MultipleObjectFormActionView.get_queryset:
|
||||
raise ImproperlyConfigured(
|
||||
'%(cls)s is overloading the get_queryset method. Subclasses '
|
||||
'should implement the get_object_list method instead. ' % {
|
||||
'should implement the get_source_queryset method instead. ' % {
|
||||
'cls': self.__class__.__name__
|
||||
}
|
||||
)
|
||||
@@ -428,23 +441,50 @@ class MultipleObjectFormActionView(ObjectActionMixin, MultipleObjectMixin, FormE
|
||||
try:
|
||||
return super(MultipleObjectFormActionView, self).get_queryset()
|
||||
except ImproperlyConfigured:
|
||||
self.queryset = self.get_object_list()
|
||||
self.queryset = self.get_source_queryset()
|
||||
return super(MultipleObjectFormActionView, self).get_queryset()
|
||||
|
||||
|
||||
class MultipleObjectConfirmActionView(ObjectActionMixin, MultipleObjectMixin, ObjectListPermissionFilterMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, TemplateView):
|
||||
class MultipleObjectConfirmActionView(
|
||||
ExtraContextMixin, ObjectActionMixin, ViewPermissionCheckMixin,
|
||||
RestrictedQuerysetMixin, MultipleObjectMixin, RedirectionMixin, TemplateView
|
||||
):
|
||||
template_name = 'appearance/generic_confirm.html'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
result = super(MultipleObjectConfirmActionView, self).__init__(*args, **kwargs)
|
||||
|
||||
if self.__class__.mro()[0].get_queryset != MultipleObjectConfirmActionView.get_queryset:
|
||||
raise ImproperlyConfigured(
|
||||
'%(cls)s is overloading the get_queryset method. Subclasses '
|
||||
'should implement the get_source_queryset method instead. ' % {
|
||||
'cls': self.__class__.__name__
|
||||
}
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def get_queryset(self):
|
||||
try:
|
||||
return super(MultipleObjectConfirmActionView, self).get_queryset()
|
||||
except ImproperlyConfigured:
|
||||
self.queryset = self.get_source_queryset()
|
||||
return super(MultipleObjectConfirmActionView, self).get_queryset()
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.view_action()
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
return HttpResponseRedirect(redirect_to=self.get_success_url())
|
||||
|
||||
|
||||
class SimpleView(ViewPermissionCheckMixin, ExtraContextMixin, TemplateView):
|
||||
pass
|
||||
|
||||
|
||||
class SingleObjectCreateView(ObjectNameMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, FormExtraKwargsMixin, CreateView):
|
||||
class SingleObjectCreateView(
|
||||
ObjectNameMixin, ViewPermissionCheckMixin, ExtraContextMixin,
|
||||
RedirectionMixin, FormExtraKwargsMixin, CreateView
|
||||
):
|
||||
error_message_duplicate = None
|
||||
template_name = 'appearance/generic_form.html'
|
||||
|
||||
def form_valid(self, form):
|
||||
@@ -461,42 +501,78 @@ class SingleObjectCreateView(ObjectNameMixin, ViewPermissionCheckMixin, ExtraCon
|
||||
else:
|
||||
save_extra_data = {}
|
||||
|
||||
try:
|
||||
self.object.validate_unique()
|
||||
except ValidationError as exception:
|
||||
context = self.get_context_data()
|
||||
|
||||
error_message = self.get_error_message_duplicate() or _(
|
||||
'Duplicate data error: %(error)s'
|
||||
) % {
|
||||
'error': '\n'.join(exception.messages)
|
||||
}
|
||||
|
||||
messages.error(
|
||||
message=error_message, request=self.request
|
||||
)
|
||||
return super(
|
||||
SingleObjectCreateView, self
|
||||
).form_invalid(form=form)
|
||||
|
||||
try:
|
||||
self.object.save(**save_extra_data)
|
||||
except Exception as exception:
|
||||
context = self.get_context_data()
|
||||
|
||||
messages.error(
|
||||
self.request,
|
||||
_('%(object)s not created, error: %(error)s') % {
|
||||
message=_('%(object)s not created, error: %(error)s') % {
|
||||
'object': self.get_object_name(context=context),
|
||||
'error': exception
|
||||
}
|
||||
}, request=self.request
|
||||
)
|
||||
return super(
|
||||
SingleObjectCreateView, self
|
||||
).form_invalid(form=form)
|
||||
else:
|
||||
context = self.get_context_data()
|
||||
|
||||
messages.success(
|
||||
self.request,
|
||||
_(
|
||||
message=_(
|
||||
'%(object)s created successfully.'
|
||||
) % {'object': self.get_object_name(context=context)}
|
||||
) % {'object': self.get_object_name(context=context)},
|
||||
request=self.request
|
||||
)
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
return HttpResponseRedirect(redirect_to=self.get_success_url())
|
||||
|
||||
def get_error_message_duplicate(self):
|
||||
return self.error_message_duplicate
|
||||
|
||||
|
||||
class SingleObjectDynamicFormCreateView(DynamicFormViewMixin, SingleObjectCreateView):
|
||||
class SingleObjectDynamicFormCreateView(
|
||||
DynamicFormViewMixin, SingleObjectCreateView
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class SingleObjectDeleteView(ObjectNameMixin, DeleteExtraDataMixin, ViewPermissionCheckMixin, ObjectPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, DeleteView):
|
||||
class SingleObjectDeleteView(
|
||||
ObjectNameMixin, DeleteExtraDataMixin, ViewPermissionCheckMixin,
|
||||
RestrictedQuerysetMixin, ExtraContextMixin, RedirectionMixin, DeleteView
|
||||
):
|
||||
template_name = 'appearance/generic_confirm.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(SingleObjectDeleteView, self).get_context_data(**kwargs)
|
||||
context.update({'delete_view': True})
|
||||
return context
|
||||
def __init__(self, *args, **kwargs):
|
||||
result = super(SingleObjectDeleteView, self).__init__(*args, **kwargs)
|
||||
|
||||
if self.__class__.mro()[0].get_queryset != SingleObjectDeleteView.get_queryset:
|
||||
raise ImproperlyConfigured(
|
||||
'%(cls)s is overloading the get_queryset method. Subclasses '
|
||||
'should implement the get_source_queryset method instead. ' % {
|
||||
'cls': self.__class__.__name__
|
||||
}
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
@@ -507,40 +583,77 @@ class SingleObjectDeleteView(ObjectNameMixin, DeleteExtraDataMixin, ViewPermissi
|
||||
result = super(SingleObjectDeleteView, self).delete(request, *args, **kwargs)
|
||||
except Exception as exception:
|
||||
messages.error(
|
||||
self.request,
|
||||
_('%(object)s not deleted, error: %(error)s.') % {
|
||||
message=_('%(object)s not deleted, error: %(error)s.') % {
|
||||
'object': object_name,
|
||||
'error': exception
|
||||
}
|
||||
}, request=self.request
|
||||
)
|
||||
|
||||
raise exception
|
||||
else:
|
||||
messages.success(
|
||||
self.request,
|
||||
_(
|
||||
message=_(
|
||||
'%(object)s deleted successfully.'
|
||||
) % {'object': object_name}
|
||||
) % {'object': object_name},
|
||||
request=self.request
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(SingleObjectDeleteView, self).get_context_data(**kwargs)
|
||||
context.update({'delete_view': True})
|
||||
return context
|
||||
|
||||
class SingleObjectDetailView(ViewPermissionCheckMixin, ObjectPermissionCheckMixin, FormExtraKwargsMixin, ExtraContextMixin, ModelFormMixin, DetailView):
|
||||
def get_queryset(self):
|
||||
try:
|
||||
return super(SingleObjectDeleteView, self).get_queryset()
|
||||
except ImproperlyConfigured:
|
||||
self.queryset = self.get_source_queryset()
|
||||
return super(SingleObjectDeleteView, self).get_queryset()
|
||||
|
||||
|
||||
class SingleObjectDetailView(
|
||||
ViewPermissionCheckMixin, RestrictedQuerysetMixin, FormExtraKwargsMixin,
|
||||
ExtraContextMixin, ModelFormMixin, DetailView
|
||||
):
|
||||
template_name = 'appearance/generic_form.html'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
result = super(SingleObjectDetailView, self).__init__(*args, **kwargs)
|
||||
|
||||
if self.__class__.mro()[0].get_queryset != SingleObjectDetailView.get_queryset:
|
||||
raise ImproperlyConfigured(
|
||||
'%(cls)s is overloading the get_queryset method. Subclasses '
|
||||
'should implement the get_source_queryset method instead. ' % {
|
||||
'cls': self.__class__.__name__
|
||||
}
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(SingleObjectDetailView, self).get_context_data(**kwargs)
|
||||
context.update({'read_only': True, 'form': self.get_form()})
|
||||
return context
|
||||
|
||||
def get_queryset(self):
|
||||
try:
|
||||
return super(SingleObjectDetailView, self).get_queryset()
|
||||
except ImproperlyConfigured:
|
||||
self.queryset = self.get_source_queryset()
|
||||
return super(SingleObjectDetailView, self).get_queryset()
|
||||
|
||||
|
||||
class SingleObjectDownloadView(ViewPermissionCheckMixin, ObjectPermissionCheckMixin, VirtualDownloadView, SingleObjectMixin):
|
||||
TextIteratorIO = TextIteratorIO
|
||||
VirtualFile = VirtualFile
|
||||
|
||||
|
||||
class SingleObjectEditView(ObjectNameMixin, ViewPermissionCheckMixin, ObjectPermissionCheckMixin, ExtraContextMixin, FormExtraKwargsMixin, RedirectionMixin, UpdateView):
|
||||
class SingleObjectEditView(
|
||||
ObjectNameMixin, ViewPermissionCheckMixin, RestrictedQuerysetMixin,
|
||||
ExtraContextMixin, FormExtraKwargsMixin, RedirectionMixin, UpdateView
|
||||
):
|
||||
template_name = 'appearance/generic_form.html'
|
||||
|
||||
def form_valid(self, form):
|
||||
@@ -564,23 +677,22 @@ class SingleObjectEditView(ObjectNameMixin, ViewPermissionCheckMixin, ObjectPerm
|
||||
self.object.save(**save_extra_data)
|
||||
except Exception as exception:
|
||||
messages.error(
|
||||
self.request,
|
||||
_('%(object)s not updated, error: %(error)s.') % {
|
||||
message=_('%(object)s not updated, error: %(error)s.') % {
|
||||
'object': object_name,
|
||||
'error': exception
|
||||
}
|
||||
}, request=self.request
|
||||
)
|
||||
|
||||
raise exception
|
||||
return super(
|
||||
SingleObjectEditView, self
|
||||
).form_invalid(form=form)
|
||||
else:
|
||||
messages.success(
|
||||
self.request,
|
||||
_(
|
||||
message=_(
|
||||
'%(object)s updated successfully.'
|
||||
) % {'object': object_name}
|
||||
) % {'object': object_name}, request=self.request
|
||||
)
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
return HttpResponseRedirect(redirect_to=self.get_success_url())
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
obj = super(SingleObjectEditView, self).get_object(queryset=queryset)
|
||||
@@ -592,11 +704,16 @@ class SingleObjectEditView(ObjectNameMixin, ViewPermissionCheckMixin, ObjectPerm
|
||||
return obj
|
||||
|
||||
|
||||
class SingleObjectDynamicFormEditView(DynamicFormViewMixin, SingleObjectEditView):
|
||||
class SingleObjectDynamicFormEditView(
|
||||
DynamicFormViewMixin, SingleObjectEditView
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class SingleObjectListView(PaginationMixin, ViewPermissionCheckMixin, ObjectListPermissionFilterMixin, ExtraContextMixin, RedirectionMixin, ListView):
|
||||
class SingleObjectListView(
|
||||
PaginationMixin, ViewPermissionCheckMixin, RestrictedQuerysetMixin,
|
||||
ExtraContextMixin, RedirectionMixin, ListView
|
||||
):
|
||||
template_name = 'appearance/generic_list.html'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -605,7 +722,7 @@ class SingleObjectListView(PaginationMixin, ViewPermissionCheckMixin, ObjectList
|
||||
if self.__class__.mro()[0].get_queryset != SingleObjectListView.get_queryset:
|
||||
raise ImproperlyConfigured(
|
||||
'%(cls)s is overloading the get_queryset method. Subclasses '
|
||||
'should implement the get_object_list method instead. ' % {
|
||||
'should implement the get_source_queryset method instead. ' % {
|
||||
'cls': self.__class__.__name__
|
||||
}
|
||||
)
|
||||
@@ -631,7 +748,7 @@ class SingleObjectListView(PaginationMixin, ViewPermissionCheckMixin, ObjectList
|
||||
try:
|
||||
queryset = super(SingleObjectListView, self).get_queryset()
|
||||
except ImproperlyConfigured:
|
||||
self.queryset = self.get_object_list()
|
||||
self.queryset = self.get_source_queryset()
|
||||
queryset = super(SingleObjectListView, self).get_queryset()
|
||||
|
||||
self.field_name = self.get_sort_field()
|
||||
|
||||
@@ -5,11 +5,14 @@ from django.utils.translation import ugettext_lazy as _
|
||||
DEFAULT_COMMON_HOME_VIEW = 'common:home'
|
||||
DELETE_STALE_UPLOADS_INTERVAL = 60 * 10 # 10 minutes
|
||||
DJANGO_SQLITE_BACKEND = 'django.db.backends.sqlite3'
|
||||
|
||||
MESSAGE_SQLITE_WARNING = _(
|
||||
'Your database backend is set to use SQLite. SQLite should only be used '
|
||||
'for development and testing, not for production.'
|
||||
)
|
||||
|
||||
PK_LIST_SEPARATOR = ','
|
||||
|
||||
TEXT_SORT_FIELD_PARAMETER = '_sort_field'
|
||||
TEXT_SORT_FIELD_VARIABLE_NAME = 'sort_field'
|
||||
TEXT_SORT_ORDER_CHOICE_ASCENDING = 'asc'
|
||||
|
||||
+126
-141
@@ -3,27 +3,23 @@ from __future__ import unicode_literals
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
|
||||
from django.db.models.query import QuerySet
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, resolve_url
|
||||
from django.utils.translation import ungettext, ugettext_lazy as _
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from mayan.apps.acls.models import AccessControlList
|
||||
from mayan.apps.permissions import Permission
|
||||
|
||||
from .exceptions import ActionError
|
||||
from .forms import DynamicForm
|
||||
|
||||
__all__ = (
|
||||
'DeleteExtraDataMixin', 'DynamicFormViewMixin', 'ExtraContextMixin',
|
||||
'FormExtraKwargsMixin', 'MultipleObjectMixin', 'ObjectActionMixin',
|
||||
'ObjectListPermissionFilterMixin', 'ObjectNameMixin',
|
||||
'ObjectPermissionCheckMixin', 'RedirectionMixin',
|
||||
'ViewPermissionCheckMixin'
|
||||
)
|
||||
from .literals import PK_LIST_SEPARATOR
|
||||
|
||||
|
||||
class DeleteExtraDataMixin(object):
|
||||
"""
|
||||
Mixin to populate the extra data needed for delete views
|
||||
"""
|
||||
def delete(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
success_url = self.get_success_url()
|
||||
@@ -44,7 +40,28 @@ class DynamicFormViewMixin(object):
|
||||
return data
|
||||
|
||||
|
||||
class ExtraContextMixin(object):
|
||||
"""
|
||||
Mixin that allows views to pass extra context to the template much easier
|
||||
than overloading .get_context_data().
|
||||
"""
|
||||
extra_context = {}
|
||||
|
||||
def get_extra_context(self):
|
||||
return self.extra_context
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(ExtraContextMixin, self).get_context_data(**kwargs)
|
||||
context.update(self.get_extra_context())
|
||||
return context
|
||||
|
||||
|
||||
class ExternalObjectMixin(object):
|
||||
"""
|
||||
Mixin to allow views to load an object with minimal code but with all
|
||||
the filtering and configurability possible. This object is often use as
|
||||
the main or master object in multi object views.
|
||||
"""
|
||||
external_object_class = None
|
||||
external_object_permission = None
|
||||
external_object_pk_url_kwarg = 'pk'
|
||||
@@ -100,21 +117,6 @@ class ExternalObjectMixin(object):
|
||||
return queryset
|
||||
|
||||
|
||||
class ExtraContextMixin(object):
|
||||
"""
|
||||
Mixin that allows views to pass extra context to the template
|
||||
"""
|
||||
extra_context = {}
|
||||
|
||||
def get_extra_context(self):
|
||||
return self.extra_context
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(ExtraContextMixin, self).get_context_data(**kwargs)
|
||||
context.update(self.get_extra_context())
|
||||
return context
|
||||
|
||||
|
||||
class FormExtraKwargsMixin(object):
|
||||
"""
|
||||
Mixin that allows a view to pass extra keyword arguments to forms
|
||||
@@ -130,62 +132,103 @@ class FormExtraKwargsMixin(object):
|
||||
return result
|
||||
|
||||
|
||||
class MultipleInstanceActionMixin(object):
|
||||
# TODO: Deprecated, replace views using this with
|
||||
# MultipleObjectFormActionView or MultipleObjectConfirmActionView
|
||||
|
||||
model = None
|
||||
success_message = _('Operation performed on %(count)d object')
|
||||
success_message_plural = _('Operation performed on %(count)d objects')
|
||||
|
||||
def get_pk_list(self):
|
||||
return self.request.GET.get(
|
||||
'id_list', self.request.POST.get('id_list', '')
|
||||
).split(',')
|
||||
|
||||
def get_queryset(self):
|
||||
return self.model.objects.filter(pk__in=self.get_pk_list())
|
||||
|
||||
def get_success_message(self, count):
|
||||
return ungettext(
|
||||
self.success_message,
|
||||
self.success_message_plural,
|
||||
count
|
||||
) % {
|
||||
'count': count,
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
count = 0
|
||||
for instance in self.get_queryset():
|
||||
try:
|
||||
self.object_action(instance=instance)
|
||||
except PermissionDenied:
|
||||
pass
|
||||
else:
|
||||
count += 1
|
||||
|
||||
messages.success(
|
||||
self.request,
|
||||
self.get_success_message(count=count)
|
||||
)
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
class MultipleObjectMixin(object):
|
||||
class MultipleObjectMixin(SingleObjectMixin):
|
||||
"""
|
||||
Mixin that allows a view to work on a single or multiple objects
|
||||
Mixin that allows a view to work on a single or multiple objects. It can
|
||||
receive a pk, a slug or a list of IDs via an id_list query.
|
||||
The pk, slug, and ID list parameter name can be changed using the
|
||||
attributes: pk_url_kwargs, slug_url_kwarg, and pk_list_key.
|
||||
"""
|
||||
model = None
|
||||
object_permission = None
|
||||
pk_list_key = 'id_list'
|
||||
pk_list_separator = ','
|
||||
pk_url_kwarg = 'pk'
|
||||
queryset = None
|
||||
slug_url_kwarg = 'slug'
|
||||
pk_list_separator = PK_LIST_SEPARATOR
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.object_list = self.get_object_list()
|
||||
if self.view_mode_single:
|
||||
self.object = self.object_list.first()
|
||||
|
||||
return super(MultipleObjectMixin, self).dispatch(request=request, *args, **kwargs)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
"""
|
||||
Override BaseDetailView.get()
|
||||
"""
|
||||
return super(SingleObjectMixin, self).get(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""
|
||||
Override SingleObjectMixin.get_context_data()
|
||||
"""
|
||||
return super(SingleObjectMixin, self).get_context_data(**kwargs)
|
||||
|
||||
def get_object(self):
|
||||
"""
|
||||
Remove this method from the subclass
|
||||
"""
|
||||
raise AttributeError
|
||||
|
||||
def get_object_list(self, queryset=None):
|
||||
"""
|
||||
Returns the list of objects the view is displaying.
|
||||
|
||||
By default this requires `self.queryset` and a `pk`, `slug` ro
|
||||
`pk_list' argument in the URLconf, but subclasses can override this
|
||||
to return any object.
|
||||
"""
|
||||
self.view_mode_single = False
|
||||
self.view_mode_multiple = False
|
||||
|
||||
# Use a custom queryset if provided; this is required for subclasses
|
||||
# like DateDetailView
|
||||
if queryset is None:
|
||||
queryset = self.get_queryset()
|
||||
|
||||
# Next, try looking up by primary key.
|
||||
pk = self.kwargs.get(self.pk_url_kwarg)
|
||||
slug = self.kwargs.get(self.slug_url_kwarg)
|
||||
pk_list = self.get_pk_list()
|
||||
|
||||
if pk is not None:
|
||||
queryset = queryset.filter(pk=pk)
|
||||
self.view_mode_single = True
|
||||
|
||||
# Next, try looking up by slug.
|
||||
if slug is not None and (pk is None or self.query_pk_and_slug):
|
||||
slug_field = self.get_slug_field()
|
||||
queryset = queryset.filter(**{slug_field: slug})
|
||||
self.view_mode_single = True
|
||||
|
||||
if pk_list is not None:
|
||||
queryset = queryset.filter(pk__in=self.get_pk_list())
|
||||
self.view_mode_multiple = True
|
||||
|
||||
# If none of those are defined, it's an error.
|
||||
if pk is None and slug is None and pk_list is None:
|
||||
raise AttributeError(
|
||||
'View %s must be called with '
|
||||
'either an object pk, a slug or an pk list.'
|
||||
% self.__class__.__name__
|
||||
)
|
||||
|
||||
try:
|
||||
# Get the single item from the filtered queryset
|
||||
queryset.get()
|
||||
except queryset.model.MultipleObjectsReturned:
|
||||
# Queryset has more than one item, this is good.
|
||||
return queryset
|
||||
except queryset.model.DoesNotExist:
|
||||
raise Http404(
|
||||
_('No %(verbose_name)s found matching the query') %
|
||||
{'verbose_name': queryset.model._meta.verbose_name}
|
||||
)
|
||||
else:
|
||||
# Queryset has one item, this is good.
|
||||
return queryset
|
||||
|
||||
def get_pk_list(self):
|
||||
# Accept pk_list even on POST request to allowing direct requests
|
||||
# to the view bypassing the initial GET request to submit the form.
|
||||
# Example: when the view is called from a test or a custom UI
|
||||
result = self.request.GET.get(
|
||||
self.pk_list_key, self.request.POST.get(self.pk_list_key)
|
||||
)
|
||||
@@ -195,43 +238,6 @@ class MultipleObjectMixin(object):
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_queryset(self):
|
||||
if self.queryset is not None:
|
||||
queryset = self.queryset
|
||||
if isinstance(queryset, QuerySet):
|
||||
queryset = queryset.all()
|
||||
elif self.model is not None:
|
||||
queryset = self.model._default_manager.all()
|
||||
|
||||
pk = self.kwargs.get(self.pk_url_kwarg)
|
||||
slug = self.kwargs.get(self.slug_url_kwarg)
|
||||
pk_list = self.get_pk_list()
|
||||
|
||||
if pk is not None:
|
||||
queryset = queryset.filter(pk=pk)
|
||||
|
||||
# Next, try looking up by slug.
|
||||
if slug is not None and (pk is None or self.query_pk_and_slug):
|
||||
slug_field = self.get_slug_field()
|
||||
queryset = queryset.filter(**{slug_field: slug})
|
||||
|
||||
if pk_list is not None:
|
||||
queryset = queryset.filter(pk__in=self.get_pk_list())
|
||||
|
||||
if pk is None and slug is None and pk_list is None:
|
||||
raise AttributeError(
|
||||
'Generic detail view %s must be called with '
|
||||
'either an object pk, a slug or an id list.'
|
||||
% self.__class__.__name__
|
||||
)
|
||||
|
||||
if self.object_permission:
|
||||
return AccessControlList.objects.filter_by_access(
|
||||
self.object_permission, self.request.user, queryset=queryset
|
||||
)
|
||||
else:
|
||||
return queryset
|
||||
|
||||
|
||||
class ObjectActionMixin(object):
|
||||
"""
|
||||
@@ -275,34 +281,6 @@ class ObjectActionMixin(object):
|
||||
)
|
||||
|
||||
|
||||
class ObjectListPermissionFilterMixin(object):
|
||||
"""
|
||||
access_object_retrieve_method is have the entire view check against
|
||||
an object permission and not the individual secondary items.
|
||||
"""
|
||||
access_object_retrieve_method = None
|
||||
object_permission = None
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if self.access_object_retrieve_method and self.object_permission:
|
||||
AccessControlList.objects.check_access(
|
||||
obj=getattr(self, self.access_object_retrieve_method)(),
|
||||
permissions=(self.object_permission,), user=request.user
|
||||
)
|
||||
return super(ObjectListPermissionFilterMixin, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super(ObjectListPermissionFilterMixin, self).get_queryset()
|
||||
|
||||
if not self.access_object_retrieve_method and self.object_permission:
|
||||
return AccessControlList.objects.filter_by_access(
|
||||
queryset=queryset, permission=self.object_permission,
|
||||
user=self.request.user
|
||||
)
|
||||
else:
|
||||
return queryset
|
||||
|
||||
|
||||
class ObjectNameMixin(object):
|
||||
def get_object_name(self, context=None):
|
||||
if not context:
|
||||
@@ -319,6 +297,7 @@ class ObjectNameMixin(object):
|
||||
return object_name
|
||||
|
||||
|
||||
# TODO: Remove this mixin and replace with restricted queryset
|
||||
class ObjectPermissionCheckMixin(object):
|
||||
object_permission = None
|
||||
|
||||
@@ -425,6 +404,12 @@ class RestrictedQuerysetMixin(object):
|
||||
|
||||
|
||||
class ViewPermissionCheckMixin(object):
|
||||
"""
|
||||
Restrict access to the view based on the user's direct permissions from
|
||||
roles. This mixing is used for views whose objects don't support ACLs or
|
||||
for views that perform actions that are not related to a specify object or
|
||||
object's permission like maintenance views.
|
||||
"""
|
||||
view_permission = None
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
|
||||
@@ -174,7 +174,7 @@ class ObjectErrorLogEntryListView(SingleObjectListView):
|
||||
klass=content_type.model_class(), pk=self.kwargs['object_id']
|
||||
)
|
||||
|
||||
def get_object_list(self):
|
||||
def get_source_queryset(self):
|
||||
return self.get_object().error_logs.all()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user