def get_app_perms(model_or_app_label): """ Get *perm* (a string in format of 'app_label.codename') list of the specified django application. Parameters ---------- model_or_app_label : model class or string A model class or app_label string to specify the particular django application. Returns ------- set A set of perms of the specified django application. Examples -------- >>> perms1 = get_app_perms('auth') >>> perms2 = get_app_perms(Permission) >>> perms1 == perms2 True """ from django.contrib.auth.models import Permission if not isstr(model_or_app_label): # assume model_or_app_label is model class app_label = model_or_app_label._meta.app_label else: app_label = model_or_app_label qs = Permission.objects.filter(content_type__app_label=app_label) perms = ["%s.%s" % (app_label, p.codename) for p in qs.iterator()] return set(perms)
def register(self, model, handler=None): """ Register a permission handler to the model Parameters ---------- model : django model class A django model class handler : permission handler class, string, or None A permission handler class or a dotted path Raises ------ ImproperlyConfigured Raise when the model is abstract model KeyError Raise when the model is already registered in registry The model cannot have more than one handler. """ from permission.handlers import PermissionHandler if model._meta.abstract: raise ImproperlyConfigured( 'The model %s is abstract, so it cannot be registered ' 'with permission.' % model) if model in self._registry: raise KeyError("A permission handler class is already " "registered for '%s'" % model) if handler is None: handler = settings.PERMISSION_DEFAULT_PERMISSION_HANDLER if isstr(handler): handler = import_string(handler) if not inspect.isclass(handler): raise AttributeError( "`handler` attribute must be a class. " "An instance was specified.") if not issubclass(handler, PermissionHandler): raise AttributeError( "`handler` attribute must be a subclass of " "`permission.handlers.PermissionHandler`") # Instantiate the handler to save in the registry instance = handler(model) self._registry[model] = instance