Merge new document API view with the document list API view (as a POST action). Move search result filtering to clases module, this way it filters results from the API too.
This commit is contained in:
@@ -41,19 +41,18 @@ class APIDocumentListView(generics.ListAPIView):
|
||||
Returns a list of all the documents.
|
||||
"""
|
||||
|
||||
serializer_class = DocumentSerializer
|
||||
queryset = Document.objects.all()
|
||||
|
||||
permission_classes = (MayanPermission,)
|
||||
filter_backends = (MayanObjectPermissionsFilter,)
|
||||
mayan_object_permissions = {'GET': [PERMISSION_DOCUMENT_VIEW]}
|
||||
mayan_object_permissions = {'GET': [PERMISSION_DOCUMENT_VIEW],
|
||||
'POST': [PERMISSION_DOCUMENT_CREATE]}
|
||||
|
||||
|
||||
class APINewDocumentView(generics.GenericAPIView):
|
||||
serializer_class = NewDocumentSerializer
|
||||
|
||||
permission_classes = (MayanPermission,)
|
||||
mayan_view_permissions = {'POST': [PERMISSION_DOCUMENT_CREATE]}
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'GET':
|
||||
return DocumentSerializer
|
||||
elif self.request.method == 'POST':
|
||||
return NewDocumentSerializer
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
"""Create a new document."""
|
||||
|
||||
@@ -146,7 +146,7 @@ class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
|
||||
# Create a blank document with no token in the header
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_descriptor:
|
||||
response = document_client.post(reverse('newdocument-view'), {'document_type': self.document_type.pk, 'file': file_descriptor})
|
||||
response = document_client.post(reverse('document-list'), {'document_type': self.document_type.pk, 'file': file_descriptor})
|
||||
|
||||
# Make sure toke authentication is working, should fail
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -155,7 +155,7 @@ class DocumentAPICreateDocumentTestCase(TestCase):
|
||||
|
||||
# Create a blank document
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_descriptor:
|
||||
document_response = document_client.post(reverse('newdocument-view'), {'document_type': self.document_type.pk, 'file': file_descriptor})
|
||||
document_response = document_client.post(reverse('document-list'), {'document_type': self.document_type.pk, 'file': file_descriptor})
|
||||
|
||||
self.assertEqual(document_response.status_code, status.HTTP_202_ACCEPTED)
|
||||
|
||||
|
||||
@@ -5,8 +5,7 @@ from django.conf.urls import patterns, url
|
||||
from .api_views import (APIDocumentView, APIDocumentImageView, APIDocumentListView,
|
||||
APIDocumentPageView, APIDocumentTypeDocumentListView,
|
||||
APIDocumentTypeListView, APIDocumentTypeView,
|
||||
APIDocumentVersionCreateView, APIDocumentVersionView,
|
||||
APINewDocumentView)
|
||||
APIDocumentVersionCreateView, APIDocumentVersionView)
|
||||
from .settings import PRINT_SIZE, DISPLAY_SIZE
|
||||
from .views import DocumentListView
|
||||
|
||||
@@ -74,7 +73,6 @@ urlpatterns = patterns('documents.views',
|
||||
|
||||
api_urls = patterns('',
|
||||
url(r'^documents/$', APIDocumentListView.as_view(), name='document-list'),
|
||||
url(r'^documents/new/$', APINewDocumentView.as_view(), name='newdocument-view'),
|
||||
url(r'^documents/(?P<pk>[0-9]+)/$', APIDocumentView.as_view(), name='document-detail'),
|
||||
url(r'^document_version/(?P<pk>[0-9]+)/$', APIDocumentVersionView.as_view(), name='documentversion-detail'),
|
||||
url(r'^document_page/(?P<pk>[0-9]+)/$', APIDocumentPageView.as_view(), name='documentpage-detail'),
|
||||
|
||||
@@ -4,10 +4,13 @@ import datetime
|
||||
import logging
|
||||
import re
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db.models import Q
|
||||
from django.db.models.loading import get_model
|
||||
|
||||
from acls.models import AccessEntry
|
||||
from common.utils import load_backend
|
||||
from permissions.models import Permission
|
||||
|
||||
from .settings import LIMIT
|
||||
|
||||
@@ -84,7 +87,7 @@ class SearchModel(object):
|
||||
"""
|
||||
return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)]
|
||||
|
||||
def simple_search(self, query_string):
|
||||
def simple_search(self, query_string, user):
|
||||
search_dict = {}
|
||||
|
||||
for search_field in self.get_all_search_fields():
|
||||
@@ -102,9 +105,9 @@ class SearchModel(object):
|
||||
|
||||
logger.debug('search_dict: %s', search_dict)
|
||||
|
||||
return self.execute_search(search_dict, global_and_search=False)
|
||||
return self.execute_search(search_dict, user=user, global_and_search=False)
|
||||
|
||||
def advanced_search(self, dictionary):
|
||||
def advanced_search(self, dictionary, user):
|
||||
search_dict = {}
|
||||
|
||||
for key, value in dictionary.items():
|
||||
@@ -130,9 +133,9 @@ class SearchModel(object):
|
||||
|
||||
logger.debug('search_dict: %s', search_dict)
|
||||
|
||||
return self.execute_search(search_dict, global_and_search=True)
|
||||
return self.execute_search(search_dict, user=user, global_and_search=True)
|
||||
|
||||
def execute_search(self, search_dict, global_and_search=False):
|
||||
def execute_search(self, search_dict, user, global_and_search=False):
|
||||
elapsed_time = 0
|
||||
start_time = datetime.datetime.now()
|
||||
result_set = set()
|
||||
@@ -183,7 +186,15 @@ class SearchModel(object):
|
||||
|
||||
elapsed_time = unicode(datetime.datetime.now() - start_time).split(':')[2]
|
||||
|
||||
return self.model.objects.in_bulk(list(result_set)[: LIMIT]).values(), result_set, elapsed_time
|
||||
queryset = self.model.objects.in_bulk(list(result_set)[: LIMIT]).values()
|
||||
|
||||
if self.permission:
|
||||
try:
|
||||
Permission.objects.check_permissions(user, [self.permission])
|
||||
except PermissionDenied:
|
||||
queryset = AccessEntry.objects.filter_objects_by_access(self.permission, user, queryset)
|
||||
|
||||
return queryset, result_set, elapsed_time
|
||||
|
||||
def assemble_query(self, terms, search_fields):
|
||||
"""
|
||||
|
||||
@@ -44,12 +44,6 @@ def results(request, extra_context=None):
|
||||
logger.debug('advanced search')
|
||||
queryset, ids, timedelta = document_search.advanced_search(request.GET)
|
||||
|
||||
if document_search.permission:
|
||||
try:
|
||||
Permission.objects.check_permissions(request.user, [document_search.permission])
|
||||
except PermissionDenied:
|
||||
queryset = AccessEntry.objects.filter_objects_by_access(document_search.permission, request.user, queryset)
|
||||
|
||||
# Update the context with the search results
|
||||
context.update({
|
||||
'object_list': queryset,
|
||||
@@ -57,7 +51,7 @@ def results(request, extra_context=None):
|
||||
'title': _(u'Results'),
|
||||
})
|
||||
|
||||
RecentSearch.objects.add_query_for_user(request.user, request.GET, len(ids))
|
||||
RecentSearch.objects.add_query_for_user(request.user, document_search, request.GET, len(ids))
|
||||
|
||||
if extra_context:
|
||||
context.update(extra_context)
|
||||
|
||||
Reference in New Issue
Block a user