-
Notifications
You must be signed in to change notification settings - Fork 108
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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' | ||
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me 👍