def post(self, request, code, suggested_format=None): if 'file' in request.FILES: uploaded_file = request.FILES['file'] else: return Response(status=status.HTTP_400_BAD_REQUEST) processor = DocumentProcessor() options = { 'user': request.user, 'barcode': code, } if uploaded_file.size == 0: return Response(status=status.HTTP_400_BAD_REQUEST) document = processor.create(uploaded_file, options) if len(processor.errors) > 0: log.error('OldFileHandler.create errors: %s' % processor.errors) error = processor.errors[0] if error.__class__.__name__ in ['unicode', 'str']: return Response(status=status.HTTP_400_BAD_REQUEST) if error.code == 409: new_processor = DocumentProcessor() options['update_file'] = uploaded_file document = new_processor.update(code, options) if len(new_processor.errors) > 0: return Response(status=status.HTTP_400_BAD_REQUEST) log.info('OldFileHandler.create request fulfilled for %s' % document.get_filename()) return Response(document.get_filename(), status=status.HTTP_200_OK)
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)
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)
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
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
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)
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)
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)
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)
def put(self, request, code, suggested_format=None): """Used to work with "update" sequence of DMS code. to update a code you need to send a PUT request here. PUT must contain: @param code: the DMS "code" of file to be updated. e.g.: ADL-0001 @param suggested_format: format of file for code to be updated. To have ability to post files in certain format. """ context = {'request': request} conten_type = request.content_type uploaded_obj = None if 'multipart/form-data' in conten_type: # We have a file upload encoded with multipart/form-data file_content = StringIO(request.body) parser = MultiPartParser() dnf = parser.parse(file_content, media_type=conten_type, parser_context=context) extra = dnf.data if 'file' in dnf.files: uploaded_obj = dnf.files['file'] else: extra = request.QUERY_PARAMS # TODO: do we require SQL tagging logic in this API call? sql_tag_string = extra.get('tag_string', None) sql_remove_tag_string = extra.get('remove_tag_string', None) new_name = extra.get('new_name', None) new_type = extra.get('new_type', None) index_data = extra.get('indexing_data', None) if index_data: index_data = json.loads(index_data) processor = DocumentProcessor() options = { 'tag_string': sql_tag_string, 'remove_tag_string': sql_remove_tag_string, 'extension': suggested_format, 'new_name': new_name, 'new_type': new_type, 'new_indexes': index_data, 'update_file': uploaded_obj, 'user': request.user, } document = processor.update(code, options) if len(processor.errors) > 0: log.error('FileHandler.update manager errors %s' % processor.errors) if settings.DEBUG: print processor.errors raise Exception('FileHandler.update manager errors') else: return Response(status=status.HTTP_400_BAD_REQUEST) log.info( 'FileHandler.update request fulfilled for code: %s, extension: %s' % (code, suggested_format)) resp = DMSOBjectRevisionsData(document).data return Response(resp, status=status.HTTP_200_OK)
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)
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)
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
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)
def put(self, request, code, suggested_format=None): """Used to work with "update" sequence of DMS code. to update a code you need to send a PUT request here. PUT must contain: @param code: the DMS "code" of file to be updated. e.g.: ADL-0001 @param suggested_format: format of file for code to be updated. To have ability to post files in certain format. """ context = {'request': request} conten_type = request.content_type uploaded_obj = None if 'multipart/form-data' in conten_type: # We have a file upload encoded with multipart/form-data file_content = StringIO(request.body) parser = MultiPartParser() dnf = parser.parse(file_content, media_type=conten_type, parser_context=context) extra = dnf.data if 'file' in dnf.files: uploaded_obj = dnf.files['file'] else: extra = request.QUERY_PARAMS # TODO: do we require SQL tagging logic in this API call? sql_tag_string = extra.get('tag_string', None) sql_remove_tag_string = extra.get('remove_tag_string', None) new_name = extra.get('new_name', None) new_type = extra.get('new_type', None) index_data = extra.get('indexing_data', None) if index_data: index_data = json.loads(index_data) processor = DocumentProcessor() options = { 'tag_string': sql_tag_string, 'remove_tag_string': sql_remove_tag_string, 'extension': suggested_format, 'new_name': new_name, 'new_type': new_type, 'new_indexes': index_data, 'update_file': uploaded_obj, 'user': request.user, } document = processor.update(code, options) if len(processor.errors) > 0: log.error('FileHandler.update manager errors %s' % processor.errors) if settings.DEBUG: print processor.errors raise Exception('FileHandler.update manager errors') else: return Response(status=status.HTTP_400_BAD_REQUEST) log.info('FileHandler.update request fulfilled for code: %s, extension: %s' % (code, suggested_format)) resp = DMSOBjectRevisionsData(document).data return Response(resp, status=status.HTTP_200_OK)
def edit_result(request, step='edit_finish', template='mdtui/indexing.html'): """Confirmation step for editing indexes""" # initialising context required_vars = ('edit_processor_indexes', 'edit_index_barcode', 'old_document_keys', 'edit_return', "edit_mdts") variables = {} warnings = [] for var in required_vars: try: variables[var] = request.session[var] except KeyError: variables[var] = '' # Error handling into warnings if not var == 'edit_return': error_name = MDTUI_ERROR_STRINGS['ERROR_EDIT_INDEXES_FINISHED'] log.error('indexing_finished error: variable: %s, %s' % (var, error_name)) if not error_name in warnings: warnings.append(error_name) pass log.debug('indexing_edit_result called with: step: "%s", variables: "%s",' % (step, variables)) if request.POST: code = variables['edit_index_barcode'] processor = DocumentProcessor() options = { 'new_indexes': variables['edit_processor_indexes'], 'user': request.user, } processor.update(code, options=options) if not processor.errors: # cleanup session here because editing is finished for var in required_vars: _cleanup_session_var(request, var) return HttpResponseRedirect(variables['edit_return']) else: for error in processor.errors: warnings.append(error) # Building new indexes for confirmation rendering context_secondary_indexes = {} if 'edit_processor_indexes' in variables.iterkeys() and variables['edit_processor_indexes']: for index, value in variables['edit_processor_indexes'].iteritems(): if not index in ['metadata_user_name', 'metadata_user_id']: context_secondary_indexes[index] = value context = { 'step': step, 'document_keys': context_secondary_indexes, 'barcode': variables['edit_index_barcode'], 'old_document_keys': variables['old_document_keys'], 'edit_return': variables['edit_return'], 'warnings': warnings, } return render(request, template, context)
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
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
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)
def delete(self, request, code, suggested_format=None): # FIXME: should return 404 if file not found, 400 if no docrule exists. revision, hashcode, extra = self._get_info(request) processor = DocumentProcessor() options = { 'revision': revision, 'extension': suggested_format, 'user': request.user, } log.debug('FileHandler.delete attempt with %s' % options) processor.delete(code, options) if len(processor.errors) > 0: log.error('Manager Errors encountered %s' % processor.errors) return rc.BAD_REQUEST log.info('FileHandler.delete request fulfilled for code: %s, format: %s, rev: %s, hash: %s.' % (code, suggested_format, revision, hashcode)) return rc.DELETED
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
def create(self, request, code, suggested_format=None): if 'file' in request.FILES: uploaded_file = request.FILES['file'] else: return rc.BAD_REQUEST processor = DocumentProcessor() options = { 'user': request.user, 'barcode': code, } document = processor.create(uploaded_file, options) if len(processor.errors) > 0: log.error('FileHandler.create manager errors: %s' % processor.errors) return rc.BAD_REQUEST log.info('FileHandler.create request fulfilled for %s' % document.get_filename()) return rc.CREATED
def handle(self, *args, **options): """Main method processor @param args: directory_name directory_name ... @param options: """ silent = 'silent' in options.keys() and options['silent'] if len(args) == 0: self.stdout.write('No arguments specified\n') return for directory in args: if not os.path.exists(directory): self.stderr.write('Could not import %s: no such directory\n' % directory) continue cnt = 0 admin = User.objects.filter(is_superuser=True)[0] for root, dirs, files in os.walk(directory): if '.svn' in dirs: dirs.remove('.svn') # don't visit svn directories for filename in files: if not silent: self.stdout.write('Importing file: "%s"\n' % filename) file_obj = open(os.path.join(root, filename)) file_obj.seek(0) processor = DocumentProcessor() try: processor.create(file_obj, {'user': admin}) except Exception, e: self.stderr.write(str(e)) self.stderr.write(traceback.format_exc() + "\n") else: if processor.errors: self.stderr.write('\nImport error: %s\n' % processor.errors) else: cnt += 1 file_obj.close() if not silent: if cnt: self.stdout.write( 'Successfully imported %s documents from directory "%s"\n' % (cnt, directory)) else: self.stdout.write('No documents were imported\n')
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)
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
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)
def delete(self, request, code, suggested_format=None): # FIXME: should return 404 if file not found, 400 if no docrule exists. revision, hashcode, extra = self._get_info(request) processor = DocumentProcessor() options = { 'revision': revision, 'extension': suggested_format, 'user': request.user, } log.debug('FileHandler.delete attempt with %s' % options) processor.delete(code, options) if len(processor.errors) > 0: log.error('Manager Errors encountered %s' % processor.errors) return Response(status=status.HTTP_400_BAD_REQUEST) log.info( 'FileHandler.delete request fulfilled for code: %s, format: %s, rev: %s, hash: %s.' % (code, suggested_format, revision, hashcode)) return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, code, suggested_format=None): if 'file' in request.FILES: uploaded_file = request.FILES['file'] else: return Response(status=status.HTTP_400_BAD_REQUEST) processor = DocumentProcessor() options = { 'user': request.user, 'barcode': code, } document = processor.create(uploaded_file, options) if len(processor.errors) > 0: log.error('FileHandler.create manager errors: %s' % processor.errors) return Response(status=status.HTTP_400_BAD_REQUEST) log.info('FileHandler.create request fulfilled for %s' % document.get_filename()) return Response(status=status.HTTP_201_CREATED)
def handle(self, *args, **options): """Main method processor @param args: directory_name directory_name ... @param options: """ silent = "silent" in options.keys() and options["silent"] if len(args) == 0: self.stdout.write("No arguments specified\n") return for directory in args: if not os.path.exists(directory): self.stderr.write("Could not import %s: no such directory\n" % directory) continue cnt = 0 admin = User.objects.filter(is_superuser=True)[0] for root, dirs, files in os.walk(directory): if ".svn" in dirs: dirs.remove(".svn") # don't visit svn directories for filename in files: if not silent: self.stdout.write('Importing file: "%s"\n' % filename) file_obj = open(os.path.join(root, filename)) file_obj.seek(0) processor = DocumentProcessor() try: processor.create(file_obj, {"user": admin}) except Exception, e: self.stderr.write(str(e)) self.stderr.write(traceback.format_exc() + "\n") else: if processor.errors: self.stderr.write("\nImport error: %s\n" % processor.errors) else: cnt += 1 file_obj.close() if not silent: if cnt: self.stdout.write('Successfully imported %s documents from directory "%s"\n' % (cnt, directory)) else: self.stdout.write("No documents were imported\n")
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)
def post(self, request, code, suggested_format=None): if 'file' in request.FILES: uploaded_file = request.FILES['file'] else: return Response(status=status.HTTP_400_BAD_REQUEST) processor = DocumentProcessor() options = { 'user': request.user, 'barcode': code, } if uploaded_file.size == 0: return Response(status=status.HTTP_400_BAD_REQUEST) document = processor.create(uploaded_file, options) if len(processor.errors) > 0: log.error('OldFileHandler.create errors: %s' % processor.errors) error = processor.errors[0] if error.__class__.__name__ in ['unicode', 'str']: return Response(status=status.HTTP_400_BAD_REQUEST) if error.code == 409: new_processor = DocumentProcessor() options['update_file'] = uploaded_file document = new_processor.update(code, options) if len(new_processor.errors) > 0: return Response(status=status.HTTP_400_BAD_REQUEST) log.info('OldFileHandler.create request fulfilled for %s' % document.get_filename()) return Response(document.get_filename(), status=status.HTTP_200_OK)
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
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)
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)
def update(self, request, code, suggested_format=None): revision, hashcode, extra = self._get_info(request) uploaded_obj = None if 'file' in request.FILES: uploaded_obj = request.FILES['file'] # TODO refactor these verbs tag_string = request.PUT.get('tag_string', None) remove_tag_string = request.PUT.get('remove_tag_string', None) new_name = request.PUT.get('new_name', None) new_type = extra.get('new_type', None) index_data = extra.get('indexing_data', None) if index_data: index_data = json.loads(index_data) processor = DocumentProcessor() options = { 'tag_string': tag_string, 'remove_tag_string': remove_tag_string, 'extension': suggested_format, 'new_name': new_name, 'new_type': new_type, 'new_indexes': index_data, 'update_file': uploaded_obj, 'user': request.user, } # FIXME hashcode missing? document = processor.update(code, options) if len(processor.errors) > 0: print processor.errors log.error('FileHandler.update manager errors %s' % processor.errors) if settings.DEBUG: raise Exception('FileHandler.update manager errors') else: return rc.BAD_REQUEST log.info('FileHandler.update request fulfilled for code: %s, format: %s, rev: %s, hash: %s.' % (code, suggested_format, revision, hashcode)) resp = DMSOBjectRevisionsData(document).jsons return HttpResponse(resp) # FIXME should be rc.ALL_OK
def create(self, request, code, suggested_format=None): if 'file' in request.FILES: uploaded_file = request.FILES['file'] else: return rc.BAD_REQUEST processor = DocumentProcessor() options = { 'user': request.user, 'barcode': code, } document = processor.create(uploaded_file, options) if len(processor.errors) > 0: log.error('OldFileHandler.create errors: %s' % processor.errors) error = processor.errors[0] if error.__class__.__name__ in ['unicode', 'str']: return rc.BAD_REQUEST # Should be "No such document type error" if error.code == 409: new_processor = DocumentProcessor() options['update_file'] = uploaded_file document = new_processor.update(code, options) if len(new_processor.errors) > 0: return rc.BAD_REQUEST log.info('OldFileHandler.create request fulfilled for %s' % document.get_filename()) return document.get_filename()
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)
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)
def edit_result(request, step='edit_finish', template='mdtui/indexing.html'): """Confirmation step for editing indexes @param request: is a Django request object @param step: is a current step name (for template rendering) @param template: is a name of template for this view""" # initialising context required_vars = ('edit_processor_indexes', 'edit_index_barcode', 'old_document_keys', 'edit_return', "edit_mdts") variables = {} warnings = [] for var in required_vars: try: variables[var] = request.session[var] except KeyError: variables[var] = '' # Error handling into warnings if not var == 'edit_return': error_name = MDTUI_ERROR_STRINGS['ERROR_EDIT_INDEXES_FINISHED'] log.error('indexing_finished error: variable: %s, %s' % (var, error_name)) if not error_name in warnings: warnings.append(error_name) pass log.debug( 'indexing_edit_result called with: step: "%s", variables: "%s",' % (step, variables)) if request.POST: code = variables['edit_index_barcode'] processor = DocumentProcessor() options = { 'new_indexes': variables['edit_processor_indexes'], 'user': request.user, } processor.update(code, options=options) if not processor.errors: # cleanup session here because editing is finished for var in required_vars: _cleanup_session_var(request, var) return HttpResponseRedirect(variables['edit_return']) else: for error in processor.errors: warnings.append(error) # Building new indexes for confirmation rendering context_secondary_indexes = {} if 'edit_processor_indexes' in variables.iterkeys( ) and variables['edit_processor_indexes']: for index, value in variables['edit_processor_indexes'].iteritems(): if not index in ['metadata_user_name', 'metadata_user_id']: context_secondary_indexes[index] = value if 'description' in variables['edit_processor_indexes']: old_description = variables['old_document_keys']['description'] else: old_description = '' context = { 'step': step, 'document_keys': context_secondary_indexes, 'new_description': context_secondary_indexes.get('description', None), 'barcode': variables['edit_index_barcode'], 'old_document_keys': variables['old_document_keys'], 'old_description': old_description, 'edit_return': variables['edit_return'], 'warnings': warnings, } return render(request, template, context)
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
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
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)
def indexing_source(request, step=None, template='mdtui/indexing.html'): """Indexing: Step 3: Upload File / Associate File / Print Barcode @param request: is a Django request object @param step: is a current step name (for template rendering) @param template: is a name of template for this view""" context = {} warnings = [] valid_call = True temp_vars = {} upload_file = None # Check session variables, init context and add proper user warnings for var_name, context_var, action in [ ('document_keys', "document_keys_dict", 'NO_INDEX'), ('barcode', 'barcode', 'NO_INDEX'), ('index_info', 'document_keys_dict', 'NO_S_KEYS'), ('docrule', 'indexing_docrule_id', 'NO_DOCRULE'), ]: try: temp_vars[var_name] = None # Make sure it will definitely be there (Proper init) temp_var = request.session[context_var] temp_vars[var_name] = temp_var except KeyError: valid_call = False if not MDTUI_ERROR_STRINGS[action] in warnings: warnings.append(MDTUI_ERROR_STRINGS[action]) document_keys = temp_vars['document_keys'] barcode = temp_vars['barcode'] index_info = temp_vars['index_info'] docrule = str(temp_vars['docrule']) # Init Forms correctly depending on url posted if request.GET.get('uploaded') is None: upload_form = DocumentUploadForm() else: upload_form = DocumentUploadForm(request.POST or None, request.FILES or None) if request.GET.get('barcoded') is None: barcode_form = BarcodePrintedForm() else: barcode_form = BarcodePrintedForm(request.POST or None) log.debug('indexing_source view called with document_keys: %s, barcode: %s, index_info: %s, docrule: %s' % (document_keys, barcode, index_info, docrule)) # Appending warnings for creating a new parrallel key/value pair. new_sec_key_pairs = check_for_secondary_keys_pairs(index_info, docrule) if new_sec_key_pairs: for new_key, new_value in new_sec_key_pairs.iteritems(): warnings.append(MDTUI_ERROR_STRINGS['NEW_KEY_VALUE_PAIR'] + new_key + ': ' + new_value) if upload_form.is_valid() or barcode_form.is_valid() and valid_call: if valid_call: # Unifying dates to CouchDB storage formats. # TODO: maybe make it a part of the CouchDB storing manager. clean_index = unify_index_info_couch_dates_fmt(index_info) # Storing into DMS with main Document Processor and current indexes processor = DocumentProcessor() options = { 'user': request.user, 'index_info': clean_index, 'barcode': barcode, } if upload_form.is_valid(): upload_file = upload_form.files['file'] else: options['only_metadata'] = True processor.create(upload_file, options) if not processor.errors: if 'only_metadata' in options and options['only_metadata'] is not None: # For silent barcode storage in MUI return HttpResponse('OK') return HttpResponseRedirect(reverse('mdtui-index-finished')) else: # FIXME: dodgy error handling log.error(str(processor.errors)) return HttpResponse(str(processor.errors)) else: warnings.append(MDTUI_ERROR_STRINGS['NOT_VALID_INDEXING']) context.update({ 'step': step, 'valid_call': valid_call, 'upload_form': upload_form, 'barcode_form': barcode_form, 'document_keys': document_keys, 'warnings': warnings, 'barcode': barcode, }) return render_to_response(template, context, context_instance=RequestContext(request))
def indexing_source(request, step=None, template='mdtui/indexing.html'): """Indexing: Step 3: Upload File / Associate File / Print Barcode @param request: is a Django request object @param step: is a current step name (for template rendering) @param template: is a name of template for this view""" context = {} warnings = [] valid_call = True temp_vars = {} upload_file = None # Check session variables, init context and add proper user warnings for var_name, context_var, action in [ ('document_keys', "document_keys_dict", 'NO_INDEX'), ('barcode', 'barcode', 'NO_INDEX'), ('index_info', 'document_keys_dict', 'NO_S_KEYS'), ('docrule', 'indexing_docrule_id', 'NO_DOCRULE'), ]: try: temp_vars[ var_name] = None # Make sure it will definitely be there (Proper init) temp_var = request.session[context_var] temp_vars[var_name] = temp_var except KeyError: valid_call = False if not MDTUI_ERROR_STRINGS[action] in warnings: warnings.append(MDTUI_ERROR_STRINGS[action]) document_keys = temp_vars['document_keys'] barcode = temp_vars['barcode'] index_info = temp_vars['index_info'] docrule = str(temp_vars['docrule']) # Init Forms correctly depending on url posted if request.GET.get('uploaded') is None: upload_form = DocumentUploadForm() else: upload_form = DocumentUploadForm(request.POST or None, request.FILES or None) if request.GET.get('barcoded') is None: barcode_form = BarcodePrintedForm() else: barcode_form = BarcodePrintedForm(request.POST or None) log.debug( 'indexing_source view called with document_keys: %s, barcode: %s, index_info: %s, docrule: %s' % (document_keys, barcode, index_info, docrule)) # Appending warnings for creating a new parrallel key/value pair. new_sec_key_pairs = check_for_secondary_keys_pairs(index_info, docrule) if new_sec_key_pairs: for new_key, new_value in new_sec_key_pairs.iteritems(): warnings.append(MDTUI_ERROR_STRINGS['NEW_KEY_VALUE_PAIR'] + new_key + ': ' + new_value) if upload_form.is_valid() or barcode_form.is_valid() and valid_call: if valid_call: # Unifying dates to CouchDB storage formats. # TODO: maybe make it a part of the CouchDB storing manager. clean_index = unify_index_info_couch_dates_fmt(index_info) # Storing into DMS with main Document Processor and current indexes processor = DocumentProcessor() options = { 'user': request.user, 'index_info': clean_index, 'barcode': barcode, } if upload_form.is_valid(): upload_file = upload_form.files['file'] else: options['only_metadata'] = True processor.create(upload_file, options) if not processor.errors: if 'only_metadata' in options and options[ 'only_metadata'] is not None: # For silent barcode storage in MUI return HttpResponse('OK') return HttpResponseRedirect(reverse('mdtui-index-finished')) else: # FIXME: dodgy error handling log.error(str(processor.errors)) return HttpResponse(str(processor.errors)) else: warnings.append(MDTUI_ERROR_STRINGS['NOT_VALID_INDEXING']) context.update({ 'step': step, 'valid_call': valid_call, 'upload_form': upload_form, 'barcode_form': barcode_form, 'document_keys': document_keys, 'warnings': warnings, 'barcode': barcode, }) return render_to_response(template, context, context_instance=RequestContext(request))