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 | |
... | ... | |
33 | 34 |
from sorl.thumbnail.shortcuts import get_thumbnail |
34 | 35 | |
35 | 36 |
from combo.data.models import CellBase |
37 |
from combo.data.utils import import_site |
|
36 | 38 | |
37 | 39 |
from .forms import AssetUploadForm, AssetsImportForm |
38 | 40 |
from .models import Asset |
... | ... | |
276 | 278 |
filepath = default_storage.path(tarinfo.name) |
277 | 279 |
if not overwrite and os.path.exists(filepath): |
278 | 280 |
continue |
279 |
assets.extract(tarinfo, path=media_prefix) |
|
281 |
if tarinfo.name == '_assets.json': |
|
282 |
json_assets = assets.extractfile(tarinfo).read() |
|
283 |
import_site(json.loads(json_assets.decode('utf-8'))) |
|
284 |
else: |
|
285 |
assets.extract(tarinfo, path=media_prefix) |
|
280 | 286 |
messages.success(self.request, _('The assets file has been imported.')) |
281 | 287 |
return super(AssetsImport, self).form_valid(form) |
282 | 288 | |
... | ... | |
292 | 298 |
assets_file.add( |
293 | 299 |
os.path.join(basedir, filename), |
294 | 300 |
os.path.join(basedir, filename)[len(media_prefix):]) |
301 |
if Asset.objects.exists(): |
|
302 |
json_file = tarfile.TarInfo('_assets.json') |
|
303 |
json_fd = BytesIO() |
|
304 |
export = {'assets': Asset.export_all_for_json(),} |
|
305 |
json_fd.write(json.dumps(export).encode('utf-8')) |
|
306 |
json_file.size = json_fd.tell() |
|
307 |
json_fd.seek(0) |
|
308 |
assets_file.addfile(json_file, fileobj=json_fd) |
|
295 | 309 |
assets_file.close() |
296 | 310 |
return HttpResponse(fd.getvalue(), content_type='application/x-tar') |
297 | 311 |
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 |
app = login(app) |
|
1024 | ||
1025 |
with override_settings(COMBO_ASSET_SLOTS={'collectivity:banner': {'label': 'Banner'}}): |
|
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 |
- |