-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathviews.py
More file actions
115 lines (94 loc) · 4.6 KB
/
views.py
File metadata and controls
115 lines (94 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
from django.shortcuts import redirect, render, get_object_or_404
from django.contrib import messages
from django.core.files.base import ContentFile
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.views.decorators.http import require_POST
from core import models as core_models
from production import logic
from security.decorators import production_user_or_editor_required
from submission import models as sub_models
from utils import setting_handler, models
from plugins.pandoc_plugin import forms, plugin_settings, convert
def index(request):
"""
Render admin page allowing users to enable or disable the plugin
"""
plugin = models.Plugin.objects.get(name=plugin_settings.SHORT_NAME)
pandoc_enabled = setting_handler.get_plugin_setting(plugin, 'pandoc_enabled', request.journal, create=True,
pretty='Enable Pandoc', types='boolean').processed_value
extract_images = setting_handler.get_plugin_setting(
plugin, 'pandoc_extract_images', request.journal, create=True,
pretty='Pandoc extract images', types='boolean').processed_value
admin_form = forms.PandocAdminForm(initial={
'pandoc_enabled': pandoc_enabled,
'pandoc_extract_images': extract_images,
})
if request.POST:
admin_form = forms.PandocAdminForm(request.POST)
if admin_form.is_valid():
for setting_name, setting_value in admin_form.cleaned_data.items():
setting_handler.save_plugin_setting(plugin, setting_name, setting_value, request.journal)
messages.add_message(request, messages.SUCCESS, '{0} setting updated.'.format(setting_name))
return redirect(reverse('pandoc_index'))
template = "pandoc_plugin/index.html"
context = {
'admin_form': admin_form,
}
return render(request, template, context)
@require_POST
@production_user_or_editor_required
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
resize_images = setting_handler.get_plugin_setting(
plugin, 'pandoc_resize_images', request.journal, create=True,
pretty='Pandoc resize images', types='boolean').processed_value
default_dpi = (
int(setting_handler.get_plugin_setting(
plugin, 'pandoc_default_dpi_x', request.journal, create=True,
pretty='Pandoc Default DPI X', types='int').processed_value),
int(setting_handler.get_plugin_setting(
plugin, 'pandoc_default_dpi_y', request.journal, create=True,
pretty='Pandoc Default DPI Y', types='int').processed_value)
)
try:
html, images = convert.generate_html_from_doc(file_path, extract_images, resize_images, default_dpi)
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 successfully")
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'))
# NEED LOGIC FOR IF HTML ALREADY GENERATED