def remove_perm(self, perm, group, obj): """ Removes permission for an instance and given group. We use Queryset.delete method for removing the permission. The post_delete signals will not be fired. """ if getattr(obj, "pk", None) is None: raise ObjectNotPersisted("Object %s needs to be persisted first" % obj) ctype = ContentType.objects.get_for_model(obj) kwargs = { "group": group, "permission__codename": perm.split(".", 1)[-1], "permission__content_type": ctype, ctype.model: obj, } return self.filter(**kwargs).delete()
def assign_perm(self, perm, group, obj): """ Assigns permission for an instance and a group. """ if not getattr(obj, "pk", None): raise ObjectNotPersisted("Object needs to be persisted first") ctype = ContentType.objects.get_for_model(obj) try: permission = Permission.objects.get( content_type=ctype, codename=perm.split(".", 1)[-1] ) except Permission.DoesNotExist: raise PermissionNameError("Please use existing permission codename") kwargs = {"permission": permission, "group": group, ctype.model: obj} obj_perm, _ = self.get_or_create(**kwargs) return obj_perm