forked from dromara/domain-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_user_api.py
178 lines (134 loc) · 4.14 KB
/
group_user_api.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
"""
group_user_api.py
"""
from __future__ import print_function, unicode_literals, absolute_import, division
from flask import request, g
from playhouse.shortcuts import model_to_dict
from domain_admin.enums.operation_enum import OperationEnum
from domain_admin.model.group_model import GroupModel
from domain_admin.model.group_user_model import GroupUserModel
from domain_admin.service import group_service, operation_service, common_service
@operation_service.operation_log_decorator(
model=GroupUserModel,
operation_type_id=OperationEnum.CREATE,
primary_key='id'
)
def add_group_user():
"""
添加
:return:
"""
current_user_id = g.user_id
group_id = request.json['group_id']
user_id = request.json['user_id']
has_edit_permission = request.json.get('has_edit_permission', False)
# 权限校验
group_service.check_group_permission(group_id, current_user_id)
row = GroupUserModel.create(
group_id=group_id,
user_id=user_id,
has_edit_permission=has_edit_permission
)
return {'id': row.id}
@operation_service.operation_log_decorator(
model=GroupUserModel,
operation_type_id=OperationEnum.UPDATE,
primary_key='group_user_id'
)
def update_group_user_by_id():
"""
更新数据
:return:
"""
current_user_id = g.user_id
group_user_id = request.json['group_user_id']
has_edit_permission = request.json.get('has_edit_permission', False)
group_user_row = GroupUserModel.get_by_id(group_user_id)
group_service.check_group_permission(group_user_row.group_id, current_user_id)
GroupUserModel.update(
has_edit_permission=has_edit_permission,
).where(
GroupUserModel.id == group_user_id
).execute()
@operation_service.operation_log_decorator(
model=GroupUserModel,
operation_type_id=OperationEnum.DELETE,
primary_key='group_user_id'
)
def delete_group_user_by_id():
"""
删除
:return:
"""
current_user_id = g.user_id
group_user_id = request.json['group_user_id']
group_user_row = GroupUserModel.get_by_id(group_user_id)
group_service.check_group_permission(group_user_row.group_id, current_user_id)
GroupUserModel.delete().where(
GroupUserModel.id == group_user_id,
).execute()
@operation_service.operation_log_decorator(
model=GroupUserModel,
operation_type_id=OperationEnum.BATCH_DELETE,
primary_key='group_user_ids'
)
def delete_group_user_by_ids():
"""
批量删除
:return:
"""
current_user_id = g.user_id
group_user_ids = request.json['group_user_ids']
group_user_rows = GroupUserModel.select().where(
GroupUserModel.id.in_(group_user_ids)
)
for group_user_row in group_user_rows:
group_service.check_group_permission(group_user_row.group_id, current_user_id)
GroupUserModel.delete().where(
GroupUserModel.id.in_(group_user_ids),
).execute()
def get_group_user_list():
"""
获取列表
:return:
"""
current_user_id = g.user_id
group_id = request.json.get('group_id')
# 分组列表数据
query = GroupUserModel.select().where(
GroupUserModel.group_id == group_id
)
total = query.count()
rows = query.order_by(
GroupUserModel.create_time.asc(),
GroupUserModel.id.asc()
)
lst = [model_to_dict(row) for row in rows]
# 添加leader
group_row = GroupModel.get_by_id(group_id)
leader = GroupUserModel()
leader.group_id = group_id
leader.user_id = group_row.user_id
leader.has_edit_permission = True
lst.insert(0, model_to_dict(leader))
common_service.load_user_name(lst)
for row in lst:
# add leader
if row['user_id'] == group_row.user_id:
row['is_leader'] = True
else:
row['is_leader'] = False
return {
'list': lst,
'total': len(lst),
}
def get_group_user_by_id():
"""
获取
:return:
"""
current_user_id = g.user_id
group_id = request.json['group_id']
group_service.check_group_permission(group_id, current_user_id)
return GroupUserModel.get_by_id(group_id)