74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
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)
|