0001-assets-import-export-slot-assets-37674.patch
combo/apps/assets/views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import json |
|
17 | 18 |
import tarfile |
18 | 19 |
import os |
19 | 20 | |
... | ... | |
276 | 277 |
filepath = default_storage.path(tarinfo.name) |
277 | 278 |
if not overwrite and os.path.exists(filepath): |
278 | 279 |
continue |
279 |
assets.extract(tarinfo, path=media_prefix) |
|
280 |
if tarinfo.name == '_assets.json': |
|
281 |
json_assets = assets.extractfile(tarinfo) |
|
282 |
Asset.load_serialized_objects(json.load(json_assets)) |
|
283 |
else: |
|
284 |
assets.extract(tarinfo, path=media_prefix) |
|
280 | 285 |
messages.success(self.request, _('The assets file has been imported.')) |
281 | 286 |
return super(AssetsImport, self).form_valid(form) |
282 | 287 | |
... | ... | |
292 | 297 |
assets_file.add( |
293 | 298 |
os.path.join(basedir, filename), |
294 | 299 |
os.path.join(basedir, filename)[len(media_prefix):]) |
300 |
if Asset.objects.exists(): |
|
301 |
json_file = tarfile.TarInfo('_assets.json') |
|
302 |
with BytesIO() as json_fd: |
|
303 |
json_fd.write(json.dumps(Asset.export_all_for_json()).encode()) |
|
304 |
json_file.size = len(json_fd.getvalue()) |
|
305 |
json_fd.seek(0) |
|
306 |
assets_file.addfile(json_file, fileobj=json_fd) |
|
295 | 307 |
assets_file.close() |
296 | 308 |
return HttpResponse(fd.getvalue(), content_type='application/x-tar') |
297 | 309 |
tests/test_manager.py | ||
---|---|---|
1019 | 1019 |
assert '>CGU<' in resp.text |
1020 | 1020 | |
1021 | 1021 | |
1022 |
def test_asset_slots_export(app, admin_user): |
|
1023 |
settings.COMBO_ASSET_SLOTS = {'collectivity:banner': {'label': 'Banner'}} |
|
1024 |
app = login(app) |
|
1025 | ||
1026 |
resp = app.get('/manage/assets/') |
|
1027 |
resp = resp.click('Overwrite') |
|
1028 |
resp.form['upload'] = Upload('test.png', |
|
1029 |
base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAgAABAADRWoApgAA\nAABJRU5ErkJggg=='), |
|
1030 |
'image/png') |
|
1031 |
resp = resp.form.submit().follow() |
|
1032 |
assert Asset.objects.filter(key='collectivity:banner').count() == 1 |
|
1033 | ||
1034 |
resp = resp.click('Export') |
|
1035 |
assert resp.content_type == 'application/x-tar' |
|
1036 |
content = resp.content |
|
1037 |
Asset.objects.filter(key='collectivity:banner').delete() |
|
1038 | ||
1039 |
resp = app.get('/manage/assets/') |
|
1040 |
resp = resp.click('Import') |
|
1041 |
resp.form['assets_file'] = Upload('test.tar', content) |
|
1042 |
resp = resp.form.submit() |
|
1043 | ||
1044 |
assert Asset.objects.filter(key='collectivity:banner').count() == 1 |
|
1045 | ||
1046 | ||
1022 | 1047 |
def test_serve_asset(settings, app, admin_user): |
1023 | 1048 |
settings.COMBO_ASSET_SLOTS = {'collectivity:banner': {'label': 'Banner'}} |
1024 | 1049 |
app = login(app) |
1025 |
- |