Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Pylons/pyramid
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonc committed Aug 16, 2012
2 parents 1431f5a + 7401b8e commit 8c7b0f1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
15 changes: 15 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ Features
- When there is a predicate mismatch exception (seen when no view matches for
a given request due to predicates not working), the exception now contains
a textual description of the predicate which didn't match.

- An ``add_permission`` directive method was added to the Configurator. This
directive registers a free-standing permission introspectable into the
Pyramid introspection system. Frameworks built atop Pyramid can thus use
the the ``permissions`` introspectable category data to build a
comprehensive list of permissions supported by a running system. Before
this method was added, permissions were already registered in this
introspectable category as a side effect of naming them in an ``add_view``
call, this method just makes it possible to arrange for a permission to be
put into the ``permissions`` introspectable category without naming it
along with an associated view. Here's an example of usage of
``add_permission``::

config = Configurator()
config.add_permission('view')
1 change: 1 addition & 0 deletions docs/api/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
.. automethod:: set_authentication_policy
.. automethod:: set_authorization_policy
.. automethod:: set_default_permission
.. automethod:: add_permission

:methodcategory:`Setting Request Properties`

Expand Down
17 changes: 6 additions & 11 deletions pyramid/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import operator
import os
import sys
import types
import warnings
import venusian

Expand All @@ -23,7 +22,6 @@
text_,
reraise,
string_types,
PY3,
)

from pyramid.events import ApplicationCreated
Expand All @@ -45,7 +43,6 @@
Introspectable,
Introspector,
Registry,
Deferred,
undefer,
)

Expand Down Expand Up @@ -553,10 +550,9 @@ def action(self, discriminator, callable=None, args=(), kw=None, order=0,
introspectables = ()

if autocommit:
if isinstance(discriminator, Deferred):
# callables can depend on the side effects of resolving a
# deferred discriminator
discriminator.resolve()
# callables can depend on the side effects of resolving a
# deferred discriminator
undefer(discriminator)
if callable is not None:
callable(*args, **kw)
for introspectable in introspectables:
Expand Down Expand Up @@ -786,10 +782,9 @@ def __getattr__(self, name):
c, action_wrap = c
if action_wrap:
c = action_method(c)
if PY3: # pragma: no cover
m = types.MethodType(c, self)
else:
m = types.MethodType(c, self, self.__class__)
# Create a bound method (works on both Py2 and Py3)
# http://stackoverflow.com/a/1015405/209039
m = c.__get__(self, self.__class__)
return m

def with_package(self, package):
Expand Down
20 changes: 20 additions & 0 deletions pyramid/config/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@ def register():
introspectables=(intr, perm_intr,))


def add_permission(self, permission_name):
"""
A configurator directive which registers a free-standing
permission without associating it with a view callable. This can be
used so that the permission shows up in the introspectable data under
the ``permissions`` category (permissions mentioned via ``add_view``
already end up in there). For example::
config = Configurator()
config.add_permission('view')
"""
intr = self.introspectable(
'permissions',
permission_name,
permission_name,
'permission'
)
intr['value'] = permission_name
self.action(None, introspectables=(intr,))

9 changes: 9 additions & 0 deletions pyramid/tests/test_config/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ def test_set_default_permission(self):
self.assertEqual(config.registry.getUtility(IDefaultPermission),
'view')

def test_add_permission(self):
config = self._makeOne(autocommit=True)
config.add_permission('perm')
cat = config.registry.introspector.get_category('permissions')
self.assertEqual(len(cat), 1)
D = cat[0]
intr = D['introspectable']
self.assertEqual(intr['value'], 'perm')

0 comments on commit 8c7b0f1

Please sign in to comment.