Project

General

Profile

0001-base-allow-specifying-resources-to-export-15269.patch

Valentin Deniaud, 19 Dec 2019 03:29 PM

Download (2.99 KB)

View differences:

Subject: [PATCH 1/2] base: allow specifying resources to export (#15269)

 passerelle/base/management/commands/export_site.py |  4 +++-
 passerelle/utils/__init__.py                       |  4 +++-
 tests/test_import_export.py                        | 11 +++++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)
passerelle/base/management/commands/export_site.py
10 10
    help = 'Export the site'
11 11

  
12 12
    def add_arguments(self, parser):
13
        parser.add_argument('--slugs', nargs='+', default=None,
14
                            help='specify resources to export')
13 15
        parser.add_argument('--output', metavar='FILE', default=None,
14 16
                            help='name of a file to write output to')
15 17

  
......
18 20
            output = open(options['output'], 'w')
19 21
        else:
20 22
            output = sys.stdout
21
        json.dump(export_site(), output, indent=4)
23
        json.dump(export_site(slugs=options['slugs']), output, indent=4)
passerelle/utils/__init__.py
285 285

  
286 286

  
287 287

  
288
def export_site():
288
def export_site(slugs=None):
289 289
    '''Dump passerelle configuration (users, resources and ACLs) to JSON dumpable dictionnary'''
290 290
    from passerelle.base.models import ApiUser
291 291
    from passerelle.base.models import BaseResource
......
297 297
        if subclass._meta.abstract:
298 298
            continue
299 299
        for resource in subclass.objects.all():
300
            if slugs and resource.slug not in slugs:
301
                continue
300 302
            try:
301 303
                resources.append(resource.export_json())
302 304
            except NotImplementedError:
tests/test_import_export.py
211 211
    import_site(first, import_users=True)
212 212
    second = export_site()
213 213
    assert first == second
214

  
215

  
216
def test_export_by_slug():
217
    Bdp.objects.create(service_url='https://bdp.example.com/', slug='bdp')
218
    Bdp.objects.create(service_url='https://bdp2.example.com/', slug='bdp2')
219
    Bdp.objects.create(service_url='https://bdp3.example.com/', slug='bdp3')
220
    output = get_output_of_command('export_site', '--slugs', 'bdp', 'bdp3')
221
    export = json.loads(output)
222

  
223
    assert len(export['resources']) == 2
224
    assert 'bdp2' not in [res['slug'] for res in export['resources']]
214
-