django 工单模板 添加审批流 后端接口 modelviewset

django 后端接口 modelviewset

  1. apps/flowconf/models.py

    1. from django.db import models
      
      
      # 新建工单模板
      
      # Create your models here.
      
      
      # 工单模板
      
      class FlowConf(models.Model):
          name = models.CharField('工作流名称', max_length=32)
          customfield = models.TextField('自定义字段')
          description = models.TextField('描述', )
      
          class Meta:
              db_table = '工单模板'
              verbose_name = "工单模板"
              verbose_name_plural = verbose_name
      
      
      # 工单分类
      # class FlowType(models.Model):
      #     name = models.CharField('工作流名称', max_length=50)
      #     description = models.TextField('描述', )
      #     flowconf = models.ForeignKey(FlowConf, on_delete=models.CASCADE)
      
      
      # 配置审批流
      
      class ApproveActionConf(models.Model):
          flowconf = models.ForeignKey(FlowConf, on_delete=models.CASCADE)
          sequence = models.IntegerField('审批序号')
          approve_type_id = models.CharField('审批类型', max_length=32, choices=(('1', '角色组审批'), ('2', '指定人员审批')))
          approve_id = models.CharField('审批id', max_length=32)
      
          class Meta:
              db_table = '配置审批流'
              verbose_name = "配置审批流"
              verbose_name_plural = verbose_name
      
      # 自动化工单配置
      
      # class AutoActionConf(models.Model):
      #     flowconf = models.ManyToManyField(FlowConf)
      #     scriptpath = models.CharField('执行脚本路径', max_length=50)
      #     url = models.CharField('自动化调用路径', max_length=200)
      #     method = models.CharField('调用自动化接口', max_length=32)
      #     timeout = models.CharField('自动化执行超时时间', max_length=32)
      
      
  2. serializers.py

    1. """
      author:翔翔
      date:
      use:
      """
      from rest_framework import serializers
      from flowconf.models import flowconf, Role
      from .models import *
      
      
      # 工单模板
      class FlowConfModelSerializer(serializers.ModelSerializer):
          # approve_type_id = serializers.SerializerMethodField(read_only=True)
      
          class Meta:
              model = FlowConf
              fields = "__all__"
      
          # def get_approve_type_id(self,row):
          #     print(row)
          #     approve_type_id = row.approveactionconf_set
          #     print(approve_type_id)
          #
          #     return {}
      
      
      # 展示审批流
      class ApproveActionConfModelSerializer(serializers.ModelSerializer):
          # 自定义字段  展示对应类型下的审批人员
          specific_flowconf = serializers.SerializerMethodField(read_only=True)
          # 序列化外键
          flowconf = FlowConfModelSerializer()
      
          class Meta:
              model = ApproveActionConf
              fields = "__all__"
      
          def get_specific_flowconf(self, row):
              # 获取审批类型id
              approve_type_id = row.approve_type_id
              # 审批人id
              approve_id = row.approve_id
              if approve_type_id == '1':
                  """用户审批"""
                  approve = flowconf.objects.get(id=approve_id)
                  return {
                      "approve": approve.flowconfname,
                      "approve_type": "指定人员",
                  }
              elif approve_type_id == '2':
                  approve = Role.objects.get(id=approve_type_id)
                  return {
                      "approve": approve.zh_name,
                      "approve_type": "角色组",
                  }
              else:
                  """待添加"""
                  pass
      
      
      # 展示 反向查询  表民_set approveactionconf_set
      class FlowConfDeepSerializers(FlowConfModelSerializer):
          approveactionconf_set = ApproveActionConfModelSerializer(many=True)
      
      
      # 添加审批流使用
      class ApproveActionConfModelSerializer2(serializers.ModelSerializer):
          class Meta:
              model = ApproveActionConf
              fields = "__all__"
      
      
  3. views.py

    1. from django.shortcuts import render
      
      # Create your views here.
      from rest_framework.viewsets import ModelViewSet
      from .serializers import *
      from .models import *
      
      
      # 工单模板  操作
      class FlowConfModelViewSet(ModelViewSet):
          queryset = FlowConf.objects.all()
          serializer_class = FlowConfModelSerializer
      
          # 根据不同的请求获取不同的序列化器
          def get_serializer_class(self):
              if self.action == 'list':
                  # print(self.action, "1")
                  return FlowConfModelSerializer
              elif self.action == "create":
                  return FlowConfModelSerializer
              elif self.action == "update":
                  return FlowConfModelSerializer
              else:
                  print(self.action, "2")
                  # 获取详细信息
                  return FlowConfDeepSerializers
      
      
      # 配置审批流
      class ApproveActionConfModelViewSet(ModelViewSet):
          queryset = ApproveActionConf.objects.all()
          serializer_class = ApproveActionConfModelSerializer
      
          def get_serializer_class(self):
              # 添加时 使用这个序列化器
              if self.action == "create":
                  return ApproveActionConfModelSerializer2
      
      
  4. urls.py

    1. from django.urls import path, include
      from rest_framework.routers import DefaultRouter
      from .views import *
      
      router = DefaultRouter()
      router.register(r'flowconf', FlowConfModelViewSet)
      router.register(r'approveactionconf', ApproveActionConfModelViewSet)
      
      urlpatterns = [
      
      ]
      
      urlpatterns += router.urls
      
      
  5. 主路由

    1. """opwf URL Configuration
      
      The `urlpatterns` list routes URLs to views. For more information please see:
          https://docs.djangoproject.com/en/2.2/topics/http/urls/
      Examples:
      Function views
          1. Add an import:  from my_app import views
          2. Add a URL to urlpatterns:  path('', views.home, name='home')
      Class-based views
          1. Add an import:  from other_app.views import Home
          2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
      Including another URLconf
          1. Import the include() function: from django.urls import include, path
          2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
      """
      from django.contrib import admin
      from django.urls import path, include
      
      urlpatterns = [
          path('admin/', admin.site.urls),
          path('workflow/', include('workflow.urls')),
      ]
      
      
posted @ 2020-11-30 07:24  心-瘾  阅读(488)  评论(0编辑  收藏  举报