diff --git a/apps/documents/forms.py b/apps/documents/forms.py index bfd4eb0d21..6963280e2f 100644 --- a/apps/documents/forms.py +++ b/apps/documents/forms.py @@ -186,21 +186,11 @@ class DocumentForm(forms.ModelForm): queryset=filenames_qs, required=False, label=_(u'Quick document rename')) - - # Put the expand field last in the field order list - expand_field_index = self.fields.keyOrder.index('expand') - expand_field = self.fields.keyOrder.pop(expand_field_index) - self.fields.keyOrder.append(expand_field) new_filename = forms.CharField( label=_('New document filename'), required=False ) - - expand = forms.BooleanField( - label=_(u'Expand compressed files'), required=False, - help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents') - ) - + class DocumentForm_edit(DocumentForm): """ @@ -265,32 +255,6 @@ class PrintForm(forms.Form): page_orientation = forms.ChoiceField(choices=PAGE_ORIENTATION_CHOICES, initial=DEFAULT_PAGE_ORIENTATION, label=_(u'Page orientation'), required=True) page_range = forms.CharField(label=_(u'Page range'), required=False) -''' -class StagingDocumentForm(DocumentForm): - """ - Form that show all the files in the staging folder specified by the - StagingFile class passed as 'cls' argument - """ - def __init__(self, *args, **kwargs): - cls = kwargs.pop('cls') - super(StagingDocumentForm, self).__init__(*args, **kwargs) - try: - self.fields['staging_file_id'].choices = [ - (staging_file.id, staging_file) for staging_file in cls.get_all() - ] - except: - pass - - # Put staging_list field first in the field order list - staging_list_index = self.fields.keyOrder.index('staging_file_id') - staging_list = self.fields.keyOrder.pop(staging_list_index) - self.fields.keyOrder.insert(0, staging_list) - - staging_file_id = forms.ChoiceField(label=_(u'Staging file')) - - class Meta(DocumentForm.Meta): - exclude = ('description', 'file', 'document_type', 'tags') -''' class DocumentTypeForm(forms.ModelForm): """ diff --git a/apps/documents/views.py b/apps/documents/views.py index 594009b1b2..817164c693 100644 --- a/apps/documents/views.py +++ b/apps/documents/views.py @@ -1,5 +1,4 @@ import os -import zipfile import urlparse import copy @@ -13,7 +12,6 @@ from django.core.urlresolvers import reverse from django.views.generic.create_update import delete_object, update_object from django.conf import settings from django.utils.http import urlencode -from django.core.files.uploadedfile import SimpleUploadedFile import sendfile from common.utils import pretty_size, parse_range, urlquote, \ @@ -112,167 +110,6 @@ def document_create_siblings(request, document_id): url = reverse('upload_interactive') return HttpResponseRedirect('%s?%s' % (url, urlencode(query_dict))) -''' -def _handle_save_document(request, document, form=None): - RecentDocument.objects.add_document_for_user(request.user, document) - - if form: - if form.cleaned_data['new_filename']: - document.file_filename = form.cleaned_data['new_filename'] - document.save() - - if form and 'document_type_available_filenames' in form.cleaned_data: - if form.cleaned_data['document_type_available_filenames']: - document.file_filename = form.cleaned_data['document_type_available_filenames'].filename - document.save() - - save_metadata_list(decode_metadata_from_url(request.GET), document, create=True) - - warnings = update_indexes(document) - if request.user.is_staff or request.user.is_superuser: - for warning in warnings: - messages.warning(request, warning) - - create_history(HISTORY_DOCUMENT_CREATED, document, {'user': request.user}) - - -def _handle_zip_file(request, uploaded_file, document_type=None): - filename = getattr(uploaded_file, 'filename', getattr(uploaded_file, 'name', '')) - if filename.lower().endswith('zip'): - zfobj = zipfile.ZipFile(uploaded_file) - for filename in zfobj.namelist(): - if not filename.endswith('/'): - zip_document = Document(file=SimpleUploadedFile( - name=filename, content=zfobj.read(filename))) - if document_type: - zip_document.document_type = document_type - zip_document.save() - _handle_save_document(request, zip_document) - messages.success(request, _(u'Extracted file: %s, uploaded successfully.') % filename) - #Signal that uploaded file was a zip file - return True - else: - #Otherwise tell parent to handle file - return False - - -def upload_document_with_type(request, source): - check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE]) - - document_type_id = request.GET.get('document_type_id', None) - if document_type_id: - document_type = get_object_or_404(DocumentType, pk=document_type_id[0]) - else: - document_type = None - - if request.method == 'POST': - if source == UPLOAD_SOURCE_LOCAL: - form = DocumentForm(request.POST, request.FILES, document_type=document_type) - if form.is_valid(): - try: - expand = form.cleaned_data['expand'] - if (not expand) or (expand and not _handle_zip_file(request, request.FILES['file'], document_type)): - instance = form.save() - instance.save() - if document_type: - instance.document_type = document_type - _handle_save_document(request, instance, form) - messages.success(request, _(u'Document uploaded successfully.')) - except Exception, e: - messages.error(request, e) - - return HttpResponseRedirect(request.get_full_path()) - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - StagingFile = create_staging_file_class(request, source) - form = StagingDocumentForm(request.POST, - request.FILES, cls=StagingFile, - document_type=document_type) - if form.is_valid(): - try: - staging_file = StagingFile.get(form.cleaned_data['staging_file_id']) - expand = form.cleaned_data['expand'] - if (not expand) or (expand and not _handle_zip_file(request, staging_file.upload(), document_type)): - document = Document(file=staging_file.upload()) - if document_type: - document.document_type = document_type - document.save() - _handle_save_document(request, document, form) - messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename) - - if DELETE_STAGING_FILE_AFTER_UPLOAD: - staging_file.delete() - messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename) - except Exception, e: - messages.error(request, e) - - return HttpResponseRedirect(request.META['HTTP_REFERER']) - else: - if source == UPLOAD_SOURCE_LOCAL: - form = DocumentForm(document_type=document_type) - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - StagingFile = create_staging_file_class(request, source) - form = StagingDocumentForm(cls=StagingFile, - document_type=document_type) - - subtemplates_list = [] - - if source == UPLOAD_SOURCE_LOCAL: - subtemplates_list.append({ - 'name': 'generic_form_subtemplate.html', - 'context': { - 'form': form, - 'title': _(u'upload a local document'), - }, - }) - - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - if source == UPLOAD_SOURCE_STAGING: - form_title = _(u'upload a document from staging') - list_title = _(u'files in staging') - else: - form_title = _(u'upload a document from user staging') - list_title = _(u'files in user staging') - try: - staging_filelist = StagingFile.get_all() - except Exception, e: - messages.error(request, e) - staging_filelist = [] - finally: - subtemplates_list = [ - { - 'name': 'generic_form_subtemplate.html', - 'context': { - 'form': form, - 'title': form_title, - } - }, - { - 'name': 'generic_list_subtemplate.html', - 'context': { - 'title': list_title, - 'object_list': staging_filelist, - 'hide_link': True, - } - }, - ] - - context = { - 'source': source, - 'document_type_id': document_type_id, - 'subtemplates_list': subtemplates_list, - 'sidebar_subtemplates_list': [ - { - 'name': 'generic_subtemplate.html', - 'context': { - 'title': _(u'Current metadata'), - 'paragraphs': metadata_repr_as_list(decode_metadata_from_url(request.GET)), - 'side_bar': True, - } - }] - } - return render_to_response('generic_form.html', context, - context_instance=RequestContext(request)) -''' def document_view(request, document_id, advanced=False): check_permissions(request.user, [PERMISSION_DOCUMENT_VIEW]) diff --git a/apps/navigation/templatetags/navigation_tags.py b/apps/navigation/templatetags/navigation_tags.py index f849f040f7..d4c73b781b 100644 --- a/apps/navigation/templatetags/navigation_tags.py +++ b/apps/navigation/templatetags/navigation_tags.py @@ -115,6 +115,9 @@ def resolve_links(context, links, current_view, current_path, parsed_query_strin new_link['url'] = urlquote(new_link['url'], parsed_query_string) else: new_link['active'] = False + + if 'conditional_highlight' in link: + new_link['active'] = link['conditional_highlight'](context) if 'conditional_disable' in link: new_link['disabled'] = link['conditional_disable'](context) diff --git a/apps/sources/__init__.py b/apps/sources/__init__.py index aa31d60f1d..fcd0f52fa0 100644 --- a/apps/sources/__init__.py +++ b/apps/sources/__init__.py @@ -8,10 +8,6 @@ from navigation.api import register_links, register_top_menu, \ from sources.staging import StagingFile -upload_document_from_local = {'text': _(u'local'), 'view': 'upload_document_from_local', 'famfam': 'drive_disk', 'keep_query': True} -upload_document_from_staging = {'text': _(u'staging'), 'view': 'upload_document_from_staging', 'famfam': 'drive_network', 'keep_query': True}#, 'condition': lambda x: USE_STAGING_DIRECTORY} -upload_document_from_user_staging = {'text': _(u'user staging'), 'view': 'upload_document_from_user_staging', 'famfam': 'drive_user', 'keep_query': True}#, 'condition': lambda x: PER_USER_STAGING_DIRECTORY} - staging_file_preview = {'text': _(u'preview'), 'class': 'fancybox-noscaling', 'view': 'staging_file_preview', 'args': ['source.source_type', 'source.pk', 'object.id'], 'famfam': 'zoom'} staging_file_delete = {'text': _(u'delete'), 'view': 'staging_file_delete', 'args': ['source.source_type', 'source.pk', 'object.id'], 'famfam': 'delete'} diff --git a/apps/sources/forms.py b/apps/sources/forms.py index 48f2a379c3..3757af3854 100644 --- a/apps/sources/forms.py +++ b/apps/sources/forms.py @@ -20,6 +20,7 @@ class StagingDocumentForm(DocumentForm): """ def __init__(self, *args, **kwargs): cls = kwargs.pop('cls') + show_expand = kwargs.pop('show_expand', False) super(StagingDocumentForm, self).__init__(*args, **kwargs) try: self.fields['staging_file_id'].choices = [ @@ -28,6 +29,12 @@ class StagingDocumentForm(DocumentForm): except: pass + if show_expand: + self.fields['expand'] = forms.BooleanField( + label=_(u'Expand compressed files'), required=False, + help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents') + ) + # Put staging_list field first in the field order list staging_list_index = self.fields.keyOrder.index('staging_file_id') staging_list = self.fields.keyOrder.pop(staging_list_index) @@ -37,3 +44,15 @@ class StagingDocumentForm(DocumentForm): class Meta(DocumentForm.Meta): exclude = ('description', 'file', 'document_type', 'tags') + + +class WebFormForm(DocumentForm): + def __init__(self, *args, **kwargs): + show_expand = kwargs.pop('show_expand', False) + super(WebFormForm, self).__init__(*args, **kwargs) + + if show_expand: + self.fields['expand'] = forms.BooleanField( + label=_(u'Expand compressed files'), required=False, + help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents') + ) diff --git a/apps/sources/models.py b/apps/sources/models.py index 54cb97436d..a755ed9ffa 100644 --- a/apps/sources/models.py +++ b/apps/sources/models.py @@ -5,19 +5,19 @@ from documents.models import DocumentType from metadata.models import MetadataType -SOURCE_UMCOMPRESS_CHOICE_Y = 'y' -SOURCE_UMCOMPRESS_CHOICE_N = 'n' -SOURCE_UMCOMPRESS_CHOICE_ASK = 'a' +SOURCE_UNCOMPRESS_CHOICE_Y = 'y' +SOURCE_UNCOMPRESS_CHOICE_N = 'n' +SOURCE_UNCOMPRESS_CHOICE_ASK = 'a' SOURCE_UNCOMPRESS_CHOICES = ( - (SOURCE_UMCOMPRESS_CHOICE_Y, _(u'Yes')), - (SOURCE_UMCOMPRESS_CHOICE_N, _(u'No')), + (SOURCE_UNCOMPRESS_CHOICE_Y, _(u'Yes')), + (SOURCE_UNCOMPRESS_CHOICE_N, _(u'No')), ) SOURCE_INTERACTIVE_UNCOMPRESS_CHOICES = ( - (SOURCE_UMCOMPRESS_CHOICE_Y, _(u'Yes')), - (SOURCE_UMCOMPRESS_CHOICE_N, _(u'No')), - (SOURCE_UMCOMPRESS_CHOICE_ASK, _(u'Ask')) + (SOURCE_UNCOMPRESS_CHOICE_Y, _(u'Yes')), + (SOURCE_UNCOMPRESS_CHOICE_N, _(u'No')), + (SOURCE_UNCOMPRESS_CHOICE_ASK, _(u'Ask')) ) SOURCE_ICON_DISK = 'disk' diff --git a/apps/sources/views.py b/apps/sources/views.py index 8bcb5b9434..13fdf6932b 100644 --- a/apps/sources/views.py +++ b/apps/sources/views.py @@ -1,4 +1,5 @@ import os +import zipfile from django.http import HttpResponseRedirect from django.shortcuts import render_to_response, get_object_or_404 @@ -7,6 +8,7 @@ from django.contrib import messages from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.conf import settings +from django.core.files.uploadedfile import SimpleUploadedFile from converter.exceptions import UnkownConvertError, UnknownFormat from documents.literals import PICTURE_ERROR_SMALL, PICTURE_ERROR_MEDIUM, \ @@ -22,14 +24,12 @@ from metadata.forms import MetadataFormSet, MetadataSelectionForm from permissions.api import check_permissions import sendfile -#TEMP -from documents.forms import DocumentForm -#TEMP - from sources.models import WebForm, StagingFolder from sources.models import SOURCE_CHOICE_WEB_FORM, SOURCE_CHOICE_STAGING +from sources.models import SOURCE_UNCOMPRESS_CHOICE_Y, \ + SOURCE_UNCOMPRESS_CHOICE_N, SOURCE_UNCOMPRESS_CHOICE_ASK from sources.staging import create_staging_file_class -from sources.forms import StagingDocumentForm +from sources.forms import StagingDocumentForm, WebFormForm def upload_interactive(request, source_type=None, source_id=None): @@ -48,7 +48,8 @@ def upload_interactive(request, source_type=None, source_id=None): 'view': 'upload_interactive', 'args': [u'"%s"' % web_form.source_type, web_form.pk], 'famfam': web_form.icon, - 'keep_query': True + 'keep_query': True, + 'conditional_highlight': lambda context: context['source'].source_type == web_form.source_type and context['source'].pk == web_form.pk, }) staging_folders = StagingFolder.objects.filter(enabled=True) @@ -58,7 +59,8 @@ def upload_interactive(request, source_type=None, source_id=None): 'view': 'upload_interactive', 'args': [u'"%s"' % staging_folder.source_type, staging_folder.pk], 'famfam': staging_folder.icon, - 'keep_query': True + 'keep_query': True, + 'conditional_highlight': lambda context: context['source'].source_type == staging_folder.source_type and context['source'].pk == staging_folder.pk, }) if web_forms.count() == 0 and staging_folders.count() == 0: @@ -90,16 +92,24 @@ def upload_interactive(request, source_type=None, source_id=None): source_type = staging_folders[0].source_type source_id = staging_folders[0].pk - if source_type and source_id: if source_type == SOURCE_CHOICE_WEB_FORM: web_form = get_object_or_404(WebForm, pk=source_id) context['source'] = web_form if request.method == 'POST': - form = DocumentForm(request.POST, request.FILES, document_type=document_type) + form = WebFormForm(request.POST, request.FILES, + document_type=document_type, + show_expand=(web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK) + ) if form.is_valid(): try: - expand = form.cleaned_data['expand'] + if web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK: + expand = form.cleaned_data['expand'] + else: + if web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_Y: + expand = True + else: + expand = False if (not expand) or (expand and not _handle_zip_file(request, request.FILES['file'], document_type)): instance = form.save() instance.save() @@ -112,7 +122,7 @@ def upload_interactive(request, source_type=None, source_id=None): return HttpResponseRedirect(request.get_full_path()) - form = DocumentForm(document_type=document_type) + form = WebFormForm(show_expand=(web_form.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK), document_type=document_type) subtemplates_list.append({ 'name': 'generic_form_subtemplate.html', @@ -127,11 +137,19 @@ def upload_interactive(request, source_type=None, source_id=None): StagingFile = create_staging_file_class(request, staging_folder.folder_path) if request.method == 'POST': form = StagingDocumentForm(request.POST, request.FILES, - cls=StagingFile, document_type=document_type) + cls=StagingFile, document_type=document_type, + show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK) + ) if form.is_valid(): try: staging_file = StagingFile.get(form.cleaned_data['staging_file_id']) - expand = form.cleaned_data['expand'] + if staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK: + expand = form.cleaned_data['expand'] + else: + if staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_Y: + expand = True + else: + expand = False if (not expand) or (expand and not _handle_zip_file(request, staging_file.upload(), document_type)): document = Document(file=staging_file.upload()) if document_type: @@ -141,17 +159,17 @@ def upload_interactive(request, source_type=None, source_id=None): messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename) if staging_folder.delete_after_upload: - staging_file.delete() + staging_file.delete(staging_folder.get_preview_size()) messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename) except Exception, e: messages.error(request, e) - #return HttpResponseRedirect(request.META['HTTP_REFERER']) return HttpResponseRedirect(request.get_full_path()) - form = StagingDocumentForm(cls=StagingFile, - document_type=document_type) + document_type=document_type, + show_expand=(staging_folder.uncompress==SOURCE_UNCOMPRESS_CHOICE_ASK) + ) try: staging_filelist = StagingFile.get_all() except Exception, e: @@ -176,7 +194,6 @@ def upload_interactive(request, source_type=None, source_id=None): }, ] - context.update({ 'document_type_id': document_type_id, 'subtemplates_list': subtemplates_list, @@ -238,124 +255,6 @@ def _handle_zip_file(request, uploaded_file, document_type=None): return False -def upload_document_with_type(request, source): - check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE]) - - document_type_id = request.GET.get('document_type_id', None) - if document_type_id: - document_type = get_object_or_404(DocumentType, pk=document_type_id[0]) - else: - document_type = None - - if request.method == 'POST': - if source == UPLOAD_SOURCE_LOCAL: - form = DocumentForm(request.POST, request.FILES, document_type=document_type) - if form.is_valid(): - try: - expand = form.cleaned_data['expand'] - if (not expand) or (expand and not _handle_zip_file(request, request.FILES['file'], document_type)): - instance = form.save() - instance.save() - if document_type: - instance.document_type = document_type - _handle_save_document(request, instance, form) - messages.success(request, _(u'Document uploaded successfully.')) - except Exception, e: - messages.error(request, e) - - return HttpResponseRedirect(request.get_full_path()) - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - StagingFile = create_staging_file_class(request, source) - form = StagingDocumentForm(request.POST, - request.FILES, cls=StagingFile, - document_type=document_type) - if form.is_valid(): - try: - staging_file = StagingFile.get(form.cleaned_data['staging_file_id']) - expand = form.cleaned_data['expand'] - if (not expand) or (expand and not _handle_zip_file(request, staging_file.upload(), document_type)): - document = Document(file=staging_file.upload()) - if document_type: - document.document_type = document_type - document.save() - _handle_save_document(request, document, form) - messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename) - - if DELETE_STAGING_FILE_AFTER_UPLOAD: - staging_file.delete() - messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename) - except Exception, e: - messages.error(request, e) - - return HttpResponseRedirect(request.META['HTTP_REFERER']) - else: - if source == UPLOAD_SOURCE_LOCAL: - form = DocumentForm(document_type=document_type) - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - StagingFile = create_staging_file_class(request, source) - form = StagingDocumentForm(cls=StagingFile, - document_type=document_type) - - subtemplates_list = [] - - if source == UPLOAD_SOURCE_LOCAL: - subtemplates_list.append({ - 'name': 'generic_form_subtemplate.html', - 'context': { - 'form': form, - 'title': _(u'upload a local document'), - }, - }) - - elif (USE_STAGING_DIRECTORY and source == UPLOAD_SOURCE_STAGING) or (PER_USER_STAGING_DIRECTORY and source == UPLOAD_SOURCE_USER_STAGING): - if source == UPLOAD_SOURCE_STAGING: - form_title = _(u'upload a document from staging') - list_title = _(u'files in staging') - else: - form_title = _(u'upload a document from user staging') - list_title = _(u'files in user staging') - try: - staging_filelist = StagingFile.get_all() - except Exception, e: - messages.error(request, e) - staging_filelist = [] - finally: - subtemplates_list = [ - { - 'name': 'generic_form_subtemplate.html', - 'context': { - 'form': form, - 'title': form_title, - } - }, - { - 'name': 'generic_list_subtemplate.html', - 'context': { - 'title': list_title, - 'object_list': staging_filelist, - 'hide_link': True, - } - }, - ] - - context = { - 'source': source, - 'document_type_id': document_type_id, - 'subtemplates_list': subtemplates_list, - 'sidebar_subtemplates_list': [ - { - 'name': 'generic_subtemplate.html', - 'context': { - 'title': _(u'Current metadata'), - 'paragraphs': metadata_repr_as_list(decode_metadata_from_url(request.GET)), - 'side_bar': True, - } - }] - } - return render_to_response('generic_form.html', context, - context_instance=RequestContext(request)) - - def staging_file_preview(request, source_type, source_id, staging_file_id): check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE]) staging_folder = get_object_or_404(StagingFolder, pk=source_id)