Exemplo n.º 1
0
def staging_file_thumbnail(request, source_id, staging_file_id):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
    staging_folder = get_object_or_404(StagingFolder, pk=source_id)
    StagingFile = create_staging_file_class(request, staging_folder.folder_path, source=staging_folder)
    transformations, errors = SourceTransformation.transformations.get_for_object_as_list(staging_folder)

    output_file = StagingFile.get(staging_file_id).get_image(
        size=THUMBNAIL_SIZE,
        transformations=transformations
    )
    if errors and (request.user.is_staff or request.user.is_superuser):
        for error in errors:
            messages.warning(request, _(u'Staging file transformation error: %(error)s') % {
                'error': error
            })

    return sendfile.sendfile(request, output_file)
Exemplo n.º 2
0
def staging_file_thumbnail(request, source_id, staging_file_id):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
    staging_folder = get_object_or_404(StagingFolder, pk=source_id)
    StagingFile = create_staging_file_class(request,
                                            staging_folder.folder_path,
                                            source=staging_folder)
    transformations, errors = SourceTransformation.transformations.get_for_object_as_list(
        staging_folder)

    output_file = StagingFile.get(staging_file_id).get_image(
        size=THUMBNAIL_SIZE, transformations=transformations)
    if errors and (request.user.is_staff or request.user.is_superuser):
        for error in errors:
            messages.warning(
                request,
                _(u'Staging file transformation error: %(error)s') %
                {'error': error})

    return sendfile.sendfile(request, output_file)
Exemplo n.º 3
0
def staging_file_delete(request, source_type, source_id, staging_file_id):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
    staging_folder = get_object_or_404(StagingFolder, pk=source_id)
    StagingFile = create_staging_file_class(request, staging_folder.folder_path)

    staging_file = StagingFile.get(staging_file_id)
    next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/')))
    previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/')))

    if request.method == 'POST':
        try:
            transformations, errors = SourceTransformation.transformations.get_for_object_as_list(staging_folder)
            staging_file.delete(
                preview_size=staging_folder.get_preview_size(),
                transformations=transformations
            )
            messages.success(request, _(u'Staging file delete successfully.'))
        except Exception, e:
            messages.error(request, _(u'Staging file delete error; %s.') % e)
        return HttpResponseRedirect(next)
Exemplo n.º 4
0
def staging_file_delete(request, source_type, source_id, staging_file_id):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
    staging_folder = get_object_or_404(StagingFolder, pk=source_id)
    StagingFile = create_staging_file_class(request,
                                            staging_folder.folder_path)

    staging_file = StagingFile.get(staging_file_id)
    next = request.POST.get(
        'next', request.GET.get('next', request.META.get('HTTP_REFERER', '/')))
    previous = request.POST.get(
        'previous',
        request.GET.get('previous', request.META.get('HTTP_REFERER', '/')))

    if request.method == 'POST':
        try:
            transformations, errors = SourceTransformation.transformations.get_for_object_as_list(
                staging_folder)
            staging_file.delete(preview_size=staging_folder.get_preview_size(),
                                transformations=transformations)
            messages.success(request, _(u'Staging file delete successfully.'))
        except Exception, e:
            messages.error(request, _(u'Staging file delete error; %s.') % e)
        return HttpResponseRedirect(next)
