Implement ^flag^command syntax and use it for sub_state=1 and

rfc_flag=1

Don't return a trailing slash if the next-to-last element has
a dot in it (the inverse of the algorithm in APPEND_SLASH in the
common middleware)
 - Legacy-Id: 488
This commit is contained in:
Bill Fenner 2007-06-18 15:06:44 +00:00
parent c03019afbe
commit 0e2d7be442
2 changed files with 52 additions and 13 deletions

View file

@ -58,7 +58,7 @@
<field type="CharField" name="cgi">public/pidtracker.cgi</field>
<field type="CharField" name="url">idtracker</field>
<field type="CharField" name="rest">%(dTag)s/%(command)s</field>
<field type="CharField" name="remove"></field>
<field type="CharField" name="remove">view_id</field>
</object>
<object pk="11" model="redirects.redirect">
<field type="CharField" name="cgi">public/status_of_item.cgi</field>
@ -194,7 +194,7 @@
</object>
<object pk="33" model="redirects.redirect">
<field type="CharField" name="cgi">public/request_area_confirm.cgi</field>
<field type="CharField" name="url">approve</field>
<field type="CharField" name="url">mailinglists/approve</field>
<field type="CharField" name="rest">%(mailing_list_id)s</field>
<field type="CharField" name="remove"></field>
</object>
@ -271,7 +271,7 @@
<field to="redirects.suffix" name="suffix" rel="ManyToOneRel">4</field>
</object>
<object pk="13" model="redirects.command">
<field type="CharField" name="command">show_wg_docs</field>
<field type="CharField" name="command">show_wg_id</field>
<field type="CharField" name="url">wgdocs</field>
<field to="redirects.redirect" name="script" rel="ManyToOneRel">7</field>
<field to="redirects.suffix" name="suffix" rel="ManyToOneRel">4</field>
@ -294,6 +294,18 @@
<field to="redirects.redirect" name="script" rel="ManyToOneRel">10</field>
<field to="redirects.suffix" name="suffix" rel="ManyToOneRel">6</field>
</object>
<object pk="17" model="redirects.command">
<field type="CharField" name="command">^sub_state^view_state_desc</field>
<field type="CharField" name="url">states/substate</field>
<field to="redirects.redirect" name="script" rel="ManyToOneRel">10</field>
<field to="redirects.suffix" name="suffix" rel="ManyToOneRel">4</field>
</object>
<object pk="18" model="redirects.command">
<field type="CharField" name="command">^rfc_flag</field>
<field type="CharField" name="url"></field>
<field to="redirects.redirect" name="script" rel="ManyToOneRel">10</field>
<field to="redirects.suffix" name="suffix" rel="ManyToOneRel">7</field>
</object>
<object pk="2" model="redirects.suffix">
<field type="CharField" name="rest">%(fl)s</field>
<field type="CharField" name="remove"></field>
@ -314,4 +326,8 @@
<field type="CharField" name="rest">%(ballot_id)s</field>
<field type="CharField" name="remove"></field>
</object>
<object pk="7" model="redirects.suffix">
<field type="CharField" name="rest">rfc%(dTag)s/%(command)s</field>
<field type="CharField" name="remove">view_id</field>
</object>
</django-objects>

View file

@ -12,9 +12,33 @@ def redirect(request, path="", script=""):
raise Http404
url = "/" + redir.url + "/"
(rest, remove) = (redir.rest, redir.remove)
remove_args = []
cmd = None
try:
cmd = redir.commands.all().get(command=request.REQUEST['command'])
#
# First look for flag items, stored in the database
# as a command with a leading "^".
for flag in redir.commands.all().filter(command__startswith='^'):
fc = flag.command[1:].split("^")
if len(fc) > 1:
if request.REQUEST.get('command') != fc[1]:
continue
if request.REQUEST.has_key(fc[0]):
remove_args.append(fc[0])
if int(request.REQUEST[fc[0]]):
cmd = flag
break
#
# If that search didn't result in a match, then look
# for an exact match for the command= parameter.
if cmd is None:
try:
cmd = redir.commands.all().get(command=request.REQUEST['command'])
except Command.DoesNotExist:
pass # it's ok, there's no more-specific request.
except KeyError:
pass # it's ok, request didn't have 'command'.
if cmd is not None:
remove_args.append('command')
if cmd.url:
rest = cmd.url + "/"
else:
@ -24,10 +48,6 @@ def redirect(request, path="", script=""):
remove = cmd.suffix.remove
else:
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
url += "/"
@ -39,15 +59,18 @@ def redirect(request, path="", script=""):
url = re.sub(r'/+', '/', url)
if remove:
url = re.sub(re.escape(remove) + "/?$", "", url)
# If there is a dot in the last url segment, remove the
# trailing slash. This is basically the inverse of the
# APPEND_SLASH middleware.
if '/' in url and '.' in url.split('/')[-2]:
url = url.rstrip('/')
# Copy the GET arguments, remove all the ones we were
# expecting and if there are any left, add them to the URL.
get = request.GET.copy()
for arg in re.findall(r'%\(([^)]+)\)', rest):
remove_args += re.findall(r'%\(([^)]+)\)', rest)
for arg in remove_args:
if get.has_key(arg):
get.pop(arg)
# If we found a command in the database, there's no need to pass it along.
if cmd and get.has_key('command'):
get.pop('command')
if get:
url += '?' + get.urlencode()
return HttpResponsePermanentRedirect(url)