Esempio n. 1
0
def edit_file_delete(request, code):
    """Deletes specified code or revision from system (Marks deleted)

    @param request: is a Django request object
    @param code is a DMS Object() code for view interactions"""
    # Decision of where to go back after or instead of removal
    return_url = reverse('mdtui-home')
    if 'edit_return' in request.session:
        return_url = request.session['edit_return']
    if request.method == 'POST':
        revision = request.POST.get('revision', False)

        if revision:
            return_url = reverse('mdtui-edit-revisions', kwargs={'code': code})
        processor = DocumentProcessor()
        processor.read(code, {'user': request.user, 'only_metadata': True})
        if not processor.errors:
            # Selecting to delete (Mark deleted) revision or whole document
            options = {'user': request.user}
            if revision:
                options['mark_revision_deleted'] = revision
            else:
                options['mark_deleted'] = True
            processor.delete(code, options)
            if not processor.errors:
                request.session['cleanup_caches'] = True
                return HttpResponseRedirect(return_url)
    return HttpResponseRedirect(return_url)
Esempio n. 2
0
def edit_file_delete(request, code):
    """Deletes specified code or revision from system (Marks deleted)

    @param request: is a Django request object
    @param code is a DMS Object() code for view interactions"""
    # Decision of where to go back after or instead of removal
    return_url = reverse('mdtui-home')
    if 'edit_return' in request.session:
        return_url = request.session['edit_return']
    if request.method == 'POST':
        revision = request.POST.get('revision', False)

        if revision:
            return_url = reverse('mdtui-edit-revisions', kwargs={'code': code})
        processor = DocumentProcessor()
        processor.read(code, {'user': request.user, 'only_metadata': True})
        if not processor.errors:
            # Selecting to delete (Mark deleted) revision or whole document
            options = {'user': request.user}
            if revision:
                options['mark_revision_deleted'] = revision
            else:
                options['mark_deleted'] = True
            processor.delete(code, options)
            if not processor.errors:
                request.session['cleanup_caches'] = True
                return HttpResponseRedirect(return_url)
    return HttpResponseRedirect(return_url)
Esempio n. 3
0
 def read(self, request, code, suggested_format=None):
     revision, hashcode, extra = self._get_info(request)
     processor = DocumentProcessor()
     options = {
         'hashcode': hashcode,
         'revision': revision,
         'extension': suggested_format,
         'user': request.user,
     }
     document = processor.read(code, options)
     if not request.user.is_superuser:
         # Hack: Used part of the code from MDTUI Wrong!
         user_permissions = list_permitted_docrules_qs(request.user)
         if not document.docrule in user_permissions:
             return rc.FORBIDDEN
     if processor.errors:
         log.error('FileHandler.read manager errors: %s' % processor.errors)
         return rc.NOT_FOUND
     if document.marked_deleted:
         log.error('FileHandler.read request to marked deleted document: %s' % code)
         return rc.NOT_FOUND
     else:
         response = DMSObjectResponse(document)
         log.info('FileHandler.read request fulfilled for code: %s, options: %s' % (code, options))
     return response
Esempio n. 4
0
def upload(request, template_name='browser/upload.html', extra_context=None):
    """Upload file processing.

    Uploaded file will be check against available rules to
    determine storage, validator, and security plugins.
    """
    extra_context = extra_context or {}

    form = UploadForm(request.POST or None, request.FILES or None)
    if request.method == 'POST':
        if form.is_valid():
            processor = DocumentProcessor()
            upl_file = form.files['file']
            # finding file in system. Updating if found and storing new if not or uncategorized.
            dms_file = processor.read(upl_file.name, {'user': request.user, 'only_metadata': True})
            if not processor.errors and not dms_file.get_docrule().uncategorized:
                processor.update(upl_file.name, {'user': request.user, 'update_file': upl_file})
            else:
                processor.errors = []
                processor.create(upl_file, {'user': request.user})
            # Handling processor errors in interactions.
            if not processor.errors:
                if dms_file.get_docrule().uncategorized:
                    messages.success(request, 'File has been uploaded into uncategorized.')
                else:
                    messages.success(request, 'File has been uploaded.')
                log.info('browser.upload file: %s sucess' % form.files['file'].name)
            else:
                error_string = "; ".join([unicode(x) for x in processor.errors])
                messages.error(request, error_string)
                log.error('browser.upload errror: %s' % error_string)

    extra_context['form'] = form
    return render(request, template_name, extra_context)
