Skip to content

Commit f4ff21b

Browse files
authored
Merge pull request #10 from spapas/master
Support Django 2.x and make tests pass
2 parents 379f020 + 2116a5e commit f4ff21b

File tree

12 files changed

+61
-77
lines changed

12 files changed

+61
-77
lines changed

.travis.yml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@ sudo: false
22
language: python
33
matrix:
44
allow_failures:
5-
- env: TOXENV=py27-djangomaster
6-
- env: TOXENV=py34-djangomaster
75
python:
8-
- "3.4"
6+
- "3.6"
97
env:
108
global:
119
- PIP_DISABLE_PIP_VERSION_CHECK=true
1210
matrix:
13-
- TOXENV=py27-django14
14-
- TOXENV=py27-django15
15-
- TOXENV=py27-django16
16-
- TOXENV=py27-django17
1711
- TOXENV=py27-django18
18-
- TOXENV=py27-djangomaster
12+
- TOXENV=py27-django19
13+
- TOXENV=py27-django110
14+
- TOXENV=py27-django111
1915

20-
- TOXENV=py34-django15
21-
- TOXENV=py34-django16
22-
- TOXENV=py34-django17
23-
- TOXENV=py34-django18
24-
- TOXENV=py34-djangomaster
16+
- TOXENV=py36-django18
17+
- TOXENV=py36-django19
18+
- TOXENV=py36-django110
19+
- TOXENV=py36-django111
20+
- TOXENV=py36-django20
21+
- TOXENV=py36-django21
2522

2623
- TOXENV=checkqa-python2
2724
- TOXENV=checkqa

README.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ Requirements
7070
------------
7171

7272
- Python 2.7+ (Python 3 supported)
73-
- Django 1.4+
73+
- Django 1.8+
7474

7575
Quick Install
7676
-------------
7777

7878
- Install module: ``pip install django-rules-light``,
7979
- Add to ``settings.INSTALLED_APPS``: ``rules_light``,
80-
- Add in ``settings.MIDDLEWARE_CLASSES``: ``rules_light.middleware.Middleware``,
81-
- Add in ``urls.py``: ``rules_light.autodiscover()`` if you have
82-
``admin.autodiscover()`` in there too (Django < 1.7),
80+
- Add in ``settings.MIDDLEWARE_CLASSES`` (or ``settings.MIDDLEWARE`` for Django 1.10+): ``rules_light.middleware.Middleware``,
81+
8382

8483
You might want to read the `tutorial
8584
<https://django-rules-light.readthedocs.org/en/latest/tutorial.html>`_.

docs/source/tutorial.rst

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Middleware
1818
``````````
1919

2020
To enable the middleware that processes ``rules_light.Denied``
21-
exception, add to ``setings.MIDDLEWARE_CLASSES``:
21+
exception, add to ``setings.MIDDLEWARE_CLASSES`` or ``settings.MIDDLEWARE`` for Django >= 1.10:
2222

2323
.. code-block:: python
2424
@@ -29,19 +29,6 @@ exception, add to ``setings.MIDDLEWARE_CLASSES``:
2929
3030
See :doc:`docs on middleware</middleware>` for more details.
3131

32-
Autodiscovery
33-
`````````````
34-
35-
To enable autodiscovery of rules in the various apps installed
36-
in your project, add to ``urls.py`` (as early as possible):
37-
38-
.. code-block:: python
39-
40-
import rules_light
41-
rules_light.autodiscover()
42-
43-
See :doc:`docs on registry</registry>` for more details.
44-
4532
Logging
4633
```````
4734

