-
Notifications
You must be signed in to change notification settings - Fork 11
/
views.py
60 lines (46 loc) · 1.77 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
from rest_framework.views import APIView
from rest_framework.response import Response
_VERSIONS = {}
def build_version_details(id, status, links=None, relative_endpoint=None):
"""
Build a standard version dictionary
"""
int_id = int(float(id))
if not relative_endpoint:
relative_endpoint = "v%s/" % int_id
mime_type = "application/vnd.openstack.adjutant-v%s+json" % int_id
version_details = {
"status": status,
"id": id,
"media-types": [{"base": "application/json", "type": mime_type}],
"links": [],
}
if links:
version_details["links"] = links
version_details["relative_endpoint"] = relative_endpoint
_VERSIONS[id] = version_details
return version_details
class VersionView(APIView):
def get(self, request):
versions = []
for version in _VERSIONS.values():
version = version.copy()
rel_endpoint = version.pop("relative_endpoint")
url = request.build_absolute_uri() + rel_endpoint
version["links"] = version["links"] + [{"href": url, "rel": "self"}]
versions.append(version)
return Response({"versions": versions}, status=200)
class SingleVersionView(APIView):
"""
A view to be added to the root of each API version detailing it's
own version details. Should be subclassed and have a version set.
"""
def get(self, request):
version = _VERSIONS.get(self.version, {}).copy()
if not version:
return Response({"error": "Not Found"}, status=404)
version.pop("relative_endpoint")
version["links"] = version["links"] + [
{"href": request.build_absolute_uri(), "rel": "self"}
]
return Response({"version": version}, status=200)