Registry / web-framework / rest-condition

rest-condition

JSON →
library1.0.3pypypi✓ verified 86d ago

Rest Condition provides a simple, declarative way to compose complex permission checks for Django REST Framework views using logical operators (&, |, ~). Current version is 1.0.3, with infrequent updates.

pip install rest-condition
INSTALL
IMPORT
SIG · REST-CONDITION
R
rest-condition
web-frameworkpythonv1.0.3
Install
4.6s avg
Import
698ms
Disk
71MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.732s · 72MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.6s · import 0.664s · 72MB
71MB installed
● package 71MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rest_condition
from rest_condition import C, And, Or, Not
Correct import for the main composable permission classes.
rest_condition
from rest_condition import And, Or, Not
from rest_condition import And, Or, Not, C
C is a shortcut for creating a condition from a custom permission class. It's not required for basic usage but often used.

Define a view with combined permission checks using logical operators. Placeholder permissions IsAdmin and IsOwner should be defined elsewhere.

from rest_framework.permissions import IsAuthenticated from rest_condition import And, Or, Not from rest_framework.views import APIView class MyView(APIView): permission_classes = [And(IsAuthenticated, ~Or(IsAdmin, IsOwner))] def get(self, request): return Response({"message": "Hello"})
Debug
Known issues
gotcharest_condition classes are not callable directly; they must be instantiated. For example, use `And(IsAuthenticated)` not `And`.
fix
Instantiate the condition: `permission_classes = [And(IsAuthenticated, IsAdmin)]`
affects: all
gotchaOrder of conditions matters: `And(cond1, cond2)` short-circuits on first False, `Or` short-circuits on first True. Unexpected results can occur if side effects exist.
fix
Ensure no side effects in permission checks, or be aware of evaluation order.
affects: all
gotchaPermission classes used inside rest_condition must have `has_permission` or `has_object_permission` methods returning bool. Mixing with DRF's `SAFE_METHODS` pattern may cause confusion.
fix
Define custom permissions with explicit boolean returns.
affects: all
Errors
Common errors & fixes
'list' object has no attribute 'has_permission'
Using a list of permission classes instead of a single condition instance.
fix
Use rest_condition classes: `permission_classes = [And(IsAuthenticated, IsAdmin)]` instead of `[IsAuthenticated, IsAdmin]`
TypeError: __init__() takes 1 positional argument but 2 were given
Calling a rest_condition class without parentheses, e.g., `And(IsAuthenticated, IsAdmin)` vs using `And` alone.
fix
Ensure you instantiate the condition: `And(IsAuthenticated, IsAdmin)`
AttributeError: 'And' object has no attribute 'has_permission'
Using an uninstantiated class reference in permission_classes, e.g., `permission_classes = [And]`.
fix
Instantiate: `permission_classes = [And(IsAuthenticated)]`
Upgrade
Version history
1.0.3latest on PyPI · released Nov 1, 2016
Audit
Dependencies
djangorequiredCore dependency for Django projects
djangorestframeworkrequiredPermission classes are used with DRF views
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
1
Resources
rest-condition — pip install rest-condition · libregistry