0001-upload_storage-add-get_json_value-and-use-it-in-json.patch
tests/test_upload_storage.py | ||
---|---|---|
185 | 185 |
# api access (json export) |
186 | 186 |
resp = admin_app.get('/api/forms/test/1/', status=200) |
187 | 187 |
assert resp.json['fields']['file']['content'].startswith('/9j/4AAQSkZJRg') |
188 |
assert 'storage' not in resp.json['fields']['file'] |
|
188 | 189 |
assert resp.json['fields']['remote_file']['content'] == '' |
189 |
assert resp.json['fields']['remote_file']['storage_attrs']['redirect_url'] == 'https://crypto.example.net/' |
|
190 |
assert resp.json['fields']['remote_file']['storage'] == 'remote' |
|
191 |
assert resp.json['fields']['remote_file']['storage_attrs'] == {'redirect_url': 'https://crypto.example.net/'} |
|
192 | ||
193 |
resp = admin_app.get('/api/forms/test/2/', status=200) |
|
194 |
assert resp.json['fields']['remote_file']['content'] == '' |
|
195 |
assert resp.json['fields']['remote_file']['storage'] == 'remote' |
|
196 |
assert resp.json['fields']['remote_file']['storage_attrs'] == { |
|
197 |
'redirect_url': 'https://crypto.example.net/', |
|
198 |
'backoffice_redirect_url': None, 'frontoffice_redirect_url': None} |
|
199 | ||
200 |
resp = admin_app.get('/api/forms/test/3/', status=200) |
|
201 |
assert resp.json['fields']['remote_file']['content'] == '' |
|
202 |
assert resp.json['fields']['remote_file']['storage'] == 'remote-bo' |
|
203 |
assert resp.json['fields']['remote_file']['storage_attrs'] == {'redirect_url': 'https://crypto.example.net/'} |
wcs/fields.py | ||
---|---|---|
1058 | 1058 |
return [str(value) if value else ''] |
1059 | 1059 | |
1060 | 1060 |
def get_json_value(self, value): |
1061 |
out = { |
|
1062 |
'field_id': self.id, |
|
1063 |
'filename': value.base_filename, |
|
1064 |
'content_type': value.content_type or 'application/octet-stream', |
|
1065 |
'content': force_text(base64.b64encode(value.get_content())), |
|
1066 |
} |
|
1067 |
if hasattr(value, 'storage_attrs'): |
|
1068 |
out['storage_attrs'] = value.storage_attrs |
|
1061 |
out = value.get_json_value() |
|
1062 |
out['field_id'] = self.id |
|
1069 | 1063 |
return out |
1070 | 1064 | |
1071 | 1065 |
def from_json_value(self, value): |
wcs/qommon/misc.py | ||
---|---|---|
539 | 539 |
if six.PY3 and isinstance(obj, bytes): |
540 | 540 |
return obj.decode('ascii') |
541 | 541 | |
542 |
if hasattr(obj, 'get_json_value'): |
|
543 |
return obj.get_json_value() |
|
544 | ||
542 | 545 |
if hasattr(obj, 'base_filename'): |
543 |
out = {
|
|
546 |
return {
|
|
544 | 547 |
'filename': obj.base_filename, |
545 | 548 |
'content_type': obj.content_type or 'application/octet-stream', |
546 | 549 |
'content': base64.b64encode(obj.get_content()), |
547 | 550 |
} |
548 |
if hasattr(obj, 'storage_attrs'): # remote storage |
|
549 |
out['storage_attrs'] = obj.storage_attrs |
|
550 |
return out |
|
551 | 551 | |
552 | 552 |
# Let the base class default method raise the TypeError |
553 | 553 |
return json.JSONEncoder.default(self, obj) |
wcs/qommon/upload_storage.py | ||
---|---|---|
20 | 20 |
from quixote import get_publisher |
21 | 21 |
from quixote.http_request import Upload |
22 | 22 | |
23 |
from django.utils.encoding import force_text |
|
23 | 24 |
from django.utils.module_loading import import_string |
24 | 25 |
from django.utils.six import StringIO |
25 | 26 | |
... | ... | |
77 | 78 |
return base64.encodestring(content) |
78 | 79 |
return b'' |
79 | 80 | |
81 |
def get_json_value(self): |
|
82 |
return get_storage_object(getattr(self, 'storage', None)).get_json_value(self) |
|
83 | ||
80 | 84 |
def can_thumbnail(self): |
81 | 85 |
return get_storage_object(getattr(self, 'storage', None)).can_thumbnail(self) |
82 | 86 | |
... | ... | |
129 | 133 |
upload.fp.seek(0) |
130 | 134 |
atomic_write(filepath, upload.fp, async_op=False) |
131 | 135 | |
136 |
def get_json_value(self, upload): |
|
137 |
return { |
|
138 |
'filename': upload.base_filename, |
|
139 |
'content_type': upload.content_type or 'application/octet-stream', |
|
140 |
'content': force_text(base64.b64encode(upload.get_content())), |
|
141 |
} |
|
142 | ||
132 | 143 |
def can_thumbnail(self, upload): |
133 | 144 |
return can_thumbnail(upload.content_type) |
134 | 145 | |
... | ... | |
202 | 213 |
def save(self, upload): |
203 | 214 |
pass |
204 | 215 | |
216 |
def get_json_value(self, upload): |
|
217 |
return { |
|
218 |
'filename': upload.base_filename, |
|
219 |
'content_type': upload.content_type or 'application/octet-stream', |
|
220 |
'content': '', |
|
221 |
'storage': upload.storage, |
|
222 |
'storage_attrs': upload.storage_attrs, |
|
223 |
} |
|
224 | ||
205 | 225 |
def can_thumbnail(self, upload): |
206 | 226 |
return False |
207 | 227 | |
208 |
- |