-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
model_views.py
268 lines (217 loc) · 9.95 KB
/
model_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
"""Endpoints for listing Projects, Versions, Builds, etc."""
from __future__ import absolute_import
import logging
from django.shortcuts import get_object_or_404
from rest_framework import decorators, permissions, viewsets, status
from rest_framework.decorators import detail_route
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from readthedocs.builds.constants import BRANCH
from readthedocs.builds.constants import TAG
from readthedocs.builds.models import Build, BuildCommandResult, Version
from readthedocs.core.utils import trigger_build
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.oauth.services import GitHubService, registry
from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
from readthedocs.projects.models import Project, EmailHook, Domain
from readthedocs.projects.version_handling import determine_stable_version
from ..permissions import (APIPermission, APIRestrictedPermission,
RelatedProjectIsOwner, IsOwner)
from ..serializers import (BuildSerializer, BuildAdminSerializer,
BuildCommandSerializer,
ProjectSerializer, ProjectAdminSerializer,
VersionSerializer, VersionAdminSerializer,
DomainSerializer, RemoteOrganizationSerializer,
RemoteRepositorySerializer)
from .. import utils as api_utils
log = logging.getLogger(__name__)
class UserSelectViewSet(viewsets.ModelViewSet):
"""
View set that varies serializer class based on request user credentials.
Viewsets using this class should have an attribute `admin_serializer_class`,
which is a serializer that might have more fields that only admin/staff
users require. If the user is staff, this class will be returned instead.
"""
def get_serializer_class(self):
try:
if self.request.user.is_staff and self.admin_serializer_class is not None:
return self.admin_serializer_class
except AttributeError:
pass
return self.serializer_class
def get_queryset(self):
"""Use our API manager method to determine authorization on queryset."""
return self.model.objects.api(self.request.user)
class ProjectViewSet(UserSelectViewSet):
"""List, filter, etc. Projects."""
permission_classes = [APIPermission]
renderer_classes = (JSONRenderer,)
serializer_class = ProjectSerializer
admin_serializer_class = ProjectAdminSerializer
model = Project
paginate_by = 100
paginate_by_param = 'page_size'
max_paginate_by = 1000
@decorators.detail_route()
def valid_versions(self, request, **kwargs):
"""Maintain state of versions that are wanted."""
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
if not project.num_major or not project.num_minor or not project.num_point:
return Response(
{'error': 'Project does not support point version control'},
status=status.HTTP_400_BAD_REQUEST)
version_strings = project.supported_versions()
# Disable making old versions inactive for now.
# project.versions.exclude(verbose_name__in=version_strings).update(active=False)
project.versions.filter(
verbose_name__in=version_strings).update(active=True)
return Response({
'flat': version_strings,
})
@detail_route()
def translations(self, *_, **__):
translations = self.get_object().translations.all()
return Response({
'translations': ProjectSerializer(translations, many=True).data
})
@detail_route()
def subprojects(self, request, **kwargs):
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
rels = project.subprojects.all()
children = [rel.child for rel in rels]
return Response({
'subprojects': ProjectSerializer(children, many=True).data
})
@detail_route()
def active_versions(self, request, **kwargs):
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
versions = project.versions.filter(active=True)
return Response({
'versions': VersionSerializer(versions, many=True).data
})
@decorators.detail_route(permission_classes=[permissions.IsAdminUser])
def token(self, request, **kwargs):
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
token = GitHubService.get_token_for_project(project, force_local=True)
return Response({
'token': token
})
@decorators.detail_route()
def canonical_url(self, request, **kwargs):
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
return Response({
'url': project.get_docs_url()
})
@decorators.detail_route(permission_classes=[permissions.IsAdminUser], methods=['post'])
def sync_versions(self, request, **kwargs): # noqa: D205
"""
Sync the version data in the repo (on the build server) with what we
have in the database.
Returns the identifiers for the versions that have been deleted.
"""
project = get_object_or_404(
Project.objects.api(request.user), pk=kwargs['pk'])
# If the currently highest non-prerelease version is active, then make
# the new latest version active as well.
old_highest_version = determine_stable_version(project.versions.all())
if old_highest_version is not None:
activate_new_stable = old_highest_version.active
else:
activate_new_stable = False
try:
# Update All Versions
data = request.data
added_versions = set()
if 'tags' in data:
ret_set = api_utils.sync_versions(
project=project, versions=data['tags'], type=TAG)
added_versions.update(ret_set)
if 'branches' in data:
ret_set = api_utils.sync_versions(
project=project, versions=data['branches'], type=BRANCH)
added_versions.update(ret_set)
deleted_versions = api_utils.delete_versions(project, data)
except Exception as e:
log.exception("Sync Versions Error: %s", e.message)
return Response({'error': e.message}, status=status.HTTP_400_BAD_REQUEST)
promoted_version = project.update_stable_version()
if promoted_version:
new_stable = project.get_stable_version()
log.info(
"Triggering new stable build: {project}:{version}".format(
project=project.slug,
version=new_stable.identifier))
trigger_build(project=project, version=new_stable)
# Marking the tag that is considered the new stable version as
# active and building it if it was just added.
if (
activate_new_stable and
promoted_version.slug in added_versions):
promoted_version.active = True
promoted_version.save()
trigger_build(project=project, version=promoted_version)
return Response({
'added_versions': added_versions,
'deleted_versions': deleted_versions,
})
class VersionViewSet(UserSelectViewSet):
permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer,)
serializer_class = VersionSerializer
admin_serializer_class = VersionAdminSerializer
model = Version
class BuildViewSetBase(UserSelectViewSet):
permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer,)
serializer_class = BuildSerializer
admin_serializer_class = BuildAdminSerializer
model = Build
class BuildViewSet(SettingsOverrideObject):
"""A pluggable class to allow for build cold storage."""
_default_class = BuildViewSetBase
class BuildCommandViewSet(UserSelectViewSet):
permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer,)
serializer_class = BuildCommandSerializer
model = BuildCommandResult
class NotificationViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = (permissions.IsAuthenticated, RelatedProjectIsOwner)
renderer_classes = (JSONRenderer,)
model = EmailHook
def get_queryset(self):
return self.model.objects.api(self.request.user)
class DomainViewSet(UserSelectViewSet):
permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer,)
serializer_class = DomainSerializer
model = Domain
class RemoteOrganizationViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsOwner]
renderer_classes = (JSONRenderer,)
serializer_class = RemoteOrganizationSerializer
model = RemoteOrganization
paginate_by = 25
def get_queryset(self):
return (self.model.objects.api(self.request.user)
.filter(account__provider__in=[service.adapter.provider_id
for service in registry]))
class RemoteRepositoryViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsOwner]
renderer_classes = (JSONRenderer,)
serializer_class = RemoteRepositorySerializer
model = RemoteRepository
def get_queryset(self):
query = self.model.objects.api(self.request.user)
org = self.request.query_params.get('org', None)
if org is not None:
query = query.filter(organization__pk=org)
query = query.filter(account__provider__in=[service.adapter.provider_id
for service in registry])
return query
def get_paginate_by(self):
return self.request.query_params.get('page_size', 25)