Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
------

Added support for Django 1.10 version.
  • Loading branch information
phpdude committed Nov 10, 2016
1 parent 098c2b2 commit 2ff37b8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
- DJANGO=1.7
- DJANGO=1.8
- DJANGO=1.9
- DJANGO=1.10
install:
- pip install -q Django==$DJANGO
script:
Expand All @@ -25,3 +26,6 @@ matrix:

- python: "2.6"
env: DJANGO=1.9

- python: "2.6"
env: DJANGO=1.10
5 changes: 5 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.3.0
------

Added support for Django 1.10 version.

v0.2.3
------

Expand Down
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.2.3 - Routing must be simple as possible
# [Django Macros URL](https://github.com/phpdude/django-macros-url/) v0.3.0 - 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
35 changes: 33 additions & 2 deletions macrosurl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import re
import warnings
from distutils.version import StrictVersion

VERSION = (0, 2, 3)
VERSION = (0, 3, 0)
DJANGO_VERSION = None

_macros_library = {
'id': r'\d+',
Expand Down Expand Up @@ -71,6 +74,13 @@ def __unicode__(self):
def url(regex, view, kwargs=None, name=None, prefix=''):
from django.conf.urls import url as baseurl

if DJANGO_VERSION is None:
global DJANGO_VERSION

from django import get_version

DJANGO_VERSION = get_version()

# Handle include()'s in views.
end_dollar = True
if isinstance(view, tuple) and len(view) == 3:
Expand All @@ -81,4 +91,25 @@ def url(regex, view, kwargs=None, name=None, prefix=''):
if hasattr(view, 'as_view') and hasattr(view.as_view, '__call__'):
view = view.as_view()

return baseurl(MacroUrlPattern(regex, end_dollar=end_dollar), view, kwargs=kwargs, name=name, prefix=prefix)
if prefix:
warnings.warn(
'Support for prefix in macrosurl.url() is deprecated and '
'will be removed in version 0.4 (support for prefix was removed in Django 1.10). '
'Please update your source code.'
'In old Django versions prefix was used like "if prefix:view = prefix + \'.\' + view".'
)

view = prefix + '.' + view

if DJANGO_VERSION >= StrictVersion('1.10') and not callable(view) and not isinstance(view, (list, tuple)):
warnings.warn(
'View "%s" must be a callable in case of Django 1.10. '
'Macrosurl will try to load the view automatically (this behavior will be removed in version 0.4), but '
'please update your source code.' % view
)

from django.utils.module_loading import import_string

view = import_string(view)

return baseurl(MacroUrlPattern(regex, end_dollar=end_dollar), view, kwargs=kwargs, name=name)
13 changes: 11 additions & 2 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import os
import warnings

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")

import sys
import uuid

Expand All @@ -13,6 +17,9 @@


class TestRegexCompilation(unittest.TestCase):
def setUp(self):
warnings.filterwarnings("ignore")

def test_nomacro(self):
self.assertEqual(MacroUrlPattern('^$').compiled, '^$')
self.assertEqual(MacroUrlPattern('^news/all/$').compiled, '^news/all/$')
Expand Down Expand Up @@ -92,13 +99,15 @@ def test_strongurl(self):
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-]+)')
self.assertEqual(str(url('users/:slug', 'tests')._regex), '^users/(?P<slug>[\\w-]+)$')
self.assertEqual(str(url('users/:slug', 'tests.views.view')._regex), '^users/(?P<slug>[\\w-]+)$')


class TestRegexUrlResolving(unittest.TestCase):
def setUp(self):
self.view = 'tests.views.view'

warnings.filterwarnings("ignore")

def test_id(self):
self.assertIsNone(url('product/:id', self.view).resolve('product/test'))
self.assertIsNotNone(url('product/:id', self.view).resolve('product/10'))
Expand Down Expand Up @@ -143,7 +152,7 @@ def test_date(self):
self.assertIsNotNone(url('news/:date', self.view).resolve('news/%s-%s-%s' % (y, m, d)))

def test_uuid(self):
self.assertIsNone(url("invoice/:uuid", 'view').resolve('invoice/123123-123123-1231231-1231312-3-1312312-'))
self.assertIsNone(url("invoice/:uuid", self.view).resolve('invoice/123123-123123-1231231-1231312-3-1312312-'))
for i in range(1, 1000):
self.assertIsNotNone(url("invoice/:uuid", self.view).resolve('invoice/%s' % uuid.uuid4()))

Expand Down

0 comments on commit 2ff37b8

Please sign in to comment.