From b8cfb2200946118ca7dd32b3a798db5833ba6b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 11 Oct 2016 10:36:33 +0200 Subject: [PATCH] general: add new export_site management command (#13540) --- combo/data/management/commands/export_site.py | 32 ++++++++++++++++++ combo/data/models.py | 5 +++ combo/manager/views.py | 4 +-- tests/test_pages.py | 48 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 combo/data/management/commands/export_site.py diff --git a/combo/data/management/commands/export_site.py b/combo/data/management/commands/export_site.py new file mode 100644 index 0000000..aff86b2 --- /dev/null +++ b/combo/data/management/commands/export_site.py @@ -0,0 +1,32 @@ +# combo - content management system +# Copyright (C) 2016 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import json +import sys + +from django.core.management.base import BaseCommand + +from combo.data.models import Page + +class Command(BaseCommand): + args = ['...'] + + def handle(self, json_file='-', *args, **kwargs): + if json_file == '-': + output = sys.stdout + else: + output = open(json_file, 'w') + json.dump(Page.export_all_for_json(), output, indent=2) diff --git a/combo/data/models.py b/combo/data/models.py index 3c238fd..d9765e9 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -250,6 +250,11 @@ class Page(models.Model): page.parent = Page.objects.get(slug=json_page.get('parent_slug')) page.save() + @classmethod + def export_all_for_json(cls): + ordered_pages = Page.get_as_reordered_flat_hierarchy(cls.objects.all()) + return [x.get_serialized_page() for x in ordered_pages] + class CellMeta(MediaDefiningClass, ModelBase): pass diff --git a/combo/manager/views.py b/combo/manager/views.py index 067da32..8512a77 100644 --- a/combo/manager/views.py +++ b/combo/manager/views.py @@ -58,9 +58,7 @@ class SiteExportView(ListView): def render_to_response(self, context, **response_kwargs): response = HttpResponse(content_type='application/json') - ordered_pages = Page.get_as_reordered_flat_hierarchy(self.object_list) - json.dump([x.get_serialized_page() for x in ordered_pages], - response, indent=2) + json.dump(Page.export_all_for_json(), response, indent=2) return response site_export = SiteExportView.as_view() diff --git a/tests/test_pages.py b/tests/test_pages.py index 5914ab2..7c71acd 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -1,7 +1,13 @@ +from StringIO import StringIO +import os import pytest +import sys +from django.conf import settings from django.contrib.auth.models import User, Group from combo.data.models import Page, CellBase, TextCell, LinkCell +from combo.data.management.commands.import_site import Command as ImportSiteCommand +from combo.data.management.commands.export_site import Command as ExportSiteCommand pytestmark = pytest.mark.django_db @@ -197,3 +203,45 @@ def test_next_previous(): page3.public = False page3.save() assert page.get_next_page(None) is None + +def test_import_export_management_commands(): + page = Page(title=u'foo', slug='foo', order=0) + page.save() + + cell = TextCell(page=page, text='foo', order=0) + cell.save() + + page2 = Page(title=u'bar', slug='bar', order=1, parent=page) + page2.save() + + cell = TextCell(page=page2, text='bar', order=0) + cell.save() + + export_filename = os.path.join(settings.MEDIA_ROOT, 'site-export.json') + if os.path.exists(export_filename): + os.unlink(export_filename) + + cmd = ExportSiteCommand() + cmd.handle(export_filename) + assert os.path.exists(export_filename) + + stdout = sys.stdout + try: + sys.stdout = StringIO() + cmd.handle('-') + assert sys.stdout.getvalue() == open(export_filename).read() + finally: + sys.stdout = stdout + + Page.objects.all().delete() + + cmd = ImportSiteCommand() + cmd.handle(export_filename) + + new_page_1 = Page.objects.all().order_by('order')[0] + new_page_2 = Page.objects.all().order_by('order')[1] + assert new_page_1.title == 'foo' + assert new_page_2.title == 'bar' + assert len(CellBase.get_cells(page_id=new_page_1.id)) == 1 + assert isinstance(CellBase.get_cells(page_id=new_page_1.id)[0], TextCell) + assert CellBase.get_cells(page_id=new_page_1.id)[0].text == 'foo' -- 2.9.3