Esempio n. 5
0
 def get(self, request, code, suggested_format=None):
     revision, hashcode, extra = self._get_info(request)
     only_metadata = True
     indexing_data = extra.get('indexing_data', None)
     # Security measure for variable
     if indexing_data:
         indexing_data = True
         only_metadata = False
     processor = DocumentProcessor()
     options = {
         'revision': revision,
         'hashcode': hashcode,
         'only_metadata': only_metadata,
         'extension': suggested_format,
         'indexing_data': indexing_data,
         'user': request.user,
     }
     document = processor.read(code, options)
     if document.marked_deleted:
         log.error('FileInfoHandler.read request to marked deleted document: %s' % code)
         return Response(status=status.HTTP_404_NOT_FOUND)
     if processor.errors:
         log.error('FileInfoHandler.read errors: %s' % processor.errors)
         if settings.DEBUG:
             raise Exception('FileInfoHandler.read manager.errors')
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     info = DMSOBjectRevisionsData(document).jsons
     log.info(
         'FileInfoHandler.read request fulfilled for %s, ext %s, rev %s, hash %s'
         % (code, suggested_format, revision, hashcode)
     )
     return Response(info, status=status.HTTP_200_OK)
Esempio n. 6
0
def edit_file_revisions(request, code, step='edit_revisions', template='mdtui/indexing.html'):
    """Editing file revisions for given code"""
    form = DocumentUploadForm(request.POST or None, request.FILES or None)
    revision_file = request.FILES.get('file', None)
    errors = []
    context = {
        'step': step,
        'doc_name': code,
        'upload_form': form,
        'barcode': None,  # for compatibility with scripts (We are reusing modal scripts in templates)
    }
    processor = DocumentProcessor()
    doc = processor.read(code, {'user': request.user, 'only_metadata': True})
    frd = doc.get_file_revisions_data()
    db_info = doc.get_db_info()
    if not processor.errors and not doc.marked_deleted:
        if revision_file and form.is_valid():
            options = {
                'user': request.user,
                'update_file': revision_file,
            }
            processor.update(code, options)
            if not processor.errors:
                return HttpResponseRedirect(request.path)
            else:
                errors.append(processor.errors)
        context.update({
            'file_revision_data': frd,
            'file_revision_data_order_list': sorted(frd.iterkeys()),
            'index_data': db_info,
        })
    if processor.errors or doc.marked_deleted or (not frd and not db_info['mdt_indexes']):
        errors = [MDTUI_ERROR_STRINGS['NO_DOC'] + '. Maybe you should go index it first?']
    context.update({'error_warnings': errors})
    return render(request, template, context)
Esempio n. 7
0
 def get(self, request, code, suggested_format=None):
     revision, hashcode, extra = self._get_info(request)
     processor = DocumentProcessor()
     options = {
         'hashcode': hashcode,
         'revision': revision,
         'extension': suggested_format,
         'user': request.user,
     }
     document = processor.read(code, options)
     if not request.user.is_superuser:
         # Hack: Used part of the code from MDTUI Wrong!
         user_permissions = list_permitted_docrules_qs(request.user)
         if not document.docrule in user_permissions:
             return Response(status=status.HTTP_401_UNAUTHORIZED)
     if processor.errors:
         log.error('OldFileHandler.read manager errors: %s' %
                   processor.errors)
         return Response(status=status.HTTP_404_NOT_FOUND)
     if document.marked_deleted:
         log.error(
             'OldFileHandler.read request to marked deleted document: %s' %
             code)
         return Response(status=status.HTTP_404_NOT_FOUND)
     else:
         response = DMSObjectResponse(document)
         log.info(
             'OldFileHandler.read request fulfilled for code: %s, options: %s'
             % (code, options))
     return response
Esempio n. 8
0
 def get(self, request, code, suggested_format=None):
     revision, hashcode, extra = self._get_info(request)
     only_metadata = True
     indexing_data = extra.get('indexing_data', None)
     # Security measure for variable
     if indexing_data:
         indexing_data = True
         only_metadata = False
     processor = DocumentProcessor()
     options = {
         'revision': revision,
         'hashcode': hashcode,
         'only_metadata': only_metadata,
         'extension': suggested_format,
         'indexing_data': indexing_data,
         'user': request.user,
     }
     document = processor.read(code, options)
     if document.marked_deleted:
         log.error(
             'FileInfoHandler.read request to marked deleted document: %s' %
             code)
         return Response(status=status.HTTP_404_NOT_FOUND)
     if processor.errors:
         log.error('FileInfoHandler.read errors: %s' % processor.errors)
         if settings.DEBUG:
             raise Exception('FileInfoHandler.read manager.errors')
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     info = DMSOBjectRevisionsData(document).jsons
     log.info(
         'FileInfoHandler.read request fulfilled for %s, ext %s, rev %s, hash %s'
         % (code, suggested_format, revision, hashcode))
     return Response(info, status=status.HTTP_200_OK)
