def handle(self, show_index_id, **options): # Find show index page show_index = ShowIndexPage.objects.get(id=show_index_id) sub_expressions_compiled = [(re.compile(sub_expr[0]), sub_expr[1]) for sub_expr in SUB_EXPRESSIONS] for student in show_index.get_students(): # Work out a URL to redirect from name = student.title for sub_expr in sub_expressions_compiled: name = sub_expr[0].sub(sub_expr[1], name) from_url = 'show2015/' + slugify(name) + '/' # Find students url inside if show_index.is_programme_page: to_url = show_index.reverse_subpage('student', programme=student.programme, slug=student.slug) else: to_url = show_index.reverse_subpage('student', school=student.school, programme=student.programme, slug=student.slug) # Normalise the URL from_url_normalised = Redirect.normalise_path(from_url) # Create the redirect redirect, created = Redirect.objects.get_or_create(old_path=from_url_normalised, defaults={'redirect_link': to_url}) # Print message if created: print "Created redirect: " + from_url_normalised + " to: " + to_url + " for student: " + student.title + " (" + str(student.id) + ")"
def handle(self, show_index_id, **options): # Find show index page show_index = ShowIndexPage.objects.get(id=show_index_id) sub_expressions_compiled = [(re.compile(sub_expr[0]), sub_expr[1]) for sub_expr in SUB_EXPRESSIONS] for student in show_index.get_students(): # Work out a URL to redirect from first_name = student.first_name last_name = student.last_name for sub_expr in sub_expressions_compiled: first_name = sub_expr[0].sub(sub_expr[1], first_name) last_name = sub_expr[0].sub(sub_expr[1], last_name) from_url = slugify(first_name) + '-' + slugify(last_name) # Find students url inside if show_index.is_programme_page: to_url = show_index.reverse_subpage('student', programme=student.programme, slug=student.slug) else: to_url = show_index.reverse_subpage('student', school=student.school, programme=student.programme, slug=student.slug) # Normalise the URL from_url_normalised = Redirect.normalise_path(from_url) # Create the redirect redirect, created = Redirect.objects.get_or_create(old_path=from_url_normalised, defaults={'redirect_link': to_url}) # Print message if created: print "Created redirect: " + from_url_normalised + " to: " + to_url + " for student: " + student.title + " (" + str(student.id) + ")"
def create_page_redirect(self, page, page_slug, old_slug): """ Construct old page path for given old slug and create redirect for that path. :param page: string current page path :param page_slug: string current page slug it will be be parent page slug for descendant page :param old_slug: string old page slug it will be be parent page slug for descendant page :return: None """ site_id, root, page_path = page.get_url_parts() old_path = self.get_old_path(page_path, page_slug, old_slug) if old_path == page_path: print( "Error: old path {!r} has to be different to current path {!r}. " "Skipping redirect creation".format(old_path, page_path)) else: old_path = Redirect.normalise_path(old_path) try: Redirect.objects.get(old_path=old_path, site_id=site_id) except Redirect.DoesNotExist: if self.dry_run: print("Redirect for {!r} path needs to be created.".format( old_path)) else: Redirect.objects.create( old_path=old_path, site_id=site_id, is_permanent=self.permanent, redirect_page=page, ) print("Redirect for {!r} path created.".format(old_path)) else: print("Redirect for {!r} path exists already. Skipping".format( old_path))
def import_redirect(self, path, redirect_to): path_normalised = Redirect.normalise_path(path) # Get redirect object redirect_object, created = Redirect.objects.get_or_create(old_path=path_normalised) redirect_object.site = self.site # Add link page = self.find_page_from_url(self.site.root_page, [component for component in redirect_to.split('/') if component]) if page: redirect_object.redirect_page = page else: print "WARN: Cannot find page for: " + redirect_to redirect_object.redirect_link = redirect_to redirect_object.save()
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()
def import_redirect(self, path, redirect_to): path_normalised = Redirect.normalise_path(path) # Get redirect object redirect_object, created = Redirect.objects.get_or_create( old_path=path_normalised) redirect_object.site = self.site # Add link page = self.find_page_from_url( self.site.root_page, [component for component in redirect_to.split('/') if component]) if page: redirect_object.redirect_page = page else: print "WARN: Cannot find page for: " + redirect_to redirect_object.redirect_link = redirect_to redirect_object.save()
def clean(self): """ The unique_together condition on the model is ignored if site is None, so need to check for duplicates manually """ cleaned_data = super(RedirectForm, self).clean() if cleaned_data.get('site') is None: old_path = cleaned_data.get('old_path') if old_path is None: # cleaned_data['old_path'] is empty because it has already failed validation, # so don't bother with our duplicate test return old_path = Redirect.normalise_path(old_path) duplicates = Redirect.objects.filter(old_path=old_path, site__isnull=True) if self.instance.pk: duplicates = duplicates.exclude(id=self.instance.pk) if duplicates: raise forms.ValidationError(_("A redirect with this path already exists."))
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!")