From e9a365333f27a29bf1a80c5d65ba9ec445053310 Mon Sep 17 00:00:00 2001 From: Michael Price Date: Mon, 19 Mar 2018 17:02:43 -0400 Subject: [PATCH] Rename the request_data variable to querystring for clarity. Turn the querystring into an IRI to support international characters in metadata values and allow JSON serializer for Celery. Signed-off-by: Michael Price --- mayan/apps/metadata/api.py | 30 +++++++++++++++----------- mayan/apps/metadata/wizard_steps.py | 8 ++++--- mayan/apps/sources/models.py | 10 ++++----- mayan/apps/sources/tasks.py | 12 +++++------ mayan/apps/sources/tests/test_views.py | 5 +++-- mayan/apps/sources/views.py | 8 +++++-- mayan/apps/sources/wizards.py | 9 +++++--- mayan/apps/tags/wizard_steps.py | 8 +++---- 8 files changed, 51 insertions(+), 39 deletions(-) diff --git a/mayan/apps/metadata/api.py b/mayan/apps/metadata/api.py index 32ac06117d..1613ec234c 100644 --- a/mayan/apps/metadata/api.py +++ b/mayan/apps/metadata/api.py @@ -1,12 +1,15 @@ from __future__ import unicode_literals +from furl import furl + from django.shortcuts import get_object_or_404 +from django.utils.encoding import force_text from django.utils.six.moves.urllib.parse import unquote_plus from .models import DocumentMetadata, MetadataType -def decode_metadata_from_url(url_dict): +def decode_metadata_from_querystring(querystring=None): """ Parse a URL query string to a list of metadata """ @@ -15,19 +18,20 @@ def decode_metadata_from_url(url_dict): 'value': {} } metadata_list = [] - # Match out of order metadata_type ids with metadata values from request - for key, value in url_dict.items(): - if 'metadata' in key: - index, element = key[8:].split('_') - metadata_dict[element][index] = value + if querystring: + # Match out of order metadata_type ids with metadata values from request + for key, value in furl(querystring).args.items(): + if 'metadata' in key: + index, element = key[8:].split('_') + metadata_dict[element][index] = value - # Convert the nested dictionary into a list of id+values dictionaries - for order, identifier in metadata_dict['id'].items(): - if order in metadata_dict['value'].keys(): - metadata_list.append({ - 'id': identifier, - 'value': metadata_dict['value'][order] - }) + # Convert the nested dictionary into a list of id+values dictionaries + for order, identifier in metadata_dict['id'].items(): + if order in metadata_dict['value'].keys(): + metadata_list.append({ + 'id': identifier, + 'value': metadata_dict['value'][order] + }) return metadata_list diff --git a/mayan/apps/metadata/wizard_steps.py b/mayan/apps/metadata/wizard_steps.py index 4eb7c5cc4c..e8feedee85 100644 --- a/mayan/apps/metadata/wizard_steps.py +++ b/mayan/apps/metadata/wizard_steps.py @@ -2,7 +2,9 @@ from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ -from metadata.api import decode_metadata_from_url, save_metadata_list +from metadata.api import ( + decode_metadata_from_querystring, save_metadata_list +) from metadata.forms import DocumentMetadataFormSet from sources.wizards import WizardStep, WizardStepDocumentType @@ -56,8 +58,8 @@ class WizardStepMetadata(WizardStep): return result @classmethod - def step_post_upload_process(cls, document, request_data=None): - metadata_dict_list = decode_metadata_from_url(url_dict=request_data) + def step_post_upload_process(cls, document, querystring=None): + metadata_dict_list = decode_metadata_from_querystring(querystring=querystring) if metadata_dict_list: save_metadata_list( metadata_list=metadata_dict_list, document=document, diff --git a/mayan/apps/sources/models.py b/mayan/apps/sources/models.py index f06625d057..e9bdca400b 100644 --- a/mayan/apps/sources/models.py +++ b/mayan/apps/sources/models.py @@ -101,22 +101,22 @@ class Source(models.Model): compressed_file = CompressedFile(file_object) for compressed_file_child in compressed_file.children(): kwargs.update({'label': force_text(compressed_file_child)}) - return self.upload_document( + self.upload_document( file_object=File(compressed_file_child), **kwargs ) compressed_file_child.close() except NotACompressedFile: logging.debug('Exception: NotACompressedFile') - return self.upload_document(file_object=file_object, **kwargs) + self.upload_document(file_object=file_object, **kwargs) else: - return self.upload_document(file_object=file_object, **kwargs) + self.upload_document(file_object=file_object, **kwargs) def get_upload_file_object(self, form_data): pass # TODO: Should raise NotImplementedError? - def upload_document(self, file_object, document_type, description=None, label=None, language=None, request_data=None, user=None): + def upload_document(self, file_object, document_type, description=None, label=None, language=None, querystring=None, user=None): """ Upload an individual document """ @@ -158,7 +158,7 @@ class Source(models.Model): raise else: WizardStep.post_upload_process( - document=document, request_data=request_data + document=document, querystring=querystring ) return document diff --git a/mayan/apps/sources/tasks.py b/mayan/apps/sources/tasks.py index e0d0e714e4..af1b73f57f 100644 --- a/mayan/apps/sources/tasks.py +++ b/mayan/apps/sources/tasks.py @@ -36,7 +36,7 @@ def task_check_interval_source(source_id): @app.task(bind=True, default_retry_delay=DEFAULT_SOURCE_TASK_RETRY_DELAY, ignore_result=True) -def task_upload_document(self, source_id, document_type_id, shared_uploaded_file_id, description=None, label=None, language=None, request_data=None, user_id=None): +def task_upload_document(self, source_id, document_type_id, shared_uploaded_file_id, description=None, label=None, language=None, querystring=None, user_id=None): SharedUploadedFile = apps.get_model( app_label='common', model_name='SharedUploadedFile' ) @@ -65,7 +65,7 @@ def task_upload_document(self, source_id, document_type_id, shared_uploaded_file source.upload_document( file_object=file_object, document_type=document_type, description=description, label=label, language=language, - request_data=request_data, user=user, + querystring=querystring, user=user, ) except OperationalError as exception: @@ -86,7 +86,7 @@ def task_upload_document(self, source_id, document_type_id, shared_uploaded_file @app.task(bind=True, default_retry_delay=DEFAULT_SOURCE_TASK_RETRY_DELAY, ignore_result=True) -def task_source_handle_upload(self, document_type_id, shared_uploaded_file_id, source_id, description=None, expand=False, label=None, language=None, skip_list=None, request_data=None, user_id=None): +def task_source_handle_upload(self, document_type_id, shared_uploaded_file_id, source_id, description=None, expand=False, label=None, language=None, querystring=None, skip_list=None, user_id=None): SharedUploadedFile = apps.get_model( app_label='common', model_name='SharedUploadedFile' ) @@ -113,8 +113,8 @@ def task_source_handle_upload(self, document_type_id, shared_uploaded_file_id, s kwargs = { 'description': description, 'document_type_id': document_type.pk, - 'label': label, 'language': language, - 'source_id': source_id, 'request_data': request_data, 'user_id': user_id + 'label': label, 'language': language, 'querystring': querystring, + 'source_id': source_id, 'user_id': user_id } if not skip_list: @@ -150,7 +150,7 @@ def task_source_handle_upload(self, document_type_id, shared_uploaded_file_id, s source_id=source_id, description=description, expand=expand, label=label, language=language, - skip_list=skip_list, request_data=request_data, + skip_list=skip_list, querystring=querystring, user_id=user_id ) return diff --git a/mayan/apps/sources/tests/test_views.py b/mayan/apps/sources/tests/test_views.py index 1f99200a92..20c40209b5 100644 --- a/mayan/apps/sources/tests/test_views.py +++ b/mayan/apps/sources/tests/test_views.py @@ -147,12 +147,13 @@ class DocumentUploadMetadataTestCase(MetadataTypeMixin, GenericDocumentViewTestC # Upload the test document with open(TEST_SMALL_DOCUMENT_PATH) as file_descriptor: - self.post( + response = self.post( path=url, data={ 'document-language': 'eng', 'source-file': file_descriptor, 'document_type_id': self.document_type.pk, - }, follow=True + } ) + self.assertEqual(response.status_code, 302) self.assertEqual(Document.objects.count(), 1) self.assertEqual( Document.objects.first().metadata.first().value, diff --git a/mayan/apps/sources/views.py b/mayan/apps/sources/views.py index bc852aca22..996e0580b2 100644 --- a/mayan/apps/sources/views.py +++ b/mayan/apps/sources/views.py @@ -4,7 +4,7 @@ from django.contrib import messages from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404 from django.urls import reverse, reverse_lazy -from django.utils.encoding import force_text +from django.utils.encoding import force_text, uri_to_iri from django.utils.translation import ugettext_lazy as _ from acls.models import AccessControlList @@ -248,7 +248,11 @@ class UploadInteractiveView(UploadBaseView): expand=expand, label=label, language=forms['document_form'].cleaned_data.get('language'), - request_data=self.request.GET, + querystring=uri_to_iri( + '?{}&{}'.format( + self.request.GET.urlencode(), self.request.POST.urlencode() + ) + ), shared_uploaded_file_id=shared_uploaded_file.pk, source_id=self.source.pk, user_id=user_id, diff --git a/mayan/apps/sources/wizards.py b/mayan/apps/sources/wizards.py index 6aab3e10c9..48ef207f79 100644 --- a/mayan/apps/sources/wizards.py +++ b/mayan/apps/sources/wizards.py @@ -47,16 +47,18 @@ class WizardStep(object): return {} @classmethod - def post_upload_process(cls, document, request_data=None): + def post_upload_process(cls, document, querystring=None): for step in cls.get_all(): - step.step_post_upload_process(document=document, request_data=request_data) + step.step_post_upload_process( + document=document, querystring=querystring + ) @classmethod def register(cls, step): cls._registry[step.name] = step @classmethod - def step_post_upload_process(cls, document, request_data=None): + def step_post_upload_process(cls, document, querystring=None): pass @@ -158,4 +160,5 @@ class DocumentCreateWizard(SessionWizardView): urlencode(query_dict, doseq=True) ] ) + return HttpResponseRedirect(url) diff --git a/mayan/apps/tags/wizard_steps.py b/mayan/apps/tags/wizard_steps.py index 7bcf07ad48..5ad9a3e8c2 100644 --- a/mayan/apps/tags/wizard_steps.py +++ b/mayan/apps/tags/wizard_steps.py @@ -41,13 +41,11 @@ class WizardStepTags(WizardStep): return result @classmethod - def step_post_upload_process(cls, document, request_data=None): - f = furl(request_data) - f.args.getlist('tags') - + def step_post_upload_process(cls, document, querystring=None): + furl_instance = furl(querystring) Tag = apps.get_model(app_label='tags', model_name='Tag') - for tag in Tag.objects.filter(pk__in=request_data.getlist('tags')): + for tag in Tag.objects.filter(pk__in=furl_instance.args.getlist('tags')): tag.documents.add(document)