Esempio n. 9
0
def view_object(request, code, step, template='mdtui/view.html'):
    """View PDF Document

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    # TODO: Add certain revision view possibility for "edit revisions" view
    revision = request.GET.get('revision', None)
    pdf_url = reverse('mdtui-download-pdf', kwargs={'code': code})
    processor = DocumentProcessor()
    document = processor.read(code, options={'only_metadata': True, 'user': request.user, 'revision': revision})
    mimetype = document.get_mimetype()
    context = {
        'pdf_url': pdf_url,
        'code': code,
        'step': step,
        'mimetype': mimetype,
        'revision': revision,
    }
    if not document.get_file_revisions_data():
        db = document.get_db_info()
        if 'metadata_doc_type_rule_id' in db.iterkeys() and db['metadata_doc_type_rule_id']:
            # Indexed Document with 0 revisions (Displaying stub document from static)
            # TODO: expand this for branding. (Using custom DMS stub document)
            stub_doc_url = settings.STATIC_URL + 'pdf/stub_document.pdf'
            context.update({'mimetype': 'stub_document', 'pdf_url': stub_doc_url})
    return render(request, template, context)
Esempio n. 10
0
 def read(self, request, code, suggested_format=None):
     revision, hashcode, extra = self._get_info(request)
     processor = DocumentProcessor()
     options = {
         'revision': revision,
         'hashcode': hashcode,
         'only_metadata': True,
         'extension': suggested_format,
         'user': request.user,
     }
     document = processor.read(code, options)
     if document.marked_deleted:
         log.error('FileInfoHandler.read request to marked deleted document: %s' % code)
         return rc.NOT_FOUND
     if processor.errors:
         log.error('FileInfoHandler.read errors: %s' % processor.errors)
         if settings.DEBUG:
             raise Exception('FileInfoHandler.read manager.errors')
         else:
             return rc.BAD_REQUEST
     info = DMSOBjectRevisionsData(document).jsons
     log.info(
         'FileInfoHandler.read request fulfilled for %s, ext %s, rev %s, hash %s'
         % (code, suggested_format, revision, hashcode)
     )
     return HttpResponse(info)
Esempio n. 11
0
def edit_type(request, code, step='edit_type', template='mdtui/indexing.html'):
    """Indexing step: Edit. Editing document type (in fact document rename)

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    context = {}
    warnings = [
        MDTUI_ERROR_STRINGS['EDIT_TYPE_WARNING'],
    ]
    error_warnings = []
    form = False
    processor = DocumentProcessor()
    return_url = reverse('mdtui-edit', kwargs={'code': code})

    log.debug('indexing_edit_type view called with code: %s' % code)
    doc = processor.read(code, {
        'user': request.user,
    })
    if not processor.errors:
        empty_form = make_document_type_select_form(
            request.user, docrule_initial=doc.get_docrule())
        form = empty_form(request.POST or None)
        if request.POST:
            if form.is_valid():
                docrule = form.cleaned_data['docrule']
                current_docrule = doc.get_docrule()
                if not docrule == current_docrule:
                    options = {
                        'user': request.user,
                        'new_type': docrule,
                    }
                    doc = processor.update(code, options)
                    if not processor.errors:
                        return HttpResponseRedirect(
                            reverse('mdtui-edit',
                                    kwargs={'code': doc.get_filename()}))
                else:
                    warnings = [
                        MDTUI_ERROR_STRINGS['EDIT_TYPE_ERROR'],
                    ]
    # Can cause errors in two places here (on doc read and update)
    if processor.errors:
        for error in processor.errors:
            error_warnings.append(error)
    context.update({
        'step': step,
        'doc_name': code,
        'docrule': doc.get_docrule(),
        'warnings': warnings,
        'form': form,
        'type_edit_return': return_url,
        'error_warnings': error_warnings,
    })
    return render(request, template, context)
Esempio n. 12
0
def revision_document(request, document):
    document_name = document
    processor = DocumentProcessor()
    document = processor.read(document_name,
                              options={
                                  'only_metadata': True,
                                  'user': request.user
                              })
    extra_context = {}
    file_revision_data = document.get_file_revisions_data()

    def get_args(f_info):
        args = []
        for arg in ['revision', 'hashcode']:
            if f_info.get(arg, None):
                args.append("%s=%s" % (arg, f_info[arg]))
        arg_string = ""
        if args:
            arg_string = "?" + "&".join(args)
        return arg_string

    if not processor.errors:
        if file_revision_data:
            revisions = map(lambda x: int(x), file_revision_data.keys())
            revisions.sort()
            fileinfos = []
            for revision in revisions:
                fileinfo = file_revision_data[str(revision)]
                fileinfo['args'] = get_args(fileinfo)
                if not 'deleted' in fileinfo:
                    fileinfo['deleted'] = False
                fileinfos.append(fileinfo)
            extra_context = {
                'fileinfo_db': fileinfos,
                'document_name': document.get_code(),
            }
        else:
            fileinfo = {
                'revision': None,
                'name': document.get_filename(),
                'created_date': document.get_creation_time(),
                'hashcode': document.get_hashcode(),
            }
            fileinfo['args'] = get_args(fileinfo)
            extra_context = {
                'fileinfo_db': [fileinfo],
                'document_name': document.get_filename(),
            }
    else:
        messages.error(request,
                       "; ".join(map(lambda x: x.parameter, processor.errors)))
    if processor.warnings:
        messages.warning(request, "; ".join(processor.warnings))
    return render(request, 'browser/revision.html', extra_context)
