Currently, the permissions module defines two OperandHolders, which I think is unnecessary.
|
class OperationHolderMixin: |
|
def __and__(self, other): |
|
return OperandHolder(AND, self, other) |
|
|
|
def __or__(self, other): |
|
return OperandHolder(OR, self, other) |
|
|
|
def __rand__(self, other): |
|
return OperandHolder(AND, other, self) |
|
|
|
def __ror__(self, other): |
|
return OperandHolder(OR, other, self) |
|
|
|
def __invert__(self): |
|
return SingleOperandHolder(NOT, self) |
|
|
|
|
|
class SingleOperandHolder(OperationHolderMixin): |
|
def __init__(self, operator_class, op1_class): |
|
self.operator_class = operator_class |
|
self.op1_class = op1_class |
|
|
|
def __call__(self, *args, **kwargs): |
|
op1 = self.op1_class(*args, **kwargs) |
|
return self.operator_class(op1) |
|
|
|
|
|
class OperandHolder(OperationHolderMixin): |
|
def __init__(self, operator_class, op1_class, op2_class): |
|
self.operator_class = operator_class |
|
self.op1_class = op1_class |
|
self.op2_class = op2_class |
|
|
|
def __call__(self, *args, **kwargs): |
|
op1 = self.op1_class(*args, **kwargs) |
|
op2 = self.op2_class(*args, **kwargs) |
|
return self.operator_class(op1, op2) |
The SingleOperandHolder is only used for the negation operation (NOT). Instead, this could be implemented with a single class that works for all operations. I believe this implementation improves redability.
class OperationHolderMixin:
...
...
def __invert__(self):
return OperandHolder(NOT, self)
# A single class for an arbitrary number of operands.
class OperandHolder(OperationHolderMixin):
def __init__(self, operator_class, *operand_classes):
self.operator_class = operator_class
self.operand_classes = operand_classes
def __call__(self, *args, **kwargs):
operands = map(lambda op: op(*args, **kwargs), self.operand_classes)
return self.operator_class(*operands)
Originally posted by @ariel-m-s in #7914
Currently, the permissions module defines two
OperandHolders, which I think is unnecessary.django-rest-framework/rest_framework/permissions.py
Lines 11 to 47 in fd017d0
The
SingleOperandHolderis only used for the negation operation (NOT). Instead, this could be implemented with a single class that works for all operations. I believe this implementation improves redability.Originally posted by @ariel-m-s in #7914