Projet

Général

Profil

0001-assets-add-export-import-commands-50399.patch

Nicolas Roche, 09 février 2021 12:25

Télécharger (6,32 ko)

Voir les différences:

Subject: [PATCH] assets: add export/import commands (#50399)

 combo/apps/assets/management/__init__.py      |  0
 .../assets/management/commands/__init__.py    |  0
 .../management/commands/export_assets.py      | 35 ++++++++++++++++
 .../management/commands/import_assets.py      | 40 +++++++++++++++++++
 tests/test_assets.py                          | 25 ++++++++++++
 5 files changed, 100 insertions(+)
 create mode 100644 combo/apps/assets/management/__init__.py
 create mode 100644 combo/apps/assets/management/commands/__init__.py
 create mode 100644 combo/apps/assets/management/commands/export_assets.py
 create mode 100644 combo/apps/assets/management/commands/import_assets.py
combo/apps/assets/management/commands/export_assets.py
1
# combo - content management system
2
# Copyright (C) 2021  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.core.management.base import BaseCommand, CommandError
18

  
19
from combo.apps.assets.utils import export_assets
20

  
21

  
22
class Command(BaseCommand):
23
    help = 'Export assets'
24
    def add_arguments(self, parser):
25
        parser.add_argument(
26
                '--output', metavar='FILE', default='assets_export.tar',
27
                help='name of a file to write output to')
28

  
29
    def handle(self, *args, **options):
30
        try:
31
            output = open(options['output'], 'wb')
32
        except IOError as e:
33
            raise CommandError(e)
34
        export_assets(output)
35
        output.close()
combo/apps/assets/management/commands/import_assets.py
1
# combo - content management system
2
# Copyright (C) 2016  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import json
18
import sys
19
import tarfile
20

  
21
from django.core.management.base import BaseCommand, CommandError
22

  
23
from combo.apps.assets.utils import import_assets
24

  
25
class Command(BaseCommand):
26
    help = 'Import an exported site'
27

  
28
    def add_arguments(self, parser):
29
        parser.add_argument('filename', metavar='FILENAME', type=str,
30
                help='name of file to import')
31
        parser.add_argument(
32
                '--overwrite', action='store_true', default=False,
33
                help='Overwrite asset files')
34

  
35
    def handle(self, filename, *args, **options):
36
        fd = open(filename, 'rb')
37
        try:
38
            import_assets(fd, overwrite=options['overwrite'])
39
        except ImportSiteError as e:
40
            raise CommandError(e)
tests/test_assets.py
2 2

  
3 3
import base64
4 4
import os
5 5
import tarfile
6 6

  
7 7
from django.urls import reverse
8 8
from django.core.files.storage import default_storage
9 9
from django.core.files import File
10
from django.core.management import call_command
10 11
from django.utils.six import BytesIO
11 12

  
12 13
import pytest
13 14

  
14 15
from combo.apps.assets.models import Asset
15 16
from combo.apps.assets.utils import add_tar_content
16 17
from combo.apps.assets.utils import clean_assets_files
17 18
from combo.apps.assets.utils import export_assets
......
138 139
    clean_assets_files()
139 140
    assert count_asset_files() == 0
140 141
    clean_assets_files()
141 142

  
142 143

  
143 144
def test_assets_export_size_view(app, some_assets):
144 145
    resp = app.get(reverse('combo-manager-assets-export-size'))
145 146
    assert resp.text.split() == ['(9', 'bytes)']
147
    clean_assets_files()
148

  
149

  
150
def test_cmd_import_export_assets(tmpdir, some_assets):
151
    assert Asset.objects.count() == 2
152
    assert count_asset_files() == 2
153

  
154
    filename = os.path.join(str(tmpdir), 'file.tar')
155
    call_command('export_assets', '--output', filename)
156
    Asset.objects.all().delete()
157
    assert Asset.objects.count() == 0
158
    clean_assets_files()
159

  
160
    Asset(key='banner', asset=File(BytesIO(b'modified'), 'test.png')).save()
161
    call_command('import_assets', filename)  # default behaviour
162
    assert Asset.objects.count() == 2
163
    assert count_asset_files() == 2
164
    assert open('%s/assets/test.png' % default_storage.path('')).read() == 'modified'
165

  
166
    call_command('import_assets', filename, '--overwrite')
167
    assert Asset.objects.count() == 2
168
    assert count_asset_files() == 2
169
    assert open('%s/assets/test.png' % default_storage.path('')).read() == 'test'
170
    clean_assets_files()
146
-