Skip to content

Instantly share code, notes, and snippets.

@wwitzel3
Created January 27, 2011 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wwitzel3/797809 to your computer and use it in GitHub Desktop.
Save wwitzel3/797809 to your computer and use it in GitHub Desktop.
like dis? like dat?
from pyramid.security import Allow, Deny
from pyramid.security import Authenticated, Everyone
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import unauthenticated_userid
import ordereddict
from riotoustools.models import DBSession
from riotoustools.models.dayzero import DayZeroList
from riotoustools.models.dayzero import DayZeroItem
from riotoustools.models.lifecal import LifeCal
from riotoustools.models.user import User
def _owned(obj, name, parent):
obj.__name__ = name
obj.__parent__ = parent
return obj
class Root(ordereddict.OrderedDict):
__name__ = None
__parent__ = None
__acl__ = [
(Allow, Everyone, 'view'),
(Deny, Everyone, ALL_PERMISSIONS),
]
def __init__(self, request):
ordereddict.OrderedDict.__init__(self)
self.request = request
self['dayzero'] = _owned(DayZeroContainer(request, cls=DayZeroList), 'dayzero', self)
self['lifecal'] = _owned(LifeCalContainer(request, cls=LifeCal), 'lifecal', self)
self['users'] = _owned(UserContainer(request, cls=User), 'users', self)
class ModelContainer(object):
def __init__(self, request, cls):
self.cls = cls
self.request = request
def __getitem__(self, k):
return _owned(DBSession().query(self.cls).filter_by(id=k).one(), str(k), self)
def __len__(self):
return DBSession().query(self.cls).count()
def __iter__(self):
return (_owned(x, str(x.id), self) for x in DBSession().query(self.cls))
class DayZeroContainer(ModelContainer):
def __init__(self, request, cls):
self.cls = cls
self.request = request
@property
def __acl__(self):
return [
(Allow, 'owner:{0}'.format(self.user.id), ('add', 'edit'))
] + Root.__acl__
class LifeCalContainer(ModelContainer):
pass
class UserContainer(ModelContainer):
__acl__ = [
(Allow, 'admin', ('add', 'edit', 'delete', 'view')),
(Deny, Everyone, ALL_PERMISSIONS),
]
def root_factory_maker():
return Root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment