Beispiel #1
0
def create_descendant_redirects(old_slug, new_slug, path, pk):
    from wagtail.wagtailcore.models import Page
    from wagtail.wagtailredirects.models import Redirect
    descendant_pages = Page.objects.filter(path__startswith=path).exclude(pk=pk)
    for page in descendant_pages:
        if page.live:
            redirect = Redirect(old_path=page.get_url().replace(new_slug, old_slug), redirect_page=page,
                                is_permanent=True)
            redirect.clean()
            if not Redirect.objects.filter(old_path=redirect.old_path).exists():
                redirect.save()
Beispiel #2
0
    def handle(self, *args, **options):
        file_path = options['file_path']
        dry_run = options['dry_run']
        from_header = options['from_header']
        to_header = options['to_header']

        updated_count = 0
        created_count = 0
        error_count = 0

        with open(file_path, 'r') as f:
            reader = DictReader(f)

            for row in reader:
                old_path = row[from_header]
                new_path = row[to_header]

                # urlparse requires at least a '//' to avoid identifying the
                # domain as a path component
                if '//' not in old_path:
                    old_path = '//' + old_path

                netloc = urlparse(old_path).netloc

                if not netloc:
                    print("Line {} - No domain provided: {}".format(
                        reader.line_num, old_path))
                    continue

                try:
                    old_site = Site.objects.get(hostname=netloc)
                except Site.DoesNotExist:
                    print("Line {} - Old Site does not exist: {}".format(
                        reader.line_num, netloc))
                    error_count += 1
                    continue

                normalised_path = Redirect.normalise_path(old_path)

                try:
                    target = get_page_from_path(new_path)
                except Page.DoesNotExist:
                    print("Line {} - Page does not exist: {}".format(
                        reader.line_num, new_path))
                    error_count += 1
                    continue
                except Site.DoesNotExist:
                    print("Line {} - New Site does not exist: {}".format(
                        reader.line_num, netloc))
                    error_count += 1
                    continue

                if len(normalised_path) > 255:
                    print(
                        "Line {} - 'From' path is too long ({} characters, maximum is 255)"
                        .format(reader.line_num, len(normalised_path)))
                    error_count += 1
                    continue

                # We don't use .get_or_create because we want to support the
                # --dry-run flag
                try:
                    redirect = Redirect.objects.get(site=old_site,
                                                    old_path=normalised_path)
                    updated_count += 1
                except Redirect.DoesNotExist:
                    redirect = Redirect(site=old_site,
                                        old_path=normalised_path)
                    created_count += 1

                if not dry_run:
                    redirect.redirect_page = target
                    redirect.save()

        print("\n")
        print("Created: {}".format(created_count))
        print("Updated: {}".format(updated_count))
        print("Errored (so no action taken): {}".format(error_count))
        print("\nDone!")