diff --git a/apps/documents/conf/settings.py b/apps/documents/conf/settings.py index 60f229fb5e..14c08a31ad 100644 --- a/apps/documents/conf/settings.py +++ b/apps/documents/conf/settings.py @@ -29,6 +29,9 @@ register_settings( # Upload {'name': u'USE_STAGING_DIRECTORY', 'global_name': u'DOCUMENTS_USE_STAGING_DIRECTORY', 'default': False}, {'name': u'STAGING_DIRECTORY', 'global_name': u'DOCUMENTS_STAGING_DIRECTORY', 'default': u'/tmp/mayan/staging', 'exists': True}, + {'name': u'PER_USER_STAGING_DIRECTORY', 'global_name': u'DOCUMENTS_PER_USER_STAGING_DIRECTORY', 'default': False}, + {'name': u'USER_STAGING_DIRECTORY_ROOT', 'global_name': u'DOCUMENTS_USER_STAGING_DIRECTORY_ROOT', 'default': u'/tmp/mayan/staging/users', 'exists': True}, + {'name': u'USER_STAGING_DIRECTORY_EXPRESSION', 'global_name': u'DOCUMENTS_USER_STAGING_DIRECTORY_EXPRESSION', 'default': u'user.username'}, {'name': u'DELETE_STAGING_FILE_AFTER_UPLOAD', 'global_name': u'DOCUMENTS_DELETE_STAGING_FILE_AFTER_UPLOAD', 'default': False}, {'name': u'STAGING_FILES_PREVIEW_SIZE', 'global_name': u'DOCUMENTS_STAGING_FILES_PREVIEW_SIZE', 'default': u'640x480'}, {'name': u'ENABLE_SINGLE_DOCUMENT_UPLOAD', 'global_name': u'DOCUMENTS_ENABLE_SINGLE_DOCUMENT_UPLOAD', 'default': True}, diff --git a/apps/documents/forms.py b/apps/documents/forms.py index a81336be5a..613c77e2a5 100644 --- a/apps/documents/forms.py +++ b/apps/documents/forms.py @@ -15,7 +15,6 @@ from common.utils import urlquote from metadata.models import MetadataSet, MetadataType from metadata.forms import MetadataFormSet -from documents.staging import StagingFile from documents.models import Document, DocumentType, \ DocumentPage, DocumentPageTransformation @@ -217,10 +216,11 @@ class DocumentForm_edit(DocumentForm): class StagingDocumentForm(forms.Form): 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 StagingFile.get_all() + (staging_file.id, staging_file) for staging_file in cls.get_all() ] except: pass diff --git a/apps/documents/staging.py b/apps/documents/staging.py index 0a449e6c49..8c0ed517d7 100644 --- a/apps/documents/staging.py +++ b/apps/documents/staging.py @@ -18,24 +18,34 @@ HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() #t1=time.time();func();t2=time.time();print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) -def get_all_files(): +def get_all_files(path): try: - return sorted([os.path.normcase(f) for f in os.listdir(STAGING_DIRECTORY) if os.path.isfile(os.path.join(STAGING_DIRECTORY, f))]) + return sorted([os.path.normcase(f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]) except OSError, exc: raise OSError(ugettext(u'Unable get list of staging files: %s') % exc) - + + +def create_staging_file_class(): + return type('StagingFile', (StagingFile,), dict(StagingFile.__dict__)) + class StagingFile(object): """ Simple class to encapsulate the files in a directory and hide the specifics to the view """ + path = STAGING_DIRECTORY + + @classmethod + def set_path(cls, path): + cls.path = path + @classmethod def get_all(cls): staging_files = [] - for filename in get_all_files(): + for filename in get_all_files(cls.path): staging_files.append(StagingFile( - filepath=os.path.join(STAGING_DIRECTORY, filename))) + filepath=os.path.join(cls.path, filename))) return staging_files diff --git a/apps/documents/views.py b/apps/documents/views.py index fd3096b334..c5bbca1909 100644 --- a/apps/documents/views.py +++ b/apps/documents/views.py @@ -38,6 +38,10 @@ from document_indexing.api import update_indexes, delete_indexes from documents.conf.settings import DELETE_STAGING_FILE_AFTER_UPLOAD from documents.conf.settings import USE_STAGING_DIRECTORY +from documents.conf.settings import PER_USER_STAGING_DIRECTORY +from documents.conf.settings import USER_STAGING_DIRECTORY_ROOT +from documents.conf.settings import USER_STAGING_DIRECTORY_EXPRESSION + from documents.conf.settings import PREVIEW_SIZE from documents.conf.settings import THUMBNAIL_SIZE from documents.conf.settings import UNCOMPRESS_COMPRESSED_LOCAL_FILES @@ -65,7 +69,7 @@ from documents.forms import DocumentTypeSelectForm, DocumentCreateWizard, \ from documents.models import Document, DocumentType, DocumentPage, \ DocumentPageTransformation, RecentDocument -from documents.staging import StagingFile +from documents.staging import create_staging_file_class from documents.literals import PICTURE_ERROR_SMALL, PICTURE_ERROR_MEDIUM, \ PICTURE_UNKNOWN_SMALL, PICTURE_UNKNOWN_MEDIUM @@ -156,10 +160,20 @@ def upload_document_with_type(request, multiple=True): document_type = get_object_or_404(DocumentType, pk=document_type_id) else: document_type = None - + local_form = DocumentForm(prefix='local', initial={'document_type': document_type}) if USE_STAGING_DIRECTORY: - staging_form = StagingDocumentForm(prefix='staging', + StagingFile = create_staging_file_class() + + staging_form = StagingDocumentForm(prefix='staging', + cls=StagingFile, + initial={'document_type': document_type}) + + if PER_USER_STAGING_DIRECTORY: + UserStagingFile = create_staging_file_class() + UserStagingFile.set_path('/tmp/mayan/staging/users/admin') + user_staging_form = StagingDocumentForm(prefix='user_staging', + cls=UserStagingFile, initial={'document_type': document_type}) if request.method == 'POST': @@ -211,38 +225,82 @@ def upload_document_with_type(request, multiple=True): }, } - if USE_STAGING_DIRECTORY: - try: - filelist = StagingFile.get_all() - except Exception, e: - messages.error(request, e) - filelist = [] - finally: - local_upload_form.update({'grid': 6}) - subtemplates_list.append(local_upload_form) - subtemplates_list.append( - { - 'name': 'generic_form_subtemplate.html', - 'grid': 6, - 'grid_clear': True, - 'context': { - 'form': staging_form, - 'title': _(u'upload a document from staging'), - } - }, - ) + if USE_STAGING_DIRECTORY or PER_USER_STAGING_DIRECTORY: + local_upload_form.update({'grid': 12}) + subtemplates_list.append(local_upload_form) + + if PER_USER_STAGING_DIRECTORY: + try: + user_filelist = UserStagingFile.get_all() + except Exception, e: + messages.error(request, e) + filelist = [] + finally: + subtemplates_list.append( + { + 'name': 'generic_form_subtemplate.html', + 'grid': 6, + #'grid_clear': False, + 'context': { + 'form': user_staging_form, + 'title': _(u'upload a document from user staging'), + } + }, + ) + + if USE_STAGING_DIRECTORY: + try: + staging_filelist = StagingFile.get_all() + except Exception, e: + messages.error(request, e) + filelist = [] + finally: + subtemplates_list.append( + { + 'name': 'generic_form_subtemplate.html', + 'grid': 6, + #'grid_clear': False, + 'context': { + 'form': staging_form, + 'title': _(u'upload a document from staging'), + } + }, + ) + + if PER_USER_STAGING_DIRECTORY: subtemplates_list.append( { 'name': 'generic_list_subtemplate.html', + 'grid': 6, + + 'context': { + 'title': _(u'files in user staging'), + 'object_list': user_filelist, + 'hide_link': True, + } + }, + ) + + + if USE_STAGING_DIRECTORY: + subtemplates_list.append( + { + 'name': 'generic_list_subtemplate.html', + 'grid': 6, + 'grid_clear': True, + 'context': { 'title': _(u'files in staging'), - 'object_list': filelist, + 'object_list': staging_filelist, 'hide_link': True, } }, ) + + else: - subtemplates_list.append(local_upload_form) + subtemplates_list.append(local_upload_form) + context = { 'document_type_id': document_type_id, @@ -597,6 +655,7 @@ def document_download(request, document_id): def staging_file_preview(request, staging_file_id): check_permissions(request.user, 'documents', [PERMISSION_DOCUMENT_CREATE]) + StagingFile = create_staging_file_class() try: output_file, errors = StagingFile.get(staging_file_id).preview() @@ -623,6 +682,7 @@ def staging_file_preview(request, staging_file_id): def staging_file_delete(request, staging_file_id): check_permissions(request.user, 'documents', [PERMISSION_DOCUMENT_CREATE]) + StagingFile = create_staging_file_class() staging_file = StagingFile.get(staging_file_id) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None)))