export page and its content using natural keys
This commit is contained in:
parent
5eca355f75
commit
87fedf536c
|
@ -3,17 +3,35 @@ 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
|
||||
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': ('parent', 'site',
|
||||
'reverse_id', 'publisher_public'),
|
||||
exclude_attrs = {'Page': ('site',),
|
||||
'Placeholder': (),
|
||||
'CMSPlugin': ('placeholder', 'parent',),
|
||||
'Title': ('page', )
|
||||
'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.slot, self.page.natural_key())
|
||||
|
||||
def plugin_natural_key(self):
|
||||
return (self.plugin_type, self.position, self.placeholder.natural_key())
|
||||
|
||||
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'
|
||||
|
@ -24,28 +42,40 @@ class Command(BaseCommand):
|
|||
make_option('--output', action='store', default='dump.json')
|
||||
)
|
||||
|
||||
def _serialize(self, data, natural_key):
|
||||
obj_type = data._meta.object_name
|
||||
raw_data = serializers.serialize('python', [data])[0]
|
||||
|
||||
raw_data.pop('pk')
|
||||
# remove useless key
|
||||
map(lambda attr: raw_data['fields'].pop(attr), exclude_attrs[obj_type])
|
||||
raw_data.update(natural_key)
|
||||
return raw_data #json.dumps(raw_data, cls=DjangoJSONEncoder)
|
||||
def _serialize(self, data):
|
||||
return serializers.serialize('python', data, use_natural_keys=True)
|
||||
|
||||
def serialize_page(self, page):
|
||||
key = {'pk': page.get_path(self.options['lang'])}
|
||||
"""
|
||||
serialize the page and its title
|
||||
"""
|
||||
title = page.title_set.get(language=self.options['lang'])
|
||||
return [self._serialize(page, key), self._serialize(title, key)]
|
||||
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):
|
||||
return serializers.serialize('python', placeholders)
|
||||
# return json.dumps(raw_data, cls=DjangoJSONEncoder)
|
||||
"""
|
||||
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'])
|
||||
data = self.serialize_page(page) + self.serialize_placeholders(page.placeholders.all())
|
||||
with open(options.get('output'), 'w') as output:
|
||||
json.dump(data, output, cls=DjangoJSONEncoder)
|
||||
json.dump(self.serialize_page(page), output, cls=DjangoJSONEncoder)
|
||||
|
|
Reference in New Issue