Rewrote redirect normalisation function

pull/46/head
Karl Hobley 2014-02-14 16:33:38 +00:00
rodzic 6c33ce0212
commit 115e20a3c4
1 zmienionych plików z 19 dodań i 18 usunięć

Wyświetl plik

@ -2,6 +2,8 @@ from django.db import models
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, PageChooserPanel
from urlparse import urlparse
class Redirect(models.Model):
old_path = models.CharField("Redirect from", max_length=255, unique=True, db_index=True)
@ -35,29 +37,28 @@ class Redirect(models.Model):
return cls.objects.all()
@staticmethod
def normalise_path(full_path):
# Split full_path into path and query_string
try:
question_mark = full_path.index('?')
path = full_path[:question_mark]
query_string = full_path[question_mark:]
except ValueError:
path = full_path
query_string = ''
def normalise_path(url):
# Parse url
url_parsed = urlparse(url)
# Check that the path has content before normalising
if path is None or path == '':
return query_string
# Make sure theres a '/' at the beginning
if path[0] != '/':
# Path must start with / but not end with /
path = url_parsed[2]
if not path.startswith('/'):
path = '/' + path
# Make sure theres not a '/' at the end
if path[-1] == '/':
if path.endswith('/'):
path = path[:-1]
return path + query_string
# Query string components must be sorted alphabetically
query_string = url_parsed[4]
query_string_components = query_string.split('&')
query_string = '&'.join(sorted(query_string_components))
# Add query string to path
if query_string:
path = path + '?' + query_string
return path
def clean(self):
# Normalise old path