-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
core_views.py
74 lines (62 loc) · 2.23 KB
/
core_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
"""Utility endpoints relating to canonical urls, embedded content, etc."""
from django.shortcuts import get_object_or_404
from rest_framework import decorators, permissions, status
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from readthedocs.api.v2.permissions import HasBuildAPIKey
from readthedocs.builds.constants import LATEST
from readthedocs.builds.models import Version
from readthedocs.core.templatetags.core_tags import make_document_url
from readthedocs.projects.models import Project
class RevokeBuildAPIKeyView(APIView):
"""
Revoke a build API key.
This is done by hitting the /api/v2/revoke/ endpoint with a POST request,
while using the API key to be revoked as the authorization key.
"""
http_method_names = ["post"]
permission_classes = [HasBuildAPIKey]
renderer_classes = [JSONRenderer]
def post(self, request, *args, **kwargs):
build_api_key = request.build_api_key
build_api_key.revoked = True
build_api_key.save()
return Response(status=status.HTTP_204_NO_CONTENT)
@decorators.api_view(["GET"])
@decorators.permission_classes((permissions.AllowAny,))
@decorators.renderer_classes((JSONRenderer,))
def docurl(request):
"""
Get the url that a slug resolves to.
Example::
GET https://readthedocs.org/api/v2/docurl/?
project=requests&
version=latest&
doc=index&
path=index.html
"""
project = request.GET.get("project")
version = request.GET.get("version", LATEST)
doc = request.GET.get("doc", "index")
path = request.GET.get("path", "")
if project is None:
return Response(
{"error": "Need project and doc"},
status=status.HTTP_400_BAD_REQUEST,
)
project = get_object_or_404(Project, slug=project)
version = get_object_or_404(
Version.objects.public(request.user, project=project, only_active=False),
slug=version,
)
return Response(
{
"url": make_document_url(
project=project,
version=version.slug,
page=doc,
path=path,
),
}
)