Remove PreserveGetQuerySet mixin. Update SingleObjectList and

MultipleObjectFormActionView views to use a new get_object_list method.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-08-16 22:11:22 -04:00
parent c43f5eb66e
commit 2052caada4
39 changed files with 540 additions and 508 deletions

View File

@@ -26,8 +26,8 @@ class AccessControlListManager(models.Manager):
def check_access(self, permissions, user, obj, related=None): def check_access(self, permissions, user, obj, related=None):
if user.is_superuser or user.is_staff: if user.is_superuser or user.is_staff:
logger.debug( logger.debug(
'Permissions "%s" on "%s" granted to user "%s" as superuser or staff', 'Permissions "%s" on "%s" granted to user "%s" as superuser '
permissions, obj, user 'or staff', permissions, obj, user
) )
return True return True
@@ -53,14 +53,15 @@ class AccessControlListManager(models.Manager):
) )
except AttributeError: except AttributeError:
# AttributeError means non model objects: ie Statistics # AttributeError means non model objects: ie Statistics
# These can't have ACLS so we raise PermissionDenied # These can't have ACLs so we raise PermissionDenied
raise PermissionDenied raise PermissionDenied
except KeyError: except KeyError:
pass pass
else: else:
try: try:
return self.check_access( return self.check_access(
permissions, user, getattr(obj, parent_accessor) obj=getattr(obj, parent_accessor),
permissions=permissions, user=user
) )
except PermissionDenied: except PermissionDenied:
pass pass

View File

@@ -139,7 +139,7 @@ class ACLListView(SingleObjectListView):
'title': _('Access control lists for: %s' % self.content_object), 'title': _('Access control lists for: %s' % self.content_object),
} }
def get_queryset(self): def get_object_list(self):
return AccessControlList.objects.filter( return AccessControlList.objects.filter(
content_type=self.object_content_type, content_type=self.object_content_type,
object_id=self.content_object.pk object_id=self.content_object.pk

View File

@@ -17,6 +17,9 @@ class CabinetViewTestCase(GenericDocumentViewTestCase):
super(CabinetViewTestCase, self).setUp() super(CabinetViewTestCase, self).setUp()
self.login_user() self.login_user()
def _create_cabinet(self):
self.cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL)
def _request_create_cabinet(self, label): def _request_create_cabinet(self, label):
return self.post( return self.post(
'cabinets:cabinet_create', data={ 'cabinets:cabinet_create', data={
@@ -40,174 +43,179 @@ class CabinetViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(Cabinet.objects.first().label, TEST_CABINET_LABEL) self.assertEqual(Cabinet.objects.first().label, TEST_CABINET_LABEL)
def test_cabinet_create_duplicate_view_with_permission(self): def test_cabinet_create_duplicate_view_with_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_create) self.grant_permission(permission=permission_cabinet_create)
response = self._request_create_cabinet(label=TEST_CABINET_LABEL) response = self._request_create_cabinet(label=TEST_CABINET_LABEL)
# HTTP 200 with error message # HTTP 200 with error message
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(Cabinet.objects.count(), 1) self.assertEqual(Cabinet.objects.count(), 1)
self.assertEqual(Cabinet.objects.first().pk, cabinet.pk) self.assertEqual(Cabinet.objects.first().pk, self.cabinet.pk)
def _delete_cabinet(self, cabinet): def _request_delete_cabinet(self):
return self.post('cabinets:cabinet_delete', args=(cabinet.pk,)) return self.post('cabinets:cabinet_delete', args=(self.cabinet.pk,))
def test_cabinet_delete_view_no_permission(self): def test_cabinet_delete_view_no_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
response = self._delete_cabinet(cabinet=cabinet) response = self._request_delete_cabinet()
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertEqual(Cabinet.objects.count(), 1) self.assertEqual(Cabinet.objects.count(), 1)
def test_cabinet_delete_view_with_permission(self): def test_cabinet_delete_view_with_access(self):
self.grant_permission(permission=permission_cabinet_delete) self._create_cabinet()
self.grant_access(obj=self.cabinet, permission=permission_cabinet_delete)
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) response = self._request_delete_cabinet()
response = self._delete_cabinet(cabinet=cabinet)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(Cabinet.objects.count(), 0) self.assertEqual(Cabinet.objects.count(), 0)
def _edit_cabinet(self, cabinet, label): def _request_edit_cabinet(self):
return self.post( return self.post(
'cabinets:cabinet_edit', args=(cabinet.pk,), data={ 'cabinets:cabinet_edit', args=(self.cabinet.pk,), data={
'label': label 'label': TEST_CABINET_EDITED_LABEL
} }
) )
def test_cabinet_edit_view_no_permission(self): def test_cabinet_edit_view_no_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
response = self._edit_cabinet( response = self._request_edit_cabinet()
cabinet=cabinet, label=TEST_CABINET_EDITED_LABEL
)
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.label, TEST_CABINET_LABEL) self.assertEqual(self.cabinet.label, TEST_CABINET_LABEL)
def test_cabinet_edit_view_with_permission(self): def test_cabinet_edit_view_with_access(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_edit) self.grant_access(obj=self.cabinet, permission=permission_cabinet_edit)
response = self._edit_cabinet( response = self._request_edit_cabinet()
cabinet=cabinet, label=TEST_CABINET_EDITED_LABEL
)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.label, TEST_CABINET_EDITED_LABEL) self.assertEqual(self.cabinet.label, TEST_CABINET_EDITED_LABEL)
def _add_document_to_cabinet(self, cabinet): def _add_document_to_cabinet(self):
return self.post( return self.post(
'cabinets:cabinet_add_document', args=(self.document.pk,), data={ 'cabinets:cabinet_add_document', args=(self.document.pk,), data={
'cabinets': cabinet.pk 'cabinets': self.cabinet.pk
} }
) )
def test_cabinet_add_document_view_no_permission(self): def test_cabinet_add_document_view_no_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_view) self.grant_permission(permission=permission_cabinet_view)
response = self._add_document_to_cabinet(cabinet=cabinet) response = self._add_document_to_cabinet()
self.assertContains( self.assertContains(
response, text='Select a valid choice.', status_code=200 response, text='Select a valid choice.', status_code=200
) )
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.documents.count(), 0) self.assertEqual(self.cabinet.documents.count(), 0)
def test_cabinet_add_document_view_with_permission(self): def test_cabinet_add_document_view_with_access(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_view) self.grant_access(obj=self.cabinet, permission=permission_cabinet_view)
self.grant_permission(permission=permission_cabinet_add_document) self.grant_access(
self.grant_permission(permission=permission_document_view) obj=self.cabinet, permission=permission_cabinet_add_document
)
response = self._add_document_to_cabinet(cabinet=cabinet) self.grant_access(
obj=self.document, permission=permission_cabinet_add_document
cabinet.refresh_from_db()
self.assertEqual(response.status_code, 302)
self.assertEqual(cabinet.documents.count(), 1)
self.assertQuerysetEqual(
cabinet.documents.all(), (repr(self.document),)
) )
def _add_multiple_documents_to_cabinet(self, cabinet): response = self._add_document_to_cabinet()
self.cabinet.refresh_from_db()
self.assertEqual(response.status_code, 302)
self.assertEqual(self.cabinet.documents.count(), 1)
self.assertQuerysetEqual(
self.cabinet.documents.all(), (repr(self.document),)
)
def _request_add_multiple_documents_to_cabinet(self):
return self.post( return self.post(
'cabinets:cabinet_add_multiple_documents', data={ 'cabinets:cabinet_add_multiple_documents', data={
'id_list': (self.document.pk,), 'cabinets': cabinet.pk 'id_list': (self.document.pk,), 'cabinets': self.cabinet.pk
} }
) )
def test_cabinet_add_multiple_documents_view_no_permission(self): def test_cabinet_add_multiple_documents_view_no_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_view) self.grant_permission(permission=permission_cabinet_view)
response = self._add_multiple_documents_to_cabinet(cabinet=cabinet) response = self._request_add_multiple_documents_to_cabinet()
self.assertContains( self.assertContains(
response, text='Select a valid choice', status_code=200 response, text='Select a valid choice', status_code=200
) )
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.documents.count(), 0) self.assertEqual(self.cabinet.documents.count(), 0)
def test_cabinet_add_multiple_documents_view_with_permission(self): def test_cabinet_add_multiple_documents_view_with_access(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
self.grant_permission(permission=permission_cabinet_view) self.grant_access(
self.grant_permission(permission=permission_cabinet_add_document) obj=self.cabinet, permission=permission_cabinet_add_document
)
response = self._add_multiple_documents_to_cabinet(cabinet=cabinet) self.grant_access(
obj=self.document, permission=permission_cabinet_add_document
self.assertEqual(response.status_code, 302)
cabinet.refresh_from_db()
self.assertEqual(cabinet.documents.count(), 1)
self.assertQuerysetEqual(
cabinet.documents.all(), (repr(self.document),)
) )
def _remove_document_from_cabinet(self, cabinet): response = self._request_add_multiple_documents_to_cabinet()
self.assertEqual(response.status_code, 302)
self.cabinet.refresh_from_db()
self.assertEqual(self.cabinet.documents.count(), 1)
self.assertQuerysetEqual(
self.cabinet.documents.all(), (repr(self.document),)
)
def _request_remove_document_from_cabinet(self):
return self.post( return self.post(
'cabinets:document_cabinet_remove', args=(self.document.pk,), 'cabinets:document_cabinet_remove', args=(self.document.pk,),
data={ data={
'cabinets': (cabinet.pk,), 'cabinets': (self.cabinet.pk,),
} }
) )
def test_cabinet_remove_document_view_no_permission(self): def test_cabinet_remove_document_view_no_permission(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
cabinet.documents.add(self.document) self.cabinet.documents.add(self.document)
response = self._remove_document_from_cabinet(cabinet=cabinet) response = self._request_remove_document_from_cabinet()
self.assertContains( self.assertContains(
response, text='Select a valid choice', status_code=200 response, text='Select a valid choice', status_code=200
) )
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.documents.count(), 1) self.assertEqual(self.cabinet.documents.count(), 1)
def test_cabinet_remove_document_view_with_permission(self): def test_cabinet_remove_document_view_with_access(self):
cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) self._create_cabinet()
cabinet.documents.add(self.document) self.cabinet.documents.add(self.document)
self.grant_permission(permission=permission_cabinet_remove_document) self.grant_access(
obj=self.cabinet, permission=permission_cabinet_remove_document
)
self.grant_access(
obj=self.document, permission=permission_cabinet_remove_document
)
response = self._remove_document_from_cabinet(cabinet=cabinet) response = self._request_remove_document_from_cabinet()
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
cabinet.refresh_from_db() self.cabinet.refresh_from_db()
self.assertEqual(cabinet.documents.count(), 0) self.assertEqual(self.cabinet.documents.count(), 0)
def _create_cabinet(self):
self.cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL)
def _request_cabinet_list(self): def _request_cabinet_list(self):
return self.get('cabinets:cabinet_list') return self.get('cabinets:cabinet_list')
@@ -219,9 +227,9 @@ class CabinetViewTestCase(GenericDocumentViewTestCase):
response, text=self.cabinet.label, status_code=200 response, text=self.cabinet.label, status_code=200
) )
def test_cabinet_list_view_with_permission(self): def test_cabinet_list_view_with_access(self):
self._create_cabinet() self._create_cabinet()
self.grant_permission(permission=permission_cabinet_view) self.grant_access(obj=self.cabinet, permission=permission_cabinet_view)
response = self._request_cabinet_list() response = self._request_cabinet_list()
self.assertContains( self.assertContains(

View File

@@ -153,7 +153,7 @@ class CabinetListView(SingleObjectListView):
'title': _('Cabinets'), 'title': _('Cabinets'),
} }
def get_queryset(self): def get_object_list(self):
return Cabinet.objects.root_nodes() return Cabinet.objects.root_nodes()
@@ -177,7 +177,7 @@ class DocumentCabinetListView(CabinetListView):
'title': _('Cabinets containing document: %s') % self.document, 'title': _('Cabinets containing document: %s') % self.document,
} }
def get_queryset(self): def get_object_list(self):
return self.document.document_cabinets().all() return self.document.document_cabinets().all()

