82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
import json
|
|
from optparse import make_option
|
|
from django.core.management.base import BaseCommand
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
|
|
from cms.models import Page, CMSPlugin, Placeholder, Title
|
|
from cms.models.managers import PageManager
|
|
from django.core import serializers
|
|
from pprint import pprint
|
|
|
|
exclude_attrs = {'Page': ('site',),
|
|
'Placeholder': (),
|
|
'CMSPlugin': (),
|
|
'Title': ()
|
|
}
|
|
|
|
def page_natural_key(self):
|
|
return (self.get_path('fr'), )
|
|
|
|
def title_natural_key(self):
|
|
return (self.path, )
|
|
|
|
def placeholder_natural_key(self):
|
|
return self.page.natural_key() + (self.slot, )
|
|
|
|
def plugin_natural_key(self):
|
|
return self.placeholder.natural_key() + (self.plugin_type, self.position)
|
|
|
|
Page.natural_key = page_natural_key
|
|
Title.natural_key = title_natural_key
|
|
Placeholder.natural_key = placeholder_natural_key
|
|
CMSPlugin.natural_key = plugin_natural_key
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Dumps CMS page and its content(if required)'
|
|
args = 'output.json'
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
make_option('--page', action='store', default=1),
|
|
make_option('--lang', action='store', default='fr'),
|
|
make_option('--output', action='store', default='dump.json')
|
|
)
|
|
|
|
def _serialize(self, data):
|
|
return serializers.serialize('python', data, use_natural_keys=True)
|
|
|
|
def serialize_page(self, page):
|
|
"""
|
|
serialize the page and its title
|
|
"""
|
|
title = page.title_set.get(language=self.options['lang'])
|
|
placeholders = page.placeholders.all()
|
|
data = self._serialize((page, title)) + self.serialize_placeholders(placeholders)
|
|
for child in page.children.all():
|
|
data += self.serialize_page(child)
|
|
return data
|
|
|
|
def serialize_placeholders(self, placeholders):
|
|
"""
|
|
serialize placeholders and their attached plugins
|
|
"""
|
|
data = self._serialize(placeholders)
|
|
for placeholder in placeholders:
|
|
data += self.serialize_plugins(placeholder.cmsplugin_set.all())
|
|
return data
|
|
|
|
def serialize_plugins(self, plugins):
|
|
"""
|
|
serialize plugins and sub plugins
|
|
"""
|
|
data = self._serialize(plugins)
|
|
for plugin in plugins:
|
|
data += self.serialize_plugins(plugin.cmsplugin_set.all())
|
|
return data
|
|
|
|
def handle(self, *args, **options):
|
|
self.options = options
|
|
page = Page.objects.get(pk=options['page'])
|
|
with open(options.get('output'), 'w') as output:
|
|
json.dump(self.serialize_page(page), output, cls=DjangoJSONEncoder)
|