def handle(self, *args, **options): """ Processes the converted data into the Mezzanine database correctly. Attributes: mezzanine_user: the user to put this data in against date_format: the format the dates are in for posts and comments """ from mezzanine.blog.models import BlogPost from mezzanine.core.models import CONTENT_STATUS_PUBLISHED from mezzanine.generic.models import AssignedKeyword, Keyword from mezzanine.generic.models import ThreadedComment mezzanine_user = options.get("mezzanine_user") site = Site.objects.get_current() verbosity = int(options.get("verbosity", 1)) # Validate the Mezzanine user. if mezzanine_user is None: raise CommandError("No Mezzanine user has been specified") try: mezzanine_user = User.objects.get(username=mezzanine_user) except User.DoesNotExist: raise CommandError("Invalid Mezzanine user: %s" % mezzanine_user) # Run the subclassed ``handle_import`` and save posts, tags and # comments to the DB. self.handle_import(options) for post in self.posts: if verbosity >= 1: print "Importing post titled: %s" % post["title"] tags = post.pop("tags") comments = post.pop("comments") old_url = post.pop("old_url") post_args = post post_args["user"] = mezzanine_user post_args["status"] = CONTENT_STATUS_PUBLISHED post, created = BlogPost.objects.get_or_create(**post_args) for tag in tags: keyword, created = Keyword.objects.get_or_create(title=tag) post.keywords.add(AssignedKeyword(keyword=keyword)) for comment in comments: if verbosity >= 1: print "Importing comment by: %s" % comment["user_name"] comment["site"] = site post.comments.add(ThreadedComment(**comment)) if old_url is not None: redirect, created = Redirect.objects.get_or_create( site=site, old_path=urlparse(old_url).path) redirect.new_path = urlparse(post.get_absolute_url()).path redirect.save()
def handle(self, *args, **options): """ Processes the converted data into the Mezzanine database correctly. Attributes: mezzanine_user: the user to put this data in against date_format: the format the dates are in for posts and comments """ mezzanine_user = options.get("mezzanine_user") site = Site.objects.get_current() verbosity = int(options.get("verbosity", 1)) prompt = options.get("interactive") # Validate the Mezzanine user. if mezzanine_user is None: raise CommandError("No Mezzanine user has been specified") try: mezzanine_user = User.objects.get(username=mezzanine_user) except User.DoesNotExist: raise CommandError("Invalid Mezzanine user: %s" % mezzanine_user) # Run the subclassed ``handle_import`` and save posts, tags, # categories, and comments to the DB. self.handle_import(options) for post_data in self.posts: categories = post_data.pop("categories") tags = post_data.pop("tags") comments = post_data.pop("comments") old_url = post_data.pop("old_url") post_data = self.trunc(ForumPost, prompt, **post_data) initial = { "title": post_data.pop("title"), "user": mezzanine_user, } post, created = ForumPost.objects.get_or_create(**initial) for k, v in post_data.items(): setattr(post, k, v) post.save() if created and verbosity >= 1: print "Imported post: %s" % post for name in categories: cat = self.trunc(ForumCategory, prompt, title=name) if not cat["title"]: continue cat, created = ForumCategory.objects.get_or_create(**cat) if created and verbosity >= 1: print "Imported category: %s" % cat post.categories.add(cat) for comment in comments: comment = self.trunc(ThreadedComment, prompt, **comment) comment["site"] = site post.comments.add(ThreadedComment(**comment)) if verbosity >= 1: print "Imported comment by: %s" % comment["user_name"] self.add_meta(post, tags, prompt, verbosity, old_url) # Create any pages imported (Wordpress can include pages) in_menus = [] footer = [ menu[0] for menu in settings.PAGE_MENU_TEMPLATES if menu[-1] == "pages/menus/footer.html" ] if options["in_navigation"]: in_menus = [menu[0] for menu in settings.PAGE_MENU_TEMPLATES] if footer and not options["in_footer"]: in_menus.remove(footer[0]) elif footer and options["in_footer"]: in_menus = footer parents = [] for page in self.pages: tags = page.pop("tags") old_url = page.pop("old_url") old_id = page.pop("old_id") old_parent_id = page.pop("old_parent_id") page = self.trunc(RichTextPage, prompt, **page) page["status"] = CONTENT_STATUS_PUBLISHED page["in_menus"] = in_menus page, created = RichTextPage.objects.get_or_create(**page) if created and verbosity >= 1: print "Imported page: %s" % page self.add_meta(page, tags, prompt, verbosity, old_url) parents.append({ 'old_id': old_id, 'old_parent_id': old_parent_id, 'page': page, }) for obj in parents: if obj['old_parent_id']: for parent in parents: if parent['old_id'] == obj['old_parent_id']: obj['page'].parent = parent['page'] obj['page'].save() break