Esempio n. 13
0
    def read(self, request, code):

        if not request.user.is_authenticated():
            log.error('ThumbnailsHandler.read attempted with unauthenticated user.')
            return rc.FORBIDDEN

        processor = DocumentProcessor()
        doc = processor.read(code, options={'user': request.user, 'thumbnail': True})
        if not processor.errors:
            return DMSObjectResponse(doc, thumbnail=True)
        else:
            return rc.NOT_FOUND
Esempio n. 14
0
def edit_file_revisions(request,
                        code,
                        step='edit_revisions',
                        template='mdtui/indexing.html'):
    """Editing file revisions for given code

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    form = DocumentUploadForm(request.POST or None, request.FILES or None)
    revision_file = request.FILES.get('file', None)
    errors = []
    context = {
        'step': step,
        'doc_name': code,
        'upload_form': form,
        'barcode':
        None,  # for compatibility with scripts (We are reusing modal scripts in templates)
    }
    processor = DocumentProcessor()
    doc = processor.read(code, {'user': request.user, 'only_metadata': True})
    frd = doc.get_file_revisions_data()
    db_info = doc.get_db_info()
    if not processor.errors and not doc.marked_deleted:
        if revision_file and form.is_valid():
            options = {
                'user': request.user,
                'update_file': revision_file,
            }
            processor.update(code, options)
            if not processor.errors:
                return HttpResponseRedirect(request.path)
            else:
                errors.append(processor.errors)
        context.update({
            'file_revision_data': frd,
            'file_revision_data_order_list': sorted(frd.iterkeys()),
            'index_data': db_info,
        })
    if not db_info:
        errors = [
            MDTUI_ERROR_STRINGS['NO_DOC'] +
            '. Maybe you should go index it first?'
        ]
    elif processor.errors or doc.marked_deleted or (
            not frd and not db_info['mdt_indexes']):
        errors = [
            MDTUI_ERROR_STRINGS['NO_DOC'] +
            '. Maybe you should go index it first?'
        ]
    context.update({'error_warnings': errors})
    return render(request, template, context)
Esempio n. 15
0
def get_file(request, code, suggested_format=None):
    hashcode = request.GET.get('hashcode', None)  # Refactor me out
    processor = DocumentProcessor()
    options = {
        'hashcode': hashcode,
        'extension': suggested_format,
        'user': request.user,
    }
    document = processor.read(code, options)
    if processor.errors:
        response = error_response(processor.errors)
    else:
        response = DMSObjectResponse(document)
    return response
Esempio n. 16
0
def get_file(request, code, suggested_format=None):
    hashcode = request.GET.get('hashcode', None) # Refactor me out
    processor = DocumentProcessor()
    options = {
        'hashcode': hashcode,
        'extension': suggested_format,
        'user': request.user,
    }
    document = processor.read(code, options)
    if processor.errors:
        response = error_response(processor.errors)
    else:
        response = DMSObjectResponse(document)
    return response
Esempio n. 17
0
def revision_document(request, document):
    document_name = document
    processor = DocumentProcessor()
    document = processor.read(document_name, options={'only_metadata': True, 'user': request.user})
    extra_context = {}
    file_revision_data = document.get_file_revisions_data()

    def get_args(f_info):
        args = []
        for arg in ['revision', 'hashcode']:
            if f_info.get(arg, None):
                args.append("%s=%s" % (arg, f_info[arg]))
        arg_string = ""
        if args:
            arg_string = "?" + "&".join(args)
        return arg_string

    if not processor.errors:
        if file_revision_data:
            revisions = map(lambda x: int(x), file_revision_data.keys())
            revisions.sort()
            fileinfos = []
            for revision in revisions:
                fileinfo = file_revision_data[str(revision)]
                fileinfo['args'] = get_args(fileinfo)
                if not 'deleted' in fileinfo:
                    fileinfo['deleted'] = False
                fileinfos.append(fileinfo)
            extra_context = {
                'fileinfo_db': fileinfos,
                'document_name': document.get_code(),
            }
        else:
            fileinfo = {
                'revision': None,
                'name': document.get_filename(),
                'created_date': document.get_creation_time(),
                'hashcode': document.get_hashcode(),
            }
            fileinfo['args'] = get_args(fileinfo)
            extra_context = {
                'fileinfo_db': [fileinfo],
                'document_name': document.get_filename(),
            }
    else:
        messages.error(request, "; ".join(map(lambda x: x.parameter, processor.errors)))
    if processor.warnings:
        messages.warning(request, "; ".join(processor.warnings))
    return render(request, 'browser/revision.html', extra_context)
Esempio n. 18
0
 def read(self, request, document):
     document, extension = os.path.splitext(document)
     processor = DocumentProcessor()
     document = processor.read(document, options={'revision_count': True, 'user': request.user,})
     rev_count = document.get_revision()
     if rev_count <= 0:
         log.info('RevisionCountHandler.read rev_count %s.' % str(rev_count))
         if settings.DEBUG:
             raise Exception('No document revisions')
         else:
             return rc.BAD_REQUEST
     if processor.errors:
         log.error('RevisionCountHandler.read Exception %s' % processor.errors[0])
         return rc.BAD_REQUEST
     log.info('RevisionCountHandler.read request fulfilled for document %s, extension %s' % (document, extension))
     return rev_count
Esempio n. 19
0
def edit_type(request, code, step='edit_type', template='mdtui/indexing.html'):
    """Indexing step: Edit. Editing document type (in fact document rename)

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    context = {}
    warnings = [MDTUI_ERROR_STRINGS['EDIT_TYPE_WARNING'], ]
    error_warnings = []
    form = False
    processor = DocumentProcessor()
    return_url = reverse('mdtui-edit', kwargs={'code': code})

    log.debug('indexing_edit_type view called with code: %s' % code)
    doc = processor.read(code, {'user': request.user, })
    if not processor.errors:
        empty_form = make_document_type_select_form(request.user, docrule_initial=doc.get_docrule())
        form = empty_form(request.POST or None)
        if request.POST:
            if form.is_valid():
                docrule = form.cleaned_data['docrule']
                current_docrule = doc.get_docrule()
                if not docrule == current_docrule:
                    options = {
                        'user': request.user,
                        'new_type': docrule,
                    }
                    doc = processor.update(code, options)
                    if not processor.errors:
                        return HttpResponseRedirect(reverse('mdtui-edit', kwargs={'code': doc.get_filename()}))
                else:
                    warnings = [MDTUI_ERROR_STRINGS['EDIT_TYPE_ERROR'], ]
    # Can cause errors in two places here (on doc read and update)
    if processor.errors:
        for error in processor.errors:
            error_warnings.append(error)
    context.update({
        'step': step,
        'doc_name': code,
        'docrule': doc.get_docrule(),
        'warnings': warnings,
        'form': form,
        'type_edit_return': return_url,
        'error_warnings': error_warnings,
    })
    return render(request, template, context)
