示例#1
0
def convert_file(request, article_id=None, file_id=None):
    """
    Try to get article's manuscript file (should be docx or rtf), convert to markdown, then convert to html,
    save new files in applicable locations, register as Galley objects in database. Refresh submission page with new galley objects.
    If request is GET, render button to convert.
    """

    # retrieve article and selected manuscript
    article = get_object_or_404(sub_models.Article, pk=article_id)
    manuscript = get_object_or_404(core_models.File,
                                   pk=file_id,
                                   article_id=article.pk)
    file_path = manuscript.self_article_path()

    plugin = models.Plugin.objects.get(name=plugin_settings.SHORT_NAME)
    extract_images = setting_handler.get_plugin_setting(
        plugin,
        'pandoc_extract_images',
        request.journal,
        create=True,
        pretty='Pandoc extract images',
        types='boolean').processed_value

    try:
        html, images = convert.generate_html_from_doc(file_path,
                                                      extract_images)
    except (TypeError, convert.PandocError) as err:
        messages.add_message(request, messages.ERROR, err)
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

    stripped, _ = os.path.splitext(file_path)
    output_path = stripped + '.html'
    with open(output_path, mode="w", encoding="utf-8") as html_file:
        print(html, file=html_file)

    galley = logic.save_galley(
        article,
        request,
        output_path,
        True,
        'HTML',
        save_to_disk=False,
    )
    messages.add_message(request, messages.INFO, "HTML generated succesfully")

    for image in images:
        image_name = os.path.basename(image)
        with open(image, 'rb') as image_reader:
            image_file = ContentFile(image_reader.read())
            image_file.name = image_name
            logic.save_galley_image(galley,
                                    request,
                                    image_file,
                                    image_name,
                                    fixed=False)

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
示例#2
0
def edit_galley(request, galley_id, typeset_id=None, article_id=None):
    if typeset_id:
        typeset_task = get_object_or_404(models.TypesetTask,
                                         pk=typeset_id,
                                         accepted__isnull=False,
                                         completed__isnull=True)
        article = typeset_task.assignment.article
    else:
        typeset_task = None
        article = get_object_or_404(submission_models.Article,
                                    pk=article_id)
    galley = get_object_or_404(core_models.Galley,
                               pk=galley_id,
                               article=article)

    if request.POST:

        if 'delete' in request.POST:
            if typeset_task:
                logic.handle_delete_request(request, galley, typeset_task=typeset_task, page="edit")
                return redirect(reverse('do_typeset_task', kwargs={'typeset_id': typeset_task.pk}))
            else:
                logic.handle_delete_request(request, galley, article=article, page="pm_edit")
                return redirect(reverse('production_article', kwargs={'article_id': article.pk}))

        label = request.POST.get('label')
        if 'fixed-image-upload' in request.POST:
            for uploaded_file in request.FILES.getlist('image'):
                logic.save_galley_image(galley, request, uploaded_file, label, fixed=True)
        if 'image-upload' in request.POST:
            for uploaded_file in request.FILES.getlist('image'):
                logic.save_galley_image(galley, request, uploaded_file, label, fixed=False)
        elif 'css-upload' in request.POST:
            for uploaded_file in request.FILES.getlist('css'):
                logic.save_galley_css(galley, request, uploaded_file, 'galley-{0}.css'.format(galley.id), label)
        if 'galley-label' in request.POST:
            galley.label = request.POST.get('galley_label')
            galley.save()
        if 'replace-galley' in request.POST:
            logic.replace_galley_file(article, request, galley, request.FILES.get('galley'))

        if typeset_task:
            return redirect(reverse('edit_galley', kwargs={'typeset_id': typeset_id, 'galley_id': galley_id}))
        else:
            return redirect(reverse('pm_edit_galley', kwargs={'article_id': article.pk, 'galley_id': galley_id}))

    template = 'production/edit_galley.html'
    context = {
        'typeset_task': typeset_task,
        'galley': galley,
        'article': galley.article,
        'image_names': logic.get_image_names(galley)
    }

    return render(request, template, context)
示例#3
0
def edit_galley(request, galley_id, typeset_id=None, article_id=None):
    """
    Allows a typesetter or editor to edit a Galley file.
    :param request: HttpRequest object
    :param galley_id: Galley object PK
    :param typeset_id: TypesetTask PK, optional
    :param article_id: Article PK, optiona
    :return: HttpRedirect or HttpResponse
    """
    return_url = request.GET.get('return', None)

    if typeset_id:
        typeset_task = get_object_or_404(
            models.TypesetTask,
            pk=typeset_id,
            assignment__article__journal=request.journal,
            accepted__isnull=False,
            completed__isnull=True,
        )
        article = typeset_task.assignment.article
    else:
        typeset_task = None
        article = get_object_or_404(
            submission_models.Article.allarticles,
            pk=article_id,
            journal=request.journal
        )
    galley = get_object_or_404(core_models.Galley,
                               pk=galley_id,
                               article=article)

    if request.POST:

        if 'delete' in request.POST:
            if typeset_task:
                logic.handle_delete_request(request, galley, typeset_task=typeset_task, page="edit")
                return redirect(reverse('do_typeset_task', kwargs={'typeset_id': typeset_task.pk}))
            else:
                logic.handle_delete_request(request, galley, article=article, page="pm_edit")
                if not return_url:
                    return redirect(reverse('production_article', kwargs={'article_id': article.pk}))
                else:
                    return redirect(return_url)

        label = request.POST.get('label')

        if 'fixed-image-upload' in request.POST:
            if request.POST.get('datafile') is not None:
                logic.use_data_file_as_galley_image(galley, request, label)
            for uploaded_file in request.FILES.getlist('image'):
                logic.save_galley_image(galley, request, uploaded_file, label, fixed=True)

        if 'image-upload' in request.POST:
            for uploaded_file in request.FILES.getlist('image'):
                logic.save_galley_image(galley, request, uploaded_file, label, fixed=False)

        elif 'css-upload' in request.POST:
            for uploaded_file in request.FILES.getlist('css'):
                logic.save_galley_css(galley, request, uploaded_file, 'galley-{0}.css'.format(galley.id), label)

        if 'galley-label' in request.POST:
            galley.label = request.POST.get('galley_label')
            galley.save()

        if 'replace-galley' in request.POST:
            logic.replace_galley_file(article, request, galley, request.FILES.get('galley'))

        if typeset_task:
            return redirect(reverse('edit_galley', kwargs={'typeset_id': typeset_id, 'galley_id': galley_id}))
        else:
            return_path = '?return={return_url}'.format(return_url=return_url) if return_url else ''
            url = reverse('pm_edit_galley', kwargs={'article_id': article.pk, 'galley_id': galley_id})
            redirect_url = '{url}{return_path}'.format(url=url, return_path=return_path)
            return redirect(redirect_url)

    template = 'production/edit_galley.html'
    context = {
        'typeset_task': typeset_task,
        'galley': galley,
        'article': galley.article,
        'image_names': logic.get_image_names(galley),
        'return_url': return_url,
        'data_files': article.data_figure_files.all(),
        'galley_images': galley.images.all()
    }

    return render(request, template, context)