Package: django-simple_perms
Really simple permission backend for djangoClass based, No database, Object levelInspired bydjango-permissionTested with Django 3.2 to 5.0 - python 3.8 to 3.12.
For older django versions, usedjango-simple_perms 0.2.8IntroductionThe app autodiscover perms.py module in your project's apps.This modules should register PermissionLogic based class.When calling django's has_perm method, it will run the corresponding method name in your PermissionLogic class.See usage section below for comprehensive example.UsageInstall from pypi :pip install django-simple_permsorpoetry add django-simple_permssettings.pyINSTALLED_APPS=(# ...'simple_perms',# Add simple_perms app to your INSTALLED_APPS# ...)AUTHENTICATION_BACKENDS=('simple_perms.PermissionBackend',# Add permission backend before django's one'django.contrib.auth.backends.ModelBackend',)project_app/perms.pyfromsimple_permsimportregister,PermissionLogicclassProjectLogic(PermissionLogic):defadd_project(self,user,project,perm):returnTruedefchange_project(self,user,project,perm):returnuser.is_admin()orproject.owner==userdelete_project=change_projectdefdefault_permission(self,user,project,perm):# Optional, default to global default permission, which default to Falsereturnuser.is_admin()register('project_app',ProjectLogic)user1.has_perm('project_app.add_project')# Trueuser1.has_perm('project_app.change_project',user1_project)# Trueuser1.has_perm('project_app.delete_project',user1_project)# Trueuser2.has_perm('project_app.change_project',user1_project)# Falseadmin.has_perm('project_app.change_project',user1_project)# TrueDefault permissionIf a checked permission doesn't exists in registered PermissionLogic based classe, the backend will run the default_permission method of this class. If no default_permission defined, it default to the global default permission which default to False.Change global default permissionsettings.pySIMPLE_PERMS_GLOBAL_DEFAULT_PERMISSION='path.to.custom_global_default_permission'path/to.pydefcustom_global_default_permission(user,obj,perm):returnuser.is_admin()global_default_permission and default_permission have the same arguments as others permissions :(user, obj, perm)Change autodiscovered module namesimple_perms autodiscover perms.py modules in every django's apps. You can change the module name to autodiscover using the SIMPLE_PERMS_MODULE_NAME setting :SIMPLE_PERMS_MODULE_NAME='permission'Run testspythonruntests.pyHelper for your testsfromdjango.testimportTestCasefromsimple_perms.helpersimportAssertPermissionsclassTestContractPermission(AssertPermissions,TestCase):defsetUp(self):self.admin=UserFactory(role="admin")self.contract=ContractFactory()deftest_permissions_for_admin(self):permissions=[{'usr':'admin','perm':'contracts.add','args':(None,),'result':True,},{'usr':'admin','perm':'contracts.view','args':(self.contract,),'result':True,},{'usr':'admin','perm':'contracts.change','args':(self.contract,),'result':True,},]self.assertPerms(permissions)Which fails:======================================================================
FAIL: test_permissions_for_admin (contracts.tests.perms.TestContractPermission)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/django/contracts/tests/perms.py", line 48, in test_permissions_of_admin
    self.assertPerms(permissions)
  File "/app/django/django-simple_perms/simple_perms/helpers.py", line 37, in assertPerms
    raise e
  File "/app/django/django-simple_perms/simple_perms/helpers.py", line 66, in _test_permission_
    getattr(self, permission['usr']).has_perm(permission['perm'], *permission['args'])
AssertionError: ('PERM ERROR admin contracts.add:  False is not true', 'PERM ERROR admin contracts.view:  False is not true', 'PERM ERROR admin contracts.change:  False is not true')

----------------------------------------------------------------------Setup dev environnement# install dev dependenciespoetryinstall--no-root# install git pre-commitpre-commitinstallBuild package and publish on PyPIChange version number inpyproject.tomlpoetrybuild
poetrypublish