-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
138 lines (119 loc) · 3.65 KB
/
views.py
File metadata and controls
138 lines (119 loc) · 3.65 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
from django.shortcuts import render, get_object_or_404, redirect, reverse
from django.http import Http404
from django.contrib import messages
from plugins.export import forms, plugin_settings, logic
from submission import models
from security import decorators
from core import models as core_models
@decorators.has_journal
@decorators.editor_user_required
def manager(request):
"""
A view for managing the export plugin.
:param request: HttpRequest object
:return: HttpReponse or HttpRedirect
"""
template = 'export/manager.html'
context = {
}
return render(request, template, context)
@decorators.has_journal
@decorators.editor_user_required
def export_articles(request):
"""
A view that presents a list of articles in this stage.
:param request: HttpRequest
:return: HttpReponse
"""
articles_in_stage = models.Article.objects.filter(
journal=request.journal,
stage=plugin_settings.STAGE,
)
if request.POST and 'archive' in request.POST:
id_to_archive = request.POST.get('archive')
try:
article_to_archive = articles_in_stage.get(
pk=id_to_archive,
)
article_to_archive.stage = models.STAGE_ARCHIVED
article_to_archive.save()
messages.add_message(
request,
messages.SUCCESS,
'Article archived.',
)
except models.Article.DoesNotExist:
messages.add_message(
request,
messages.WARNING,
'No article with ID {} found in this stage.'.format(id_to_archive),
)
return redirect(
reverse(
'export_articles',
)
)
template = 'export/articles.html'
context = {
'articles_in_stage': articles_in_stage,
}
return render(request, template, context)
@decorators.has_journal
@decorators.editor_user_required
def export_article(request, article_id, format='csv'):
"""
A view that exports either a CSV or HTML representation of an article.
:param request: HttpRequest object
:param article_id: Article object PK
:param format: string, csv or html
:return: HttpResponse or Http404
"""
in_stage = request.GET.get('in_stage', 'true')
if in_stage == 'true':
article = get_object_or_404(
models.Article,
pk=article_id,
journal=request.journal,
stage=plugin_settings.STAGE,
)
else:
article = get_object_or_404(
models.Article,
pk=article_id,
journal=request.journal,
)
files = core_models.File.objects.filter(
article_id=article.pk,
)
if request.GET.get('action') == 'output_html':
context = {
'article': article,
'journal': request.journal,
'files': files,
}
return render(
request,
'export/export.html',
context,
)
if format == 'csv':
return logic.export_csv(request, article, files)
elif format == 'html':
return logic.export_html(request, article, files)
raise Http404
@decorators.has_journal
@decorators.editor_user_required
def export_articles_all(request):
"""
A view that displays all articles in a journal and allows export.
"""
articles = models.Article.objects.filter(
journal=request.journal,
).select_related(
'correspondence_author',
)
template = 'export/articles_all.html'
context = {
'articles_in_stage': articles,
}
return render(request, template, context)