Skip to content

Commit

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

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 ;-)
  • Loading branch information
phpdude committed Jul 11, 2015
1 parent a57cec2 commit 8399bc8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: python
sudo: false
python:
- "2.6"
- "2.7"
Expand All @@ -11,7 +12,7 @@ env:
install:
- pip install -q Django==$DJANGO
script:
- python setup.py test
- DJANGO_SETTINGS_MODULE=tests.settings python setup.py test

matrix:
exclude:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
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 ;-)

v0.1.7
------

Expand Down
14 changes: 12 additions & 2 deletions 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.6 - Routing must be simple as possible
# [Django Macros Url](https://github.com/phpdude/django-macros-url/) v0.2.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 Expand Up @@ -61,14 +61,21 @@ Once Macros Url finished compile regex pattern, it makes normalization of it by

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

### Auto-calling as_view on CBV objects.

Library check type of view and if view is type object with defined 'as_view' function, call this. This allow
you omit ".as_view()" calls in your urls.py files. But you can call this manual with params if you need.

This feature help you to keep your urls.py files must clean as possible. I hope you like this feature!

### Examples

Macro Url example urls.py file

```python
from django.conf.urls import patterns
from macrosurl import url

from project.portal.views import IndexView

urlpatterns = patterns(
'yourapp.views',
Expand All @@ -79,6 +86,7 @@ urlpatterns = patterns(
url('news/:year/:month/:day', 'news_date'),
url('news/:slug', 'news_entry'),
url('^order/:id$', 'order'),
url('^$', IndexView),
)
```

Expand All @@ -87,6 +95,7 @@ Django way urls example
```python
from django.conf.urls import patterns
from macrosurl import url
from project.portal.views import IndexView


urlpatterns = patterns(
Expand All @@ -98,6 +107,7 @@ urlpatterns = patterns(
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'),
url('^news/(?P<slug>[\w-]+>)$', 'news_entry'),
url('^order/(?P<id>\d+>)$', 'order'),
url('^$', IndexView.as_view()),
)
```

Expand Down
8 changes: 7 additions & 1 deletion macrosurl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

VERSION = (0, 1, 7)
VERSION = (0, 2, 0)

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

# Handle include()'s in views.
end_dollar = True
if isinstance(view, tuple) and len(view) == 3:
end_dollar = False

# Auto-calling as_view on CBVs objects. Now you can omit as_view() in your views by default.
if isinstance(view, type):
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)
3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DEBUG = True
SECRET_KEY = '123'
USE_I18N=False
19 changes: 15 additions & 4 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import uuid

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

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

if not settings.configured:
settings.configure(USE_I18N=False)

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 @@ -142,3 +138,18 @@ def test_no_match_for_invalid_uuid(self):
https://github.com/phpdude/django-macros-url/pull/2
"""
self.assertIsNone(url("invoice/:uuid", self.view).resolve('invoice/3e41b04d-0978-9027-86c2-aa90c63ecb54'))

def test_cdv_as_view_calling(self):
from .views import CBVView

self.assertIsInstance(url("", CBVView).resolve('').func, type(lambda: 1))

def test_cdv_as_view_pass_manual(self):
from .views import CBVView

self.assertIsInstance(url("", CBVView.as_view()).resolve('').func, type(lambda: 1))

def test_cdv_as_view_pass_manual_params(self):
from .views import CBVView

self.assertIsInstance(url("", CBVView.as_view(x='test.html')).resolve('').func, type(lambda: 1))
9 changes: 8 additions & 1 deletion tests/views.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
from django.views.generic import View


def view(request):
pass
pass


class CBVView(View):
x = None

0 comments on commit 8399bc8

Please sign in to comment.