-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
196 lines (167 loc) · 5.48 KB
/
views.py
File metadata and controls
196 lines (167 loc) · 5.48 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import io
import os
import traceback
from django.contrib import messages
from django.core.management import call_command
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from submission.models import Article
from journal.models import Issue
from plugins.scripts.forms import TransformForm
XSLT_FILE_PATH = os.path.join(
settings.BASE_DIR,
"plugins",
"scripts",
"xsl",
"abstracts.xsl",
)
TITLE_XSLT_FILE_PATH = os.path.join(
settings.BASE_DIR,
"plugins",
"scripts",
"xsl",
"titles.xsl",
)
@staff_member_required
def scripts_manager(request):
template = 'manager.html'
context = {}
return render(request, template, context)
@staff_member_required
def transform_abstract_view(
request,
):
"""
View to trigger the abstract transformation command.
"""
if request.method == "POST":
form = TransformForm(request.POST)
if form.is_valid():
article_id = form.cleaned_data.get("article_id")
issue_id = form.cleaned_data.get("issue_id")
options = {
"xslt_file": XSLT_FILE_PATH,
}
if article_id:
try:
article = Article.objects.get(
pk=article_id,
journal=request.journal,
)
options["article_id"] = article.pk
except Article.DoesNotExist:
messages.error(
request,
f"Article ID {article_id} does not exist for this journal.",
)
return HttpResponseRedirect(request.path)
elif issue_id:
try:
issue = Issue.objects.get(
pk=issue_id,
journal=request.journal,
)
options["issue_ids"] = [issue.pk]
except Issue.DoesNotExist:
messages.error(
request,
f"Issue ID {issue_id} does not exist for this journal.",
)
return HttpResponseRedirect(request.path)
else:
options["journal_codes"] = [request.journal.code]
try:
stdout = io.StringIO()
call_command(
"jats_abstract_to_html",
stdout=stdout,
stderr=stdout,
**options,
)
messages.success(request, f"Transformation complete.\n{stdout.getvalue()}")
except Exception as e:
messages.error(
request,
f"An error occurred: {str(e)}\n{traceback.format_exc()}",
)
return HttpResponseRedirect(request.path)
else:
form = TransformForm()
template = "transform_abstract_form.html"
context = {
"form": form,
}
return render(
request,
template,
context,
)
@staff_member_required
def transform_title_view(
request,
):
"""
View to trigger the title transformation command.
"""
if request.method == "POST":
form = TransformForm(request.POST)
if form.is_valid():
article_id = form.cleaned_data.get("article_id")
issue_id = form.cleaned_data.get("issue_id")
options = {
"xslt_file": TITLE_XSLT_FILE_PATH,
}
if article_id:
try:
article = Article.objects.get(
pk=article_id,
journal=request.journal,
)
options["article_id"] = article.pk
except Article.DoesNotExist:
messages.error(
request,
f"Article ID {article_id} does not exist for this journal.",
)
return HttpResponseRedirect(request.path)
elif issue_id:
try:
issue = Issue.objects.get(
pk=issue_id,
journal=request.journal,
)
options["issue_ids"] = [issue.pk]
except Issue.DoesNotExist:
messages.error(
request,
f"Issue ID {issue_id} does not exist for this journal.",
)
return HttpResponseRedirect(request.path)
else:
options["journal_codes"] = [request.journal.code]
try:
stdout = io.StringIO()
call_command(
"jats_title_to_html",
stdout=stdout,
stderr=stdout,
**options,
)
messages.success(request, f"Transformation complete.\n{stdout.getvalue()}")
except Exception as e:
messages.error(
request,
f"An error occurred: {str(e)}\n{traceback.format_exc()}",
)
return HttpResponseRedirect(request.path)
else:
form = TransformForm()
return render(
request,
"transform_title_form.html",
{
"form": form,
},
)