exporting tool initial version
This commit is contained in:
parent
e286c686ae
commit
2b1f256a18
|
@ -0,0 +1,79 @@
|
||||||
|
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
|
||||||
|
from django.core import serializers
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
exclude_attrs = {'Page': ('parent', 'site', 'placeholders',
|
||||||
|
'reverse_id', 'publisher_public'),
|
||||||
|
'Placeholder': (),
|
||||||
|
'CMSPlugin': ('placeholder', 'parent',),
|
||||||
|
'Title': ('page', )
|
||||||
|
}
|
||||||
|
|
||||||
|
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('--output', action='store', default='dump.json')
|
||||||
|
)
|
||||||
|
|
||||||
|
def _serialize(self, data):
|
||||||
|
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)
|
||||||
|
|
||||||
|
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 handle(self, *args, **options):
|
||||||
|
page = Page.objects.get(pk=options['page'])
|
||||||
|
with open(options.get('output'), 'w') as output:
|
||||||
|
json.dump(self._get_object_descendants(page), output)
|
|
@ -0,0 +1,73 @@
|
||||||
|
import json
|
||||||
|
from optparse import make_option
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.db.models.loading import get_model
|
||||||
|
from django.contrib.sites.models import Site
|
||||||
|
|
||||||
|
from cms.models import Page, CMSPlugin, Title, Placeholder
|
||||||
|
|
||||||
|
import pprint
|
||||||
|
|
||||||
|
|
||||||
|
# Title -> page
|
||||||
|
# Page -> parent
|
||||||
|
# Placehodler page.placeholders.add()
|
||||||
|
# CMSPlugin placeholder -> , parent ->
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Imports CMS page and its content'
|
||||||
|
option_list = BaseCommand.option_list + (
|
||||||
|
make_option('--dump', action='store', default='dump.json'),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _get_model(self, model_label):
|
||||||
|
return get_model(*model_label.split('.'))
|
||||||
|
|
||||||
|
def _unserialize(self, dump):
|
||||||
|
return json.loads(dump)
|
||||||
|
|
||||||
|
def _browse_object_descendants(self, obj, parent_obj=None):
|
||||||
|
|
||||||
|
if isinstance(obj, list):
|
||||||
|
for item in obj:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
self._browse_object_descendants(item, parent_obj)
|
||||||
|
else:
|
||||||
|
item = self._unserialize(item)
|
||||||
|
model = self._get_model(item['model'])
|
||||||
|
if model == Title:
|
||||||
|
item['fields'].update({'page': parent_obj})
|
||||||
|
print 'Creating %s object' % item['model']
|
||||||
|
instance = model.objects.create(**item['fields'])
|
||||||
|
print "instance: %s" % instance
|
||||||
|
if isinstance(instance, Placeholder):
|
||||||
|
print 'placeholder %s linked to %s' % (instance.id, parent_obj.id)
|
||||||
|
parent_obj.placeholders.add(instance)
|
||||||
|
elif isinstance(instance, Title):
|
||||||
|
print 'title %s linked to %s' % (instance.id, parent_obj.id)
|
||||||
|
instance.page = parent_obj
|
||||||
|
instance.save()
|
||||||
|
|
||||||
|
elif isinstance(obj, dict):
|
||||||
|
for parent, children in obj.iteritems():
|
||||||
|
parent = self._unserialize(parent)
|
||||||
|
model = self._get_model(parent['model'])
|
||||||
|
print 'Creating %s object' % parent['model']
|
||||||
|
if model == Page:
|
||||||
|
parent['fields'].update({'site': Site.objects.get_current(), 'parent': parent_obj})
|
||||||
|
elif model == CMSPlugin:
|
||||||
|
if isinstance(parent_obj, CMSPlugin):
|
||||||
|
parent['fields'].update({'parent': parent_obj})
|
||||||
|
elif isinstance(parent_obj, Placeholder):
|
||||||
|
parent['fields'].update({'placeholder': parent_obj})
|
||||||
|
instance = model.objects.create(**parent['fields'])
|
||||||
|
|
||||||
|
self._browse_object_descendants(children, instance)
|
||||||
|
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
with open(options.get('dump')) as dump:
|
||||||
|
data = json.load(dump)
|
||||||
|
self._browse_object_descendants(data)
|
Reference in New Issue