View File

@@ -6,7 +6,6 @@ from kombu import Exchange, Queue
from django.apps import apps from django.apps import apps
from django.db.models.signals import pre_save from django.db.models.signals import pre_save
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from acls import ModelPermission from acls import ModelPermission

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import logging
import time import time
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@@ -61,6 +62,9 @@ class DocumentCheckoutTestCase(BaseTestCase):
) )
def test_version_creation_blocking(self): def test_version_creation_blocking(self):
# Silence unrelated logging
logging.getLogger('documents.models').setLevel(logging.CRITICAL)
expiration_datetime = now() + datetime.timedelta(days=1) expiration_datetime = now() + datetime.timedelta(days=1)
DocumentCheckout.objects.checkout_document( DocumentCheckout.objects.checkout_document(
@@ -123,6 +127,9 @@ class DocumentCheckoutTestCase(BaseTestCase):
self.assertFalse(self.document.is_checked_out()) self.assertFalse(self.document.is_checked_out())
def test_blocking_new_versions(self): def test_blocking_new_versions(self):
# Silence unrelated logging
logging.getLogger('documents.models').setLevel(logging.CRITICAL)
NewVersionBlock.objects.block(document=self.document) NewVersionBlock.objects.block(document=self.document)
with self.assertRaises(NewDocumentVersionNotAllowed): with self.assertRaises(NewDocumentVersionNotAllowed):

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import logging
from django.utils.timezone import now from django.utils.timezone import now
@@ -21,9 +22,7 @@ from ..permissions import (
class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase): class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
def test_checkin_document_view_no_permission(self): def test_checkin_document_view_no_permission(self):
self.login( self.login_user()
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
expiration_datetime = now() + datetime.timedelta(days=1) expiration_datetime = now() + datetime.timedelta(days=1)
@@ -42,10 +41,8 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
self.assertTrue(self.document.is_checked_out()) self.assertTrue(self.document.is_checked_out())
def test_checkin_document_view_with_permission(self): def test_checkin_document_view_with_access(self):
self.login( self.login_user()
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
expiration_datetime = now() + datetime.timedelta(days=1) expiration_datetime = now() + datetime.timedelta(days=1)
@@ -56,11 +53,12 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
self.assertTrue(self.document.is_checked_out()) self.assertTrue(self.document.is_checked_out())
self.role.permissions.add( self.grant_access(
permission_document_checkin.stored_permission obj=self.document, permission=permission_document_checkin
) )
self.role.permissions.add( self.grant_access(
permission_document_checkout_detail_view.stored_permission obj=self.document,
permission=permission_document_checkout_detail_view
) )
response = self.post( response = self.post(
@@ -78,9 +76,7 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
) )
def test_checkout_document_view_no_permission(self): def test_checkout_document_view_no_permission(self):
self.login( self.login_user()
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
response = self.post( response = self.post(
'checkouts:checkout_document', args=(self.document.pk,), data={ 'checkouts:checkout_document', args=(self.document.pk,), data={
@@ -94,15 +90,14 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
self.assertFalse(self.document.is_checked_out()) self.assertFalse(self.document.is_checked_out())
def test_checkout_document_view_with_permission(self): def test_checkout_document_view_with_access(self):
self.login( self.login_user()
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD self.grant_access(
obj=self.document, permission=permission_document_checkout
) )
self.role.permissions.add( self.grant_access(
permission_document_checkout.stored_permission obj=self.document,
) permission=permission_document_checkout_detail_view
self.role.permissions.add(
permission_document_checkout_detail_view.stored_permission
) )
response = self.post( response = self.post(
@@ -127,7 +122,6 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
- Link to upload version view should not resolve - Link to upload version view should not resolve
- Upload version view should reject request - Upload version view should reject request
""" """
self.login( self.login(
username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD
) )
@@ -167,6 +161,9 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase):
# Forcefully checking in a document by a user without adequate # Forcefully checking in a document by a user without adequate
# permissions throws out an error # permissions throws out an error
# Silence unrelated logging
logging.getLogger('navigation.classes').setLevel(logging.CRITICAL)
expiration_datetime = now() + datetime.timedelta(days=1) expiration_datetime = now() + datetime.timedelta(days=1)
DocumentCheckout.objects.checkout_document( DocumentCheckout.objects.checkout_document(

View File

@@ -3,6 +3,7 @@ from __future__ import absolute_import, unicode_literals
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@@ -25,8 +26,7 @@ from .mixins import (
DeleteExtraDataMixin, DynamicFormViewMixin, ExtraContextMixin, DeleteExtraDataMixin, DynamicFormViewMixin, ExtraContextMixin,
FormExtraKwargsMixin, MultipleObjectMixin, ObjectActionMixin, FormExtraKwargsMixin, MultipleObjectMixin, ObjectActionMixin,
ObjectListPermissionFilterMixin, ObjectNameMixin, ObjectListPermissionFilterMixin, ObjectNameMixin,
ObjectPermissionCheckMixin, PreserveGetQuerysetMixin, RedirectionMixin, ObjectPermissionCheckMixin, RedirectionMixin, ViewPermissionCheckMixin
ViewPermissionCheckMixin
) )
from .settings import setting_paginate_by from .settings import setting_paginate_by
@@ -282,17 +282,37 @@ class MultiFormView(DjangoFormView):
return self.forms_invalid(forms) return self.forms_invalid(forms)
class MultipleObjectFormActionView(PreserveGetQuerysetMixin, ObjectActionMixin, MultipleObjectMixin, FormExtraKwargsMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, DjangoFormView): class MultipleObjectFormActionView(ObjectActionMixin, MultipleObjectMixin, FormExtraKwargsMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, DjangoFormView):
""" """
This view will present a form and upon receiving a POST request will This view will present a form and upon receiving a POST request will
perform an action on an object or queryset perform an action on an object or queryset
""" """
template_name = 'appearance/generic_form.html' template_name = 'appearance/generic_form.html'
def __init__(self, *args, **kwargs):
result = super(MultipleObjectFormActionView, self).__init__(*args, **kwargs)
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. ' % {
'cls': self.__class__.__name__
}
)
return result
def form_valid(self, form): def form_valid(self, form):
self.view_action(form=form) self.view_action(form=form)
return super(MultipleObjectFormActionView, self).form_valid(form=form) return super(MultipleObjectFormActionView, self).form_valid(form=form)
def get_queryset(self):
try:
return super(MultipleObjectFormActionView, self).get_queryset()
except ImproperlyConfigured:
self.queryset = self.get_object_list()
return super(MultipleObjectFormActionView, self).get_queryset()
class MultipleObjectConfirmActionView(ObjectActionMixin, MultipleObjectMixin, ObjectListPermissionFilterMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, TemplateView): class MultipleObjectConfirmActionView(ObjectActionMixin, MultipleObjectMixin, ObjectListPermissionFilterMixin, ViewPermissionCheckMixin, ExtraContextMixin, RedirectionMixin, TemplateView):
template_name = 'appearance/generic_confirm.html' template_name = 'appearance/generic_confirm.html'
@@ -458,8 +478,28 @@ class SingleObjectDynamicFormEditView(DynamicFormViewMixin, SingleObjectEditView
pass pass
class SingleObjectListView(PreserveGetQuerysetMixin, PaginationMixin, ViewPermissionCheckMixin, ObjectListPermissionFilterMixin, ExtraContextMixin, RedirectionMixin, ListView): class SingleObjectListView(PaginationMixin, ViewPermissionCheckMixin, ObjectListPermissionFilterMixin, ExtraContextMixin, RedirectionMixin, ListView):
template_name = 'appearance/generic_list.html' template_name = 'appearance/generic_list.html'
def __init__(self, *args, **kwargs):
result = super(SingleObjectListView, self).__init__(*args, **kwargs)
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. ' % {
'cls': self.__class__.__name__
}
)
return result
def get_paginate_by(self, queryset): def get_paginate_by(self, queryset):
return setting_paginate_by.value return setting_paginate_by.value
def get_queryset(self):
try:
return super(SingleObjectListView, self).get_queryset()
except ImproperlyConfigured:
self.queryset = self.get_object_list()
return super(SingleObjectListView, self).get_queryset()

View File

@@ -18,8 +18,8 @@ __all__ = (
'DeleteExtraDataMixin', 'DynamicFormViewMixin', 'ExtraContextMixin', 'DeleteExtraDataMixin', 'DynamicFormViewMixin', 'ExtraContextMixin',
'FormExtraKwargsMixin', 'MultipleObjectMixin', 'ObjectActionMixin', 'FormExtraKwargsMixin', 'MultipleObjectMixin', 'ObjectActionMixin',
'ObjectListPermissionFilterMixin', 'ObjectNameMixin', 'ObjectListPermissionFilterMixin', 'ObjectNameMixin',
'ObjectPermissionCheckMixin', 'PreserveGetQuerysetMixin', 'ObjectPermissionCheckMixin', 'RedirectionMixin',
'RedirectionMixin', 'ViewPermissionCheckMixin' 'ViewPermissionCheckMixin'
) )
@@ -266,26 +266,6 @@ class ObjectPermissionCheckMixin(object):
).dispatch(request, *args, **kwargs) ).dispatch(request, *args, **kwargs)
class PreserveGetQuerysetMixin(object):
"""
Allows class based views to define a get_queryset method that doesn't
overrided the parent classe's get_queryset method
"""
def __init__(self, *args, **kwargs):
result = super(PreserveGetQuerysetMixin, self).__init__(*args, **kwargs)
if not hasattr(self.__class__, 'original_get_queryset'):
if not self.__class__.mro()[0].get_queryset == PreserveGetQuerysetMixin.get_queryset:
setattr(self.__class__, 'original_get_queryset', self.__class__.mro()[0].get_queryset)
self.__class__.mro()[0].get_queryset = PreserveGetQuerysetMixin.get_queryset
return result
def get_queryset(self, *args, **kwargs):
if hasattr(self.__class__, 'original_get_queryset'):
self.queryset = self.__class__.original_get_queryset(self, *args, **kwargs)
return super(PreserveGetQuerysetMixin, self).get_queryset(*args, **kwargs)
class RedirectionMixin(object): class RedirectionMixin(object):
post_action_redirect = None post_action_redirect = None
action_cancel_redirect = None action_cancel_redirect = None

View File

@@ -177,7 +177,7 @@ class FilterResultListView(SingleObjectListView):
except KeyError: except KeyError:
raise Http404(ugettext('Filter not found')) raise Http404(ugettext('Filter not found'))
def get_queryset(self): def get_object_list(self):
return self.get_filter().get_queryset(user=self.request.user) return self.get_filter().get_queryset(user=self.request.user)

View File

@@ -108,6 +108,15 @@ class TransformationCreateView(SingleObjectCreateView):
else: else:
return super(TransformationCreateView, self).form_valid(form) return super(TransformationCreateView, self).form_valid(form)
def get_extra_context(self):
return {
'content_object': self.content_object,
'navigation_object_list': ('content_object',),
'title': _(
'Create new transformation for: %s'
) % self.content_object,
}
def get_post_action_redirect(self): def get_post_action_redirect(self):
return reverse( return reverse(
'converter:transformation_list', args=( 'converter:transformation_list', args=(
@@ -119,15 +128,6 @@ class TransformationCreateView(SingleObjectCreateView):
def get_queryset(self): def get_queryset(self):
return Transformation.objects.get_for_model(self.content_object) return Transformation.objects.get_for_model(self.content_object)
def get_extra_context(self):
return {
'content_object': self.content_object,
'navigation_object_list': ('content_object',),
'title': _(
'Create new transformation for: %s'
) % self.content_object,
}
class TransformationEditView(SingleObjectEditView): class TransformationEditView(SingleObjectEditView):
fields = ('name', 'arguments', 'order') fields = ('name', 'arguments', 'order')
@@ -158,15 +158,6 @@ class TransformationEditView(SingleObjectEditView):
else: else:
return super(TransformationEditView, self).form_valid(form) return super(TransformationEditView, self).form_valid(form)
def get_post_action_redirect(self):
return reverse(
'converter:transformation_list', args=(
self.transformation.content_type.app_label,
self.transformation.content_type.model,
self.transformation.object_id
)
)
def get_extra_context(self): def get_extra_context(self):
return { return {
'content_object': self.transformation.content_object, 'content_object': self.transformation.content_object,
@@ -180,6 +171,15 @@ class TransformationEditView(SingleObjectEditView):
'transformation': self.transformation, 'transformation': self.transformation,
} }
def get_post_action_redirect(self):
return reverse(
'converter:transformation_list', args=(
self.transformation.content_type.app_label,
self.transformation.content_type.model,
self.transformation.object_id
)
)
class TransformationListView(SingleObjectListView): class TransformationListView(SingleObjectListView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
@@ -204,9 +204,6 @@ class TransformationListView(SingleObjectListView):
request, *args, **kwargs request, *args, **kwargs
) )
def get_queryset(self):
return Transformation.objects.get_for_model(self.content_object)
def get_extra_context(self): def get_extra_context(self):
return { return {
'content_object': self.content_object, 'content_object': self.content_object,
@@ -215,3 +212,6 @@ class TransformationListView(SingleObjectListView):
'navigation_object_list': ('content_object',), 'navigation_object_list': ('content_object',),
'title': _('Transformations for: %s') % self.content_object, 'title': _('Transformations for: %s') % self.content_object,
} }
def get_object_list(self):
return Transformation.objects.get_for_model(self.content_object)

View File

@@ -119,7 +119,7 @@ class KeyQueryResultView(SingleObjectListView):
'title': _('Key query results'), 'title': _('Key query results'),
} }
def get_queryset(self): def get_object_list(self):
term = self.request.GET.get('term') term = self.request.GET.get('term')
if term: if term:
return Key.objects.search(query=term) return Key.objects.search(query=term)

View File

@@ -90,14 +90,6 @@ class DocumentCommentListView(SingleObjectListView):
def get_document(self): def get_document(self):
return get_object_or_404(Document, pk=self.kwargs['pk']) return get_object_or_404(Document, pk=self.kwargs['pk'])
def get_queryset(self):
AccessControlList.objects.check_access(
permissions=permission_comment_view, user=self.request.user,
obj=self.get_document()
)
return self.get_document().comments.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_link': True, 'hide_link': True,
@@ -105,3 +97,11 @@ class DocumentCommentListView(SingleObjectListView):
'object': self.get_document(), 'object': self.get_document(),
'title': _('Comments for document: %s') % self.get_document(), 'title': _('Comments for document: %s') % self.get_document(),
} }
def get_object_list(self):
AccessControlList.objects.check_access(
permissions=permission_comment_view, user=self.request.user,
obj=self.get_document()
)
return self.get_document().comments.all()

View File

@@ -119,14 +119,6 @@ class SetupIndexDocumentTypesView(AssignRemoveView):
class SetupIndexTreeTemplateListView(SingleObjectListView): class SetupIndexTreeTemplateListView(SingleObjectListView):
object_permission = permission_document_indexing_edit object_permission = permission_document_indexing_edit
def get_index(self):
return get_object_or_404(Index, pk=self.kwargs['pk'])
def get_queryset(self):
return self.get_index().template_root.get_descendants(
include_self=True
)
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_object': True, 'hide_object': True,
@@ -135,6 +127,14 @@ class SetupIndexTreeTemplateListView(SingleObjectListView):
'title': _('Tree template nodes for index: %s') % self.get_index(), 'title': _('Tree template nodes for index: %s') % self.get_index(),
} }
def get_index(self):
return get_object_or_404(Index, pk=self.kwargs['pk'])
def get_object_list(self):
return self.get_index().template_root.get_descendants(
include_self=True
)
class TemplateNodeCreateView(SingleObjectCreateView): class TemplateNodeCreateView(SingleObjectCreateView):
form_class = IndexTemplateNodeForm form_class = IndexTemplateNodeForm
@@ -242,19 +242,6 @@ class IndexInstanceNodeView(DocumentListView):
return SingleObjectListView.dispatch(self, request, *args, **kwargs) return SingleObjectListView.dispatch(self, request, *args, **kwargs)
def get_queryset(self):
if self.index_instance_node:
if self.index_instance_node.index_template_node.link_documents:
return DocumentListView.get_queryset(self)
else:
self.object_permission = None
return self.index_instance_node.get_children().order_by(
'value'
)
else:
self.object_permission = None
return IndexInstanceNode.objects.none()
def get_document_queryset(self): def get_document_queryset(self):
if self.index_instance_node: if self.index_instance_node:
if self.index_instance_node.index_template_node.link_documents: if self.index_instance_node.index_template_node.link_documents:
@@ -286,6 +273,19 @@ class IndexInstanceNodeView(DocumentListView):
return context return context
def get_object_list(self):
if self.index_instance_node:
if self.index_instance_node.index_template_node.link_documents:
return DocumentListView.get_queryset(self)
else:
self.object_permission = None
return self.index_instance_node.get_children().order_by(
'value'
)
else:
self.object_permission = None
return IndexInstanceNode.objects.none()
class DocumentIndexNodeListView(SingleObjectListView): class DocumentIndexNodeListView(SingleObjectListView):
""" """
@@ -317,7 +317,7 @@ class DocumentIndexNodeListView(SingleObjectListView):
) % self.get_document(), ) % self.get_document(),
} }
def get_queryset(self): def get_object_list(self):
return DocumentIndexInstanceNode.objects.get_for(self.get_document()) return DocumentIndexInstanceNode.objects.get_for(self.get_document())

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import hashlib import hashlib
import logging
import time import time
from django.core.files import File from django.core.files import File
@@ -284,6 +285,9 @@ class EmbeddedSignaturesTestCase(BaseTestCase):
) )
def test_task_verify_missing_embedded_signature(self): def test_task_verify_missing_embedded_signature(self):
# Silence converter logging
logging.getLogger('converter.backends').setLevel(logging.CRITICAL)
old_hooks = DocumentVersion._post_save_hooks old_hooks = DocumentVersion._post_save_hooks
DocumentVersion._post_save_hooks = {} DocumentVersion._post_save_hooks = {}

