Esempio n. 1
0
def submit_files(request, article_id):
    """
    Allows the submitting author to upload files and links them to the submission
    :param request: HttpRequest object
    :param article_id: Article PK
    :return: HttpResponse
    """
    article = get_object_or_404(models.Article, pk=article_id)
    form = forms.FileDetails()

    if article.current_step < 3 and not request.user.is_staff:
        return redirect(reverse('submit_authors', kwargs={'article_id': article_id}))

    error, modal = None, None

    if request.POST:

        if 'delete' in request.POST:
            file_id = request.POST.get('delete')
            file = get_object_or_404(core_models.File, pk=file_id, article_id=article.pk)
            file.delete()
            messages.add_message(request, messages.WARNING, 'File deleted')
            return redirect(reverse('submit_files', kwargs={'article_id': article_id}))

        if 'manuscript' in request.POST:
            form = forms.FileDetails(request.POST)
            uploaded_file = request.FILES.get('file')
            if logic.check_file(uploaded_file, request, form):
                if form.is_valid():
                    new_file = files.save_file_to_article(uploaded_file, article, request.user)
                    article.manuscript_files.add(new_file)
                    new_file.label = form.cleaned_data['label']
                    new_file.description = form.cleaned_data['description']
                    new_file.save()
                    return redirect(reverse('submit_files', kwargs={'article_id': article_id}))
                else:
                    modal = 'manuscript'
            else:
                modal = 'manuscript'

        if 'data' in request.POST:
            for uploaded_file in request.FILES.getlist('file'):
                form = forms.FileDetails(request.POST)
                if form.is_valid():
                    new_file = files.save_file_to_article(uploaded_file, article, request.user)
                    article.data_figure_files.add(new_file)
                    new_file.label = form.cleaned_data['label']
                    new_file.description = form.cleaned_data['description']
                    new_file.save()
                    return redirect(reverse('submit_files', kwargs={'article_id': article_id}))
                else:
                    modal = 'data'

        if 'next_step' in request.POST:
            if article.manuscript_files.all().count() >= 1:
                article.current_step = 4
                article.save()
                return redirect(reverse('submit_review', kwargs={'article_id': article_id}))
            else:
                error = "You must upload a manuscript file."

    template = "admin/submission//submit_files.html"
    context = {
        'article': article,
        'error': error,
        'form': form,
        'modal': modal,
    }

    return render(request, template, context)
Esempio n. 2
0
def preprints_files(request, article_id):
    """
    Allows authors to upload files to their preprint. Files are stored against the press in /files/preprints/
    File submission can be limited to PDF only.
    :param request: HttpRequest
    :param article_id: Article object PK
    :return: HttpRedirect or HttpResponse
    """
    article = get_object_or_404(submission_models.Article.preprints,
                                pk=article_id,
                                owner=request.user,
                                date_submitted__isnull=True)

    error, modal, form = None, None, submission_forms.FileDetails()

    if request.POST and 'delete' in request.POST:
        file_id = request.POST.get('delete')
        file = get_object_or_404(core_models.File, pk=file_id)
        file.unlink_file(journal=None)
        file.delete()
        messages.add_message(request, messages.WARNING, 'File deleted')
        return redirect(
            reverse('preprints_files', kwargs={'article_id': article_id}))

    if request.POST and request.FILES:

        form = submission_forms.FileDetails(request.POST)
        uploaded_file = request.FILES.get('file')

        # If required, check if the file is a PDF:
        if request.press.preprint_pdf_only and 'manuscript' in request.POST:
            if not files.check_in_memory_mime(
                    in_memory_file=uploaded_file) == 'application/pdf':
                form.add_error(None,
                               'You must upload a PDF for your manuscript')
                modal = 'manuscript'

        # Check if the form is valid
        if form.is_valid():

            file = files.save_file_to_article(uploaded_file, article,
                                              request.user,
                                              form.cleaned_data['label'],
                                              form.cleaned_data['description'])

            if 'manuscript' in request.POST:
                article.manuscript_files.add(file)

            elif 'data' in request.POST:
                article.data_figure_files.add(file)

            messages.add_message(request, messages.INFO, 'File saved.')
            return redirect(
                reverse('preprints_files', kwargs={'article_id': article.pk}))

        # Handle displaying modals in event of an error:
        else:
            modal = preprint_logic.get_display_modal(request)

    elif request.POST and 'next_step' in request.POST:
        return redirect(
            reverse('preprints_review', kwargs={'article_id': article.pk}))

    template = 'preprints/submit_files.html'
    context = {
        'article': article,
        'form': form,
        'modal': modal,
    }

    return render(request, template, context)