Esempio n. 20
0
    def read(self, request, code):

        # TODO: stabilize by removing try/except here and fixing ALL the possible issues.
        try:
            if not request.user.is_authenticated():
                log.error('ThumbnailsHandler.read attempted with unauthenticated user.')
                return rc.FORBIDDEN

            processor = DocumentProcessor()
            doc = processor.read(code, options={'user': request.user, 'thumbnail': True})
            if not processor.errors:
                return DMSObjectResponse(doc, thumbnail=True)
            else:
                return rc.NOT_FOUND
        except:
            log.error('ThumbnailsHandler Error: %s' % traceback.print_exc())
            raise
Esempio n. 21
0
def upload(request, template_name='browser/upload.html', extra_context=None):
    """Upload file processing.

    Uploaded file will be check against available rules to
    determine storage, validator, and security plugins.
    """
    extra_context = extra_context or {}

    form = UploadForm(request.POST or None, request.FILES or None)
    if request.method == 'POST':
        if form.is_valid():
            processor = DocumentProcessor()
            upl_file = form.files['file']
            # finding file in system. Updating if found and storing new if not or uncategorized.
            dms_file = processor.read(upl_file.name, {
                'user': request.user,
                'only_metadata': True
            })
            if not processor.errors and not dms_file.get_docrule(
            ).uncategorized:
                processor.update(upl_file.name, {
                    'user': request.user,
                    'update_file': upl_file
                })
            else:
                processor.errors = []
                processor.create(upl_file, {'user': request.user})
            # Handling processor errors in interactions.
            if not processor.errors:
                if dms_file.get_docrule().uncategorized:
                    messages.success(
                        request, 'File has been uploaded into uncategorized.')
                else:
                    messages.success(request, 'File has been uploaded.')
                log.info('browser.upload file: %s sucess' %
                         form.files['file'].name)
            else:
                error_string = "; ".join(
                    [unicode(x) for x in processor.errors])
                messages.error(request, error_string)
                log.error('browser.upload errror: %s' % error_string)

    extra_context['form'] = form
    return render(request, template_name, extra_context)
Esempio n. 22
0
    def get(self, request, code):

        # TODO: stabilize by removing try/except here and fixing ALL the possible issues.
        try:
            if not request.user.is_authenticated():
                log.error(
                    'ThumbnailsHandler.read attempted with unauthenticated user.'
                )
                return Response(status=status.HTTP_401_UNAUTHORIZED)

            processor = DocumentProcessor()
            doc = processor.read(code,
                                 options={
                                     'user': request.user,
                                     'thumbnail': True
                                 })
            if not processor.errors:
                return DMSObjectResponse(doc, thumbnail=True)
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)
        except:
            log.error('ThumbnailsHandler Error: %s' % traceback.print_exc())
            raise
