8000 added support for include('path.to.urls') and end_dollar appending in… · phpdude/django-macros-url@521c43e · GitHub
[go: up one dir, main page]

Skip to content

Commit 521c43e

Browse files
author
Alexandr Shurigin
committed
added support for include('path.to.urls') and end_dollar appending in normalized urls. added tests.
version bumped.
1 parent b81b275 commit 521c43e

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.1.0 - Routing must be simple as possible
1+
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.1.1 - Routing must be simple as possible
22

33
Django Macros Url makes it easy to write (and read) url patterns in your django applications by using macros.
44

macrosurl/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.conf.urls import url as baseurl
44

5-
VERSION = (0, 1, 0)
5+
VERSION = (0, 1, 1)
66

77
_macros_library = {
88
'id': r'\d+',
@@ -27,14 +27,18 @@ def regex_group(macro, pattern):
2727
return '(?P<%s>%s)' % (macro, pattern)
2828

2929

30-
def normalize_pattern(url):
31-
return '^%s$' % url.lstrip("^ \n").rstrip("$ \n")
30+
def normalize_pattern(url, end_dollar=True):
31+
pattern = '^%s$'
32+
if not end_dollar:
33+
pattern = '^%s'
34+
35+
return pattern % url.lstrip("^ \n").rstrip("$ \n")
3236

3337

3438
class MacroUrlPattern(object):
35-
def __init__(self, pattern):
39+
def __init__(self, pattern, end_dollar=True):
3640
self.pattern = pattern
37-
41+
self.end_dollar = end_dollar
3842

3943
def compile(self):
4044
pattern = self.pattern
@@ -48,7 +52,7 @@ def compile(self):
4852
pattern = pattern.replace(match, regex_group(macro, _macros_library[_macro]))
4953
continue
5054

51-
return normalize_pattern(pattern)
55+
return normalize_pattern(pattern, self.end_dollar)
5256

5357
@property
5458
def compiled(self):
@@ -65,4 +69,8 @@ def __unicode__(self):
6569

6670

6771
def url(regex, view, kwargs=None, name=None, prefix=''):
68-
return baseurl(MacroUrlPattern(regex), view, kwargs=kwargs, name=name, prefix=prefix)
72+
end_dollar = True
73+
if isinstance(view, tuple) and len(view) == 3:
74+
end_dollar = False
75+
76+
return baseurl(MacroUrlPattern(regex, end_dollar=end_dollar), view, kwargs=kwargs, name=name, prefix=prefix)

tests/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import uuid
33

44
from django.conf import settings
5+
from django.conf.urls import include
56

67
from macrosurl import MacroUrlPattern, url
78

@@ -63,6 +64,11 @@ def test_strongurl(self):
6364
self.assertEqual(MacroUrlPattern('orders/:date/:uuid/products/:slug/:variant_id').compiled,
6465
'^orders/(?P<date>\\d{4}-(0?([1-9])|10|11|12)-((0|1|2)?([1-9])|[1-3]0|31))/(?P<uuid>[a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{12})/products/(?P<slug>[\\w-]+)/(?P<variant_id>\\d+)$')
6566

67+
# noinspection PyProtectedMember
68+
def test_includes_end(self):
69+
self.assertEqual(str(url('users/:slug', include('tests'))._regex), '^users/(?P<slug>[\\w-]+)')
70+
self.assertEqual(str(url('users/:slug', include('tests', namespace='1'))._regex), '^users/(?P<slug>[\\w-]+)')
71+
6672

6773
class TestRegexUrlResolving(unittest.TestCase):
6874
def setUp(self):

0 commit comments

Comments
 (0)
0