datatracker/ietf/redirects/views.py
Bill Fenner 47f60e49d1 * Add leading "/" to url.
* Remove extra "/" in case the database has extra leading or trailing slashes.
* Catch KeyError, not IndexError, since that's what gets thrown in the case I was thinking about.
 - Legacy-Id: 335
2007-06-12 17:41:15 +00:00

37 lines
1.1 KiB
Python

from django.http import HttpResponseRedirect,Http404
import re
from ietf.redirects.models import Redirect, Command
def redirect(request, path="", script=""):
if path:
script = path + "/" + script
try:
redir = Redirect.objects.get(cgi=script)
except Redirect.DoesNotExist:
raise Http404
url = "/" + redir.url + "/"
(rest, remove) = (redir.rest, redir.remove)
try:
cmd = redir.commands.all().get(command=request.REQUEST['command'])
if cmd.url:
rest = cmd.url + "/" + cmd.suffix.rest
else:
rest = cmd.suffix.rest
remove = cmd.suffix.remove
except Command.DoesNotExist:
pass # it's ok, there's no more-specific request.
except KeyError:
pass # it's ok, request didn't have 'command'.
try:
url += rest % request.REQUEST
except:
# rest had something in it that request didn't have, so just
# redirect to the root of the tool.
pass
# Be generous in what you accept: collapse multiple slashes
url = re.sub(r'/+', '/', url)
if remove:
url = re.sub(re.escape(remove) + "/?$", "", url)
return HttpResponseRedirect(url)