general: add new export_site management command (#13540)

This commit is contained in:
Frédéric Péters 2016-10-11 10:36:33 +02:00
parent d107771bba
commit f3391a6279
4 changed files with 86 additions and 3 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

@ -250,6 +250,11 @@ class Page(models.Model):
page.parent = Page.objects.get(slug=json_page.get('parent_slug')) page.parent = Page.objects.get(slug=json_page.get('parent_slug'))
page.save() 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): class CellMeta(MediaDefiningClass, ModelBase):
pass pass

View File

@ -58,9 +58,7 @@ class SiteExportView(ListView):
def render_to_response(self, context, **response_kwargs): def render_to_response(self, context, **response_kwargs):
response = HttpResponse(content_type='application/json') response = HttpResponse(content_type='application/json')
ordered_pages = Page.get_as_reordered_flat_hierarchy(self.object_list) json.dump(Page.export_all_for_json(), response, indent=2)
json.dump([x.get_serialized_page() for x in ordered_pages],
response, indent=2)
return response return response
site_export = SiteExportView.as_view() site_export = SiteExportView.as_view()

View File

@ -1,7 +1,13 @@
from StringIO import StringIO
import os
import pytest import pytest
import sys
from django.conf import settings
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from combo.data.models import Page, CellBase, TextCell, LinkCell 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 pytestmark = pytest.mark.django_db
@ -197,3 +203,45 @@ def test_next_previous():
page3.public = False page3.public = False
page3.save() page3.save()
assert page.get_next_page(None) is None 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'