django-orchestra-test/orchestra/apps/users/roles/__init__.py

30 lines
692 B
Python
Raw Normal View History

2014-08-22 15:31:44 +00:00
from django.db import models
from ..models import User
class Register(object):
def __init__(self):
self._registry = {}
def __contains__(self, key):
return key in self._registry
def register(self, name, model):
if name in self._registry:
raise KeyError("%s already registered" % name)
2014-09-10 16:53:09 +00:00
def has_role(user, model=model):
2014-08-22 15:31:44 +00:00
try:
getattr(user, name)
2014-09-10 16:53:09 +00:00
except model.DoesNotExist:
2014-08-22 15:31:44 +00:00
return False
return True
setattr(User, 'has_%s' % name, has_role)
self._registry[name] = model
def get(self):
return self._registry
roles = Register()