View File

@@ -1,5 +1,7 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import logging
from django.core.files import File from django.core.files import File
from django_downloadview.test import assert_download_response from django_downloadview.test import assert_download_response
@@ -8,9 +10,6 @@ from django_gpg.models import Key
from documents.models import DocumentVersion from documents.models import DocumentVersion
from documents.tests.literals import TEST_DOCUMENT_PATH from documents.tests.literals import TEST_DOCUMENT_PATH
from documents.tests.test_views import GenericDocumentViewTestCase from documents.tests.test_views import GenericDocumentViewTestCase
from user_management.tests import (
TEST_USER_USERNAME, TEST_USER_PASSWORD
)
from ..models import DetachedSignature, EmbeddedSignature from ..models import DetachedSignature, EmbeddedSignature
from ..permissions import ( from ..permissions import (
@@ -45,7 +44,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
response = self.get( response = self.get(
'signatures:document_version_signature_list', 'signatures:document_version_signature_list',
@@ -54,7 +53,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_signature_list_view_with_permission(self): def test_signature_list_view_with_access(self):
with open(TEST_KEY_FILE) as file_object: with open(TEST_KEY_FILE) as file_object:
Key.objects.create(key_data=file_object.read()) Key.objects.create(key_data=file_object.read())
@@ -69,10 +68,11 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_view.stored_permission obj=document,
permission=permission_document_version_signature_view
) )
response = self.get( response = self.get(
@@ -97,7 +97,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
response = self.get( response = self.get(
'signatures:document_version_signature_details', 'signatures:document_version_signature_details',
@@ -106,7 +106,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_signature_detail_view_with_permission(self): def test_signature_detail_view_with_access(self):
with open(TEST_KEY_FILE) as file_object: with open(TEST_KEY_FILE) as file_object:
Key.objects.create(key_data=file_object.read()) Key.objects.create(key_data=file_object.read())
@@ -121,10 +121,11 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_view.stored_permission obj=document,
permission=permission_document_version_signature_view
) )
response = self.get( response = self.get(
@@ -140,7 +141,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
file_object=file_object file_object=file_object
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
with open(TEST_SIGNATURE_FILE_PATH) as file_object: with open(TEST_SIGNATURE_FILE_PATH) as file_object:
response = self.post( response = self.post(
@@ -152,16 +153,17 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertEqual(DetachedSignature.objects.count(), 0) self.assertEqual(DetachedSignature.objects.count(), 0)
def test_signature_upload_view_with_permission(self): def test_signature_upload_view_with_access(self):
with open(TEST_DOCUMENT_PATH) as file_object: with open(TEST_DOCUMENT_PATH) as file_object:
document = self.document_type.new_document( document = self.document_type.new_document(
file_object=file_object file_object=file_object
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_upload.stored_permission obj=document,
permission=permission_document_version_signature_upload
) )
with open(TEST_SIGNATURE_FILE_PATH) as file_object: with open(TEST_SIGNATURE_FILE_PATH) as file_object:
@@ -186,7 +188,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
response = self.get( response = self.get(
'signatures:document_version_signature_download', 'signatures:document_version_signature_download',
@@ -195,7 +197,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_signature_download_view_with_permission(self): def test_signature_download_view_with_access(self):
with open(TEST_DOCUMENT_PATH) as file_object: with open(TEST_DOCUMENT_PATH) as file_object:
document = self.document_type.new_document( document = self.document_type.new_document(
file_object=file_object file_object=file_object
@@ -207,10 +209,11 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_download.stored_permission obj=document,
permission=permission_document_version_signature_download
) )
self.expected_content_type = 'application/octet-stream; charset=utf-8' self.expected_content_type = 'application/octet-stream; charset=utf-8'
@@ -240,10 +243,11 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_view.stored_permission obj=document,
permission=permission_document_version_signature_view
) )
response = self.post( response = self.post(
@@ -254,7 +258,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertEqual(DetachedSignature.objects.count(), 1) self.assertEqual(DetachedSignature.objects.count(), 1)
def test_signature_delete_view_with_permission(self): def test_signature_delete_view_with_access(self):
with open(TEST_KEY_FILE) as file_object: with open(TEST_KEY_FILE) as file_object:
Key.objects.create(key_data=file_object.read()) Key.objects.create(key_data=file_object.read())
@@ -269,13 +273,15 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
signature_file=File(file_object) signature_file=File(file_object)
) )
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_access(
permission_document_version_signature_delete.stored_permission obj=document,
permission=permission_document_version_signature_delete
) )
self.role.permissions.add( self.grant_access(
permission_document_version_signature_view.stored_permission obj=document,
permission=permission_document_version_signature_view
) )
response = self.post( response = self.post(
@@ -287,6 +293,9 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
self.assertEqual(DetachedSignature.objects.count(), 0) self.assertEqual(DetachedSignature.objects.count(), 0)
def test_missing_signature_verify_view_no_permission(self): def test_missing_signature_verify_view_no_permission(self):
# Silence converter logging
logging.getLogger('converter.backends').setLevel(logging.CRITICAL)
for document in self.document_type.documents.all(): for document in self.document_type.documents.all():
document.delete(to_trash=False) document.delete(to_trash=False)
@@ -311,7 +320,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
DocumentVersion._post_save_hooks = old_hooks DocumentVersion._post_save_hooks = old_hooks
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
response = self.post( response = self.post(
'signatures:all_document_version_signature_verify', follow=True 'signatures:all_document_version_signature_verify', follow=True
@@ -325,6 +334,9 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
) )
def test_missing_signature_verify_view_with_permission(self): def test_missing_signature_verify_view_with_permission(self):
# Silence converter logging
logging.getLogger('converter.backends').setLevel(logging.CRITICAL)
for document in self.document_type.documents.all(): for document in self.document_type.documents.all():
document.delete(to_trash=False) document.delete(to_trash=False)
@@ -349,10 +361,10 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase):
DocumentVersion._post_save_hooks = old_hooks DocumentVersion._post_save_hooks = old_hooks
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add( self.grant_permission(
permission_document_version_signature_verify.stored_permission permission=permission_document_version_signature_verify
) )
response = self.post( response = self.post(

View File

@@ -300,7 +300,7 @@ class DocumentVersionSignatureListView(SingleObjectListView):
) % self.get_document_version(), ) % self.get_document_version(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document_version().signatures.all() return self.get_document_version().signatures.all()

View File

@@ -61,7 +61,7 @@ class DocumentWorkflowInstanceListView(SingleObjectListView):
) % self.get_document(), ) % self.get_document(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document().workflows.all() return self.get_document().workflows.all()
@@ -87,7 +87,7 @@ class WorkflowInstanceDetailView(SingleObjectListView):
'workflow_instance': self.get_workflow_instance(), 'workflow_instance': self.get_workflow_instance(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_workflow_instance().log_entries.order_by('-datetime') return self.get_workflow_instance().log_entries.order_by('-datetime')
def get_workflow_instance(self): def get_workflow_instance(self):
@@ -221,7 +221,7 @@ class SetupWorkflowStateListView(SingleObjectListView):
'title': _('States of workflow: %s') % self.get_workflow() 'title': _('States of workflow: %s') % self.get_workflow()
} }
def get_queryset(self): def get_object_list(self):
return self.get_workflow().states.all() return self.get_workflow().states.all()
def get_workflow(self): def get_workflow(self):
@@ -330,7 +330,7 @@ class SetupWorkflowStateActionListView(SingleObjectListView):
def get_form_schema(self): def get_form_schema(self):
return {'fields': self.get_class().fields} return {'fields': self.get_class().fields}
def get_queryset(self): def get_object_list(self):
return self.get_workflow_state().actions.all() return self.get_workflow_state().actions.all()
def get_workflow_state(self): def get_workflow_state(self):
@@ -379,7 +379,7 @@ class SetupWorkflowStateCreateView(SingleObjectCreateView):
def get_workflow(self): def get_workflow(self):
return get_object_or_404(Workflow, pk=self.kwargs['pk']) return get_object_or_404(Workflow, pk=self.kwargs['pk'])
def get_queryset(self): def get_object_list(self):
return self.get_workflow().states.all() return self.get_workflow().states.all()
def get_success_url(self): def get_success_url(self):
@@ -437,12 +437,6 @@ class SetupWorkflowStateEditView(SingleObjectEditView):
class SetupWorkflowTransitionListView(SingleObjectListView): class SetupWorkflowTransitionListView(SingleObjectListView):
view_permission = permission_workflow_view view_permission = permission_workflow_view
def get_workflow(self):
return get_object_or_404(Workflow, pk=self.kwargs['pk'])
def get_queryset(self):
return self.get_workflow().transitions.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_link': True, 'hide_link': True,
@@ -452,11 +446,32 @@ class SetupWorkflowTransitionListView(SingleObjectListView):
) % self.get_workflow() ) % self.get_workflow()
} }
def get_object_list(self):
return self.get_workflow().transitions.all()
def get_workflow(self):
return get_object_or_404(Workflow, pk=self.kwargs['pk'])
class SetupWorkflowTransitionCreateView(SingleObjectCreateView): class SetupWorkflowTransitionCreateView(SingleObjectCreateView):
form_class = WorkflowTransitionForm form_class = WorkflowTransitionForm
view_permission = permission_workflow_edit view_permission = permission_workflow_edit
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.workflow = self.get_workflow()
try:
self.object.save()
except IntegrityError:
messages.error(
self.request, _('Unable to save transition; integrity error.')
)
return super(
SetupWorkflowTransitionCreateView, self
).form_invalid(form)
else:
return HttpResponseRedirect(self.get_success_url())
def get_extra_context(self): def get_extra_context(self):
return { return {
'object': self.get_workflow(), 'object': self.get_workflow(),
@@ -472,10 +487,7 @@ class SetupWorkflowTransitionCreateView(SingleObjectCreateView):
kwargs['workflow'] = self.get_workflow() kwargs['workflow'] = self.get_workflow()
return kwargs return kwargs
def get_workflow(self): def get_object_list(self):
return get_object_or_404(Workflow, pk=self.kwargs['pk'])
def get_queryset(self):
return self.get_workflow().transitions.all() return self.get_workflow().transitions.all()
def get_success_url(self): def get_success_url(self):
@@ -484,20 +496,8 @@ class SetupWorkflowTransitionCreateView(SingleObjectCreateView):
args=(self.kwargs['pk'],) args=(self.kwargs['pk'],)
) )
def form_valid(self, form): def get_workflow(self):
self.object = form.save(commit=False) return get_object_or_404(Workflow, pk=self.kwargs['pk'])
self.object.workflow = self.get_workflow()
try:
self.object.save()
except IntegrityError:
messages.error(
self.request, _('Unable to save transition; integrity error.')
)
return super(
SetupWorkflowTransitionCreateView, self
).form_invalid(form)
else:
return HttpResponseRedirect(self.get_success_url())
class SetupWorkflowTransitionDeleteView(SingleObjectDeleteView): class SetupWorkflowTransitionDeleteView(SingleObjectDeleteView):
@@ -547,15 +547,15 @@ class SetupWorkflowTransitionEditView(SingleObjectEditView):
class WorkflowListView(SingleObjectListView): class WorkflowListView(SingleObjectListView):
view_permission = permission_workflow_view view_permission = permission_workflow_view
def get_queryset(self):
return WorkflowRuntimeProxy.objects.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_object': True, 'hide_object': True,
'title': _('Workflows') 'title': _('Workflows')
} }
def get_object_list(self):
return WorkflowRuntimeProxy.objects.all()
class WorkflowDocumentListView(DocumentListView): class WorkflowDocumentListView(DocumentListView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
@@ -641,7 +641,7 @@ class WorkflowStateListView(SingleObjectListView):
'title': _('States of workflow: %s') % self.get_workflow() 'title': _('States of workflow: %s') % self.get_workflow()
} }
def get_queryset(self): def get_object_list(self):
return WorkflowStateRuntimeProxy.objects.filter( return WorkflowStateRuntimeProxy.objects.filter(
workflow=self.get_workflow() workflow=self.get_workflow()
) )

View File

@@ -82,7 +82,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase):
response = self.get('documents:document_list') response = self.get('documents:document_list')
self.assertContains(response, 'Total: 0', status_code=200) self.assertContains(response, 'Total: 0', status_code=200)
def test_document_list_view_with_permissions(self): def test_document_list_view_with_access(self):
self.grant_access( self.grant_access(
obj=self.document, permission=permission_document_view obj=self.document, permission=permission_document_view
) )

View File

@@ -41,9 +41,6 @@ class DocumentPageListView(SingleObjectListView):
def get_document(self): def get_document(self):
return get_object_or_404(Document, pk=self.kwargs['pk']) return get_object_or_404(Document, pk=self.kwargs['pk'])
def get_queryset(self):
return self.get_document().pages.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'list_as_items': True, 'list_as_items': True,
@@ -51,6 +48,9 @@ class DocumentPageListView(SingleObjectListView):
'title': _('Pages for document: %s') % self.get_document(), 'title': _('Pages for document: %s') % self.get_document(),
} }
def get_object_list(self):
return self.get_document().pages.all()
class DocumentPageNavigationBase(RedirectView): class DocumentPageNavigationBase(RedirectView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):

View File

@@ -196,5 +196,5 @@ class DocumentTypeFilenameListView(SingleObjectListView):
) % self.get_document_type(), ) % self.get_document_type(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document_type().filenames.all() return self.get_document_type().filenames.all()

View File

@@ -47,7 +47,7 @@ class DocumentVersionListView(SingleObjectListView):
'title': _('Versions of document: %s') % self.get_document(), 'title': _('Versions of document: %s') % self.get_document(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document().versions.order_by('-timestamp') return self.get_document().versions.order_by('-timestamp')

View File

@@ -64,7 +64,7 @@ class DocumentListView(SingleObjectListView):
'title': _('All documents'), 'title': _('All documents'),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document_queryset().filter(is_stub=False) return self.get_document_queryset().filter(is_stub=False)
@@ -203,7 +203,7 @@ class DocumentDuplicatesListView(DocumentListView):
) )
return context return context
def get_queryset(self): def get_object_list(self):
try: try:
return DuplicatedDocument.objects.get( return DuplicatedDocument.objects.get(
document=self.get_document() document=self.get_document()

View File

@@ -27,7 +27,7 @@ class ResultsView(SearchModelMixin, SingleObjectListView):
return context return context
def get_queryset(self): def get_object_list(self):
self.search_model = self.get_search_model() self.search_model = self.get_search_model()
if self.request.GET: if self.request.GET:

View File

@@ -19,9 +19,6 @@ from .widgets import event_object_link
class EventListView(SingleObjectListView): class EventListView(SingleObjectListView):
view_permission = permission_events_view view_permission = permission_events_view
def get_queryset(self):
return Action.objects.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'extra_columns': ( 'extra_columns': (
@@ -36,6 +33,9 @@ class EventListView(SingleObjectListView):
'title': _('Events'), 'title': _('Events'),
} }
def get_object_list(self):
return Action.objects.all()
class ObjectEventListView(EventListView): class ObjectEventListView(EventListView):
view_permissions = None view_permissions = None
@@ -69,14 +69,11 @@ class ObjectEventListView(EventListView):
'title': _('Events for: %s') % self.content_object, 'title': _('Events for: %s') % self.content_object,
} }
def get_queryset(self): def get_object_list(self):
return any_stream(self.content_object) return any_stream(self.content_object)
class VerbEventListView(SingleObjectListView): class VerbEventListView(SingleObjectListView):
def get_queryset(self):
return Action.objects.filter(verb=self.kwargs['verb'])
def get_extra_context(self): def get_extra_context(self):
return { return {
'extra_columns': ( 'extra_columns': (
@@ -92,3 +89,6 @@ class VerbEventListView(SingleObjectListView):
'Events of type: %s' 'Events of type: %s'
) % Event.get_label(self.kwargs['verb']), ) % Event.get_label(self.kwargs['verb']),
} }
def get_object_list(self):
return Action.objects.filter(verb=self.kwargs['verb'])

