-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathviews.py
executable file
·2718 lines (2269 loc) · 86.5 KB
/
views.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "Martin Paul Eve & Andy Byers"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"
import json
import re
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.templatetags.static import static
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.files.base import ContentFile
from django.urls import reverse
from django.db.models import Q, Count
from django.http import Http404, HttpResponse, JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.core.management import call_command
from cms import models as cms_models
from core import (
email as core_email,
files,
forms as core_forms,
models as core_models,
plugin_loader,
logic as core_logic,
views as core_views,
)
from identifiers import models as id_models
from journal import logic, models, issue_forms, forms, decorators
from journal.logic import get_best_galley, get_galley_content
from metrics.logic import store_article_access
from review import forms as review_forms, models as review_models
from submission import encoding
from security.decorators import article_stage_accepted_or_later_required, \
article_stage_accepted_or_later_or_staff_required, article_exists, file_user_required, has_request, has_journal, \
file_history_user_required, file_edit_user_required, production_user_or_editor_required, \
editor_user_required, keyword_page_enabled
from submission import models as submission_models
from utils import models as utils_models, shared, setting_handler
from utils.logger import get_logger
from events import logic as event_logic
logger = get_logger(__name__)
@has_journal
@decorators.frontend_enabled
def home(request):
""" Renders a journal homepage.
:param request: the request associated with this call
:return: a rendered template of the journal homepage
"""
issues_objects = models.Issue.objects.filter(journal=request.journal)
sections = submission_models.Section.objects.filter(
journal=request.journal,
)
homepage_elements, homepage_element_names = core_logic.get_homepage_elements(
request,
)
template = 'journal/index.html'
context = {
'homepage_elements': homepage_elements,
'issues': issues_objects,
'sections': sections,
}
# call all registered plugin block hooks to get relevant contexts
for hook in settings.PLUGIN_HOOKS.get('yield_homepage_element_context', []):
if hook.get('name') in homepage_element_names:
try:
hook_module = plugin_loader.import_module(hook.get('module'))
function = getattr(hook_module, hook.get('function'))
element_context = function(request, homepage_elements)
for k, v in element_context.items():
context[k] = v
except utils_models.Plugin.DoesNotExist as e:
if settings.DEBUG:
logger.debug(e)
else:
pass
return render(request, template, context)
@has_journal
def serve_journal_cover(request):
""" Serves the cover image for this journal or, if not affiliated with a journal, serves the press logo.
:param request: the request associated with this call
:return: a streaming response of the retrieved image file
"""
if not request.journal:
# URL accessed from press site so serve press cover
response = files.serve_press_cover(request, request.press.thumbnail_image)
return response
if not request.journal.thumbnail_image:
logic.install_cover(request.journal, request)
response = files.serve_journal_cover(request, request.journal.thumbnail_image)
return response
@has_journal
def funder_articles(request, funder_id):
""" Renders the list of articles in the journal.
:param request: the request associated with this call
:return: a rendered template of all articles
"""
if request.POST and 'clear' in request.POST:
return logic.unset_article_session_variables(request)
sections = submission_models.Section.objects.filter(
journal=request.journal,
is_filterable=True,
)
page, show, filters, sort, redirect, active_filters = logic.handle_article_controls(request, sections)
if redirect:
return redirect
pinned_articles = [pin.article for pin in models.PinnedArticle.objects.filter(
journal=request.journal)]
pinned_article_pks = [article.pk for article in pinned_articles]
article_objects = submission_models.Article.objects.filter(
journal=request.journal,
funders__fundref_id=funder_id,
date_published__lte=timezone.now(),
section__pk__in=filters,
).prefetch_related(
'frozenauthor_set').order_by(sort).exclude(
pk__in=pinned_article_pks)
paginator = Paginator(article_objects, show)
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
template = 'journal/articles.html'
context = {
'pinned_articles': pinned_articles,
'articles': articles,
'sections': sections,
'filters': filters,
'sort': sort,
'show': show,
'active_filters': active_filters,
'search_form': forms.SearchForm(),
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
def articles(request):
"""
Deprecated in version 1.5.1. Use PublishedArticlesListView.
Renders the list of articles in the journal.
:param request: the request associated with this call
:return: a rendered template of all articles
"""
if request.POST and 'clear' in request.POST:
return logic.unset_article_session_variables(request)
sections = submission_models.Section.objects.filter(
journal=request.journal,
is_filterable=True,
)
page, show, filters, sort, redirect, active_filters = logic.handle_article_controls(request, sections)
if redirect:
return redirect
pinned_articles = [pin.article for pin in models.PinnedArticle.objects.filter(
journal=request.journal)]
pinned_article_pks = [article.pk for article in pinned_articles]
article_objects = submission_models.Article.objects.filter(
journal=request.journal,
stage=submission_models.STAGE_PUBLISHED,
date_published__lte=timezone.now(),
section__pk__in=filters,
).prefetch_related(
'frozenauthor_set',
).order_by(sort).exclude(
pk__in=pinned_article_pks,
)
paginator = Paginator(article_objects, show)
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
template = 'journal/articles.html'
context = {
'pinned_articles': pinned_articles,
'articles': articles,
'sections': sections,
'filters': filters,
'sort': sort,
'show': show,
'active_filters': active_filters,
'search_form': forms.SearchForm(),
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
def issues(request):
""" Renders the list of issues in the journal.
:param request: the request associated with this call
:return: a rendered template of all issues
"""
issue_type = models.IssueType.objects.get(
code="issue",
journal=request.journal,
)
issue_objects = models.Issue.objects.filter(
journal=request.journal,
issue_type=issue_type,
date__lte=timezone.now(),
)
template = 'journal/issues.html'
context = {
'issues': issue_objects,
'issue_type': issue_type,
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
def current_issue(request, show_sidebar=True):
""" Renders the current journal issue"""
issue_id = request.journal.current_issue_id
if not issue_id:
latest_issue = models.Issue.objects.filter(
date=timezone.now(),
).order_by("-date").values("id").first()
if latest_issue:
issue_id = latest_issue.id
if not issue_id:
return redirect(reverse('journal_issues'))
return issue(request, request.journal.current_issue_id, show_sidebar=show_sidebar)
@has_journal
@decorators.frontend_enabled
def volume(request, volume_number, issue_number):
""" Redirects to an issue from its issue/volume number combination"""
issue = models.Issue.objects.filter(
issue=issue_number,
volume=volume_number,
issue_type__code="issue",
journal=request.journal,
).first()
if issue:
return redirect(reverse(
'journal_issue', kwargs={'issue_id': issue.pk}
))
raise Http404
@has_journal
@decorators.frontend_enabled
def issue(request, issue_id, show_sidebar=True):
""" Renders a specific issue/collection in the journal.
It also returns all the other issues/collections in the journal
for building a navigation menu
:param request: the request associated with this call
:param issue_id: the ID of the issue to render
:param show_sidebar: whether or not to show the sidebar of issues
:return: a rendered template of this issue
"""
issue_object = get_object_or_404(
models.Issue.objects.prefetch_related('editors'),
pk=issue_id,
journal=request.journal,
date__lte=timezone.now(),
)
page = request.GET.get("page", 1)
paginator = Paginator(issue_object.get_sorted_articles(), 50)
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
issue_objects = models.Issue.objects.filter(
journal=request.journal,
issue_type=issue_object.issue_type,
date__lte=timezone.now(),
).exclude(
# This has to be an .exclude because.filter won't do an INNER join
articles__isnull=True,
)
editors = models.IssueEditor.objects.filter(
issue=issue_object,
)
template = 'journal/issue.html'
context = {
'issue': issue_object,
'issues': issue_objects,
'structure': issue_object.structure, # for backwards compatibility
'articles': articles,
'editors': editors,
'show_sidebar': show_sidebar,
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
def collections(request, issue_type_code="collection"):
"""
Displays a list of collection Issues.
:param request: request object
:return: a rendered template of the collections
"""
issue_type = get_object_or_404(
models.IssueType,
journal=request.journal,
code=issue_type_code,
)
collections = models.Issue.objects.filter(
journal=request.journal,
issue_type=issue_type,
date__lte=timezone.now(),
).exclude(
# This has to be an .exclude because.filter won't do an INNER join
articles__isnull=True,
)
template = 'journal/collections.html'
context = {
'collections': collections,
'issue_type': issue_type,
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
def collection(request, collection_id, show_sidebar=True):
"""
A proxy view for an issue of type `Collection`.
:param request: request object
:param collection_id: primary key of an Issue object
:param show_sidebar: boolean
:return: a rendered template
"""
return issue(request, collection_id, show_sidebar)
@has_journal
@decorators.frontend_enabled
def collection_by_code(request, collection_code):
"""
A proxy view for an issue or collection by its code
:param request: request object
:param collection_code: alphanumeric string matching an Issue.code
:return: a rendered template
"""
issue = get_object_or_404(
models.Issue,
code=collection_code,
journal=request.journal,
)
if issue.issue_type.code == "issue":
return redirect(reverse(
'journal_issue', kwargs={'issue_id': issue.pk},
))
return redirect(reverse(
"journal_collection", kwargs={"collection_id": issue.pk},
))
@decorators.frontend_enabled
@article_exists
@article_stage_accepted_or_later_required
def article(request, identifier_type, identifier):
""" Renders an article.
:param request: the request associated with this call
:param identifier_type: the identifier type
:param identifier: the identifier
:return: a rendered template of the article
"""
article_object = submission_models.Article.get_article(
request.journal,
identifier_type,
identifier,
)
content, tables_in_galley = None, None
galleys = article_object.galley_set.filter(public=True)
# check if there is a galley file attached that needs rendering
if article_object.is_published:
galley = get_best_galley(article_object, galleys)
if galley:
content = galley.file_content(recover=True)
else:
content = ''
tables_in_galley = logic.get_all_tables_from_html(content)
store_article_access(
request,
article_object,
"view",
galley.type if galley else None)
else:
article_object.abstract = (
"<p><strong>This is an accepted article with a DOI pre-assigned"
" that is not yet published.</strong></p>"
) + (article_object.abstract or "")
if request.journal.disable_html_downloads:
# exclude any HTML galleys.
galleys = galleys.exclude(
file__mime_type='text/html',
)
template = 'journal/article.html'
context = {
'article': article_object,
'galleys': galleys,
'identifier_type': identifier_type,
'identifier': identifier,
'article_content': content,
'tables_in_galley': tables_in_galley,
}
return render(request, template, context)
def article_from_identifier(request, identifier_type, identifier):
identifier = get_object_or_404(
id_models.Identifier,
id_type=identifier_type,
identifier=identifier,
article__journal = request.journal
)
return redirect(identifier.article.url)
@decorators.frontend_enabled
@article_exists
@article_stage_accepted_or_later_required
def print_article(request, identifier_type, identifier):
""" Renders an article.
:param request: the request associated with this call
:param identifier_type: the identifier type
:param identifier: the identifier
:return: a rendered template of the article
"""
article_object = submission_models.Article.get_article(
request.journal, identifier_type, identifier)
content = None
galleys = article_object.galley_set.filter(public=True)
galley = None
# check if there is a galley file attached that needs rendering
if article_object.stage == submission_models.STAGE_PUBLISHED:
galley = get_best_galley(article_object, galleys)
if galley:
content = galley.file_content(recover=True)
else:
content = ''
else:
article_object.abstract = "This is an accepted article with a DOI pre-assigned that is not yet published."
if not article_object.large_image_file or article_object.large_image_file.uuid_filename == '':
article_object.large_image_file = core_models.File()
# assign the default image with a hacky patch
# TODO: this should be set to a journal-wide setting
article_object.large_image_file.uuid_filename = "carousel1.png"
article_object.large_image_file.is_remote = True
store_article_access(
request, article_object, 'view', galley.type if galley else None)
template = 'journal/print.html'
context = {
'article': article_object,
'galleys': galleys,
'identifier_type': identifier_type,
'identifier': identifier,
'article_content': content,
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
@keyword_page_enabled
def keywords(request):
"""
Renders a list of keywords
:param request: HttpRequest object
:return: a rendered template
"""
keywords = request.journal.article_keywords()
template = 'journal/keywords.html'
context = {
'keywords': keywords,
}
return render(request, template, context)
@has_journal
@decorators.frontend_enabled
@keyword_page_enabled
def keyword(request, keyword_id):
"""
Displays a list of articles that use a given keyword.
:param request: HttpRequest object
:param keyword_id: Keyword object PK
:return: a rendered template
"""
keyword = get_object_or_404(submission_models.Keyword, pk=keyword_id)
articles = request.journal.published_articles.filter(
keywords__pk=keyword.pk,
)
template = 'journal/keyword.html'
context = {
'keyword': keyword,
'articles': articles,
}
return render(request, template, context)
@staff_member_required
@has_journal
@article_exists
def edit_article(request, identifier_type, identifier):
""" Renders the page to edit an article. Note that security enforcement on this view is handled in the submission
views. All this function does is to redirect to the 'submit_info' view with any identifiers translated to a PK.
:param request: the request associated with this call
:param identifier_type: the identifier type
:param identifier: the identifier
:return: a rendered template to edit the article
"""
article_object = submission_models.Article.get_article(request.journal, identifier_type, identifier)
return redirect(reverse('submit_info', kwargs={'article_id': article_object.pk}))
def download_galley(request, article_id, galley_id):
""" Serves a galley file for an article
:param request: an HttpRequest object
:param article_id: an Article object PK
:param galley_id: an Galley object PK
:return: a streaming response of the requested file or a 404.
"""
article = get_object_or_404(submission_models.Article,
pk=article_id,
journal=request.journal,
date_published__lte=timezone.now(),
stage__in=submission_models.PUBLISHED_STAGES)
galley = get_object_or_404(
core_models.Galley,
pk=galley_id,
public=True,
)
embed = request.GET.get('embed', False)
store_article_access(
request,
article,
'view' if embed else 'download',
galley_type=galley.type,
)
return files.serve_file(request, galley.file, article, public=True)
def view_galley(request, article_id, galley_id):
"""
Serves a PDF article to the browser.
:param request: HttpRequest object
:param article_id: an Article object PK
:param galley_id: a Galley object PK
:return: an HttpResponse with a PDF attachment
"""
article_to_serve = get_object_or_404(
submission_models.Article,
pk=article_id,
journal=request.journal,
date_published__lte=timezone.now(),
stage__in=submission_models.PUBLISHED_STAGES
)
galley = get_object_or_404(
core_models.Galley,
pk=galley_id,
article=article_to_serve,
file__mime_type='application/pdf'
)
store_article_access(
request,
article_to_serve,
'view',
galley_type=galley.type
)
return files.serve_pdf_galley_to_browser(
request,
galley.file,
article_to_serve
)
@has_request
@article_stage_accepted_or_later_or_staff_required
@file_user_required
def serve_article_file(request, identifier_type, identifier, file_id):
""" Serves an article file.
:param request: the request associated with this call
:param identifier_type: the identifier type for the article
:param identifier: the identifier for the article
:param file_id: the file ID to serve
:return: a streaming response of the requested file or 404
"""
if not request.journal and request.site_type.code == 'press':
article_object = submission_models.Article.get_press_article(
request.press,
identifier_type,
identifier,
)
else:
article_object = submission_models.Article.get_article(
request.journal,
identifier_type,
identifier,
)
try:
if file_id != "None":
file_object = get_object_or_404(core_models.File, pk=file_id)
return files.serve_file(request, file_object, article_object)
else:
raise Http404
except Http404:
if file_id != "None":
raise Http404
# if we are here then the carousel is requesting an image for an article that doesn't exist
# return a default image instead
return redirect(static('common/img/default_carousel/carousel1.png'))
@has_request
@file_user_required
def serve_article_file_history(
request, identifier_type, identifier, file_id,
):
filehistory = get_object_or_404(core_models.FileHistory, pk=file_id)
# File History objects are not storing article ids, check parent's instead
file = filehistory.file_set.first()
if file:
return files.serve_file(request, filehistory, file.article)
raise Http404
@login_required
@article_exists
@file_edit_user_required
def replace_article_file(request, identifier_type, identifier, file_id):
""" Renders the page to replace an article file
:param request: the request associated with this call
:param identifier_type: the identifier type for the article
:param identifier: the identifier for the article
:param file_id: the file ID to replace
:return: a rendered template to replace the file
"""
article_to_replace = submission_models.Article.get_article(request.journal, identifier_type, identifier)
file_to_replace = get_object_or_404(core_models.File, pk=file_id)
error = None
if request.GET.get('delete', False):
file_delete(request, article_to_replace.pk, file_to_replace.pk)
return redirect(reverse('submit_files', kwargs={'article_id': article_to_replace.id}))
if request.POST:
if 'replacement' in request.POST and request.FILES:
uploaded_file = request.FILES.get('replacement-file')
files.overwrite_file(
uploaded_file,
file_to_replace,
('articles', article_to_replace.pk),
)
elif not request.FILES and 'back' not in request.POST:
messages.add_message(
request,
messages.WARNING,
_('No file uploaded'),
)
url = '{url}?return={get}'.format(
url=reverse('article_file_replace',
kwargs={'identifier_type': 'id',
'identifier': article_to_replace.pk,
'file_id': file_to_replace.pk}
),
get=request.GET.get('return', ''),
)
return redirect(url)
return redirect(request.GET.get('return', reverse('core_dashboard')))
template = "journal/replace_file.html"
context = {
'article': article_to_replace,
'old_file': file_to_replace,
'error': error,
}
return render(request, template, context)
@login_required
@article_exists
@file_edit_user_required
def file_reinstate(request, article_id, file_id, file_history_id):
""" Replaces a file with an older version of itself
:param request: the request associated with this call
:param article_id: the article on which to replace the file
:param file_id: the file ID to replace
:param file_history_id: the file history object to reinstate
:return: a redirect to the contents of the GET parameter 'return'
"""
current_file = get_object_or_404(core_models.File, pk=file_id)
file_history = get_object_or_404(core_models.FileHistory, pk=file_history_id)
files.reinstate_historic_file(article, current_file, file_history)
return redirect(request.GET['return'])
@login_required
@file_edit_user_required
def submit_files_info(request, article_id, file_id):
""" Renders a template to submit information about a file.
:param request: the request associated with this call
:param article_id: the ID of the associated article
:param file_id: the file ID for which to submit information
:return: a rendered template to submit file information
"""
article_object = get_object_or_404(submission_models.Article, pk=article_id)
file_object = get_object_or_404(core_models.File, pk=file_id)
form = review_forms.ReplacementFileDetails(instance=file_object)
if request.POST:
form = review_forms.ReplacementFileDetails(request.POST, instance=file_object)
if form.is_valid():
form.save()
# TODO: this needs a better redirect
return redirect(reverse('kanban'))
template = "review/submit_replacement_files_info.html"
context = {
'article': article_object,
'file': file_object,
'form': form,
}
return render(request, template, context)
@login_required
@file_history_user_required
def file_history(request, article_id, file_id):
""" Renders a template to show the history of a file.
:param request: the request associated with this call
:param article_id: the ID of the associated article
:param file_id: the file ID for which to view the history
:return: a rendered template showing the file history
"""
if request.POST:
return redirect(request.GET['return'])
article_object = get_object_or_404(submission_models.Article, pk=article_id)
file_object = get_object_or_404(core_models.File, pk=file_id)
template = "journal/file_history.html"
context = {
'article': article_object,
'file': file_object,
}
return render(request, template, context)
@editor_user_required
def issue_file_history(request, issue_id):
""" Returns the file history of a given Issue Galley file
"""
# TODO: Combine with `file_history` above, disabled until GH #865
raise Http404
issue_galley = get_object_or_404(models.IssueGalley, issue__pk=issue_id)
file_object = issue_galley.file
template = "journal/file_history.html"
context = {
'article': None,
'file': file_object,
}
return render(request, template, context)
@login_required
@file_edit_user_required
def file_delete(request, article_id, file_id):
""" Renders a template to delete a file.
:param request: the request associated with this call
:param article_id: the ID of the associated articled
:param file_id: the file ID for which to view the history
:return: a redirect to the URL at the GET parameter 'return'
"""
article_object = get_object_or_404(submission_models.Article, pk=article_id)
file_object = get_object_or_404(core_models.File, pk=file_id)
file_object.delete()
return redirect(request.GET['return'])
@file_user_required
@production_user_or_editor_required
def article_file_make_galley(request, article_id, file_id):
""" Copies a file to be a publicly available galley
:param request: the request associated with this call
:param article_id: the ID of the associated articled
:param file_id: the file ID for which to view the history
:return: a redirect to the URL at the GET parameter 'return'
"""
article_object = get_object_or_404(submission_models.Article, pk=article_id)
janeway_file = get_object_or_404(core_models.File, pk=file_id)
blob = janeway_file.get_file(article_object, as_bytes=True)
content_file = ContentFile(blob)
content_file.name = janeway_file.original_filename
# Avoid circular import.
from production import logic as production_logic
production_logic.save_galley(
article_object, request, content_file,
is_galley=True,
)
return redirect(request.GET['return'])
def identifier_figure(request, identifier_type, identifier, file_name):
"""
Returns a galley figure from identifier.
:param request: HttpRequest object
:param identifier_type: Identifier type string
:param identifier: An Identifier
:param file_name: a File object name
:return: a streaming file reponse
"""
figure_article = submission_models.Article.get_article(
request.journal,
identifier_type,
identifier
)
if not figure_article:
raise Http404
article_galleys = figure_article.galley_set.all()
galley = logic.get_best_galley(figure_article, article_galleys)
if not galley:
raise Http404
# Use a filter with .first() here to avoid an error when two images with
# the same name are present.
figure = galley.images.filter(
original_filename=file_name
).order_by(
'-last_modified',
).first()
if not figure:
raise Http404
return files.serve_file(request, figure, figure_article)
def article_figure(request, article_id, galley_id, file_name):
""" Returns a galley article figure
:param request: an HttpRequest object
:param article_id: an Article object PK
:param galley_id: an Galley object PK
:param file_name: an File object name
:return: a streaming file response or a 404 if not found
"""
figure_article = get_object_or_404(
submission_models.Article,
pk=article_id,
)
galley = get_object_or_404(
core_models.Galley,
pk=galley_id,
article=figure_article,
)
# Use a filter with .first() here to avoid an error when two images with
# the same name are present.
figure = galley.images.filter(
original_filename=file_name
).order_by(
'-last_modified',
).first()
if not figure:
raise Http404
return files.serve_file(request, figure, figure_article)
@editor_user_required
def publish(request):
"""
Displays a list of articles in pre publication for the current journal
:param request: django request object
:return: contextualised django object
"""
articles = submission_models.Article.objects.filter(
stage=submission_models.STAGE_READY_FOR_PUBLICATION,