This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
/
contents.py
354 lines (282 loc) · 13.5 KB
/
contents.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
# -*- coding: utf-8 -*-
#
# Copyright © 2011-2012 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
from gettext import gettext as _
import web
from pulp.common.tags import action_tag, resource_tag
from pulp.server.auth.authorization import CREATE, READ, UPDATE, DELETE, EXECUTE
from pulp.server.db.model.criteria import Criteria
from pulp.server.dispatch import constants as dispatch_constants
from pulp.server.dispatch.call import CallRequest
from pulp.server.exceptions import MissingResource, InvalidValue
from pulp.server.managers import factory
from pulp.server.webservices import execution, serialization
from pulp.server.webservices.controllers.base import JSONController
from pulp.server.webservices.controllers.decorators import auth_required
# content types controller classes ---------------------------------------------
from pulp.server.webservices.controllers.search import SearchController
class ContentTypesCollection(JSONController):
@auth_required(READ)
def GET(self):
"""
List the available content types.
"""
collection = []
cqm = factory.content_query_manager()
type_ids = cqm.list_content_types()
for id in type_ids:
link = serialization.link.child_link_obj(id)
link.update({'content_type': id})
collection.append(link)
return self.ok(collection)
class ContentTypeResource(JSONController):
@auth_required(READ)
def GET(self, type_id):
"""
Return information about a content type.
"""
cqm = factory.content_query_manager()
content_type = cqm.get_content_type(type_id)
if content_type is None:
return self.not_found(_('No content type resource: %(r)s') %
{'r': type_id})
resource = serialization.content.content_type_obj(content_type)
links = {'actions': serialization.link.child_link_obj('actions'),
'content_units': serialization.link.child_link_obj('units')}
resource.update(links)
return self.ok(resource)
# content units controller classes ---------------------------------------------
class ContentUnitsCollection(JSONController):
@staticmethod
def process_unit(unit):
unit = serialization.content.content_unit_obj(unit)
unit.update(serialization.link.search_safe_link_obj(unit['_id']))
unit.update({'children': serialization.content.content_unit_child_link_objs(unit)})
return unit
@auth_required(READ)
def GET(self, type_id):
"""
List all the available content units.
"""
cqm = factory.content_query_manager()
units = cqm.find_by_criteria(type_id, Criteria())
return self.ok([self.process_unit(unit) for unit in units])
class ContentUnitsSearch(SearchController):
def __init__(self):
super(ContentUnitsSearch, self).__init__(self._proxy_query_method)
def _proxy_query_method(self, criteria):
"""
Normally the constructor passes a manager's query method to the
super-class constructor. Since our manager's query method takes an extra
parameter to tell it what content type to look in, we have this proxy
query method that will make the correct call at the time.
Also, at the time of instantiation, we don't know what the content
type_id will be, so each request handler method will set self._type_id
to the correct value, and this method will use it at the time of being
called.
This sounds like it's asking for a race condition, I know, but web.py
instantiates a new controller for each and every request, so that isn't
a concern.
@param criteria: Criteria representing a search
@type criteria: models.db.criteria.Criteria
@return: same as PulpCollection.query
"""
return factory.content_query_manager().find_by_criteria(
self._type_id, criteria)
@staticmethod
def _add_repo_memberships(units, type_id):
"""
For a list of units, find what repos each is a member of, and add a list
of repo_ids to each unit.
:param units: list of unit documents
:type units: list of dicts
:param type_id: content type id
:type type_id: str
:return: same list of units that was passed in, only for convenience.
units are modified in-place
"""
# quick return if there is nothing to do
if not units:
return units
unit_ids = [unit['_id'] for unit in units]
criteria = Criteria(
filters={'unit_id': {'$in': unit_ids}, 'unit_type_id': type_id},
fields=('repo_id', 'unit_id')
)
associations = factory.repo_unit_association_query_manager().find_by_criteria(criteria)
unit_ids = None
criteria = None
association_map = {}
for association in associations:
association_map.setdefault(association['unit_id'], set()).add(
association['repo_id'])
for unit in units:
unit['repository_memberships'] = list(association_map.get(unit['_id'], []))
return units
@auth_required(READ)
def GET(self, type_id):
"""
Does a normal GET after setting the query method from the appropriate
PulpCollection.
Include query parameter "repos" with any value that evaluates to True to
get the attribute "repository_memberships" added to each unit.
@param type_id: id of a ContentType that we are searching.
@type type_id: basestring
"""
self._type_id = type_id
raw_units = self._get_query_results_from_get(ignore_fields=('include_repos',))
units = [ContentUnitsCollection.process_unit(unit) for unit in raw_units]
if web.input().get('include_repos'):
self._add_repo_memberships(units, type_id)
return self.ok(units)
@auth_required(READ)
def POST(self, type_id):
"""
Does a normal POST after setting the query method from the appropriate
PulpCollection.
In the body, include key "repos" with any value that evaluates to True
to get the attribute "repository_memberships" added to each unit.
@param type_id: id of a ContentType that we are searching.
@type type_id: basestring
"""
self._type_id = type_id
raw_units = self._get_query_results_from_post()
units = [ContentUnitsCollection.process_unit(unit) for unit in raw_units]
if self.params().get('include_repos'):
self._add_repo_memberships(units, type_id)
return self.ok(units)
class ContentUnitResource(JSONController):
@auth_required(READ)
def GET(self, type_id, unit_id):
"""
Return information about a content unit.
"""
cqm = factory.content_query_manager()
try:
unit = cqm.get_content_unit_by_id(type_id, unit_id)
except MissingResource:
return self.not_found(_('No content unit resource: %(r)s') %
{'r': unit_id})
resource = serialization.content.content_unit_obj(unit)
resource.update({'children': serialization.content.content_unit_child_link_objs(resource)})
return self.ok(resource)
# content uploads controller classes -------------------------------------------
class UploadsCollection(JSONController):
# Scope: Collection
# GET: Retrieve all upload request IDs
# POST: Create a new upload request (and return the ID)
@auth_required(READ)
def GET(self):
upload_manager = factory.content_upload_manager()
upload_ids = upload_manager.list_upload_ids()
return self.ok({'upload_ids' : upload_ids})
@auth_required(CREATE)
def POST(self):
upload_manager = factory.content_upload_manager()
upload_id = upload_manager.initialize_upload()
location = serialization.link.child_link_obj(upload_id)
return self.created(location['_href'], {'_href' : location['_href'], 'upload_id' : upload_id})
class UploadResource(JSONController):
# Scope: Resource
# DELETE: Delete an uploaded file
@auth_required(DELETE)
def DELETE(self, upload_id):
upload_manager = factory.content_upload_manager()
upload_manager.delete_upload(upload_id)
return self.ok(None)
class UploadSegmentResource(JSONController):
# Scope: Sub-Resource
# PUT: Upload bits into a file upload
@auth_required(UPDATE)
def PUT(self, upload_id, offset):
# If the upload ID doesn't exists, either because it was not initialized
# or was deleted, the call to the manager will raise missing resource
try:
offset = int(offset)
except ValueError:
raise InvalidValue(['offset'])
upload_manager = factory.content_upload_manager()
data = self.data()
upload_manager.save_data(upload_id, offset, data)
return self.ok(None)
# content orphans controller classes -------------------------------------------
class OrphanCollection(JSONController):
@auth_required(READ)
def GET(self):
orphan_manager = factory.content_orphan_manager()
# NOTE this can still potentially stomp on memory, but hopefully the
# _with_search_indexes methods will reduce the foot print enough that
# we'll never see this bug again
orphans = list(orphan_manager.generate_all_orphans_with_unit_keys())
map(lambda o: o.update(serialization.link.child_link_obj(o['_content_type_id'], o['_id'])), orphans)
return self.ok(orphans)
@auth_required(DELETE)
def DELETE(self):
orphan_manager = factory.content_orphan_manager()
tags = [resource_tag(dispatch_constants.RESOURCE_CONTENT_UNIT_TYPE, 'orphans')]
call_request = CallRequest(orphan_manager.delete_all_orphans, tags=tags, archive=True)
return execution.execute_async(self, call_request)
class OrphanTypeSubCollection(JSONController):
@auth_required(READ)
def GET(self, content_type):
orphan_manager = factory.content_orphan_manager()
# NOTE this can still potentially stomp on memory, but hopefully the
# _with_search_indexes methods will reduce the foot print enough that
# we'll never see this bug again
orphans = list(orphan_manager.generate_orphans_by_type_with_unit_keys(content_type))
map(lambda o: o.update(serialization.link.child_link_obj(o['_id'])), orphans)
return self.ok(orphans)
@auth_required(DELETE)
def DELETE(self, content_type):
orphan_manager = factory.content_orphan_manager()
tags = [resource_tag(dispatch_constants.RESOURCE_CONTENT_UNIT_TYPE, 'orphans')]
call_request = CallRequest(orphan_manager.delete_orphans_by_type, [content_type], tags=tags, archive=True)
return execution.execute_async(self, call_request)
class OrphanResource(JSONController):
@auth_required(READ)
def GET(self, content_type, content_id):
orphan_manager = factory.content_orphan_manager()
orphan = orphan_manager.get_orphan(content_type, content_id)
orphan.update(serialization.link.current_link_obj())
return self.ok(orphan)
@auth_required(DELETE)
def DELETE(self, content_type, content_id):
orphan_manager = factory.content_orphan_manager()
orphan_manager.get_orphan(content_type, content_id)
ids = [{'content_type_id': content_type, 'unit_id': content_id}]
tags = [resource_tag(dispatch_constants.RESOURCE_CONTENT_UNIT_TYPE, 'orphans')]
call_request = CallRequest(orphan_manager.delete_orphans_by_id, [ids], tags=tags, archive=True)
return execution.execute_async(self, call_request)
# content actions controller classes -------------------------------------------
class DeleteOrphansAction(JSONController):
@auth_required(DELETE)
def POST(self):
orphans = self.params()
orphan_manager = factory.content_orphan_manager()
tags = [action_tag('delete_orphans'),
resource_tag(dispatch_constants.RESOURCE_CONTENT_UNIT_TYPE, 'orphans')]
call_request = CallRequest(orphan_manager.delete_orphans_by_id, [orphans], tags=tags, archive=True)
return execution.execute_async(self, call_request)
# wsgi application -------------------------------------------------------------
_URLS = ('/types/$', ContentTypesCollection,
'/types/([^/]+)/$', ContentTypeResource,
'/units/([^/]+)/$', ContentUnitsCollection,
'/units/([^/]+)/search/$', ContentUnitsSearch,
'/units/([^/]+)/([^/]+)/$', ContentUnitResource,
'/uploads/$', UploadsCollection,
'/uploads/([^/]+)/$', UploadResource,
'/uploads/([^/]+)/([^/]+)/$', UploadSegmentResource,
'/orphans/$', OrphanCollection,
'/orphans/([^/]+)/$', OrphanTypeSubCollection,
'/orphans/([^/]+)/([^/]+)/$', OrphanResource,
'/actions/delete_orphans/$', DeleteOrphansAction,)
application = web.application(_URLS, globals())