8000 v0.2.0 · phpdude/django-macros-url@8399bc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8399bc8

Browse files
committed
v0.2.0
------ Added auto-calling as_view on CBVs objects. Now you can omit as_view() in your views by default. Have your code more clean then before ;-)
1 parent a57cec2 commit 8399bc8

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: python
2+
sudo: false
23
python:
34
- "2.6"
45
- "2.7"
@@ -11,7 +12,7 @@ env:
1112
install:
1213
- pip install -q Django==$DJANGO
1314
script:
14-
- python setup.py test
15+
- DJANGO_SETTINGS_MODULE=tests.settings python setup.py test
1516

1617
matrix:
1718
exclude:

CHANGELOG.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v0.2.0
2+
------
3+
4+
Added auto-calling as_view on CBVs objects. Now you can omit as_view() in your views by default.
5+
Have your code more clean then before ;-)
6+
17
v0.1.7
28
------
39

README.markdown

Lines changed: 12 additions & 2 deletions
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.6 - Routing must be simple as possible
1+
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.2.0 - 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

@@ -61,14 +61,21 @@ Once Macros Url finished compile regex pattern, it makes normalization of it by
6161

6262
This makes your urls always very strong to adding any unexpected params into path.
6363

64+
### Auto-calling as_view on CBV objects.
65+
66+
Library check type of view and if view is type object with defined 'as_view' function, call this. This allow
67+
you omit ".as_view()" calls in your urls.py files. But you can call this manual with params if you need.
68+
69+
This feature help you to keep your urls.py files must clean as possible. I hope you like this feature!
70+
6471
### Examples
6572

6673
Macro Url example urls.py file
6774

6875
```python
6976
from django.conf.urls import patterns
7077
from macrosurl import url
71-
78+
from project.portal.views import IndexView
7279

7380
urlpatterns = patterns(
7481
'yourapp.views',
@@ -79,6 +86,7 @@ urlpatterns = patterns(
7986
url('news/:year/:month/:day', 'news_date'),
8087
url('news/:slug', 'news_entry'),
8188
url('^order/:id$', 'order'),
89+
url('^$', IndexView),
8290
)
8391
```
8492

@@ -87,6 +95,7 @@ Django way urls example
8795
```python
8896
from django.conf.urls import patterns
8997
from macrosurl import url
98+
from project.portal.views import IndexView
9099

91100

92101
urlpatterns = patterns(
@@ -98,6 +107,7 @@ urlpatterns = patterns(
98107
url('^news/(?P<year>\d{4}>)/(?P<month>(0?([1-9])|10|11|12)>)/(?P<day>((0|1|2)?([1-9])|[1-3]0|31)>)$', 'news_date'),
99108
url('^news/(?P<slug>[\w-]+>)$', 'news_entry'),
100109
url('^order/(?P<id>\d+>)$', 'order'),
110+
url('^$', IndexView.as_view()),
101111
)
102112
```
103113

macrosurl/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
VERSION = (0, 1, 7)
3+
VERSION = (0, 2, 0)
44

55
_macros_library = {
66
'id': r'\d+',
@@ -70,8 +70,14 @@ def __unicode__(self):
7070
def url(regex, view, kwargs=None, name=None, prefix=''):
7171
from django.conf.urls import url as baseurl
7272

73+
# Handle include()'s in views.
7374
end_dollar = True
7475
if isinstance(view, tuple) and len(view) == 3:
7576
end_dollar = False
7677

78+
# Auto-calling as_view on CBVs objects. Now you can omit as_view() in your views by default.
79+
if isinstance(view, type):
80+
if hasattr(view, 'as_view') and hasattr(view.as_view, '__call__'):
81+
view = view.as_view()
82+
7783
return baseurl(MacroUrlPattern(regex, end_dollar=end_dollar), view, kwargs=kwargs, name=name, prefix=prefix)

tests/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DEBUG = True
2+
SECRET_KEY = '123'
3+
USE_I18N=False

tests/urls.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import uuid
22

3-
from django.conf import settings
43
from django.conf.urls import include
54
from django.utils import unittest
65

@@ -83,9 +82,6 @@ class TestRegexUrlResolving(unittest.TestCase):
8382
def setUp(self):
8483
self.view = 'tests.views.view'
8584

86-
if not settings.configured:
87-
settings.configure(USE_I18N=False)
88-
8985
def test_id(self):
9086
self.assertIsNone(url('product/:id', self.view).resolve('product/test'))
9187
self.assertIsNotNone(url('product/:id', self.view).resolve('product/10'))
@@ -142,3 +138,18 @@ def test_no_match_for_invalid_uuid(self):
142138
https://github.com/phpdude/django-macros-url/pull/2
143139
"""
144140
self.assertIsNone(url("invoice/:uuid", self.view).resolve('invoice/3e41b04d-0978-9027-86c2-aa90c63ecb54'))
141+
142+
def test_cdv_as_view_calling(self):
143+
from .views import CBVView
144+
145+
self.assertIsInstance(url("", CBVView).resolve('').func, type(lambda: 1))
146+
147+
def test_cdv_as_view_pass_manual(self):
148+
from .views import CBVView
149+
150+
self.assertIsInstance(url("", CBVView.as_view()).resolve('').func, type(lambda: 1))
151+
152+
def test_cdv_as_view_pass_manual_params(self):
153+
from .views import CBVView
154+
155+
self.assertIsInstance(url("", CBVView.as_view(x='test.html')).resolve('').func, type(lambda: 1))

tests/views.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
from django.views.generic import View
2+
3+
14
def view(request):
2-
pass
5+
pass
6+
7+
8+
class CBVView(View):
9+
x = None

0 commit comments

Comments
 (0)
0