-
Notifications
You must be signed in to change notification settings - Fork 53
/
views.py
236 lines (185 loc) · 7.36 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
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
import re
from django.conf import settings
from django.shortcuts import get_object_or_404
from rest_framework import generics, pagination, response, views
from pulpcore.app.models import Artifact
from pulpcore.app.response import OperationPostponedResponse
from pulpcore.tasking.tasks import enqueue_with_reservation
from pulpcore.plugin.models import ContentArtifact
from pulp_ansible.app.tasks.collections import import_collection
from pulp_ansible.app.models import AnsibleDistribution, Collection, CollectionVersion, Role
from .serializers import (
GalaxyCollectionSerializer,
GalaxyCollectionUploadSerializer,
GalaxyCollectionVersionSerializer,
GalaxyRoleSerializer,
GalaxyRoleVersionSerializer,
)
class GalaxyVersionView(views.APIView):
"""
APIView for Galaxy versions.
"""
authentication_classes = []
permission_classes = []
def get(self, request, path):
"""
Return a response to the "GET" action.
"""
api_info = {"available_versions": {"v1": "v1/", "v2": "v2/"}, "current_version": "v1"}
return response.Response(api_info)
class RoleList(generics.ListAPIView):
"""
APIView for Roles.
"""
model = Role
serializer_class = GalaxyRoleSerializer
authentication_classes = []
permission_classes = []
def get_queryset(self):
"""
Get the list of items for this view.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=self.kwargs["path"])
if distro.repository_version:
distro_content = distro.repository_version.content
else:
distro_content = distro.repository.latest_version().content
roles = Role.objects.distinct("namespace", "name").filter(pk__in=distro_content)
namespace = self.request.query_params.get("owner__username", None)
if namespace:
roles = roles.filter(namespace=namespace)
name = self.request.query_params.get("name", None)
if name:
roles = roles.filter(name=name)
return roles
class RoleVersionList(generics.ListAPIView):
"""
APIView for Role Versions.
"""
model = Role
serializer_class = GalaxyRoleVersionSerializer
authentication_classes = []
permission_classes = []
def get_queryset(self):
"""
Get the list of items for this view.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=self.kwargs["path"])
if distro.repository_version:
distro_content = distro.repository_version.content
else:
distro_content = distro.repository.latest_version().content
namespace, name = re.split(r"\.", self.kwargs["role_pk"])
versions = Role.objects.filter(pk__in=distro_content, name=name, namespace=namespace)
for version in versions:
version.distro_path = distro.base_path
return versions
class GalaxyCollectionDetailView(generics.RetrieveAPIView):
"""
View for a Collection Detail.
"""
model = Collection
serializer_class = GalaxyCollectionSerializer
authentication_classes = []
permission_classes = []
def get(self, request, path=None, namespace=None, name=None):
"""
Get the detail view of a Collection.
"""
collection = get_object_or_404(Collection, namespace=namespace, name=name)
collection.path = path
return response.Response(GalaxyCollectionSerializer(collection).data)
class GalaxyCollectionView(generics.ListAPIView):
"""
View for Collection models.
"""
model = Collection
serializer_class = GalaxyCollectionSerializer
authentication_classes = []
permission_classes = []
pagination_class = pagination.PageNumberPagination
def get_queryset(self):
"""
Get the list of Collections for this view.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=self.kwargs["path"])
if distro.repository_version:
distro_content = distro.repository_version.content
else:
distro_content = distro.repository.latest_version().content
collections = Collection.objects.filter(versions__pk__in=distro_content).distinct()
for c in collections:
c.path = self.kwargs["path"] # annotation needed by the serializer
return collections
def post(self, request, path):
"""
Queues a task that creates a new Collection from an uploaded artifact.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=path)
serializer = GalaxyCollectionUploadSerializer(
data=request.data, context={"request": request}
)
serializer.is_valid(raise_exception=True)
artifact = Artifact.init_and_validate(serializer.validated_data["file"])
artifact.save()
locks = [str(artifact.pk)]
kwargs = {"artifact_pk": artifact.pk}
if distro.repository:
locks.append(distro.repository)
kwargs["repository_pk"] = distro.repository.pk
async_result = enqueue_with_reservation(import_collection, locks, kwargs=kwargs)
return OperationPostponedResponse(async_result, request)
class GalaxyCollectionVersionList(generics.ListAPIView):
"""
APIView for Collections by namespace/name.
"""
model = CollectionVersion
serializer_class = GalaxyCollectionVersionSerializer
authentication_classes = []
permission_classes = []
def get_queryset(self):
"""
Get the list of items for this view.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=self.kwargs["path"])
if distro.repository_version:
distro_content = distro.repository_version.content
else:
distro_content = distro.repository.latest_version().content
collection = get_object_or_404(
Collection, namespace=self.kwargs["namespace"], name=self.kwargs["name"]
)
versions = collection.versions.filter(pk__in=distro_content)
for c in versions:
c.path = self.kwargs["path"] # annotation needed by the serializer
return versions
class GalaxyCollectionVersionDetail(views.APIView):
"""
APIView for Galaxy Collections Detail view.
"""
authentication_classes = []
permission_classes = []
def get(self, request, path, namespace, name, version):
"""
Return a response to the "GET" action.
"""
distro = get_object_or_404(AnsibleDistribution, base_path=self.kwargs["path"])
if distro.repository_version:
distro_content = distro.repository_version.content
else:
distro_content = distro.repository.latest_version().content
version = CollectionVersion.objects.get(
collection__namespace=namespace, collection__name=name, version=version
)
get_object_or_404(
ContentArtifact, content__in=distro_content, relative_path=version.relative_path
)
download_url = "{content_hostname}/{base_path}/{relative_path}".format(
content_hostname=settings.ANSIBLE_CONTENT_HOSTNAME,
base_path=distro.base_path,
relative_path=version.relative_path,
)
version.path = path
data = GalaxyCollectionVersionSerializer(version).data
data["download_url"] = download_url
return response.Response(data)