Revision b3a77863 snf-cyclades-app/synnefo/api/images.py
b/snf-cyclades-app/synnefo/api/images.py | ||
---|---|---|
40 | 40 |
from django.template.loader import render_to_string |
41 | 41 |
from django.utils import simplejson as json |
42 | 42 |
|
43 |
from contextlib import contextmanager |
|
44 |
|
|
43 | 45 |
from synnefo.api import util |
44 | 46 |
from synnefo.api.common import method_not_allowed |
45 | 47 |
from synnefo.api.faults import BadRequest, ItemNotFound, ServiceUnavailable |
... | ... | |
104 | 106 |
return d |
105 | 107 |
|
106 | 108 |
|
109 |
@contextmanager |
|
110 |
def image_backend(userid): |
|
111 |
backend = ImageBackend(userid) |
|
112 |
try: |
|
113 |
yield backend |
|
114 |
finally: |
|
115 |
backend.close() |
|
116 |
|
|
107 | 117 |
@api_method('GET') |
108 | 118 |
def list_images(request, detail=False): |
109 | 119 |
# Normal Response Codes: 200, 203 |
... | ... | |
114 | 124 |
# overLimit (413) |
115 | 125 |
|
116 | 126 |
log.debug('list_images detail=%s', detail) |
117 |
backend = ImageBackend(request.user_uniq) |
|
118 |
|
|
119 |
since = isoparse(request.GET.get('changes-since')) |
|
120 |
if since: |
|
121 |
images = [] |
|
122 |
for image in backend.iter(): |
|
123 |
updated = dateutil.parser.parse(image['updated_at']) |
|
124 |
if updated >= since: |
|
125 |
images.append(image) |
|
126 |
if not images: |
|
127 |
return HttpResponse(status=304) |
|
128 |
else: |
|
129 |
images = backend.list() |
|
127 |
with image_backend(request.user_uniq) as backend: |
|
128 |
since = isoparse(request.GET.get('changes-since')) |
|
129 |
if since: |
|
130 |
images = [] |
|
131 |
for image in backend.iter(): |
|
132 |
updated = dateutil.parser.parse(image['updated_at']) |
|
133 |
if updated >= since: |
|
134 |
images.append(image) |
|
135 |
if not images: |
|
136 |
return HttpResponse(status=304) |
|
137 |
else: |
|
138 |
images = backend.list() |
|
130 | 139 |
|
131 | 140 |
images = sorted(images, key=lambda x: x['id']) |
132 | 141 |
reply = [image_to_dict(image, detail) for image in images] |
... | ... | |
190 | 199 |
# overLimit (413) |
191 | 200 |
|
192 | 201 |
log.info('delete_image %s', image_id) |
193 |
backend = ImageBackend(request.user_uniq) |
|
194 |
backend.delete(image_id) |
|
195 |
backend.close() |
|
202 |
with image_backend(request.user_uniq) as backend: |
|
203 |
backend.delete(image_id) |
|
196 | 204 |
log.info('User %s deleted image %s', request.user_uniq, image_id) |
197 | 205 |
return HttpResponse(status=204) |
198 | 206 |
|
... | ... | |
235 | 243 |
properties = image['properties'] |
236 | 244 |
properties.update(metadata) |
237 | 245 |
|
238 |
backend = ImageBackend(request.user_uniq) |
|
239 |
backend.update(image_id, dict(properties=properties)) |
|
240 |
backend.close() |
|
246 |
with image_backend(request.user_uniq) as backend: |
|
247 |
backend.update(image_id, dict(properties=properties)) |
|
241 | 248 |
|
242 | 249 |
return util.render_metadata(request, properties, status=201) |
243 | 250 |
|
... | ... | |
287 | 294 |
properties = image['properties'] |
288 | 295 |
properties[key] = val |
289 | 296 |
|
290 |
backend = ImageBackend(request.user_uniq) |
|
291 |
backend.update(image_id, dict(properties=properties)) |
|
292 |
backend.close() |
|
297 |
with image_backend(request.user_uniq) as backend: |
|
298 |
backend.update(image_id, dict(properties=properties)) |
|
293 | 299 |
|
294 | 300 |
return util.render_meta(request, {key: val}, status=201) |
295 | 301 |
|
... | ... | |
311 | 317 |
properties = image['properties'] |
312 | 318 |
properties.pop(key, None) |
313 | 319 |
|
314 |
backend = ImageBackend(request.user_uniq) |
|
315 |
backend.update(image_id, dict(properties=properties)) |
|
316 |
backend.close() |
|
320 |
with image_backend(request.user_uniq) as backend: |
|
321 |
backend.update(image_id, dict(properties=properties)) |
|
317 | 322 |
|
318 | 323 |
return HttpResponse(status=204) |
Also available in: Unified diff