rules_light/class_decorator.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class class_decorator(object):
5454
- ``DetailView``, it will decorate ``get_object()``, to run
5555
``rules_light.require('yourapp.yourmodel.read', obj)``,
5656
- others views, if the rule name is specified in the decorator for example
57-
``@class_decorator('some_rule')``, then it will decorate ``dispatch()``,
57+
``@class_decorator('some_rule')``, then it will decorate either
58+
``get_object()`` if the CBV is subclassed from ``SingleObjectMixin``
59+
else it will decorate ``dispatch()``,
5860
- Else it raises an exception.
5961
"""
6062
rule = None
@@ -119,13 +121,16 @@ def new_get_form(self, form_class, *args, **kwargs):
119121
patch_get_object(cls, 'delete', self.rule)
120122

121123
elif self.rule:
122-
old_dispatch = cls.dispatch
124+
if issubclass(cls, generic.detail.SingleObjectMixin):
125+
patch_get_object(cls, 'generic', self.rule)
126+
else:
127+
old_dispatch = cls.dispatch
123128

124-
def new_dispatch(self, request, *args, **kwargs):
125-
registry.require(request.user, self.dispatch._rule)
126-
return old_dispatch(self, request, *args, **kwargs)
127-
new_dispatch._rule = self.rule
128-
cls.dispatch = new_dispatch
129+
def new_dispatch(self, request, *args, **kwargs):
130+
registry.require(request.user, self.dispatch._rule)
131+
return old_dispatch(self, request, *args, **kwargs)
132+
new_dispatch._rule = self.rule
133+
cls.dispatch = new_dispatch
129134

130135
else:
131136
raise RulesLightException('Dont understand what to do')

rules_light/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Middleware(object):
1515
"""
1616
Install this middleware by adding `rules_light.middleware.Middleware`` to
17-
``settings.MIDDLEWARE_CLASSES``.
17+
``settings.MIDDLEWARE_CLASSES`` or ``settings.MIDDLEWARE`` for Django1.10+
1818
"""
1919
def process_exception(self, request, exception):
2020
"""
@@ -32,7 +32,7 @@ def process_exception(self, request, exception):
3232
return http.HttpResponseForbidden(template.loader.render_to_string(
3333
'rules_light/exception.html', ctx))
3434

35-
def __init__(self, get_response):
35+
def __init__(self, get_response=None):
3636
super(Middleware, self).__init__()
3737
# Support Django 1.10 middleware.
3838
if get_response is not None:

rules_light/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _autodiscover(registry):
146146
try:
147147
before_import_registry = copy.copy(registry)
148148
import_module('%s.rules_light_registry' % app)
149-
except:
149+
except Exception:
150150
# Reset the model registry to the state before the last import as
151151
# this import will have to reoccur on the next request and this
152152
# could raise NotRegistered and AlreadyRegistered exceptions

rules_light/shortcuts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ def is_authenticated(user, rulename, *args, **kwargs):
6969
"""
7070
Return user.is_authenticated().
7171
"""
72-
return user and user.is_authenticated()
72+
try:
73+
return user and user.is_authenticated()
74+
except Exception:
75+
return user and user.is_authenticated

rules_light/templatetags/rules_light_tags.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Rule(AsTag):
3232
)
3333

3434
def get_value(self, context, rule_name, args, kwargs):
35-
return rules_light.run(context['request'].user, rule_name, *args,
35+
return rules_light.run(context.request.user, rule_name, *args,
3636
**kwargs)
3737

38+
3839
register.tag(Rule)

rules_light/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import unicode_literals
22

3-
from django.conf.urls import patterns, url
3+
from django.conf.urls import url
44

55
from .views import RegistryView
66

77

8-
urlpatterns = patterns('',
8+
urlpatterns = [
99
url(r'$', RegistryView.as_view(), name='rules_light_registry'),
10-
)
10+
]

test_project/test_project/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
# 'django.template.loaders.eggs.Loader',
100100
)
101101

102-
MIDDLEWARE_CLASSES = (
102+
MIDDLEWARE = MIDDLEWARE_CLASSES = (
103103
'django.middleware.common.CommonMiddleware',
104104
'django.contrib.sessions.middleware.SessionMiddleware',
105105
'django.middleware.csrf.CsrfViewMiddleware',
@@ -125,10 +125,10 @@
125125
if django.VERSION < (1, 8):
126126
TEMPLATE_CONTEXT_PROCESSORS = (
127127
'django.contrib.auth.context_processors.auth',
128-
'django.core.context_processors.debug',
128+
'django.core.context_processors.debug',
129129
'django.core.context_processors.i18n',
130130
'django.core.context_processors.media',
131-
'django.core.context_processors.static',
131+
'django.core.context_processors.static',
132132
'django.core.context_processors.tz',
133133
'django.contrib.messages.context_processors.messages'
134134
)

0 commit comments

Comments
 (0)