Skip to content

Commit

Permalink
Evaluating views permissions dynamically, so that the second operand …
Browse files Browse the repository at this point in the history
…is never evaluated in case the first one returns `False`.
  • Loading branch information
alaouimehdi1995 committed Jun 11, 2020
1 parent 8bb4109 commit 2baf8c5
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions django_rest/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def calculae(value1, value2):
"""

@staticmethod
def calculate(value1, value2):
# type:(bool, bool) -> bool
def calculate(first_function, second_function, *args, **kwargs):
# type:(Callable, Callable, *Any, **Any) -> bool
raise NotImplementedError(
"`calculate()` method should be defined in subclasses"
)
Expand Down Expand Up @@ -99,9 +99,9 @@ def build_permission_from(cls, permission_class_1, permission_class_2):

def has_permission(self, request, view):
# type:(HttpRequest, Callable) -> bool
first_result = permission_class_1().has_permission(request, view)
second_result = permission_class_2().has_permission(request, view)
return cls.calculate(first_result, second_result)
permission_func_1 = permission_class_1().has_permission
permission_func_2 = permission_class_2().has_permission
return cls.calculate(permission_func_1, permission_func_2, request, view)

result_class.has_permission = has_permission
return result_class
Expand Down Expand Up @@ -132,8 +132,8 @@ def calculae(value):
OPERATOR_NAME = None

@staticmethod
def calculate(value):
# type:(bool) -> bool
def calculate(function, *args, **kwargs):
# type:(Callable, *Any, **Any) -> bool
raise NotImplementedError(
"`calculate()` method should be defined in subclasses"
)
Expand All @@ -157,8 +157,8 @@ def build_permission_from(cls, permission_class):

def has_permission(self, request, view):
# type:(HttpRequest, Callable) -> bool
result = permission_class().has_permission(request, view)
return cls.calculate(result)
permission_func = permission_class().has_permission
return cls.calculate(permission_func, request, view)

result_class.has_permission = has_permission
return result_class
Expand All @@ -180,9 +180,9 @@ class AND(six.with_metaclass(MetaOperand, BinaryOperator)):
OPERATOR_NAME = "_AND_"

@staticmethod
def calculate(value1, value2):
# type:(bool, bool) -> bool
return value1 and value2
def calculate(first_function, second_function, *args, **kwargs):
# type:(Callable, Callable, *Any, **Any) -> bool
return first_function(*args, **kwargs) and second_function(*args, **kwargs)


class OR(six.with_metaclass(MetaOperand, BinaryOperator)):
Expand All @@ -198,9 +198,9 @@ class OR(six.with_metaclass(MetaOperand, BinaryOperator)):
OPERATOR_NAME = "_OR_"

@staticmethod
def calculate(value1, value2):
# type:(bool, bool) -> bool
return value1 or value2
def calculate(first_function, second_function, *args, **kwargs):
# type:(Callable, Callable, *Any, **Any) -> bool
return first_function(*args, **kwargs) or second_function(*args, **kwargs)


class XOR(six.with_metaclass(MetaOperand, BinaryOperator)):
Expand All @@ -216,9 +216,9 @@ class XOR(six.with_metaclass(MetaOperand, BinaryOperator)):
OPERATOR_NAME = "_XOR_"

@staticmethod
def calculate(value1, value2):
# type:(bool, bool) -> bool
return value1 ^ value2
def calculate(first_function, second_function, *args, **kwargs):
# type:(Callable, Callable, *Any, **Any) -> bool
return first_function(*args, **kwargs) ^ second_function(*args, **kwargs)


class NOT(six.with_metaclass(MetaOperand, UnaryOperator)):
Expand All @@ -234,9 +234,9 @@ class NOT(six.with_metaclass(MetaOperand, UnaryOperator)):
OPERATOR_NAME = "NOT_"

@staticmethod
def calculate(value):
# type:(bool) -> bool
return not value
def calculate(function, *args, **kwargs):
# type:(Callable, *Any, **Any) -> bool
return not function(*args, **kwargs)


class BasePermission(six.with_metaclass(MetaOperand, object)):
Expand Down

0 comments on commit 2baf8c5

Please sign in to comment.