инизиализация элементов AAA объекта/пользователя

 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
class InitObjectActions:
"""инициализация базовых значений и ограничителей объекта"""
def __init__(self,user, object):
self.basic_object = self._get_basic_object(object.__class__.__name__)
self.user = user
def _get_basic_action(self, basic_action_name):
"""создание/извлечение базовых действий"""
actions_set = [
BasicAction.objects.get_or_create(
name = action[0],
description = action[1]
) for action in BASIC_ACTIONS if action[0] == basic_action_name
]
actions = [ item[0] for item in actions_set ]
return actions[0]
def _get_basic_object(self, basic_object_name):
"""извлечение/создание базового объекта"""
basic_object, created = BasicObject.objects.get_or_create(
name = basic_object_name
)
return basic_object
def _get_object_action(self, basic_action_name):
"""создание/извлечение действий для объектов"""
object_action, created = ObjectAction.objects.get_or_create(
object = self.basic_object,
action = self._get_basic_action(basic_action_name)
)
return object_action
def set_delimiters(self, basic_object_names, basic_action_name):
"""создание ограничителей для действия над объектом"""
objects = [ self._get_basic_object(name) for name in basic_object_names]
action = self._get_object_action(basic_action_name)
delimiters_set = [
Delimiter.objects.get_or_create(object_action = action, object = object) \
for object in objects
]
self.set_role_action(action) #установка действия для роли пользователя
delimiters = [item[0] for item in delimiters_set ]
return delimiters
def set_role_action(self, object_action, is_delimited=True):
"""создания ограниченного действия для роли"""
user_role = self.user.get_profile().role
role_action, created = RoleAction.objects.get_or_create(
role = user_role,
object_action = object_action,
is_delimited = is_delimited
)
return role_action
def set_delimiter_value(self, *args):
"""создание пользовательских значений ограничителей"""
return UserDelimiter.objects.create(
user = self.user,
delimiter = args[0],
value = args[1]
)