View File

@@ -132,7 +132,7 @@ class SmartLinkListView(SingleObjectListView):
'title': _('Smart links'), 'title': _('Smart links'),
} }
def get_queryset(self): def get_object_list(self):
return self.get_smart_link_queryset() return self.get_smart_link_queryset()
def get_smart_link_queryset(self): def get_smart_link_queryset(self):
@@ -209,7 +209,7 @@ class SmartLinkConditionListView(SingleObjectListView):
) % self.get_smart_link(), ) % self.get_smart_link(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_smart_link().conditions.all() return self.get_smart_link().conditions.all()
def get_smart_link(self): def get_smart_link(self):

View File

@@ -210,7 +210,7 @@ class UserMailerLogEntryListView(SingleObjectListView):
'title': _('%s error log') % self.get_user_mailer(), 'title': _('%s error log') % self.get_user_mailer(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_user_mailer().error_log.all() return self.get_user_mailer().error_log.all()
def get_user_mailer(self): def get_user_mailer(self):

View File

@@ -1,5 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import logging
from django.core.files.base import File from django.core.files.base import File
from documents.models import DocumentType from documents.models import DocumentType
from documents.permissions import ( from documents.permissions import (
@@ -140,12 +142,14 @@ class DocumentMetadataTestCase(GenericDocumentViewTestCase):
'form-MAX_NUM_FORMS': '', 'form-MAX_NUM_FORMS': '',
}, follow=True }, follow=True
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(self.document.metadata.all()), 1) self.assertEqual(len(self.document.metadata.all()), 1)
def test_metadata_remove_view_with_permission(self): def test_metadata_remove_view_with_permission(self):
# Silence unrelated logging
logging.getLogger('navigation.classes').setLevel(logging.CRITICAL)
self.login_user() self.login_user()
document_metadata = self.document.metadata.create( document_metadata = self.document.metadata.create(

View File

@@ -359,7 +359,7 @@ class DocumentMetadataListView(SingleObjectListView):
'title': _('Metadata for document: %s') % document, 'title': _('Metadata for document: %s') % document,
} }
def get_queryset(self): def get_object_list(self):
return self.get_document().metadata.all() return self.get_document().metadata.all()
@@ -545,9 +545,6 @@ class MetadataTypeEditView(SingleObjectEditView):
class MetadataTypeListView(SingleObjectListView): class MetadataTypeListView(SingleObjectListView):
view_permission = permission_metadata_type_view view_permission = permission_metadata_type_view
def get_queryset(self):
return MetadataType.objects.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'extra_columns': ( 'extra_columns': (
@@ -560,6 +557,9 @@ class MetadataTypeListView(SingleObjectListView):
'title': _('Metadata types'), 'title': _('Metadata types'),
} }
def get_object_list(self):
return MetadataType.objects.all()
class SetupDocumentTypeMetadataTypes(FormView): class SetupDocumentTypeMetadataTypes(FormView):
form_class = DocumentTypeMetadataTypeRelationshipFormSet form_class = DocumentTypeMetadataTypeRelationshipFormSet

View File

@@ -156,7 +156,7 @@ class EntryListView(SingleObjectListView):
} }
view_permission = permission_ocr_document view_permission = permission_ocr_document
def get_queryset(self): def get_object_list(self):
return DocumentVersionOCRError.objects.all() return DocumentVersionOCRError.objects.all()
@@ -173,7 +173,7 @@ class DocumentOCRErrorsListView(SingleObjectListView):
'title': _('OCR errors for document: %s') % self.get_document(), 'title': _('OCR errors for document: %s') % self.get_document(),
} }
def get_queryset(self): def get_object_list(self):
return self.get_document().latest_version.ocr_errors.all() return self.get_document().latest_version.ocr_errors.all()

