Skip to content

Commit

Permalink
added support for include('path.to.urls') and end_dollar appending in…
Browse files Browse the repository at this point in the history
… normalized urls. added tests.

version bumped.
  • Loading branch information
Alexandr Shurigin committed Jun 3, 2014
1 parent b81b275 commit 521c43e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.1.0 - Routing must be simple as possible
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.1.1 - Routing must be simple as possible

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

Expand Down
22 changes: 15 additions & 7 deletions macrosurl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf.urls import url as baseurl

VERSION = (0, 1, 0)
VERSION = (0, 1, 1)

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


def normalize_pattern(url):
return '^%s$' % url.lstrip("^ \n").rstrip("$ \n")
def normalize_pattern(url, end_dollar=True):
pattern = '^%s$'
if not end_dollar:
pattern = '^%s'

return pattern % url.lstrip("^ \n").rstrip("$ \n")


class MacroUrlPattern(object):
def __init__(self, pattern):
def __init__(self, pattern, end_dollar=True):
self.pattern = pattern

self.end_dollar = end_dollar

def compile(self):
pattern = self.pattern
Expand All @@ -48,7 +52,7 @@ def compile(self):
pattern = pattern.replace(match, regex_group(macro, _macros_library[_macro]))
continue

return normalize_pattern(pattern)
return normalize_pattern(pattern, self.end_dollar)

@property
def compiled(self):
Expand All @@ -65,4 +69,8 @@ def __unicode__(self):


def url(regex, view, kwargs=None, name=None, prefix=''):
return baseurl(MacroUrlPattern(regex), view, kwargs=kwargs, name=name, prefix=prefix)
end_dollar = True
if isinstance(view, tuple) and len(view) == 3:
end_dollar = False

return baseurl(MacroUrlPattern(regex, end_dollar=end_dollar), view, kwargs=kwargs, name=name, prefix=prefix)
6 changes: 6 additions & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid

from django.conf import settings
from django.conf.urls import include

from macrosurl import MacroUrlPattern, url

Expand Down Expand Up @@ -63,6 +64,11 @@ def test_strongurl(self):
self.assertEqual(MacroUrlPattern('orders/:date/:uuid/products/:slug/:variant_id').compiled,
'^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+)$')

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


class TestRegexUrlResolving(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 521c43e

Please sign in to comment.