-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathhosting.py
758 lines (671 loc) · 28.7 KB
/
hosting.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
"""Views for hosting features."""
from functools import lru_cache
import packaging
import structlog
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.http import Http404, JsonResponse
from django.shortcuts import get_object_or_404
from rest_framework import permissions
from rest_framework.renderers import JSONRenderer
from rest_framework.views import APIView
from readthedocs.api.mixins import CDNCacheTagsMixin
from readthedocs.api.v2.permissions import IsAuthorizedToViewVersion
from readthedocs.api.v3.serializers import (
BuildSerializer,
ProjectSerializer,
VersionSerializer,
)
from readthedocs.builds.constants import BUILD_STATE_FINISHED, LATEST
from readthedocs.builds.models import Build, Version
from readthedocs.core.resolver import Resolver
from readthedocs.core.unresolver import UnresolverError, unresolver
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.filesections import get_section_manifest
from readthedocs.filetreediff import get_diff
from readthedocs.projects.constants import (
ADDONS_FLYOUT_SORTING_CALVER,
ADDONS_FLYOUT_SORTING_CUSTOM_PATTERN,
ADDONS_FLYOUT_SORTING_PYTHON_PACKAGING,
ADDONS_FLYOUT_SORTING_SEMVER_READTHEDOCS_COMPATIBLE,
)
from readthedocs.projects.models import Project
from readthedocs.projects.version_handling import (
comparable_version,
sort_versions_calver,
sort_versions_custom_pattern,
sort_versions_python_packaging,
)
log = structlog.get_logger(__name__) # noqa
ADDONS_VERSIONS_SUPPORTED = (1, 2)
class ClientError(Exception):
VERSION_NOT_CURRENTLY_SUPPORTED = (
"The version specified in 'api-version' is currently not supported"
)
VERSION_INVALID = "The version specifified in 'api-version' is invalid"
PROJECT_NOT_FOUND = "There is no project with the 'project-slug' requested"
class IsAuthorizedToViewProject(permissions.BasePermission):
"""
Checks if the user from the request has permissions to see the project.
This is only valid if the view doesn't have a version,
since the version permissions must be checked by the
IsAuthorizedToViewVersion permission.
"""
def has_permission(self, request, view):
project = view._get_project()
version = view._get_version()
if version:
return False
has_access = (
Project.objects.public(user=request.user).filter(pk=project.pk).exists()
)
return has_access
class BaseReadTheDocsConfigJson(CDNCacheTagsMixin, APIView):
"""
API response consumed by our JavaScript client.
The code for the JavaScript client lives at:
https://github.com/readthedocs/addons/
Attributes:
api-version (required): API JSON structure version (e.g. ``0``, ``1``, ``2``).
project-slug (required): slug of the project.
Optional if "url" is sent.
version-slug (required): slug of the version.
Optional if "url" is sent.
url (optional): absolute URL from where the request is performed.
When sending "url" attribute, "project-slug" and "version-slug" are ignored.
(e.g. ``window.location.href``).
client-version (optional): JavaScript client version (e.g. ``0.6.0``).
"""
http_method_names = ["get"]
permission_classes = [IsAuthorizedToViewProject | IsAuthorizedToViewVersion]
renderer_classes = [JSONRenderer]
project_cache_tag = "rtd-addons"
@lru_cache(maxsize=1)
def _resolve_resources(self):
url = self.request.GET.get("url")
project_slug = self.request.GET.get("project-slug")
version_slug = self.request.GET.get("version-slug")
project = None
version = None
build = None
filename = None
if url:
try:
unresolved_url = unresolver.unresolve_url(url)
# Project from the URL: if it's a subproject it will differ from
# the main project got from the domain.
project = unresolved_url.project
version = unresolved_url.version
filename = unresolved_url.filename
except UnresolverError as exc:
# If an exception is raised and there is a ``project`` in the
# exception, it's a partial match. This could be because of an
# invalid URL path, but on a valid project domain. In this case, we
# continue with the ``project``, but without a ``version``.
# Otherwise, we return 404 NOT FOUND.
project = getattr(exc, "project", None)
else:
# When not sending "url", we require "project-slug" and "version-slug".
project = get_object_or_404(Project, slug=project_slug)
version = get_object_or_404(project.versions.all(), slug=version_slug)
# A project is always required.
if not project:
raise Http404(ClientError.PROJECT_NOT_FOUND)
# If we have a version, we also return its latest successful build.
if version:
# This query should use a particular index:
# ``builds_build_version_id_state_date_success_12dfb214_idx``.
# Otherwise, if the index is not used, the query gets too slow.
build = (
Build.objects.api(user=self.request.user)
.filter(
project=project,
version=version,
success=True,
state=BUILD_STATE_FINISHED,
)
.select_related("project", "version")
.first()
)
return project, version, build, filename
def _get_project(self):
project, _, _, _ = self._resolve_resources()
return project
def _get_version(self):
_, version, _, _ = self._resolve_resources()
return version
def dispatch(self, request, *args, **kwargs):
# We check if the correct parameters are sent
# in dispatch, since we want to return a useful error message
# before checking for permissions.
url = request.GET.get("url")
project_slug = request.GET.get("project-slug")
version_slug = request.GET.get("version-slug")
if not url and (not project_slug or not version_slug):
return JsonResponse(
{
"error": "'project-slug' and 'version-slug' GET attributes are required when not sending 'url'"
},
status=400,
)
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
url = request.GET.get("url")
addons_version = request.GET.get("api-version")
if not addons_version:
return JsonResponse(
{"error": "'api-version' GET attribute is required"},
status=400,
)
try:
addons_version = packaging.version.parse(addons_version)
if addons_version.major not in ADDONS_VERSIONS_SUPPORTED:
raise ClientError
except packaging.version.InvalidVersion:
return JsonResponse(
{
"error": ClientError.VERSION_INVALID,
},
status=400,
)
except ClientError:
return JsonResponse(
{"error": ClientError.VERSION_NOT_CURRENTLY_SUPPORTED},
status=400,
)
project, version, build, filename = self._resolve_resources()
data = AddonsResponse().get(
addons_version=addons_version,
project=project,
request=request,
version=version,
build=build,
filename=filename,
url=url,
)
return JsonResponse(data, json_dumps_params={"indent": 4, "sort_keys": True})
class NoLinksMixin:
"""Mixin to remove conflicting fields from serializers."""
FIELDS_TO_REMOVE = ("_links",)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.FIELDS_TO_REMOVE:
if field in self.fields:
del self.fields[field]
if field in self.Meta.fields:
del self.Meta.fields[self.Meta.fields.index(field)]
# NOTE: the following serializers are required only to remove some fields we
# can't expose yet in this API endpoint because it's running under El Proxito
# which cannot resolve URLs pointing to the APIv3 because they are not defined
# on El Proxito.
#
# See https://github.com/readthedocs/readthedocs-ops/issues/1323
class ProjectSerializerNoLinks(NoLinksMixin, ProjectSerializer):
def __init__(self, *args, **kwargs):
resolver = kwargs.pop("resolver", Resolver())
super().__init__(
*args,
resolver=resolver,
**kwargs,
)
class VersionSerializerNoLinks(NoLinksMixin, VersionSerializer):
def __init__(self, *args, **kwargs):
resolver = kwargs.pop("resolver", Resolver())
super().__init__(
*args,
resolver=resolver,
version_serializer=VersionSerializerNoLinks,
**kwargs,
)
class BuildSerializerNoLinks(NoLinksMixin, BuildSerializer):
pass
class AddonsResponseBase:
def get(
self,
addons_version,
project,
request,
version=None,
build=None,
filename=None,
url=None,
):
"""
Unique entry point to get the proper API response.
It will evaluate the ``addons_version`` passed and decide which is the
best JSON structure for that particular version.
"""
if addons_version.major == 1:
return self._v1(project, version, build, filename, url, request)
if addons_version.major == 2:
return self._v2(project, version, build, filename, url, request)
def _get_versions(self, request, project):
"""
Get all active for a project that the user has access to.
This includes versions matching the following conditions:
- The user has access to it
- They are built
- They are active
- They are not hidden
"""
return Version.internal.public(
project=project,
user=request.user,
only_active=True,
only_built=True,
include_hidden=False,
)
def _has_permission(self, request, version):
"""
Check if user from the request is authorized to access `version`.
This is mainly to be overridden in .com to make use of
the auth backends in the proxied API.
"""
return True
def _v1(self, project, version, build, filename, url, request):
"""
Initial JSON data structure consumed by the JavaScript client.
This response is definitely in *alpha* state currently and shouldn't be
used for anyone to customize their documentation or the integration
with the Read the Docs JavaScript client. It's under active development
and anything can change without notice.
It tries to follow some similarity with the APIv3 for already-known resources
(Project, Version, Build, etc).
"""
resolver = Resolver()
versions_active_built_not_hidden = Version.objects.none()
sorted_versions_active_built_not_hidden = Version.objects.none()
user = request.user
versions_active_built_not_hidden = (
self._get_versions(request, project)
.select_related("project")
.order_by("-slug")
)
sorted_versions_active_built_not_hidden = versions_active_built_not_hidden
if not project.supports_multiple_versions:
# Return only one version when the project doesn't support multiple versions.
# That version is the only one the project serves.
sorted_versions_active_built_not_hidden = (
sorted_versions_active_built_not_hidden.filter(
slug=project.get_default_version()
)
)
else:
if (
project.addons.flyout_sorting
== ADDONS_FLYOUT_SORTING_SEMVER_READTHEDOCS_COMPATIBLE
):
sorted_versions_active_built_not_hidden = sorted(
versions_active_built_not_hidden,
key=lambda version: comparable_version(
version.verbose_name,
repo_type=project.repo_type,
),
reverse=True,
)
elif (
project.addons.flyout_sorting == ADDONS_FLYOUT_SORTING_PYTHON_PACKAGING
):
sorted_versions_active_built_not_hidden = (
sort_versions_python_packaging(
versions_active_built_not_hidden,
project.addons.flyout_sorting_latest_stable_at_beginning,
)
)
elif project.addons.flyout_sorting == ADDONS_FLYOUT_SORTING_CALVER:
sorted_versions_active_built_not_hidden = sort_versions_calver(
versions_active_built_not_hidden,
project.addons.flyout_sorting_latest_stable_at_beginning,
)
elif project.addons.flyout_sorting == ADDONS_FLYOUT_SORTING_CUSTOM_PATTERN:
sorted_versions_active_built_not_hidden = sort_versions_custom_pattern(
versions_active_built_not_hidden,
project.addons.flyout_sorting_custom_pattern,
project.addons.flyout_sorting_latest_stable_at_beginning,
)
main_project = project.main_language_project or project
# Exclude the current project since we don't want to return itself as a translation
project_translations = (
Project.objects.public(user=user)
.filter(pk__in=main_project.translations.all())
.exclude(slug=project.slug)
)
# Include main project as translation if the current project is one of the translations
if project != main_project:
project_translations |= Project.objects.public(user=user).filter(
slug=main_project.slug
)
project_translations = project_translations.order_by("language").select_related(
"main_language_project"
)
data = {
"api_version": "1",
"projects": {
"current": ProjectSerializerNoLinks(
project,
resolver=resolver,
version_slug=version.slug if version else None,
).data,
"translations": ProjectSerializerNoLinks(
project_translations,
resolver=resolver,
version_slug=version.slug if version else None,
many=True,
).data,
},
"versions": {
"current": VersionSerializerNoLinks(
version,
resolver=resolver,
).data
if version
else None,
# These are "sorted active, built, not hidden versions"
"active": VersionSerializerNoLinks(
sorted_versions_active_built_not_hidden,
resolver=resolver,
many=True,
).data,
},
"builds": {
"current": BuildSerializerNoLinks(build).data if build else None,
},
# TODO: consider creating one serializer per field here.
# The resulting JSON will be the same, but maybe it's easier/cleaner?
"domains": {
"dashboard": settings.PRODUCTION_DOMAIN,
},
"readthedocs": {
"analytics": {
"code": settings.GLOBAL_ANALYTICS_CODE,
},
},
# TODO: the ``features`` is not polished and we expect to change drastically.
# Mainly, all the fields including a Project, Version or Build will use the exact same
# serializer than the keys ``project``, ``version`` and ``build`` from the top level.
"addons": {
"options": {
"load_when_embedded": project.addons.options_load_when_embedded,
"root_selector": project.addons.options_root_selector,
},
"analytics": {
"enabled": project.addons.analytics_enabled,
# TODO: consider adding this field into the ProjectSerializer itself.
# NOTE: it seems we are removing this feature,
# so we may not need the ``code`` attribute here
# https://github.com/readthedocs/readthedocs.org/issues/9530
"code": project.analytics_code,
},
"notifications": {
"enabled": project.addons.notifications_enabled,
"show_on_latest": project.addons.notifications_show_on_latest,
"show_on_non_stable": project.addons.notifications_show_on_non_stable,
"show_on_external": project.addons.notifications_show_on_external,
},
"flyout": {
"enabled": project.addons.flyout_enabled,
# TODO: find a way to get this data in a reliably way.
# We don't have a simple way to map a URL to a file in the repository.
# This feature may be deprecated/removed in this implementation since it relies
# on data injected at build time and sent as `docroot=`, `source_suffix=` and `page=`.
# Example URL:
# /_/api/v2/footer_html/?project=weblate&version=latest&page=index&theme=furo&docroot=/docs/&source_suffix=.rst
# Data injected at:
# https://github.com/rtfd/readthedocs-sphinx-ext/blob/7c60d1646c12ac0b83d61abfbdd5bcd77d324124/readthedocs_ext/_templates/readthedocs-insert.html.tmpl#L23
#
# "vcs": {
# "url": "https://github.com",
# "username": "readthedocs",
# "repository": "test-builds",
# "branch": version.identifier if version else None,
# "filepath": "/docs/index.rst",
# },
},
"customscript": {
"enabled": project.addons.customscript_enabled,
"src": project.addons.customscript_src,
},
"search": {
"enabled": project.addons.search_enabled,
# TODO: figure it out where this data comes from.
#
# Originally, this was thought to be customizable by the user
# adding these filters from the Admin UI.
#
# I'm removing this feature for now until we implement it correctly.
"filters": [
# NOTE: this is an example of the structure of the this object.
# It contains the name of the filter and the search syntax to prepend
# to the user's query.
# It uses "Search query sintax":
# https://docs.readthedocs.io/en/stable/server-side-search/syntax.html
# [
# "Include subprojects",
# f"subprojects:{project.slug}/{version.slug}",
# ],
],
"default_filter": f"project:{project.slug}/{version.slug}"
if version
else None,
},
"linkpreviews": {
"enabled": project.addons.linkpreviews_enabled,
},
"hotkeys": {
"enabled": project.addons.hotkeys_enabled,
"doc_diff": {
"enabled": True,
"trigger": "KeyD", # Could be something like "Ctrl + D"
},
"search": {
"enabled": True,
"trigger": "Slash", # Could be something like "Ctrl + D"
},
},
"filetreediff": {
"enabled": False,
},
"filesections": {
"enabled": False,
},
},
}
if version:
response = self._get_filetreediff_response(
request=request,
project=project,
version=version,
resolver=resolver,
)
if response:
data["addons"]["filetreediff"].update(response)
sections_response = self._get_filesections_response(
project=project,
version=version,
)
if sections_response:
data["addons"]["filesections"].update(sections_response)
# Show the subprojects filter on the parent project and subproject
# TODO: Remove these queries and try to find a way to get this data
# from the resolver, which has already done these queries.
# TODO: Replace this fixed filters with the work proposed in
# https://github.com/readthedocs/addons/issues/22
if project.subprojects.exists():
data["addons"]["search"]["filters"].append(
[
"Include subprojects",
f"subprojects:{project.slug}/{version.slug}",
]
)
if project.superprojects.exists():
superproject = project.superprojects.first().parent
data["addons"]["search"]["filters"].append(
[
"Include subprojects",
f"subprojects:{superproject.slug}/{version.slug}",
]
)
# DocDiff depends on `url=` GET attribute.
# This attribute allows us to know the exact filename where the request was made.
# If we don't know the filename, we cannot return the data required by DocDiff to work.
# In that case, we just don't include the `doc_diff` object in the response.
if url:
base_version_slug = (
project.addons.options_base_version.slug
if project.addons.options_base_version
else LATEST
)
data["addons"].update(
{
"doc_diff": {
"enabled": project.addons.doc_diff_enabled,
# "http://test-builds-local.devthedocs.org/en/latest/index.html"
"base_url": resolver.resolve(
project=project,
version_slug=base_version_slug,
language=project.language,
filename=filename,
)
if filename
else None,
"inject_styles": True,
},
}
)
# Update this data with ethicalads
if "readthedocsext.donate" in settings.INSTALLED_APPS:
from readthedocsext.donate.utils import ( # noqa
get_campaign_types,
get_project_keywords,
get_publisher,
is_ad_free_project,
is_ad_free_user,
)
data["addons"].update(
{
"ethicalads": {
"enabled": project.addons.ethicalads_enabled,
# NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now
#
# NOTE: it requires ``settings.USE_PROMOS=True`` to return ``ad_free=false`` here
"ad_free": is_ad_free_user(AnonymousUser())
or is_ad_free_project(project),
"campaign_types": get_campaign_types(AnonymousUser(), project),
"keywords": get_project_keywords(project),
"publisher": get_publisher(project),
},
}
)
return data
def _get_filetreediff_response(self, *, request, project, version, resolver):
"""
Get the file tree diff response for the given version.
This response is only enabled for external versions,
we do the comparison between the current version and the latest version.
"""
if not version.is_external:
return None
if not project.addons.filetreediff_enabled:
return None
base_version = (
project.addons.options_base_version or project.get_latest_version()
)
if not base_version or not self._has_permission(
request=request, version=base_version
):
return None
diff = get_diff(version_a=version, version_b=base_version)
if not diff:
return None
return {
"enabled": True,
"outdated": diff.outdated,
"diff": {
"added": [
{
"filename": filename,
"urls": {
"current": resolver.resolve_version(
project=project,
filename=filename,
version=version,
),
"base": resolver.resolve_version(
project=project,
filename=filename,
version=base_version,
),
},
}
for filename in diff.added
],
"deleted": [
{
"filename": filename,
"urls": {
"current": resolver.resolve_version(
project=project,
filename=filename,
version=version,
),
"base": resolver.resolve_version(
project=project,
filename=filename,
version=base_version,
),
},
}
for filename in diff.deleted
],
"modified": [
{
"filename": filename,
"urls": {
"current": resolver.resolve_version(
project=project,
filename=filename,
version=version,
),
"base": resolver.resolve_version(
project=project,
filename=filename,
version=base_version,
),
},
}
for filename in diff.modified
],
},
}
def _get_filesections_response(self, *, project, version):
"""
Get the file sections response for the given version.
"""
manifest = get_section_manifest(version)
if not manifest:
return None
return {
"enabled": True,
"pages": [
{
"path": page.path,
"sections": [
{
"id": section.id,
"title": section.title,
}
for section in page.sections
],
}
for page in manifest.pages
],
}
def _v2(self, project, version, build, filename, url, user):
return {
"api_version": "2",
"comment": "Undefined yet. Use v1 for now",
}
class AddonsResponse(SettingsOverrideObject):
_default_class = AddonsResponseBase
class ReadTheDocsConfigJson(SettingsOverrideObject):
_default_class = BaseReadTheDocsConfigJson