export refactored
This commit is contained in:
parent
d58869d99d
commit
5eca355f75
|
@ -7,7 +7,7 @@ from cms.models import Page, CMSPlugin
|
|||
from django.core import serializers
|
||||
from pprint import pprint
|
||||
|
||||
exclude_attrs = {'Page': ('parent', 'site', 'placeholders',
|
||||
exclude_attrs = {'Page': ('parent', 'site',
|
||||
'reverse_id', 'publisher_public'),
|
||||
'Placeholder': (),
|
||||
'CMSPlugin': ('placeholder', 'parent',),
|
||||
|
@ -20,60 +20,32 @@ class Command(BaseCommand):
|
|||
|
||||
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):
|
||||
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])
|
||||
# return '%s(%s)' % (data.id, type(data))
|
||||
return json.dumps(raw_data, cls=DjangoJSONEncoder)
|
||||
raw_data.update(natural_key)
|
||||
return raw_data #json.dumps(raw_data, cls=DjangoJSONEncoder)
|
||||
|
||||
def _get_object_descendants(self, obj, obj_dict={}, **options):
|
||||
"""
|
||||
returns the hierarchy
|
||||
"""
|
||||
obj_dump = self._serialize(obj)
|
||||
obj_dict[obj_dump] = []
|
||||
|
||||
if isinstance(obj, Page):
|
||||
# its a CMS Page
|
||||
placeholders = obj.placeholders.all()
|
||||
if placeholders:
|
||||
for placeholder in placeholders:
|
||||
placeholder_dump = self._serialize(placeholder)
|
||||
plugins = placeholder.cmsplugin_set.filter(parent__isnull=True)
|
||||
if plugins:
|
||||
hierarchy = []
|
||||
for plugin in plugins:
|
||||
hierarchy.append(self._get_object_descendants(plugin, {}))
|
||||
obj_dict[obj_dump].append({placeholder_dump: hierarchy})
|
||||
else:
|
||||
obj_dict[obj_dump].append(placeholder_dump)
|
||||
|
||||
# add related titles
|
||||
titles = obj.title_set.all()
|
||||
if titles:
|
||||
for title in titles:
|
||||
obj_dict[obj_dump].append(self._serialize(title))
|
||||
|
||||
children = obj.get_children()
|
||||
if children:
|
||||
for child in children:
|
||||
descendants = self._get_object_descendants(child, {})
|
||||
child_dump = self._serialize(child)
|
||||
if descendants:
|
||||
obj_dict[obj_dump].append(descendants)
|
||||
else:
|
||||
obj_dict[obj_dump].append(child)
|
||||
return obj_dict
|
||||
def serialize_page(self, page):
|
||||
key = {'pk': page.get_path(self.options['lang'])}
|
||||
title = page.title_set.get(language=self.options['lang'])
|
||||
return [self._serialize(page, key), self._serialize(title, key)]
|
||||
|
||||
def serialize_placeholders(self, placeholders):
|
||||
return serializers.serialize('python', placeholders)
|
||||
# return json.dumps(raw_data, cls=DjangoJSONEncoder)
|
||||
|
||||
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(self._get_object_descendants(page), output)
|
||||
json.dump(data, output, cls=DjangoJSONEncoder)
|
||||
|
|
Reference in New Issue