Projet

Général

Profil

0001-general-add-new-export_site-management-command-13540.patch

Frédéric Péters, 11 octobre 2016 10:38

Télécharger (5,19 ko)

Voir les différences:

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
combo/data/management/commands/export_site.py
1
# combo - content management system
2
# Copyright (C) 2016  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import json
18
import sys
19

  
20
from django.core.management.base import BaseCommand
21

  
22
from combo.data.models import Page
23

  
24
class Command(BaseCommand):
25
    args = ['...']
26

  
27
    def handle(self, json_file='-', *args, **kwargs):
28
        if json_file == '-':
29
            output = sys.stdout
30
        else:
31
            output = open(json_file, 'w')
32
        json.dump(Page.export_all_for_json(), output, indent=2)
combo/data/models.py
250 250
                page.parent = Page.objects.get(slug=json_page.get('parent_slug'))
251 251
                page.save()
252 252

  
253
    @classmethod
254
    def export_all_for_json(cls):
255
        ordered_pages = Page.get_as_reordered_flat_hierarchy(cls.objects.all())
256
        return [x.get_serialized_page() for x in ordered_pages]
257

  
253 258

  
254 259
class CellMeta(MediaDefiningClass, ModelBase):
255 260
    pass
combo/manager/views.py
58 58

  
59 59
    def render_to_response(self, context, **response_kwargs):
60 60
        response = HttpResponse(content_type='application/json')
61
        ordered_pages = Page.get_as_reordered_flat_hierarchy(self.object_list)
62
        json.dump([x.get_serialized_page() for x in ordered_pages],
63
                response, indent=2)
61
        json.dump(Page.export_all_for_json(), response, indent=2)
64 62
        return response
65 63

  
66 64
site_export = SiteExportView.as_view()
tests/test_pages.py
1
from StringIO import StringIO
2
import os
1 3
import pytest
4
import sys
2 5

  
6
from django.conf import settings
3 7
from django.contrib.auth.models import User, Group
4 8
from combo.data.models import Page, CellBase, TextCell, LinkCell
9
from combo.data.management.commands.import_site import Command as ImportSiteCommand
10
from combo.data.management.commands.export_site import Command as ExportSiteCommand
5 11

  
6 12
pytestmark = pytest.mark.django_db
7 13

  
......
197 203
    page3.public = False
198 204
    page3.save()
199 205
    assert page.get_next_page(None) is None
206

  
207
def test_import_export_management_commands():
208
    page = Page(title=u'foo', slug='foo', order=0)
209
    page.save()
210

  
211
    cell = TextCell(page=page, text='foo', order=0)
212
    cell.save()
213

  
214
    page2 = Page(title=u'bar', slug='bar', order=1, parent=page)
215
    page2.save()
216

  
217
    cell = TextCell(page=page2, text='bar', order=0)
218
    cell.save()
219

  
220
    export_filename = os.path.join(settings.MEDIA_ROOT, 'site-export.json')
221
    if os.path.exists(export_filename):
222
        os.unlink(export_filename)
223

  
224
    cmd = ExportSiteCommand()
225
    cmd.handle(export_filename)
226
    assert os.path.exists(export_filename)
227

  
228
    stdout = sys.stdout
229
    try:
230
        sys.stdout = StringIO()
231
        cmd.handle('-')
232
        assert sys.stdout.getvalue() == open(export_filename).read()
233
    finally:
234
        sys.stdout = stdout
235

  
236
    Page.objects.all().delete()
237

  
238
    cmd = ImportSiteCommand()
239
    cmd.handle(export_filename)
240

  
241
    new_page_1 = Page.objects.all().order_by('order')[0]
242
    new_page_2 = Page.objects.all().order_by('order')[1]
243
    assert new_page_1.title == 'foo'
244
    assert new_page_2.title == 'bar'
245
    assert len(CellBase.get_cells(page_id=new_page_1.id)) == 1
246
    assert isinstance(CellBase.get_cells(page_id=new_page_1.id)[0], TextCell)
247
    assert CellBase.get_cells(page_id=new_page_1.id)[0].text == 'foo'
200
-