Package to provide additional user check mechanics.
Project description
WebCase User checks
Package to provide additional user check mechanics.
Installation
pip install wc-django-user-checks
In settings.py
:
INSTALLED_APPS += [
'wcd_uer_checks',
]
WCD_USER_CHECKS = {
'REGISTRY' = 'wcd_user_checks.globals.registry', # default
'DEFINITIONS' = None, # default
'CACHE' = 'default', # default
'DEFINITIONS' = [
'wcd_user_checks.builtins.MANUAL_CHECK_DEFINITION', # Manual check reason
],
}
Usage
For all your interactions use client
module. It has everything that you would need.
Simple check interaction
# Importing client
from wcd_user_checks import client
# Getting check definition
from wcd_user_checks.builtins import MANUAL_CHECK_DEFINITION
# Setting a check reason to an initial value(invalid).
client.set_check(user_id, MANUAL_CHECK_DEFINITION.reason)
# Getting a user state
state = client.get_state(user_id)
# It will be invalid
# > state.is_passed == False
# Setting a valid state value
client.set_check(user_id, MANUAL_CHECK_DEFINITION.reason, state=builtins.ManualCheckState.VALID)
# Will force user check state to become valid
state = client.get_state(user_id)
# > state.is_passed == True
Creating a custom checker
File: apps/another/checker.py
from wcd_user_checks import client
from wcd_user_checks.registry import CheckDefinition
# This state might be django's `models.TextChoices` or any other Enum.
class AnotherState(str, Enum):
VALID = 'valid'
PROCESS = 'process'
INVALID = 'invalid'
# This is your checker definition object
ANOTHER = CheckDefinition(
# Reason to check. Must be unique for the project.
reason='ANOTHER',
# States enum, that your check workflow could be resolved.
states=AnotherState,
# Initial state. Better to be invalid,
initial_state=AnotherState.INVALID,
# Inspector - function that checks whether the check is passed or not.
inspector=lambda x: x.state == AnotherState.VALID
)
You may register this manually:
client.get_registry().register(ANOTHER)
Or by defining tha path to a definitions in settings.py
:
WCD_USER_CHECKS_DEFINITIONS = [
'wcd_user_checks.builtins.MANUAL_CHECK_DEFINITION', # Build in
'apps.another.checker.ANOTHER', # Yours
]
Administrative panel
By default there will be a separate model to change user checks. But it might happen this to be not enough. An you'll need to add an inliner to a user model.
There is functionality for that:
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as Base
from django.contrib.auth import get_user_model
# This inline here has a little bit more optimized user checks displaying.
from wcd_user_checks.admin import UserCheckInlineAdmin
UserModel = get_user_model()
admin.site.unregister(UserModel)
@admin.register(UserModel)
class UserAdmin(Base):
inlines = Base.inlines + [UserCheckInlineAdmin]
TODO
- [_] Middleware for user checks auto injection into
request
. - [_] Context manager for checks for templates injection.
- [_] DRF permissions.
- [_] DRF views maybe.
- [_] Some simple view decorator to check user's ability to perform action.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file wc-django-user-checks-0.1.8.tar.gz
.
File metadata
- Download URL: wc-django-user-checks-0.1.8.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dad9d02d0c06c1d61adb24339b48ec14e3f33299532e7b505b9f334c68e16453 |
|
MD5 | 4ca4c02da57f5d204fae9235dacf5af0 |
|
BLAKE2b-256 | 8ad0aca458ec8a1f639b863be97fa966ed902aebeee1b8aa6b8d06191d9407ea |