-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathconditions.py
99 lines (73 loc) · 3.06 KB
/
conditions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import typing
from django.conf import settings
from . import conf
from .exceptions import ConditionFailed
from .helpers import get_latest_draft_version, version_is_unlocked_for_user
class Conditions(list):
def __add__(self, other: list) -> "Conditions":
return Conditions(super().__add__(other))
def __get__(self, instance: object, cls) -> typing.Union["Conditions", "BoundConditions"]:
if instance:
return BoundConditions(self, instance)
return self
def __call__(self, instance: object, user: settings.AUTH_USER_MODEL) -> None:
for func in self:
func(instance, user)
def as_bool(self, instance: object, user: settings.AUTH_USER_MODEL) -> bool:
try:
self(instance, user)
except ConditionFailed:
return False
return True
class BoundConditions:
def __init__(self, conditions: Conditions, instance: object) -> None:
self.conditions = conditions
self.instance = instance
def __call__(self, user) -> None:
self.conditions(self.instance, user)
def as_bool(self, user) -> bool:
return self.conditions.as_bool(self.instance, user)
def in_state(states: list, message: str) -> callable:
def inner(version, user):
if version.state not in states:
raise ConditionFailed(message)
return inner
def is_not_locked(message: str) -> callable:
"""Condition that the version is not locked. Is only effective if ``settings.DJANGOCMS_VERSIONING_LOCK_VERSIONS``
is set to ``True``"""
def inner(version, user):
if conf.LOCK_VERSIONS:
if not version_is_unlocked_for_user(version, user):
raise ConditionFailed(message.format(user=version.locked_by))
return inner
def draft_is_not_locked(message: str) -> callable:
def inner(version, user):
if conf.LOCK_VERSIONS:
draft_version = get_latest_draft_version(version)
if draft_version and not version_is_unlocked_for_user(draft_version, user):
raise ConditionFailed(message.format(user=draft_version.locked_by))
return inner
def draft_is_locked(message: str) -> callable:
def inner(version, user):
if conf.LOCK_VERSIONS:
draft_version = get_latest_draft_version(version)
if not draft_version or version_is_unlocked_for_user(draft_version, user):
raise ConditionFailed(message)
else:
raise ConditionFailed(message)
return inner
def user_can_unlock(message: str) -> callable:
def inner(version, user):
if not user.has_perm("djangocms_versioning.delete_versionlock"):
raise ConditionFailed(message)
return inner
def user_can_publish(message: str) -> callable:
def inner(version, user):
if not version.has_publish_permission(user):
raise ConditionFailed(message)
return inner
def user_can_change(message: str) -> callable:
def inner(version, user):
if not version.has_change_permission(user):
raise ConditionFailed(message)
return inner