-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathmodel_views.py
350 lines (287 loc) · 11 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
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
# -*- coding: utf-8 -*-
"""Endpoints for listing Projects, Versions, Builds, etc."""
import logging
from allauth.socialaccount.models import SocialAccount
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
from rest_framework import decorators, permissions, status, viewsets
from rest_framework.parsers import MultiPartParser, JSONParser, FormParser
from rest_framework.renderers import BaseRenderer, JSONRenderer
from rest_framework.response import Response
from readthedocs.builds.constants import BRANCH, 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.models import RemoteOrganization, RemoteRepository
from readthedocs.oauth.services import GitHubService, registry
from readthedocs.projects.models import Domain, EmailHook, Project
from readthedocs.projects.version_handling import determine_stable_version
from .. import utils as api_utils
from ..permissions import (
APIPermission,
APIRestrictedPermission,
IsOwner,
RelatedProjectIsOwner,
)
from ..serializers import (
BuildAdminSerializer,
BuildCommandSerializer,
BuildSerializer,
DomainSerializer,
ProjectAdminSerializer,
ProjectSerializer,
RemoteOrganizationSerializer,
RemoteRepositorySerializer,
SocialAccountSerializer,
VersionAdminSerializer,
VersionSerializer,
)
log = logging.getLogger(__name__)
class PlainTextBuildRenderer(BaseRenderer):
"""
Custom renderer for text/plain format.
charset is 'utf-8' by default.
"""
media_type = 'text/plain'
format = 'txt'
def render(self, data, accepted_media_type=None, renderer_context=None):
renderer_context = renderer_context or {}
response = renderer_context.get('response')
if not response or response.exception:
return data.get('detail', '').encode(self.charset)
data = render_to_string(
'restapi/log.txt',
{'build': data},
)
return data.encode(self.charset)
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
pagination_class = api_utils.ProjectPagination
filterset_fields = ('slug',)
@decorators.action(detail=True)
def translations(self, *_, **__):
translations = self.get_object().translations.all()
return Response({
'translations': ProjectSerializer(translations, many=True).data,
})
@decorators.action(detail=True)
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,
})
@decorators.action(detail=True)
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.action(
detail=True,
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.action(detail=True)
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.action(
detail=True,
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).
Version data in the repo is synced 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')
return Response(
{
'error': str(e),
},
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
filterset_fields = (
'active',
'project__slug',
)
class BuildViewSetBase(UserSelectViewSet):
permission_classes = [APIRestrictedPermission]
renderer_classes = (JSONRenderer, PlainTextBuildRenderer)
serializer_class = BuildSerializer
admin_serializer_class = BuildAdminSerializer
model = Build
filterset_fields = ('project__slug', 'commit')
class BuildViewSet(SettingsOverrideObject):
"""A pluggable class to allow for build cold storage."""
_default_class = BuildViewSetBase
class BuildCommandViewSet(UserSelectViewSet):
parser_classes = [JSONParser, MultiPartParser]
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
pagination_class = api_utils.RemoteOrganizationPagination
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
pagination_class = api_utils.RemoteProjectPagination
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)
own = self.request.query_params.get('own', None)
if own is not None:
query = query.filter(
account__provider=own,
organization=None,
)
query = query.filter(
account__provider__in=[
service.adapter.provider_id for service in registry
],
)
return query
class SocialAccountViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsOwner]
renderer_classes = (JSONRenderer,)
serializer_class = SocialAccountSerializer
model = SocialAccount
def get_queryset(self):
return self.model.objects.filter(user=self.request.user.pk)