Revision 469d0997 snf-pithos-app/pithos/api/functions.py
b/snf-pithos-app/pithos/api/functions.py | ||
---|---|---|
42 | 42 |
from django.utils.encoding import smart_str |
43 | 43 |
from django.views.decorators.csrf import csrf_exempt |
44 | 44 |
|
45 |
from synnefo.lib.astakos import get_user |
|
45 |
from synnefo.lib.astakos import get_user, get_uuids as _get_uuids
|
|
46 | 46 |
|
47 | 47 |
from pithos.api.faults import ( |
48 | 48 |
Fault, NotModified, BadRequest, Unauthorized, Forbidden, ItemNotFound, |
... | ... | |
58 | 58 |
copy_or_move_object, get_int_parameter, get_content_length, |
59 | 59 |
get_content_range, socket_read_iterator, SaveToBackendHandler, |
60 | 60 |
object_data_response, put_object_block, hashmap_md5, simple_list_response, |
61 |
api_method, |
|
62 |
# retrieve_uuid
|
|
61 |
api_method, is_uuid,
|
|
62 |
retrieve_uuid, retrieve_displayname, retrieve_uuids, retrieve_displaynames
|
|
63 | 63 |
) |
64 | 64 |
|
65 |
from pithos.api.settings import UPDATE_MD5 |
|
65 |
from pithos.api.settings import (UPDATE_MD5, TRANSLATE_UUIDS, |
|
66 |
SERVICE_TOKEN, AUTHENTICATION_URL) |
|
66 | 67 |
|
67 | 68 |
from pithos.backends.base import ( |
68 | 69 |
NotAllowedError, QuotaError, ContainerNotEmpty, ItemNotExists, |
... | ... | |
73 | 74 |
import logging |
74 | 75 |
import hashlib |
75 | 76 |
|
76 |
|
|
77 | 77 |
logger = logging.getLogger(__name__) |
78 | 78 |
|
79 |
def get_uuids(names): |
|
80 |
try: |
|
81 |
uuids = _get_uuids(SERVICE_TOKEN, names, |
|
82 |
url=AUTHENTICATION_URL.replace( |
|
83 |
'im/authenticate', |
|
84 |
'service/api/user_catalogs')) |
|
85 |
except Exception, e: |
|
86 |
logger.exception(e) |
|
87 |
return {} |
|
88 |
|
|
89 |
return uuids |
|
90 |
|
|
79 | 91 |
|
80 | 92 |
@csrf_exempt |
81 | 93 |
def top_demux(request): |
... | ... | |
94 | 106 |
|
95 | 107 |
@csrf_exempt |
96 | 108 |
def account_demux(request, v_account): |
109 |
if TRANSLATE_UUIDS: |
|
110 |
if not is_uuid(v_account): |
|
111 |
uuids = get_uuids([v_account]) |
|
112 |
if not uuids or not v_account in uuids: |
|
113 |
return HttpResponse(status=404) |
|
114 |
v_account = uuids[v_account] |
|
115 |
|
|
97 | 116 |
if request.method == 'HEAD': |
98 | 117 |
return account_meta(request, v_account) |
99 | 118 |
elif request.method == 'POST': |
... | ... | |
106 | 125 |
|
107 | 126 |
@csrf_exempt |
108 | 127 |
def container_demux(request, v_account, v_container): |
128 |
if TRANSLATE_UUIDS: |
|
129 |
if not is_uuid(v_account): |
|
130 |
uuids = get_uuids([v_account]) |
|
131 |
if not uuids or not v_account in uuids: |
|
132 |
return HttpResponse(status=404) |
|
133 |
v_account = uuids[v_account] |
|
134 |
|
|
109 | 135 |
if request.method == 'HEAD': |
110 | 136 |
return container_meta(request, v_account, v_container) |
111 | 137 |
elif request.method == 'PUT': |
... | ... | |
123 | 149 |
@csrf_exempt |
124 | 150 |
def object_demux(request, v_account, v_container, v_object): |
125 | 151 |
# Helper to avoid placing the token in the URL when loading objects from a browser. |
152 |
if TRANSLATE_UUIDS: |
|
153 |
if not is_uuid(v_account): |
|
154 |
uuids = get_uuids([v_account]) |
|
155 |
if not uuids or not v_account in uuids: |
|
156 |
return HttpResponse(status=404) |
|
157 |
v_account = uuids[v_account] |
|
158 |
|
|
126 | 159 |
if request.method == 'HEAD': |
127 | 160 |
return object_meta(request, v_account, v_container, v_object) |
128 | 161 |
elif request.method == 'GET': |
... | ... | |
181 | 214 |
accounts = request.backend.list_accounts(request.user_uniq, marker, limit) |
182 | 215 |
|
183 | 216 |
if request.serialization == 'text': |
217 |
if TRANSLATE_UUIDS: |
|
218 |
accounts = retrieve_displaynames( |
|
219 |
getattr(request, 'token', None), accounts) |
|
184 | 220 |
if len(accounts) == 0: |
185 | 221 |
# The cloudfiles python bindings expect 200 if json/xml. |
186 | 222 |
response.status_code = 204 |
... | ... | |
208 | 244 |
meta['X-Account-Group'] = printable_header_dict( |
209 | 245 |
dict([(k, ','.join(v)) for k, v in groups.iteritems()])) |
210 | 246 |
account_meta.append(printable_header_dict(meta)) |
247 |
|
|
248 |
if TRANSLATE_UUIDS: |
|
249 |
uuids = list(d['name'] for d in account_meta) |
|
250 |
catalog = retrieve_displaynames( |
|
251 |
getattr(request, 'token', None), uuids, return_dict=True) |
|
252 |
for meta in account_meta: |
|
253 |
meta['name'] = catalog.get(meta.get('name')) |
|
254 |
|
|
211 | 255 |
if request.serialization == 'xml': |
212 | 256 |
data = render_to_string('accounts.xml', {'accounts': account_meta}) |
213 | 257 |
elif request.serialization == 'json': |
... | ... | |
231 | 275 |
external_quota=request.user_usage) |
232 | 276 |
groups = request.backend.get_account_groups( |
233 | 277 |
request.user_uniq, v_account) |
278 |
|
|
279 |
if TRANSLATE_UUIDS: |
|
280 |
for k in groups: |
|
281 |
groups[k] = retrieve_displaynames( |
|
282 |
getattr(request, 'token', None), groups[k]) |
|
234 | 283 |
policy = request.backend.get_account_policy( |
235 | 284 |
request.user_uniq, v_account, external_quota=request.user_usage) |
236 | 285 |
except NotAllowedError: |
... | ... | |
251 | 300 |
# badRequest (400) |
252 | 301 |
|
253 | 302 |
meta, groups = get_account_headers(request) |
254 |
# for k in groups: |
|
255 |
# try: |
|
256 |
# groups[k] = [retrieve_uuid(request.token, x) for x in groups[k]] |
|
257 |
# except ItemNotExists, e: |
|
258 |
# raise BadRequest( |
|
259 |
# 'Bad X-Account-Group header value: unknown account: %s' % e) |
|
303 |
for k in groups: |
|
304 |
if TRANSLATE_UUIDS: |
|
305 |
try: |
|
306 |
groups[k] = retrieve_uuids( |
|
307 |
getattr(request, 'token', None), |
|
308 |
groups[k], |
|
309 |
fail_silently=False) |
|
310 |
except ItemNotExists, e: |
|
311 |
raise BadRequest( |
|
312 |
'Bad X-Account-Group header value: %s' % e) |
|
313 |
else: |
|
314 |
try: |
|
315 |
retrieve_displaynames( |
|
316 |
getattr(request, 'token', None), |
|
317 |
groups[k], |
|
318 |
fail_silently=False) |
|
319 |
except ItemNotExists, e: |
|
320 |
raise BadRequest( |
|
321 |
'Bad X-Account-Group header value: %s' % e) |
|
260 | 322 |
replace = True |
261 | 323 |
if 'update' in request.GET: |
262 | 324 |
replace = False |
... | ... | |
636 | 698 |
|
637 | 699 |
object_meta = [] |
638 | 700 |
for meta in objects: |
701 |
if TRANSLATE_UUIDS: |
|
702 |
modified_by = meta.get('modified_by') |
|
703 |
if modified_by: |
|
704 |
l = retrieve_displaynames( |
|
705 |
getattr(request, 'token', None), [meta['modified_by']]) |
|
706 |
if l is not None and len(l) == 1: |
|
707 |
meta['modified_by'] = l[0] |
|
708 |
|
|
639 | 709 |
if len(meta) == 1: |
640 | 710 |
# Virtual objects/directories. |
641 | 711 |
object_meta.append(meta) |
... | ... | |
661 | 731 |
if public: |
662 | 732 |
update_public_meta(public, meta) |
663 | 733 |
object_meta.append(printable_header_dict(meta)) |
734 |
|
|
664 | 735 |
if request.serialization == 'xml': |
665 | 736 |
data = render_to_string( |
666 | 737 |
'objects.xml', {'container': v_container, 'objects': object_meta}) |
... | ... | |
715 | 786 |
return response |
716 | 787 |
|
717 | 788 |
response = HttpResponse(status=200) |
718 |
put_object_headers(response, meta) |
|
789 |
put_object_headers(response, meta, token=getattr(request, 'token', None))
|
|
719 | 790 |
return response |
720 | 791 |
|
721 | 792 |
|
... | ... | |
851 | 922 |
data = json.dumps(d) |
852 | 923 |
|
853 | 924 |
response = HttpResponse(data, status=200) |
854 |
put_object_headers(response, meta) |
|
925 |
put_object_headers( |
|
926 |
response, meta, token=getattr(request, 'token', None)) |
|
855 | 927 |
response['Content-Length'] = len(data) |
856 | 928 |
return response |
857 | 929 |
|
Also available in: Unified diff