View File

@@ -16,7 +16,7 @@ class NamespaceListView(SingleObjectListView):
} }
view_permission = permission_settings_view view_permission = permission_settings_view
def get_queryset(self): def get_object_list(self):
return Namespace.get_all() return Namespace.get_all()
@@ -37,5 +37,5 @@ class NamespaceDetailView(SingleObjectListView):
_('Namespace: %s, not found') % self.kwargs['namespace_name'] _('Namespace: %s, not found') % self.kwargs['namespace_name']
) )
def get_queryset(self): def get_object_list(self):
return self.get_namespace().settings return self.get_namespace().settings

View File

@@ -5,7 +5,6 @@ import shutil
from django.test import override_settings from django.test import override_settings
from acls.models import AccessControlList
from checkouts.models import NewVersionBlock from checkouts.models import NewVersionBlock
from common.tests.test_views import GenericViewTestCase from common.tests.test_views import GenericViewTestCase
from common.utils import fs_cleanup, mkdtemp from common.utils import fs_cleanup, mkdtemp
@@ -80,10 +79,9 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase):
# Create an access control entry giving the role the document # Create an access control entry giving the role the document
# create permission for the selected document type. # create permission for the selected document type.
acl = AccessControlList.objects.create( self.grant_access(
content_object=self.document_type, role=self.role obj=self.document_type, permission=permission_document_create
) )
acl.permissions.add(permission_document_create.stored_permission)
with open(TEST_SMALL_DOCUMENT_PATH) as file_object: with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
response = self.post( response = self.post(

View File

@@ -45,14 +45,6 @@ from .utils import get_class, get_form_class, get_upload_form_class
class SourceLogListView(SingleObjectListView): class SourceLogListView(SingleObjectListView):
view_permission = permission_sources_setup_view view_permission = permission_sources_setup_view
def get_source(self):
return get_object_or_404(
Source.objects.select_subclasses(), pk=self.kwargs['pk']
)
def get_queryset(self):
return self.get_source().logs.all()
def get_extra_context(self): def get_extra_context(self):
return { return {
'hide_object': True, 'hide_object': True,
@@ -60,6 +52,14 @@ class SourceLogListView(SingleObjectListView):
'title': _('Log entries for source: %s') % self.get_source(), 'title': _('Log entries for source: %s') % self.get_source(),
} }
def get_object_list(self):
return self.get_source().logs.all()
def get_source(self):
return get_object_or_404(
Source.objects.select_subclasses(), pk=self.kwargs['pk']
)
class UploadBaseView(MultiFormView): class UploadBaseView(MultiFormView):
template_name = 'appearance/generic_form.html' template_name = 'appearance/generic_form.html'

View File

@@ -2,17 +2,13 @@ from __future__ import unicode_literals
from common.tests.test_views import GenericViewTestCase from common.tests.test_views import GenericViewTestCase
from user_management.tests.literals import (
TEST_USER_PASSWORD, TEST_USER_USERNAME
)
from ..classes import Statistic from ..classes import Statistic
from ..permissions import permission_statistics_view from ..permissions import permission_statistics_view
class StatisticsViewTestCase(GenericViewTestCase): class StatisticsViewTestCase(GenericViewTestCase):
def test_statistic_detail_view_no_permissions(self): def test_statistic_detail_view_no_permissions(self):
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
statistic = Statistic.get_all()[0] statistic = Statistic.get_all()[0]
@@ -23,9 +19,9 @@ class StatisticsViewTestCase(GenericViewTestCase):
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_statistic_detail_view_with_permissions(self): def test_statistic_detail_view_with_permissions(self):
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add(permission_statistics_view.stored_permission) self.grant_permission(permission=permission_statistics_view)
statistic = Statistic.get_all()[0] statistic = Statistic.get_all()[0]
@@ -36,16 +32,16 @@ class StatisticsViewTestCase(GenericViewTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_statistic_namespace_list_view_no_permissions(self): def test_statistic_namespace_list_view_no_permissions(self):
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
response = self.get('statistics:namespace_list') response = self.get('statistics:namespace_list')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_statistic_namespace_list_view_with_permissions(self): def test_statistic_namespace_list_view_with_permissions(self):
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) self.login_user()
self.role.permissions.add(permission_statistics_view.stored_permission) self.grant_permission(permission=permission_statistics_view)
response = self.get('statistics:namespace_list') response = self.get('statistics:namespace_list')

View File

@@ -19,7 +19,7 @@ class NamespaceListView(SingleObjectListView):
template_name = 'appearance/generic_list.html' template_name = 'appearance/generic_list.html'
view_permission = permission_statistics_view view_permission = permission_statistics_view
def get_queryset(self): def get_object_list(self):
return StatisticNamespace.get_all() return StatisticNamespace.get_all()
@@ -36,7 +36,7 @@ class NamespaceDetailView(SingleObjectListView):
def get_namespace(self): def get_namespace(self):
return StatisticNamespace.get(self.kwargs['slug']) return StatisticNamespace.get(self.kwargs['slug'])
def get_queryset(self): def get_object_list(self):
return self.get_namespace().statistics return self.get_namespace().statistics

View File

@@ -24,7 +24,7 @@ class TagMultipleSelectionForm(forms.Form):
super(TagMultipleSelectionForm, self).__init__(*args, **kwargs) super(TagMultipleSelectionForm, self).__init__(*args, **kwargs)
queryset = AccessControlList.objects.filter_by_access( queryset = AccessControlList.objects.filter_by_access(
permission_tag_view, user, queryset=queryset permission=permission_tag_view, queryset=queryset, user=user
) )
self.fields['tags'] = forms.ModelMultipleChoiceField( self.fields['tags'] = forms.ModelMultipleChoiceField(

View File

@@ -16,30 +16,23 @@ from .literals import (
class TagViewTestCase(GenericDocumentViewTestCase): class TagViewTestCase(GenericDocumentViewTestCase):
def setUp(self): def _create_tag(self):
super(TagViewTestCase, self).setUp()
self.tag = Tag.objects.create( self.tag = Tag.objects.create(
color=TEST_TAG_COLOR, label=TEST_TAG_LABEL color=TEST_TAG_COLOR, label=TEST_TAG_LABEL
) )
def tearDown(self): def _request_create_tag(self):
if self.tag.pk: return self.post(
self.tag.delete() 'tags:tag_create', data={
super(TagViewTestCase, self).tearDown() 'label': TEST_TAG_LABEL,
'color': TEST_TAG_COLOR
}, follow=True
)
def test_tag_create_view_no_permissions(self): def test_tag_create_view_no_permissions(self):
self.login_user() self.login_user()
self.tag.delete() response = self._request_create_tag()
self.assertEqual(Tag.objects.count(), 0)
response = self.post(
'tags:tag_create', data={
'label': TEST_TAG_LABEL,
'color': TEST_TAG_COLOR
}
)
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertEqual(Tag.objects.count(), 0) self.assertEqual(Tag.objects.count(), 0)
@@ -47,204 +40,201 @@ class TagViewTestCase(GenericDocumentViewTestCase):
def test_tag_create_view_with_permissions(self): def test_tag_create_view_with_permissions(self):
self.login_user() self.login_user()
self.tag.delete()
self.assertEqual(Tag.objects.count(), 0)
self.grant_permission(permission=permission_tag_create) self.grant_permission(permission=permission_tag_create)
response = self.post( response = self._request_create_tag()
'tags:tag_create', data={
'label': TEST_TAG_LABEL,
'color': TEST_TAG_COLOR
}, follow=True
)
self.assertContains(response, text='created', status_code=200) self.assertEqual(response.status_code, 200)
self.assertEqual(Tag.objects.count(), 1) self.assertEqual(Tag.objects.count(), 1)
tag = Tag.objects.first() tag = Tag.objects.first()
self.assertEqual(tag.label, TEST_TAG_LABEL) self.assertEqual(tag.label, TEST_TAG_LABEL)
self.assertEqual(tag.color, TEST_TAG_COLOR) self.assertEqual(tag.color, TEST_TAG_COLOR)
def test_tag_delete_view_no_permissions(self): def _request_delete_tag(self):
self.login_user() return self.post(
self.assertEqual(Tag.objects.count(), 1)
response = self.post(
'tags:tag_delete', args=(self.tag.pk,)
)
self.assertEqual(response.status_code, 302)
self.assertEqual(Tag.objects.count(), 1)
def test_tag_delete_view_with_permissions(self):
self.login_user()
self.assertEqual(Tag.objects.count(), 1)
self.grant_permission(permission=permission_tag_delete)
response = self.post(
'tags:tag_delete', args=(self.tag.pk,), follow=True 'tags:tag_delete', args=(self.tag.pk,), follow=True
) )
self.assertContains(response, text='deleted', status_code=200) def test_tag_delete_view_no_permissions(self):
self.login_user()
self._create_tag()
response = self._request_delete_tag()
self.assertEqual(response.status_code, 200)
self.assertEqual(Tag.objects.count(), 1)
def test_tag_delete_view_with_access(self):
self.login_user()
self._create_tag()
self.grant_access(obj=self.tag, permission=permission_tag_delete)
response = self._request_delete_tag()
self.assertEqual(response.status_code, 200)
self.assertEqual(Tag.objects.count(), 0) self.assertEqual(Tag.objects.count(), 0)
def test_tag_multiple_delete_view_no_permissions(self): def _request_multiple_delete(self):
self.login_user() return self.post(
self.assertEqual(Tag.objects.count(), 1)
response = self.post(
'tags:tag_multiple_delete', data={'id_list': self.tag.pk}
)
self.assertEqual(response.status_code, 302)
self.assertEqual(Tag.objects.count(), 1)
def test_tag_multiple_delete_view_with_permissions(self):
self.login_user()
self.assertEqual(Tag.objects.count(), 1)
self.grant_permission(permission=permission_tag_delete)
response = self.post(
'tags:tag_multiple_delete', data={'id_list': self.tag.pk}, 'tags:tag_multiple_delete', data={'id_list': self.tag.pk},
follow=True follow=True
) )
self.assertContains(response, text='deleted', status_code=200) def test_tag_multiple_delete_view_no_permissions(self):
self.login_user()
self._create_tag()
response = self._request_multiple_delete()
self.assertEqual(response.status_code, 200)
self.assertEqual(Tag.objects.count(), 1)
def test_tag_multiple_delete_view_with_access(self):
self.login_user()
self._create_tag()
self.grant_access(obj=self.tag, permission=permission_tag_delete)
response = self._request_multiple_delete()
self.assertEqual(response.status_code, 200)
self.assertEqual(Tag.objects.count(), 0) self.assertEqual(Tag.objects.count(), 0)
def test_tag_edit_view_no_permissions(self): def _request_edit_tag(self):
self.login_user() return self.post(
response = self.post(
'tags:tag_edit', args=(self.tag.pk,), data={ 'tags:tag_edit', args=(self.tag.pk,), data={
'label': TEST_TAG_LABEL_EDITED, 'color': TEST_TAG_COLOR_EDITED 'label': TEST_TAG_LABEL_EDITED, 'color': TEST_TAG_COLOR_EDITED
} }, follow=True
) )
def test_tag_edit_view_no_permissions(self):
self.login_user()
self._create_tag()
response = self._request_edit_tag()
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
tag = Tag.objects.get(pk=self.tag.pk) tag = Tag.objects.get(pk=self.tag.pk)
self.assertEqual(tag.label, TEST_TAG_LABEL) self.assertEqual(tag.label, TEST_TAG_LABEL)
self.assertEqual(tag.color, TEST_TAG_COLOR) self.assertEqual(tag.color, TEST_TAG_COLOR)
def test_tag_edit_view_with_permissions(self): def test_tag_edit_view_with_access(self):
self.login_user() self.login_user()
self._create_tag()
self.grant_permission(permission=permission_tag_edit) self.grant_access(obj=self.tag, permission=permission_tag_edit)
response = self.post( response = self._request_edit_tag()
'tags:tag_edit', args=(self.tag.pk,), data={
'label': TEST_TAG_LABEL_EDITED, 'color': TEST_TAG_COLOR_EDITED
}, follow=True
)
self.assertContains(response, text='update', status_code=200) self.assertEqual(response.status_code, 200)
tag = Tag.objects.get(pk=self.tag.pk) tag = Tag.objects.get(pk=self.tag.pk)
self.assertEqual(tag.label, TEST_TAG_LABEL_EDITED) self.assertEqual(tag.label, TEST_TAG_LABEL_EDITED)
self.assertEqual(tag.color, TEST_TAG_COLOR_EDITED) self.assertEqual(tag.color, TEST_TAG_COLOR_EDITED)
def _request_document_list(self):
return self.get('documents:document_list')
def test_document_tags_widget_no_permissions(self): def test_document_tags_widget_no_permissions(self):
self.login_user() self.login_user()
self._create_tag()
self.tag.documents.add(self.document) self.tag.documents.add(self.document)
response = self.get('documents:document_list') response = self._request_document_list()
self.assertNotContains(response, text=TEST_TAG_LABEL, status_code=200) self.assertNotContains(response, text=TEST_TAG_LABEL, status_code=200)
def test_document_tags_widget_with_permissions(self): def test_document_tags_widget_with_access(self):
self.login_user() self.login_user()
self._create_tag()
self.tag.documents.add(self.document) self.tag.documents.add(self.document)
self.grant_permission(permission=permission_tag_view) self.grant_access(obj=self.tag, permission=permission_tag_view)
self.grant_permission(permission=permission_document_view) self.grant_access(
obj=self.document, permission=permission_document_view
)
response = self.get('documents:document_list') response = self._request_document_list()
self.assertContains(response, text=TEST_TAG_LABEL, status_code=200) self.assertContains(response, text=TEST_TAG_LABEL, status_code=200)
def test_document_attach_tag_view_no_permission(self): def _request_attach_tag(self):
self.login_user() return self.post(
self.assertEqual(self.document.tags.count(), 0)
self.grant_permission(permission=permission_tag_view)
response = self.post(
'tags:tag_attach', args=(self.document.pk,), data={
'tag': self.tag.pk,
'user': self.user.pk
}
)
# Redirect to previous URL and show warning message about having to
# select at least one object.
self.assertEqual(response.status_code, 302)
self.assertEqual(self.document.tags.count(), 0)
def test_document_attach_tag_view_with_permission(self):
self.login_user()
self.assertEqual(self.document.tags.count(), 0)
self.grant_permission(permission=permission_tag_attach)
# permission_tag_view is needed because the form filters the
# choices
self.grant_permission(permission=permission_tag_view)
response = self.post(
'tags:tag_attach', args=(self.document.pk,), data={ 'tags:tag_attach', args=(self.document.pk,), data={
'tags': self.tag.pk, 'tags': self.tag.pk,
'user': self.user.pk 'user': self.user.pk
}, follow=True }, follow=True
) )
def test_document_attach_tag_view_no_permission(self):
self.login_user()
self._create_tag()
self.assertEqual(self.document.tags.count(), 0)
self.grant_access(obj=self.tag, permission=permission_tag_attach)
response = self._request_attach_tag()
# Redirect to previous URL and show warning message about having to
# select at least one object.
self.assertEqual(response.status_code, 200)
self.assertEqual(self.document.tags.count(), 0)
def test_document_attach_tag_view_with_access(self):
self.login_user()
self._create_tag()
self.assertEqual(self.document.tags.count(), 0)
self.grant_access(obj=self.document, permission=permission_tag_attach)
self.grant_access(obj=self.tag, permission=permission_tag_attach)
# permission_tag_view is needed because the form filters the
# choices
self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self._request_attach_tag()
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual( self.assertQuerysetEqual(
self.document.tags.all(), (repr(self.tag),) self.document.tags.all(), (repr(self.tag),)
) )
def _request_multiple_attach_tag(self):
return self.post(
'tags:multiple_documents_tag_attach', data={
'id_list': self.document.pk, 'tags': self.tag.pk,
'user': self.user.pk
}, follow=True
)
def test_document_multiple_attach_tag_view_no_permission(self): def test_document_multiple_attach_tag_view_no_permission(self):
self.login_user() self.login_user()
self._create_tag()
self.assertEqual(self.document.tags.count(), 0)
self.grant_permission(permission=permission_tag_view) self.grant_permission(permission=permission_tag_view)
response = self.post( response = self._request_multiple_attach_tag()
'tags:multiple_documents_tag_attach', data={
'id_list': self.document.pk, 'tags': self.tag.pk,
'user': self.user.pk
}
)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 200)
self.assertEqual(self.document.tags.count(), 0) self.assertEqual(self.document.tags.count(), 0)
def test_document_multiple_attach_tag_view_with_permission(self): def test_document_multiple_attach_tag_view_with_access(self):
self.login_user() self.login_user()
self._create_tag()
self.assertEqual(self.document.tags.count(), 0) self.grant_access(obj=self.document, permission=permission_tag_attach)
self.grant_access(obj=self.tag, permission=permission_tag_attach)
self.grant_permission(permission=permission_tag_attach)
# permission_tag_view is needed because the form filters the # permission_tag_view is needed because the form filters the
# choices # choices
self.grant_permission(permission=permission_tag_view) self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self.post( response = self._request_multiple_attach_tag()
'tags:multiple_documents_tag_attach', data={
'id_list': self.document.pk, 'tags': self.tag.pk,
'user': self.user.pk
}, follow=True
)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@@ -252,35 +242,8 @@ class TagViewTestCase(GenericDocumentViewTestCase):
self.document.tags.all(), (repr(self.tag),) self.document.tags.all(), (repr(self.tag),)
) )
def test_single_document_multiple_tag_remove_view_no_permissions(self): def _request_single_document_multiple_tag_remove(self):
self.login_user() return self.post(
self.document.tags.add(self.tag)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
self.grant_permission(permission=permission_tag_view)
response = self.post(
'tags:single_document_multiple_tag_remove',
args=(self.document.pk,), data={
'id_list': self.document.pk,
'tags': self.tag.pk,
}
)
self.assertEqual(response.status_code, 302)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
def test_single_document_multiple_tag_remove_view_with_permission(self):
self.login_user()
self.document.tags.add(self.tag)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
self.grant_permission(permission=permission_tag_remove)
self.grant_permission(permission=permission_tag_view)
response = self.post(
'tags:single_document_multiple_tag_remove', 'tags:single_document_multiple_tag_remove',
args=(self.document.pk,), data={ args=(self.document.pk,), data={
'id_list': self.document.pk, 'id_list': self.document.pk,
@@ -288,44 +251,67 @@ class TagViewTestCase(GenericDocumentViewTestCase):
}, follow=True }, follow=True
) )
def test_single_document_multiple_tag_remove_view_no_permissions(self):
self.login_user()
self._create_tag()
self.document.tags.add(self.tag)
self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self._request_single_document_multiple_tag_remove()
self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
def test_single_document_multiple_tag_remove_view_with_access(self):
self.login_user()
self._create_tag()
self.document.tags.add(self.tag)
self.grant_access(obj=self.document, permission=permission_tag_remove)
self.grant_access(obj=self.tag, permission=permission_tag_remove)
self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self._request_single_document_multiple_tag_remove()
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(self.document.tags.count(), 0) self.assertEqual(self.document.tags.count(), 0)
def _request_multiple_documents_selection_tag_remove(self):
return self.post(
'tags:multiple_documents_selection_tag_remove',
data={
'id_list': self.document.pk,
'tags': self.tag.pk,
}, follow=True
)
def test_multiple_documents_selection_tag_remove_view_no_permissions(self): def test_multiple_documents_selection_tag_remove_view_no_permissions(self):
self.login_user() self.login_user()
self._create_tag()
self.document.tags.add(self.tag) self.document.tags.add(self.tag)
self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self._request_multiple_documents_selection_tag_remove()
self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),)) self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
self.grant_permission(permission=permission_tag_view) def test_multiple_documents_selection_tag_remove_view_with_access(self):
response = self.post(
'tags:multiple_documents_selection_tag_remove',
data={
'id_list': self.document.pk,
'tags': self.tag.pk,
}
)
self.assertEqual(response.status_code, 302)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
def test_multiple_documents_selection_tag_remove_view_with_permission(self):
self.login_user() self.login_user()
self._create_tag()
self.document.tags.add(self.tag) self.document.tags.add(self.tag)
self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),))
self.grant_permission(permission=permission_tag_remove) self.grant_access(obj=self.document, permission=permission_tag_remove)
self.grant_permission(permission=permission_tag_view) self.grant_access(obj=self.tag, permission=permission_tag_remove)
self.grant_access(obj=self.tag, permission=permission_tag_view)
response = self.post( response = self._request_multiple_documents_selection_tag_remove()
'tags:multiple_documents_selection_tag_remove',
data={
'id_list': self.document.pk,
'tags': self.tag.pk,
}, follow=True
)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(self.document.tags.count(), 0) self.assertEqual(self.document.tags.count(), 0)

View File

@@ -183,7 +183,7 @@ class TagListView(SingleObjectListView):
'title': _('Tags'), 'title': _('Tags'),
} }
def get_queryset(self): def get_object_list(self):
return self.get_tag_queryset() return self.get_tag_queryset()
def get_tag_queryset(self): def get_tag_queryset(self):

View File

@@ -240,7 +240,7 @@ class UserListView(SingleObjectListView):
'title': _('Users'), 'title': _('Users'),
} }
def get_queryset(self): def get_object_list(self):
return get_user_model().objects.exclude( return get_user_model().objects.exclude(
is_superuser=True is_superuser=True
).exclude(is_staff=True).order_by('last_name', 'first_name') ).exclude(is_staff=True).order_by('last_name', 'first_name')