|
|
# _*_ coding: utf-8 _*_
|
|
|
# @Time : 2020/10/7 10:40
|
|
|
# @Author vanwhebin
|
|
|
import re
|
|
|
from rest_framework.generics import CreateAPIView, RetrieveAPIView, UpdateAPIView, ListAPIView
|
|
|
from rest_framework.permissions import IsAuthenticated, IsAdminUser
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
from django.urls import reverse
|
|
|
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 usercenter.models import User
|
|
|
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 = (
|
|
|
(2, '邓望明'),
|
|
|
(3, '杜波')
|
|
|
)
|
|
|
|
|
|
|
|
|
class CreateProject(CreateAPIView):
|
...
|
...
|
@@ -23,13 +33,15 @@ class CreateProject(CreateAPIView): |
|
|
wx_client = WxPushHelper()
|
|
|
serializer = ProjectSerializer(data=request.data)
|
|
|
if not serializer.is_valid():
|
|
|
raise ValidationError
|
|
|
raise ValidationError(serializer.errors)
|
|
|
else:
|
|
|
serializer.save(creator=request.user, auditor=Auditor.objects.order_by('-order').all())
|
|
|
# 企业微信推送
|
|
|
obj_dict = serializer.data
|
|
|
wx_client.push_card(request.user.wx_token,
|
|
|
reverse("project:retrieve_project", kwargs={"pk": obj_dict['id']}), u"流程创建成功")
|
|
|
url = re.sub("PK", obj_dict['id'], FRONT_URL['flow_detail'])
|
|
|
first_auditor = User.objects.filter(pk=AUDITORS[0][0]).first()
|
|
|
wx_client.push_card(first_auditor.wx_token, url, f"{request.user.username}提交了一个产品立项申请")
|
|
|
wx_client.push_card(request.user.wx_token, url, u"流程创建成功")
|
|
|
return response(obj_dict)
|
|
|
|
|
|
|
...
|
...
|
@@ -38,7 +50,9 @@ class ProjectDetail(RetrieveAPIView): |
|
|
serializer_class = ProjectSerializer
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
# def get_object(self):
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
obj = self.get_object()
|
|
|
return response(ProjectSerializer(obj).data)
|
|
|
|
|
|
|
|
|
class AuditProject(UpdateAPIView):
|
...
|
...
|
@@ -81,6 +95,7 @@ class AuditProject(UpdateAPIView): |
|
|
|
|
|
wx_client = WxPushHelper()
|
|
|
full_audit_done = self._check_audit(obj)
|
|
|
url = re.sub("PK", obj.id, FRONT_URL['flow_detail'])
|
|
|
desc = "产品立项流程所有审批已完成" if full_audit_done else f"{request.user.username}已审批完成"
|
|
|
if full_audit_done:
|
|
|
obj.is_done = True
|
...
|
...
|
@@ -89,20 +104,45 @@ class AuditProject(UpdateAPIView): |
|
|
if not accept_param:
|
|
|
obj.is_done = True
|
|
|
obj.is_pass = False
|
|
|
else:
|
|
|
second_auditor = User.objects.filter(pk=AUDITORS[1][0]).first()
|
|
|
wx_client.push_card(second_auditor.wx_token, url, f"{request.user.username}提交了一个产品立项申请")
|
|
|
obj.save()
|
|
|
wx_client.push_card(obj.creator.wx_token, reverse("project:retrieve_project", kwargs={"pk": obj.id}), desc)
|
|
|
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, IsAdminUser)
|
|
|
|
|
|
def get_queryset(self, **kwargs):
|
|
|
return Project.objects.filter(auditor__user_id=kwargs['user'].id)
|
|
|
def get_queryset(self):
|
|
|
# auditor = Auditor.objects.get(user=self.request.user)
|
|
|
# sql = Project.objects.filter(
|
|
|
# auditor__user_id=self.request.user.id,
|
|
|
# auditor__result_auditor__exact=auditor) \
|
|
|
# .order_by('-is_done')
|
|
|
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('is_accept').first()
|
|
|
item.result = result['is_accept'] if result else False
|
|
|
# print(data.query)
|
|
|
return data
|
|
|
#
|
|
|
# def get(self, request, *args, **kwargs):
|
|
|
# qs = self.get_queryset(user=request.user, **kwargs)
|
|
|
# return response(ProjectSerializer(qs, many=True).data)
|
|
|
|
|
|
|
|
|
class CheckAuth(APIView):
|
|
|
""" 检查是否有权限进行审批 """
|
|
|
allowed_methods = ('GET',)
|
|
|
permission_classes = (IsAuthenticated, IsAdminUser)
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
qs = self.get_queryset(user=request.user)
|
|
|
return response(ProjectSerializer(qs, many=True).data) |
|
|
@staticmethod
|
|
|
def get(request, *args, **kwargs):
|
|
|
project_auditor = Project.objects.filter(pk=kwargs['pk'], auditor__user_id=request.user.id)
|
|
|
return response(True) if project_auditor else response(False) |
...
|
...
|
|