importing tool with no CMSPlugin derivative working version
This commit is contained in:
parent
45d28617db
commit
c244fd6bfe
|
@ -7,9 +7,6 @@ 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()
|
||||
|
@ -25,49 +22,68 @@ class Command(BaseCommand):
|
|||
def _get_model(self, model_label):
|
||||
return get_model(*model_label.split('.'))
|
||||
|
||||
def _unserialize(self, dump):
|
||||
return json.loads(dump)
|
||||
def load_object(self, obj):
|
||||
model = obj['model'].split('.')[1]
|
||||
getattr(self, 'load_%s' % model)(obj['fields'])
|
||||
# return json.loads(dump)
|
||||
|
||||
def _browse_object_descendants(self, obj, parent_obj=None):
|
||||
def get_page_parent(self, page_path):
|
||||
|
||||
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()
|
||||
if page_path:
|
||||
return Page.objects.get(title_set__path__in=(page_path,), publisher_is_draft=True)
|
||||
return Page.objects.get_home().publisher_draft
|
||||
|
||||
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'])
|
||||
def get_title_parent(self, page_path):
|
||||
|
||||
self._browse_object_descendants(children, instance)
|
||||
try:
|
||||
parent_title, child_title = page_path[0].rsplit('/', 1)
|
||||
except ValueError:
|
||||
parent_title = None
|
||||
parent = self.get_page_parent(parent_title)
|
||||
return parent.children.get(title_set__isnull=True,
|
||||
publisher_is_draft=True,
|
||||
publisher_draft__isnull=True)
|
||||
|
||||
def load_page(self, obj):
|
||||
obj.pop('publisher_public')
|
||||
|
||||
# because a page needs a site for unknown reason!
|
||||
obj['site'] = Site.objects.get_current()
|
||||
if obj['parent']:
|
||||
parent = self.get_page_parent(obj['parent'][0])
|
||||
else:
|
||||
parent = self.get_page_parent(obj['parent'])
|
||||
obj['parent'] = parent
|
||||
page = Page(**obj)
|
||||
page.save(no_signals=True)
|
||||
|
||||
def load_placeholder(self, obj):
|
||||
page = obj.pop('page')
|
||||
page = Page.objects.get(title_set__path__in=page, publisher_draft__isnull=True)
|
||||
placeholder = Placeholder(**obj)
|
||||
placeholder.save()
|
||||
page.placeholders.add(placeholder)
|
||||
|
||||
def load_title(self, obj):
|
||||
obj['page'] = self.get_title_parent(obj['page'])
|
||||
title = Title(**obj)
|
||||
title.save()
|
||||
|
||||
def load_cmsplugin(self, obj):
|
||||
parent = obj.pop('parent')
|
||||
placeholder = obj.pop('placeholder')
|
||||
obj['placeholder'] = Placeholder.objects.get(slot=placeholder[1],
|
||||
page__title_set__path__in=(placeholder[0],),
|
||||
page__publisher_draft__isnull=True)
|
||||
if parent:
|
||||
obj['parent'] = CMSPlugin.objects.get(position=parent[3],
|
||||
plugin_type=parent[2],
|
||||
placeholder=obj['placeholder'])
|
||||
plugin = CMSPlugin(**obj)
|
||||
plugin.save(no_signals=True)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with open(options.get('dump')) as dump:
|
||||
data = json.load(dump)
|
||||
self._browse_object_descendants(data)
|
||||
for fixture in data:
|
||||
self.load_object(fixture)
|
||||
|
|
Reference in New Issue