summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbbangert <none@none>2005-09-24 16:05:31 -0700
committerbbangert <none@none>2005-09-24 16:05:31 -0700
commit654dc7c18b7750b47ad992f561c161520cb4733d (patch)
treedc4fd401463f402cdc6396ceee6f11a6048a2440
parent0dc883b6b92cc51702d18f4ccb4aaa65136f006e (diff)
downloadroutes-654dc7c18b7750b47ad992f561c161520cb4733d.tar.gz
[svn] Now allows for the user of a Mapper prefix. Prefix will be stripped before matching, and prepended before returning a generated result.v0.2
--HG-- branch : trunk
-rw-r--r--routes/base.py14
-rw-r--r--tests/test_recognition.py16
2 files changed, 30 insertions, 0 deletions
diff --git a/routes/base.py b/routes/base.py
index e9e8a6f..6fa0d1f 100644
--- a/routes/base.py
+++ b/routes/base.py
@@ -327,6 +327,8 @@ class Mapper(object):
self.urlcache = None
self._created_regs = False
self._created_gens = False
+ self.prefix = None
+ self._regprefix = None
def connect(self, *args, **kargs):
"""
@@ -400,6 +402,11 @@ class Mapper(object):
for key,val in self.maxkeys.iteritems():
for route in val:
route.makeregexp(clist)
+
+
+ # Create our regexp to strip the prefix
+ if self.prefix:
+ self._regprefix = re.compile(self.prefix + '(.*)')
self._created_regs = True
def match(self, url):
@@ -414,6 +421,11 @@ class Mapper(object):
raise Exception, "Must created regexps first"
for route in self.matchlist:
+ if self.prefix:
+ if re.match(self._regprefix, url):
+ url = re.sub(self._regprefix, r'\1', url)
+ else:
+ continue
match = route.match(url)
if match: return match
return None
@@ -505,6 +517,8 @@ class Mapper(object):
if fail: continue
path = route.generate(**kargs)
if path:
+ if self.prefix:
+ path = self.prefix + path
if self.urlcache is not None:
self.urlcache[str(kargs)] = path
return path
diff --git a/tests/test_recognition.py b/tests/test_recognition.py
index 91f4d2d..f1f13c0 100644
--- a/tests/test_recognition.py
+++ b/tests/test_recognition.py
@@ -319,6 +319,22 @@ class TestRecognition(unittest.TestCase):
self.assertEqual(None, m.match('/hello/world/how/are/you/today'))
self.assertEqual({'controller':'content','action':'index'}, m.match('/'))
+
+ def test_dynamic_with_prefix(self):
+ m = Mapper()
+ m.prefix = '/blog'
+ m.connect(':controller/:action/:id')
+ m.create_regs(['content', 'archive', 'admin/comments'])
+
+ self.assertEqual(None, m.match('/x'))
+ self.assertEqual(None, m.match('/admin/comments'))
+ self.assertEqual(None, m.match('/content/view'))
+ self.assertEqual(None, m.match('/archive/view/4'))
+
+ self.assertEqual({'controller':'content','action':'index','id':None}, m.match('/blog/content'))
+ self.assertEqual({'controller':'admin/comments','action':'view','id':None}, m.match('/blog/admin/comments/view'))
+ self.assertEqual({'controller':'archive','action':'index','id':None}, m.match('/blog/archive'))
+ self.assertEqual({'controller':'archive','action':'view', 'id':'4'}, m.match('/blog/archive/view/4'))
if __name__ == '__main__':