def normalize_website(cls, w):
from django.core.validators import EMPTY_VALUES
from urlparse import urlparse, urlunparse, ParseResult
w = w.decode('utf-8')
if w in EMPTY_VALUES:
return None
w = w.lower().strip()
if not w.startswith('http://') and not w.startswith('https://'):
w = 'http://' + w.lstrip('/')
try:
parsed = urlparse(w)
except ValueError as e:
return None
else:
new_parsed = ParseResult(scheme='http',
netloc=cls.get_website_tld(w),
path=parsed.path.rstrip('/'),
params='',
query=parsed.query,
fragment='')
return urlunparse(new_parsed)
评论列表
文章目录