Exemplo n.º 5
0
def upload_interactive(request, source_type=None, source_id=None):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])

    subtemplates_list = []

    context = {}

    results = get_active_tab_links()

    if results[SOURCE_CHOICE_WEB_FORM].count() == 0 and results[SOURCE_CHOICE_STAGING].count() == 0:
        source_setup_link = mark_safe('<a href="%s">%s</a>' % (reverse('setup_web_form_list'), ugettext(u'here')))
        subtemplates_list.append(
            {
                'name': 'generic_subtemplate.html',
                'context': {
                    'title': _(u'Upload sources'),
                    'paragraphs': [
                        _(u'No interactive document sources have been defined or none have been enabled.'),
                        _(u'Click %(setup_link)s to add or enable some document sources.') % {
                            'setup_link': source_setup_link
                        }
                    ],
                }
            })

    document_type_id = request.GET.get('document_type_id', None)
    if document_type_id:
        document_type = get_object_or_404(DocumentType, pk=document_type_id[0])
    else:
        document_type = None

    if source_type is None and source_id is None:
        if results[SOURCE_CHOICE_WEB_FORM].count():
            source_type = results[SOURCE_CHOICE_WEB_FORM][0].source_type
            source_id = results[SOURCE_CHOICE_WEB_FORM][0].pk
        elif results[SOURCE_CHOICE_STAGING].count():
            source_type = results[SOURCE_CHOICE_STAGING][0].source_type
            source_id = results[SOURCE_CHOICE_STAGING][0].pk

    if source_type and source_id:
        if source_type == SOURCE_CHOICE_WEB_FORM:
            web_form = get_object_or_404(WebForm, pk=source_id)
            context['source'] = web_form
            if request.method == 'POST':
                form = WebFormForm(request.POST, request.FILES,
                    document_type=document_type,
                    show_expand=(web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
                    source=web_form
                )
                if form.is_valid():
                    try:
                        if web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
                            expand = form.cleaned_data['expand']
                        else:
                            if web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
                                expand = True
                            else:
                                expand = False

                        new_filename = get_form_filename(form)
                        web_form.upload_file(request.FILES['file'],
                            new_filename, document_type=document_type,
                            expand=expand,
                            metadata_dict_list=decode_metadata_from_url(request.GET),
                            user=request.user
                        )
                        messages.success(request, _(u'Document uploaded successfully.'))
                    except Exception, e:
                        messages.error(request, e)

                    return HttpResponseRedirect(request.get_full_path())
            else:
                form = WebFormForm(
                    show_expand=(web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
                    document_type=document_type,
                    source=web_form
                )

            subtemplates_list.append({
                'name': 'generic_form_subtemplate.html',
                'context': {
                    'form': form,
                    'title': _(u'upload a local document from source: %s') % web_form.title,
                },
            })
        elif source_type == SOURCE_CHOICE_STAGING:
            staging_folder = get_object_or_404(StagingFolder, pk=source_id)
            context['source'] = staging_folder
            StagingFile = create_staging_file_class(request, staging_folder.folder_path, source=staging_folder)
            if request.method == 'POST':
                form = StagingDocumentForm(request.POST, request.FILES,
                    cls=StagingFile, document_type=document_type,
                    show_expand=(staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
                    source=staging_folder
                )
                if form.is_valid():
                    try:
                        staging_file = StagingFile.get(form.cleaned_data['staging_file_id'])
                        if staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
                            expand = form.cleaned_data['expand']
                        else:
                            if staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
                                expand = True
                            else:
                                expand = False
                        new_filename = get_form_filename(form)
                        staging_folder.upload_file(staging_file.upload(),
                            new_filename, document_type=document_type,
                            expand=expand,
                            metadata_dict_list=decode_metadata_from_url(request.GET),
                            user=request.user
                        )
                        messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename)

                        if staging_folder.delete_after_upload:
                            transformations, errors = staging_folder.get_transformation_list()
                            staging_file.delete(preview_size=staging_folder.get_preview_size(), transformations=transformations)
                            messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename)
                    except Exception, e:
                        messages.error(request, e)

                    return HttpResponseRedirect(request.get_full_path())
            else:
                form = StagingDocumentForm(cls=StagingFile,
                    document_type=document_type,
                    show_expand=(staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
                    source=staging_folder
                )
            try:
                staging_filelist = StagingFile.get_all()
            except Exception, e:
                messages.error(request, e)
                staging_filelist = []
Exemplo n.º 6
0
def upload_interactive(request,
                       source_type=None,
                       source_id=None,
                       document_pk=None):
    check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])

    subtemplates_list = []

    if document_pk:
        document = get_object_or_404(Document, pk=document_pk)
        results = get_active_tab_links(document)
    else:
        document = None
        results = get_active_tab_links()

    context = {}

    if results[SOURCE_CHOICE_WEB_FORM].count(
    ) == 0 and results[SOURCE_CHOICE_STAGING].count() == 0:
        source_setup_link = mark_safe(
            '<a href="%s">%s</a>' %
            (reverse('setup_web_form_list'), ugettext(u'here')))
        subtemplates_list.append({
            'name': 'generic_subtemplate.html',
            'context': {
                'title':
                _(u'Upload sources'),
                'paragraphs': [
                    _(u'No interactive document sources have been defined or none have been enabled.'
                      ),
                    _(u'Click %(setup_link)s to add or enable some document sources.'
                      ) % {
                          'setup_link': source_setup_link
                      }
                ],
            }
        })

    document_type_id = request.GET.get('document_type_id', None)
    if document_type_id:
        document_type = get_object_or_404(DocumentType, pk=document_type_id[0])
    else:
        document_type = None

    if source_type is None and source_id is None:
        if results[SOURCE_CHOICE_WEB_FORM].count():
            source_type = results[SOURCE_CHOICE_WEB_FORM][0].source_type
            source_id = results[SOURCE_CHOICE_WEB_FORM][0].pk
        elif results[SOURCE_CHOICE_STAGING].count():
            source_type = results[SOURCE_CHOICE_STAGING][0].source_type
            source_id = results[SOURCE_CHOICE_STAGING][0].pk

    if source_type and source_id:
        if source_type == SOURCE_CHOICE_WEB_FORM:
            web_form = get_object_or_404(WebForm, pk=source_id)
            context['source'] = web_form
            if request.method == 'POST':
                form = WebFormForm(
                    request.POST,
                    request.FILES,
                    document_type=document_type,
                    show_expand=(web_form.uncompress
                                 == SOURCE_UNCOMPRESS_CHOICE_ASK)
                    and not document,
                    source=web_form,
                    instance=document)
                if form.is_valid():
                    try:
                        if document:
                            expand = False
                        else:
                            if web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
                                expand = form.cleaned_data.get('expand')
                            else:
                                if web_form.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
                                    expand = True
                                else:
                                    expand = False

                        new_filename = get_form_filename(form)

                        web_form.upload_file(
                            request.FILES['file'],
                            new_filename,
                            use_file_name=form.cleaned_data.get(
                                'use_file_name', False),
                            document_type=document_type,
                            expand=expand,
                            metadata_dict_list=decode_metadata_from_url(
                                request.GET),
                            user=request.user,
                            document=document,
                            new_version_data=form.cleaned_data.get(
                                'new_version_data'))
                        if document:
                            messages.success(
                                request,
                                _(u'Document version uploaded successfully.'))
                            return HttpResponseRedirect(
                                reverse('document_view_simple',
                                        args=[document.pk]))
                        else:
                            messages.success(
                                request, _(u'Document uploaded successfully.'))
                            return HttpResponseRedirect(
                                request.get_full_path())
                    except Exception, e:
                        messages.error(request,
                                       _(u'Unhandled exception: %s') % e)
            else:
                form = WebFormForm(
                    show_expand=(web_form.uncompress
                                 == SOURCE_UNCOMPRESS_CHOICE_ASK)
                    and not document,
                    document_type=document_type,
                    source=web_form,
                    instance=document)
            if document:
                title = _(
                    u'upload a new version from source: %s') % web_form.title
            else:
                title = _(u'upload a local document from source: %s'
                          ) % web_form.title

            subtemplates_list.append({
                'name': 'generic_form_subtemplate.html',
                'context': {
                    'form': form,
                    'title': title,
                },
            })
        elif source_type == SOURCE_CHOICE_STAGING:
            staging_folder = get_object_or_404(StagingFolder, pk=source_id)
            context['source'] = staging_folder
            StagingFile = create_staging_file_class(request,
                                                    staging_folder.folder_path,
                                                    source=staging_folder)
            if request.method == 'POST':
                form = StagingDocumentForm(
                    request.POST,
                    request.FILES,
                    cls=StagingFile,
                    document_type=document_type,
                    show_expand=(staging_folder.uncompress
                                 == SOURCE_UNCOMPRESS_CHOICE_ASK)
                    and not document,
                    source=staging_folder,
                    instance=document)
                if form.is_valid():
                    try:
                        staging_file = StagingFile.get(
                            form.cleaned_data['staging_file_id'])
                        if document:
                            expand = False
                        else:
                            if staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
                                expand = form.cleaned_dataget('expand')
                            else:
                                if staging_folder.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
                                    expand = True
                                else:
                                    expand = False

                        new_filename = get_form_filename(form)

                        staging_folder.upload_file(
                            staging_file.upload(),
                            new_filename,
                            use_file_name=form.cleaned_data.get(
                                'use_file_name', False),
                            document_type=document_type,
                            expand=expand,
                            metadata_dict_list=decode_metadata_from_url(
                                request.GET),
                            user=request.user,
                            document=document,
                            new_version_data=form.cleaned_data.get(
                                'new_version_data'))
                        if document:
                            messages.success(
                                request,
                                _(u'Document version from staging file: %s, uploaded successfully.'
                                  ) % staging_file.filename)
                        else:
                            messages.success(
                                request,
                                _(u'Staging file: %s, uploaded successfully.')
                                % staging_file.filename)

                        if staging_folder.delete_after_upload:
                            transformations, errors = staging_folder.get_transformation_list(
                            )
                            staging_file.delete(
                                preview_size=staging_folder.get_preview_size(),
                                transformations=transformations)
                            messages.success(
                                request,
                                _(u'Staging file: %s, deleted successfully.') %
                                staging_file.filename)
                        if document:
                            return HttpResponseRedirect(
                                reverse('document_view_simple',
                                        args=[document.pk]))
                        else:
                            return HttpResponseRedirect(
                                request.get_full_path())
                    except Exception, e:
                        messages.error(request,
                                       _(u'Unhandled exception: %s') % e)
            else:
                form = StagingDocumentForm(
                    cls=StagingFile,
                    document_type=document_type,
                    show_expand=(staging_folder.uncompress
                                 == SOURCE_UNCOMPRESS_CHOICE_ASK)
                    and not document,
                    source=staging_folder,
                    instance=document)
            try:
                staging_filelist = StagingFile.get_all()
            except Exception, e:
                messages.error(request, e)
                staging_filelist = []