Esempio n. 23
0
def view_object(request, code, step, template='mdtui/view.html'):
    """View PDF Document

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    # TODO: Add certain revision view possibility for "edit revisions" view
    revision = request.GET.get('revision', None)
    pdf_url = reverse('mdtui-download-pdf', kwargs={'code': code})
    processor = DocumentProcessor()
    document = processor.read(code,
                              options={
                                  'only_metadata': True,
                                  'user': request.user,
                                  'revision': revision
                              })
    mimetype = document.get_mimetype()
    context = {
        'pdf_url': pdf_url,
        'code': code,
        'step': step,
        'mimetype': mimetype,
        'revision': revision,
    }
    if not document.get_file_revisions_data():
        db = document.get_db_info()
        if 'metadata_doc_type_rule_id' in db.iterkeys(
        ) and db['metadata_doc_type_rule_id']:
            # Indexed Document with 0 revisions (Displaying stub document from static)
            # TODO: expand this for branding. (Using custom DMS stub document)
            stub_doc_url = settings.STATIC_URL + 'pdf/stub_document.pdf'
            context.update({
                'mimetype': 'stub_document',
                'pdf_url': stub_doc_url
            })
    return render(request, template, context)
Esempio n. 24
0
 def get(self, request, document):
     document, extension = os.path.splitext(document)
     processor = DocumentProcessor()
     document = processor.read(document,
                               options={
                                   'revision_count': True,
                                   'user': request.user,
                               })
     rev_count = document.get_revision()
     if rev_count <= 0:
         log.info('RevisionCountHandler.read rev_count %s.' %
                  str(rev_count))
         if settings.DEBUG:
             raise Exception('No document revisions')
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     if processor.errors:
         log.error('RevisionCountHandler.read Exception %s' %
                   processor.errors[0])
         return Response(status=status.HTTP_400_BAD_REQUEST)
     log.info(
         'RevisionCountHandler.read request fulfilled for document %s, extension %s'
         % (document, extension))
     return HttpResponse(rev_count)
Esempio n. 25
0
def edit(request, code, step='edit', template='mdtui/indexing.html'):
    """Indexing step: Edit. Made for editing indexes of document that is indexed already.

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    context = {}
    warnings = []
    error_warnings = []
    form = False
    processor = DocumentProcessor()
    autocomplete_list = None
    changed_indexes = None
    # Storing cancel (return back from edit) url
    try:
        return_url = request.session['edit_return']
    except KeyError:
        try:
            return_url = request.META['HTTP_REFERER']
            request.session['edit_return'] = return_url
        except KeyError:
            return_url = '/'
            pass
        pass

    # Only preserve indexes if returning from edit indexes confirmation step
    if 'HTTP_REFERER' in request.META and request.META['HTTP_REFERER'].endswith(reverse('mdtui-edit-finished')):
        try:
            changed_indexes = request.session['edit_processor_indexes']
        except KeyError:
            pass

    log.debug('indexing_edit view called with return_url: %s, changed_indexes: %s' % (return_url, changed_indexes))
    doc = processor.read(code, {'user': request.user, 'only_metadata': True})
    if not processor.errors and not doc.marked_deleted:
        if not request.POST:
            form = initEditIndexesForm(request, doc, changed_indexes)
            # Setting context variables required for autocomplete
            docrule_id = str(doc.get_docrule().id)
            request.session['indexing_docrule_id'] = docrule_id
            request.session['edit_mdts'] = get_mdts_for_docrule(docrule_id)
        else:
            old_db_info = doc.get_db_info()
            secondary_indexes = processEditDocumentIndexForm(request, doc)
            request.session['edit_processor_indexes'] = secondary_indexes
            request.session['edit_index_barcode'] = code
            old_docs_indexes = {'description': old_db_info['description']}
            for index_name, index_value in old_db_info['mdt_indexes'].iteritems():
                # Converting Old index dates to render according to DMS date format
                if index_value.__class__.__name__ == 'datetime':
                    old_docs_indexes[index_name] = datetime.datetime.strftime(index_value, settings.DATE_FORMAT)
                else:
                    old_docs_indexes[index_name] = index_value
            request.session['old_document_keys'] = old_docs_indexes
            return HttpResponseRedirect(reverse('mdtui-edit-finished'))
    else:
        for error in processor.errors:
            # Intercepting type Exception and using it's message or using error.__str__
            if not error.__class__.__name__ == 'unicode' and 'parameter' in error.__dict__.iterkeys():
                error_warnings.append(error.parameter)
            else:
                error_warnings.append(error)
        if doc.marked_deleted:
            error_warnings.append(MDTUI_ERROR_STRINGS['NO_DOC'])

    if form:
        autocomplete_list = extract_secondary_keys_from_form(form)
        # No form is possible when document does not exist
        context.update({'form': form, })
    # In case of no doc type (empty document) fix
    type_name = None
    if doc.docrule:
        type_name = doc.get_docrule().title
    context.update({
        'step': step,
        'doc_name': code,
        'type_name': type_name,
        'warnings': warnings,
        'autocomplete_fields': autocomplete_list,
        'edit_return': return_url,
        'error_warnings': error_warnings,
    })
    return render(request, template, context)
