-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6011 from cfpb/feature/menu-export-import
Add management commands to export/import mega menu
- Loading branch information
Showing
6 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import argparse | ||
import json | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from mega_menu.models import Menu | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Export mega menu content to JSON' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('language') | ||
parser.add_argument( | ||
'filename', | ||
type=argparse.FileType('w'), | ||
nargs='?', | ||
default='-' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
menu = Menu.objects.get(language=options['language']) | ||
json.dump( | ||
menu.submenus.stream_data, | ||
options['filename'], | ||
indent=4 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import argparse | ||
import json | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from mega_menu.models import Menu | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ( | ||
'Import (and overwrite) language-specific mega menu content as JSON.' | ||
) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('language') | ||
parser.add_argument( | ||
'filename', | ||
type=argparse.FileType('r'), | ||
nargs='?', | ||
default='-' | ||
) | ||
|
||
def handle(self, *args, **options): | ||
language = options['language'] | ||
|
||
submenus = json.load(options['filename']) | ||
|
||
_, created = Menu.objects.update_or_create( | ||
language=language, | ||
defaults={ | ||
'submenus': json.dumps(submenus), | ||
} | ||
) | ||
|
||
self.stdout.write('%s mega menu for language %s.' % ( | ||
'Created' if created else 'Updated', | ||
language | ||
)) |
Empty file.
Empty file.
38 changes: 38 additions & 0 deletions
38
cfgov/mega_menu/tests/management/commands/test_export_mega_menu.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
import uuid | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from mega_menu.models import Menu | ||
|
||
|
||
class ExportMegaMenuTests(TestCase): | ||
def test_export(self): | ||
submenus = [ | ||
{ | ||
'type': 'submenu', | ||
'id': str(uuid.uuid4()), | ||
'value': { | ||
'title': 'A submenu', | ||
'overview_page': 123, | ||
'links': [ | ||
{ | ||
'text': 'A link', | ||
'url': '/foo/bar', | ||
}, | ||
], | ||
}, | ||
} | ||
] | ||
|
||
Menu.objects.create( | ||
language='en', | ||
submenus=json.dumps(submenus) | ||
) | ||
|
||
stdout = StringIO() | ||
call_command('export_mega_menu', 'en', filename=stdout) | ||
|
||
self.assertEqual(stdout.getvalue(), json.dumps(submenus, indent=4)) |
43 changes: 43 additions & 0 deletions
43
cfgov/mega_menu/tests/management/commands/test_import_mega_menu.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
import uuid | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from mega_menu.models import Menu | ||
|
||
|
||
class ImportMegaMenuTests(TestCase): | ||
def test_no_menus_exist_by_default(self): | ||
self.assertFalse(Menu.objects.exists()) | ||
|
||
def test_import(self): | ||
submenus = [ | ||
{ | ||
'type': 'submenu', | ||
'id': str(uuid.uuid4()), | ||
'value': { | ||
'title': 'A submenu', | ||
'overview_page': 123, | ||
'links': [ | ||
{ | ||
'text': 'A link', | ||
'url': '/foo/bar', | ||
}, | ||
], | ||
}, | ||
} | ||
] | ||
|
||
stdin = StringIO(json.dumps(submenus)) | ||
stdout = StringIO() | ||
|
||
call_command('import_mega_menu', 'en', filename=stdin, stdout=stdout) | ||
self.assertEqual( | ||
stdout.getvalue(), | ||
'Created mega menu for language en.\n' | ||
) | ||
|
||
menu = Menu.objects.get(language='en') | ||
self.assertEqual(menu.submenus.stream_data, submenus) |