def upload_document_with_type(request, document_type_id, multiple=True): check_permissions(request.user, "documents", [PERMISSION_DOCUMENT_CREATE]) document_type = get_object_or_404(DocumentType, pk=document_type_id) local_form = DocumentForm(prefix="local", initial={"document_type": document_type}) if USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm(prefix="staging", initial={"document_type": document_type}) if request.method == "POST": if "local-submit" in request.POST.keys(): local_form = DocumentForm( request.POST, request.FILES, prefix="local", initial={"document_type": document_type} ) if local_form.is_valid(): try: if (not UNCOMPRESS_COMPRESSED_LOCAL_FILES) or ( UNCOMPRESS_COMPRESSED_LOCAL_FILES and not _handle_zip_file(request, request.FILES["local-file"], document_type) ): instance = local_form.save() _handle_save_document(request, instance, local_form) messages.success(request, _(u"Document uploaded successfully.")) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.get_full_path()) else: return HttpResponseRedirect(reverse("document_list")) elif "staging-submit" in request.POST.keys() and USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm( request.POST, request.FILES, prefix="staging", initial={"document_type": document_type} ) if staging_form.is_valid(): try: staging_file = StagingFile.get(staging_form.cleaned_data["staging_file_id"]) if (not UNCOMPRESS_COMPRESSED_STAGING_FILES) or ( UNCOMPRESS_COMPRESSED_STAGING_FILES and not _handle_zip_file(request, staging_file.upload(), document_type) ): document = Document(file=staging_file.upload(), document_type=document_type) document.save() _handle_save_document(request, document, staging_form) messages.success( request, _(u"Staging file: %s, uploaded successfully.") % staging_file.filename ) if DELETE_STAGING_FILE_AFTER_UPLOAD: staging_file.delete() messages.success(request, _(u"Staging file: %s, deleted successfully.") % staging_file.filename) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.META["HTTP_REFERER"]) else: return HttpResponseRedirect(reverse("document_list"))
def staging_file_delete(request, staging_file_id): staging_file = StagingFile.get(staging_file_id) next = request.POST.get("next", request.GET.get("next", request.META.get("HTTP_REFERER", None))) previous = request.POST.get("previous", request.GET.get("previous", request.META.get("HTTP_REFERER", None))) if request.method == "POST": try: staging_file.delete() messages.success(request, _(u"Staging file delete successfully.")) except Exception, e: messages.error(request, e) return HttpResponseRedirect(next)
def staging_file_delete(request, staging_file_id): staging_file = StagingFile.get(staging_file_id) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', None))) if request.method == 'POST': try: staging_file.delete() messages.success(request, _(u'Staging file delete successfully.')) except Exception, e: messages.error(request, e) return HttpResponseRedirect(next)
def upload_document_with_type(request, document_type_id, multiple=True): check_permissions(request.user, 'documents', [PERMISSION_DOCUMENT_CREATE]) document_type = get_object_or_404(DocumentType, pk=document_type_id) local_form = DocumentForm(prefix='local', initial={'document_type':document_type}) if USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm(prefix='staging', initial={'document_type':document_type}) if request.method == 'POST': if 'local-submit' in request.POST.keys(): local_form = DocumentForm(request.POST, request.FILES, prefix='local', initial={'document_type':document_type}) if local_form.is_valid(): try: if (not UNCOMPRESS_COMPRESSED_LOCAL_FILES) or (UNCOMPRESS_COMPRESSED_LOCAL_FILES and not _handle_zip_file(request, request.FILES['local-file'], document_type)): instance = local_form.save() _handle_save_document(request, instance, local_form) messages.success(request, _(u'Document uploaded successfully.')) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.get_full_path()) else: return HttpResponseRedirect(reverse('document_list')) elif 'staging-submit' in request.POST.keys() and USE_STAGING_DIRECTORY: staging_form = StagingDocumentForm(request.POST, request.FILES, prefix='staging', initial={'document_type':document_type}) if staging_form.is_valid(): try: staging_file = StagingFile.get(staging_form.cleaned_data['staging_file_id']) if (not UNCOMPRESS_COMPRESSED_STAGING_FILES) or (UNCOMPRESS_COMPRESSED_STAGING_FILES and not _handle_zip_file(request, staging_file.upload(), document_type)): document = Document(file=staging_file.upload(), document_type=document_type) document.save() _handle_save_document(request, document, staging_form) messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename) if DELETE_STAGING_FILE_AFTER_UPLOAD: staging_file.delete() messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename) except Exception, e: messages.error(request, e) if multiple: return HttpResponseRedirect(request.META['HTTP_REFERER']) else: return HttpResponseRedirect(reverse('document_list'))
if page_transformation["name"] in TRANFORMATION_CHOICES: output = TRANFORMATION_CHOICES[page_transformation["name"]] % eval(page_transformation["arguments"]) transformation_list.append(output) except Exception, e: if request.user.is_staff: messages.warning( request, _(u"Error for transformation %(transformation)s:, %(error)s") % {"transformation": page_transformation.get_transformation_display(), "error": e}, ) else: pass tranformation_string = " ".join(transformation_list) try: filepath = StagingFile.get(staging_file_id).filepath output_file = convert( filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string, cleanup_files=False ) return serve_file(request, File(file=open(output_file, "r")), content_type="image/jpeg") except UnkownConvertError, e: if request.user.is_staff or request.user.is_superuser: messages.error(request, e) return serve_file(request, File(file=open(u"%simages/%s" % (settings.MEDIA_ROOT, PICTURE_ERROR_MEDIUM), "r"))) except UnknownFormat: return serve_file(request, File(file=open(u"%simages/%s" % (settings.MEDIA_ROOT, PICTURE_UNKNOWN_MEDIUM), "r"))) except Exception, e: if request.user.is_staff or request.user.is_superuser: messages.error(request, e) return serve_file(request, File(file=open(u"%simages/%s" % (settings.MEDIA_ROOT, PICTURE_ERROR_MEDIUM), "r")))
for page_transformation in DEFAULT_TRANSFORMATIONS: try: if page_transformation['name'] in TRANFORMATION_CHOICES: output = TRANFORMATION_CHOICES[page_transformation['name']] % eval(page_transformation['arguments']) transformation_list.append(output) except Exception, e: if request.user.is_staff: messages.warning(request, _(u'Error for transformation %(transformation)s:, %(error)s') % {'transformation':page_transformation.get_transformation_display(), 'error':e}) else: pass tranformation_string = ' '.join(transformation_list) try: filepath = StagingFile.get(staging_file_id).filepath output_file = convert(filepath, size=STAGING_FILES_PREVIEW_SIZE, extra_options=tranformation_string, cleanup_files=False) return serve_file(request, File(file=open(output_file, 'r')), content_type='image/jpeg') except UnkownConvertError, e: if request.user.is_staff or request.user.is_superuser: messages.error(request, e) return serve_file(request, File(file=open(u'%simages/%s' % (settings.MEDIA_ROOT, PICTURE_ERROR_MEDIUM), 'r'))) except UnknownFormat: return serve_file(request, File(file=open(u'%simages/%s' % (settings.MEDIA_ROOT, PICTURE_UNKNOWN_MEDIUM), 'r'))) except Exception, e: if request.user.is_staff or request.user.is_superuser: messages.error(request, e) return serve_file(request, File(file=open(u'%simages/%s' % (settings.MEDIA_ROOT, PICTURE_ERROR_MEDIUM), 'r'))) #TODO: Need permission