Esempio n. 26
0
    def store(self, document):
        """Stores CouchDB object into DB.

        (Updates or overwrites CouchDB document)

        @param document: is a DMS Document() instance
        """
        # FIXME: Refactor me. We should upload new "secondary_indexes" or metatags with update() workflow,
        # not a create(), like it is now. Because this method is a mess.
        docrule = document.get_docrule()
        # doing nothing for no docrule documents
        if docrule.uncategorized:
            return document
        else:
            user = self.check_user(document)
            processor = DocumentProcessor()
            # FIXME: there might be more than one mapping
            mapping = docrule.get_docrule_plugin_mappings()
            # doing nothing for documents without mapping has DB plugins
            if not mapping.get_database_storage_plugins():
                return document
            else:
                # if not exists all required metadata getting them from docrule retrieve sequence
                if not document.file_revision_data:
                    # HACK: Preserving db_info here... (May be Solution!!!)
                    db_info = document.get_db_info()
                    document = processor.read(document.file_name, options={
                        'only_metadata': True,
                        'user': document.user
                    })

                    # saving NEW file_revision_data ONLY if they exist in new uploaded doc (Preserving old indexes)
                    if db_info:
                        # Storing new indexes
                        document.set_db_info(db_info)
                    else:
                        # TODO: move this code into a proper place (UPDATE method)
                        # Asking couchdb about if old file_revision_data exists and updating them properly
                        current_revisions = document.file_revision_data
                        try:
                            # Only if document exists in DB. Falling gracefully if not.
                            temp_doc = self.retrieve(document)
                            old_metadata = temp_doc.get_db_info()
                            old_index_revisions = None
                            if old_metadata['mdt_indexes']:
                                # Preserving Description, User, Created Date, indexes revisions
                                if temp_doc.index_revisions:
                                    old_index_revisions = temp_doc.index_revisions
                                old_metadata['mdt_indexes']['description'] = old_metadata['description']
                                old_metadata['mdt_indexes']['metadata_user_name'] = old_metadata['metadata_user_name']
                                old_metadata['mdt_indexes']['metadata_user_id'] = old_metadata['metadata_user_id']
                                old_cr_date = datetime.datetime.strftime(
                                    old_metadata['metadata_created_date'],
                                    settings.DATE_FORMAT
                                )
                                old_metadata['mdt_indexes']['date'] = old_cr_date
                                document.set_db_info(old_metadata['mdt_indexes'])
                                document.set_index_revisions(old_index_revisions)
                                document.set_file_revisions_data(current_revisions)
                            else:
                                # Preserving set revisions anyway.
                                document.set_file_revisions_data(current_revisions)
                        except ResourceNotFound:
                            pass
                # updating tags to sync with Django DB
                self.sync_document_tags(document)
                # assuming no document with this _id exists. SAVING or overwriting existing
                couchdoc = CouchDocument()

                couchdoc.populate_from_dms(user, document)
                couchdoc.save(force_update=True)
                return document
Esempio n. 27
0
    def store(self, document):
        """Stores CouchDB object into DB.

        (Updates or overwrites CouchDB document)

        @param document: is a DMS Document() instance
        """
        # FIXME: Refactor me. We should upload new "secondary_indexes" or metatags with update() workflow,
        # not a create(), like it is now. Because this method is a mess.
        docrule = document.get_docrule()
        # doing nothing for no docrule documents
        if docrule.uncategorized:
            return document
        else:
            user = self.check_user(document)
            processor = DocumentProcessor()
            # FIXME: there might be more than one mapping
            mapping = docrule.get_docrule_plugin_mappings()
            # doing nothing for documents without mapping has DB plugins
            if not mapping.get_database_storage_plugins():
                return document
            else:
                # if not exists all required metadata getting them from docrule retrieve sequence
                if not document.file_revision_data:
                    # HACK: Preserving db_info here... (May be Solution!!!)
                    db_info = document.get_db_info()
                    document = processor.read(document.file_name,
                                              options={
                                                  'only_metadata': True,
                                                  'user': document.user
                                              })

                    # saving NEW file_revision_data ONLY if they exist in new uploaded doc (Preserving old indexes)
                    if db_info:
                        # Storing new indexes
                        document.set_db_info(db_info)
                    else:
                        # TODO: move this code into a proper place (UPDATE method)
                        # Asking couchdb about if old file_revision_data exists and updating them properly
                        current_revisions = document.file_revision_data
                        try:
                            # Only if document exists in DB. Falling gracefully if not.
                            temp_doc = self.retrieve(document)
                            old_metadata = temp_doc.get_db_info()
                            old_index_revisions = None
                            if old_metadata['mdt_indexes']:
                                # Preserving Description, User, Created Date, indexes revisions
                                if temp_doc.index_revisions:
                                    old_index_revisions = temp_doc.index_revisions
                                old_metadata['mdt_indexes'][
                                    'description'] = old_metadata[
                                        'description']
                                old_metadata['mdt_indexes'][
                                    'metadata_user_name'] = old_metadata[
                                        'metadata_user_name']
                                old_metadata['mdt_indexes'][
                                    'metadata_user_id'] = old_metadata[
                                        'metadata_user_id']
                                old_cr_date = datetime.datetime.strftime(
                                    old_metadata['metadata_created_date'],
                                    settings.DATE_FORMAT)
                                old_metadata['mdt_indexes'][
                                    'date'] = old_cr_date
                                document.set_db_info(
                                    old_metadata['mdt_indexes'])
                                document.set_index_revisions(
                                    old_index_revisions)
                                document.set_file_revisions_data(
                                    current_revisions)
                            else:
                                # Preserving set revisions anyway.
                                document.set_file_revisions_data(
                                    current_revisions)
                        except ResourceNotFound:
                            pass
                # updating tags to sync with Django DB
                self.sync_document_tags(document)
                # assuming no document with this _id exists. SAVING or overwriting existing
                couchdoc = CouchDocument()

                couchdoc.populate_from_dms(user, document)
                couchdoc.save(force_update=True)
                return document
