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'))
def __init__(self, *args, **kwargs): super(StagingDocumentForm, self).__init__(*args, **kwargs) try: self.fields["staging_file_id"].choices = [ (staging_file.id, staging_file) for staging_file in StagingFile.get_all() ] except: pass if "initial" in kwargs: if "document_type" in kwargs["initial"]: filenames_qs = kwargs["initial"]["document_type"].documenttypefilename_set.filter(enabled=True) if filenames_qs.count() > 0: self.fields["document_type_available_filenames"] = forms.ModelChoiceField( queryset=filenames_qs, required=False, label=_(u"Quick document rename") )
context = { "document_type_id": document_type_id, "form_list": [ { "form": local_form, "title": _(u"upload a local document"), "grid": 6, "grid_clear": False if USE_STAGING_DIRECTORY else True, } ], } if USE_STAGING_DIRECTORY: try: filelist = StagingFile.get_all() except Exception, e: messages.error(request, e) filelist = [] finally: context.update( { "subtemplates_dict": [ { "name": "generic_list_subtemplate.html", "title": _(u"files in staging"), "object_list": filelist, "hide_link": True, } ] }
def __init__(self, *args, **kwargs): super(StagingDocumentForm, self).__init__(*args, **kwargs) try: self.fields['staging_file_id'].choices=[(staging_file.id, staging_file) for staging_file in StagingFile.get_all()] except: pass if 'initial' in kwargs: if 'document_type' in kwargs['initial']: filenames_qs = kwargs['initial']['document_type'].documenttypefilename_set.filter(enabled=True) if filenames_qs.count() > 0: self.fields['document_type_available_filenames'] = forms.ModelChoiceField( queryset=filenames_qs, required=False, label=_(u'Quick document rename'))
context = { 'document_type_id':document_type_id, 'form_list':[ { 'form':local_form, 'title':_(u'upload a local document'), 'grid':6, 'grid_clear':False if USE_STAGING_DIRECTORY else True, }, ], } if USE_STAGING_DIRECTORY: try: filelist = StagingFile.get_all() except Exception, e: messages.error(request, e) filelist = [] finally: context.update({ 'subtemplates_dict':[ { 'name':'generic_list_subtemplate.html', 'title':_(u'files in staging'), 'object_list':filelist, 'hide_link':True, }, ], }) context['form_list'].append(