コード例 #1
0
    def migrate_page(self, request, imported_data, converter):
        try:
            existing = Page.objects.get(slug=imported_data.get('slug'))
        except Page.DoesNotExist:
            existing = None

        if existing and not self.overwrite:
            self.results['existed'] += 1
            self.results['existed_slugs'].append(imported_data.get('slug'))
            return

        # Get parent. If parent doesn't exist, then raise Page.DoesNotExist
        try:
            parent_page = Page.objects.get(slug=self.parent)
        except:
            raise Page.DoesNotExist('Parent page does not exist')

        # Convert the imported data into Wagtail readable data then publish it.
        request.POST = converter.convert(imported_data)
        request.POST['action-publish'] = 'Publish on WWW'

        # Create the page or overwrite the existing one. Since we aren't
        # hitting the middleware we have to catch the MessageFailure exception.
        try:
            if self.overwrite and existing:
                pages_views.edit(request, existing.id)
            else:
                pages_views.create(request, self.app, self.wagtail_type,
                                   parent_page.id)
        except (IntegrityError, MessageFailure):
            self.is_valid(existing, imported_data)
            try:
                if existing:
                    existing = existing.specific
                page = existing or Page.objects.descendant_of(parent_page).get(
                    slug=imported_data['slug']).specific
                if 'blog_category' in imported_data:
                    cat_key = 'blog_category'
                else:
                    cat_key = 'category'
                for i in range(len(imported_data[cat_key])):
                    for categories_tuple in categories:
                        if categories_tuple[0] in ['Blog', 'Newsroom']:
                            for category_tuple in categories_tuple[1]:
                                if category_tuple[1].lower(
                                ) == imported_data[cat_key][i].lower():
                                    c, created = page.categories.get_or_create(
                                        page=page, name=category_tuple[0])
                                    c.save()

                page.save()
                page.save_revision().publish()
            except Page.DoesNotExist:
                print imported_data[
                    'slug'], 'is not a slug for a page in the database, so no categories were made.'