Esempio n. 28
0
def edit(request, code, step='edit', template='mdtui/indexing.html'):
    """Indexing step: Edit. Made for editing indexes of document that is indexed already.

    @param request: is a Django request object
    @param code: is a DMS Object() code for view interactions
    @param step: is a current step name (for template rendering)
    @param template: is a name of template for this view"""
    context = {}
    warnings = []
    error_warnings = []
    form = False
    processor = DocumentProcessor()
    autocomplete_list = None
    changed_indexes = None
    # Storing cancel (return back from edit) url
    try:
        return_url = request.session['edit_return']
    except KeyError:
        try:
            return_url = request.META['HTTP_REFERER']
            request.session['edit_return'] = return_url
        except KeyError:
            return_url = '/'
            pass
        pass

    # Only preserve indexes if returning from edit indexes confirmation step
    if 'HTTP_REFERER' in request.META and request.META[
            'HTTP_REFERER'].endswith(reverse('mdtui-edit-finished')):
        try:
            changed_indexes = request.session['edit_processor_indexes']
        except KeyError:
            pass

    log.debug(
        'indexing_edit view called with return_url: %s, changed_indexes: %s' %
        (return_url, changed_indexes))
    doc = processor.read(code, {'user': request.user, 'only_metadata': True})
    if not processor.errors and not doc.marked_deleted:
        if not request.POST:
            form = initEditIndexesForm(request, doc, changed_indexes)
            # Setting context variables required for autocomplete
            docrule_id = str(doc.get_docrule().id)
            request.session['indexing_docrule_id'] = docrule_id
            request.session['edit_mdts'] = get_mdts_for_docrule(docrule_id)
        else:
            old_db_info = doc.get_db_info()
            secondary_indexes = processEditDocumentIndexForm(request, doc)
            request.session['edit_processor_indexes'] = secondary_indexes
            request.session['edit_index_barcode'] = code
            old_docs_indexes = {'description': old_db_info['description']}
            for index_name, index_value in old_db_info[
                    'mdt_indexes'].iteritems():
                # Converting Old index dates to render according to DMS date format
                if index_value.__class__.__name__ == 'datetime':
                    old_docs_indexes[index_name] = datetime.datetime.strftime(
                        index_value, settings.DATE_FORMAT)
                else:
                    old_docs_indexes[index_name] = index_value
            request.session['old_document_keys'] = old_docs_indexes
            return HttpResponseRedirect(reverse('mdtui-edit-finished'))
    else:
        for error in processor.errors:
            # Intercepting type Exception and using it's message or using error.__str__
            if not error.__class__.__name__ == 'unicode' and 'parameter' in error.__dict__.iterkeys(
            ):
                error_warnings.append(error.parameter)
            else:
                error_warnings.append(error)
        if doc.marked_deleted:
            error_warnings.append(MDTUI_ERROR_STRINGS['NO_DOC'])

    if form:
        autocomplete_list = extract_secondary_keys_from_form(form)
        # No form is possible when document does not exist
        context.update({
            'form': form,
        })
    # In case of no doc type (empty document) fix
    type_name = None
    if doc.docrule:
        type_name = doc.get_docrule().title
    context.update({
        'step': step,
        'doc_name': code,
        'type_name': type_name,
        'warnings': warnings,
        'autocomplete_fields': autocomplete_list,
        'edit_return': return_url,
        'error_warnings': error_warnings,
    })
    return render(request, template, context)