views_cbv.py
6.0 KB
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
# _*_ coding: utf-8 _*_
# @Time : 2020/10/7 10:40
# @Author vanwhebin
import re
from urllib import parse
from rest_framework.generics import CreateAPIView, RetrieveAPIView, UpdateAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.exceptions import ValidationError
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods
from .serializers import ProjectSerializer
from .models import Auditor, Project, Result
from utils.helpers import WxPushHelper
from utils.pagination import MyPageNumberPagination
from utils.util import response
from wxProject.settings import FRONT_URL
AUDITORS = (
(1, '邓望明'),
(2, '杜波')
)
class CreateProject(CreateAPIView):
serializer_class = ProjectSerializer
permission_classes = (IsAuthenticated,)
def post(self, request, *args, **kwargs):
super(CreateProject, self).__init__()
wx_client = WxPushHelper()
serializer = ProjectSerializer(data=request.data)
if not serializer.is_valid():
raise ValidationError(serializer.errors)
else:
serializer.save(creator=request.user, auditor=Auditor.objects.order_by('-order').all())
# 企业微信推送
obj_dict = serializer.data
for i in AUDITORS:
Result.objects.create(
auditor_id=i[0],
project_id=obj_dict['id']
)
url = re.sub("PK", str(obj_dict['id']), FRONT_URL['flow_detail'])
url = re.sub("REDIRECT_URL", parse.quote(url, safe=''), FRONT_URL['wx_authorize'])
first_auditor = Auditor.objects.filter(pk=AUDITORS[0][0]).first()
wx_client.push_card(first_auditor.user.wx_token, url, f"{request.user.username}提交了一个产品立项申请")
wx_client.push_card(request.user.wx_token, url, u"流程创建成功")
return response(obj_dict)
class ProjectDetail(RetrieveAPIView):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
permission_classes = (IsAuthenticated,)
def get(self, request, *args, **kwargs):
obj = self.get_object()
obj.creator_name = obj.creator.username
obj.result = []
auditors = Auditor.objects.all()
# result = Result.objects.filter(project=obj)
for i in auditors:
res = i.result_auditor.filter(project=obj).values('is_accept', 'memo').first()
obj.result.append({
"auditor": i.user.username,
"is_accept": res['is_accept'] if res else None,
"memo": res['memo'] if res else '',
})
return response(ProjectSerializer(obj).data)
class AuditProject(UpdateAPIView):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
permission_classes = (IsAuthenticated, IsAdminUser)
@staticmethod
def _check_audit(project_obj):
# 查看是否已经全部审核完毕 进行更新project表
# 所有的审核人员, 是否有result记录
aud_result_len = Result.objects.filter(project=project_obj, is_accept__isnull=False).count()
auditor_len = Project.objects.filter(pk=project_obj.id).values('auditor__user_id').count()
return aud_result_len == auditor_len
def partial_update(self, request, *args, **kwargs):
# 对项目进行更新
obj = self.get_object()
accept_choices = (
('accept', '通过'),
('reject', '否决')
)
accept_param = accept_choices[0][0] if request.data.get('is_accept') else accept_choices[1][0]
target = Project.objects.filter(auditor__user_id=request.user.id, pk=obj.id)
if not target:
raise PermissionError
else:
auditor = Auditor.objects.get(user=request.user)
result = Result.objects.filter(auditor=auditor, project=obj).first()
result.is_accept = accept_param
result.memo = request.data.get('memo', '')
result.save()
wx_client = WxPushHelper()
full_audit_done = self._check_audit(obj)
url = re.sub("PK", str(obj.id), FRONT_URL['flow_detail'])
url = re.sub("REDIRECT_URL", parse.quote(url, safe=''), FRONT_URL['wx_authorize'])
desc = "产品立项流程所有审批已完成" if full_audit_done else f"{request.user.username}已审批完成"
if full_audit_done:
obj.is_done = True
obj.is_pass = bool(request.data.get('is_accept'))
else:
if accept_param == accept_choices[1][0]:
obj.is_done = True
obj.is_pass = False
else:
second_auditor = Auditor.objects.filter(pk=AUDITORS[1][0]).first()
wx_client.push_card(second_auditor.user.wx_token, url, f"{request.user.username}已审核了一个产品立项申请")
obj.save()
wx_client.push_card(obj.creator.wx_token, url, desc)
return response(ProjectSerializer(obj).data)
class AuditProjectsList(ListAPIView):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
pagination_class = MyPageNumberPagination
permission_classes = (IsAuthenticated,)
def get_queryset(self):
data = Project.objects.filter(auditor__user_id=self.request.user.id).order_by('is_done')
for item in data:
result = item.result_project.filter(
auditor__user_id=self.request.user.id).values_list('is_accept', flat=True).first()
item.creator_name = item.creator.username
item.result = True if result else False
return data
class CheckAuth(APIView):
""" 检查是否有权限进行审批 """
allowed_methods = ('GET',)
permission_classes = (IsAuthenticated, )
@staticmethod
def get(request, *args, **kwargs):
project_auditor = Project.objects.filter(
pk=kwargs['pk'],
auditor__user_id=request.user.id)
if not project_auditor:
return response(False)
else:
order = int(Auditor.objects.filter(user_id=request.user.id).values_list('order', flat=True).first())
results = Result.objects.filter(project_id=kwargs['pk'])
if order > 0:
# 当审核人员排队时 需要判断是否已经流转到自己 前一个人员是否已经有处理
# 还要判断自己是否已经审核过了
index = order - 1
if bool(results[index].is_accept) and not bool(results[order].is_accept):
return response(True)
else:
return response(False)
else:
return response(False) if bool(results[0].is_accept) else response(True)