This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
155 lines (108 loc) · 5.12 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
from django.http import HttpResponse, Http404
from rest_framework import viewsets, parsers
from plugins.journal_transporter.parsers import MultiPartJSONParser
from plugins.journal_transporter import serializers
from journal.models import Journal, Issue
from review.models import (ReviewForm, ReviewFormElement, ReviewRound, ReviewAssignment, ReviewFormAnswer,
EditorAssignment, RevisionRequest)
from submission.models import Section, Article, FrozenAuthor
from core.models import Account, AccountRole, File
from utils.models import LogEntry
from core import files
# Adapted from chibisov's drf-extensions module
# See https://github.com/chibisov/drf-extensions/blob/master/rest_framework_extensions/mixins.py
class NestedViewSetMixin:
def get_queryset(self):
return self.filter_queryset_by_parents_lookups(
super().get_queryset()
)
def filter_queryset_by_parents_lookups(self, queryset):
parents_query_dict = self.get_parents_query_dict()
if parents_query_dict:
filtered_query = {
key: parents_query_dict[key] for key in self.parent_keys
} if hasattr(self, "parent_keys") else parents_query_dict
try:
return queryset.filter(**filtered_query)
except ValueError:
raise Http404
else:
return queryset
def get_parents_query_dict(self):
result = {}
for kwarg_name, kwarg_value in self.kwargs.items():
if kwarg_name.startswith('parent_lookup_'):
query_lookup = kwarg_name.replace(
'parent_lookup_',
'',
1
)
query_value = kwarg_value
result[query_lookup] = query_value
return result
def manager(request):
return HttpResponse("OH HI")
class TransporterViewSet(NestedViewSetMixin, viewsets.ModelViewSet):
http_method_names = ['get', 'post']
parser_classes = [parsers.JSONParser, MultiPartJSONParser]
def delete(self, _request, *_args, **_kwargs):
"""Deleting resources through this plugin is not allowed."""
return HttpResponse(status=405)
class UserViewSet(TransporterViewSet):
queryset = Account.objects.all()
serializer_class = serializers.UserSerializer
class JournalViewSet(TransporterViewSet):
queryset = Journal.objects.all()
serializer_class = serializers.JournalSerializer
class JournalReviewFormViewSet(TransporterViewSet):
queryset = ReviewForm.objects.all()
serializer_class = serializers.JournalReviewFormSerializer
class JournalReviewFormElementViewSet(TransporterViewSet):
queryset = ReviewFormElement.objects.all()
serializer_class = serializers.JournalReviewFormElementSerializer
class JournalRoleViewSet(TransporterViewSet):
queryset = AccountRole.objects.all()
serializer_class = serializers.JournalRoleSerializer
class JournalIssueViewSet(TransporterViewSet):
queryset = Issue.objects.all()
serializer_class = serializers.JournalIssueSerializer
class JournalSectionViewSet(TransporterViewSet):
queryset = Section.objects.all()
serializer_class = serializers.JournalSectionSerializer
class JournalArticleViewSet(TransporterViewSet):
queryset = Article.objects.all()
serializer_class = serializers.JournalArticleSerializer
class JournalArticleEditorViewSet(TransporterViewSet):
queryset = EditorAssignment.objects.all()
serializer_class = serializers.JournalArticleEditorSerializer
class JournalArticleAuthorViewSet(TransporterViewSet):
queryset = FrozenAuthor.objects.all()
serializer_class = serializers.JournalArticleAuthorSerializer
class JournalArticleFileViewSet(TransporterViewSet):
"""
Handles files for a journal/article set.
The list route returns a list of all article file metadata. The retrieve route returns
the file itself.
"""
queryset = File.objects.all()
serializer_class = serializers.JournalArticleFileSerializer
parent_keys = ["article_id"]
def retrieve(self, request, *args, **kwargs):
article = Article.objects.get(pk=kwargs["parent_lookup_article_id"])
file = File.objects.get(pk=kwargs["pk"])
return files.serve_file(request, file, article)
class JournalArticleLogEntryViewSet(TransporterViewSet):
queryset = LogEntry.objects.all()
serializer_class = serializers.JournalArticleLogEntrySerializer
class JournalArticleRevisionRequestViewSet(TransporterViewSet):
queryset = RevisionRequest.objects.all()
serializer_class = serializers.JournalArticleRevisionRequestSerializer
class JournalArticleRoundViewSet(TransporterViewSet):
queryset = ReviewRound.objects.all()
serializer_class = serializers.JournalArticleRoundSerializer
class JournalArticleRoundAssignmentViewSet(TransporterViewSet):
queryset = ReviewAssignment.objects.all()
serializer_class = serializers.JournalArticleRoundAssignmentSerializer
class JournalArticleRoundAssignmentResponseViewSet(TransporterViewSet):
queryset = ReviewFormAnswer.objects.all()
serializer_class = serializers.JournalArticleRoundAssignmentResponseSerializer