Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add management commands to export/import mega menu #6011

Merged
merged 2 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cfgov/mega_menu/management/commands/export_mega_menu.py
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
)
36 changes: 36 additions & 0 deletions cfgov/mega_menu/management/commands/import_mega_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import argparse
import json

from django.core.management.base import BaseCommand

from mega_menu.models import Menu


class Command(BaseCommand):
help = 'Import mega menu content as JSON'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add that it will overwrite any existing mega menu for the given language?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion, @schbetsy. Unfortunately it's tricky to add multiple lines in command help, so we're limited to one line here. I went with "Import (and overwrite) language-specific mega menu content as JSON." -- does that capture this well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me 👍


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 cfgov/mega_menu/tests/management/commands/test_export_mega_menu.py
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 cfgov/mega_menu/tests/management/commands/test_import_mega_menu.py
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)