factory/apps/mtm/views.py

93 lines
2.6 KiB
Python

from django.shortcuts import render
from rest_framework.mixins import ListModelMixin
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
from apps.mtm.models import Material, Mgroup, Shift, Team, Goal
from apps.mtm.serializers import MaterialSerializer, MgroupSerializer, ShiftSerializer, TeamSerializer, GoalSerializer
from apps.mtm.filters import GoalFilter
from rest_framework.decorators import action
from rest_framework.response import Response
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from django.db.models import Q
from apps.mtm.services import get_mgroup_goals
from apps.mtm.serializers import MgroupGoalYearSerializer
# Create your views here.
class MaterialViewSet(CustomModelViewSet):
"""
list:产品
产品
"""
queryset = Material.objects.all()
serializer_class = MaterialSerializer
filterset_fields = ['code']
search_fields = ['name', 'code']
ordering = ['id']
class ShiftViewSet(ListModelMixin, CustomGenericViewSet):
"""
list:班次
班次
"""
perms_map = {'get': '*'}
queryset = Shift.objects.all()
serializer_class = ShiftSerializer
search_fields = ['name']
ordering = ['id']
class MgroupViewSet(CustomModelViewSet):
"""
list:测点集
测点集
"""
queryset = Mgroup.objects.all()
serializer_class = MgroupSerializer
select_related_fields = ['create_by', 'belong_dept']
filterset_fields = ['belong_dept']
search_fields = ['number']
class TeamViewSet(CustomModelViewSet):
"""
list:班组
班组
"""
queryset = Team.objects.all()
serializer_class = TeamSerializer
select_related_fields = ['belong_dept', 'leader']
filterset_fields = ['belong_dept']
search_fields = ['name']
class GoalViewSet(CustomModelViewSet):
"""
list: 目标
目标
"""
queryset = Goal.objects.all()
serializer_class = GoalSerializer
select_related_fields = ['mgroup', 'goal_cate']
filterset_class = GoalFilter
search_fields = ['name']
ordering = ['mgroup__sort', 'goal_cate__sort', 'goal_cate__create_time']
@action(methods=['post'], detail=False, perms_map={'post': '*'}, serializer_class=MgroupGoalYearSerializer)
def mgroup_goals_year(self, request, pk=None):
"""
获取工段全年目标
获取工段全年目标
"""
sr = MgroupGoalYearSerializer(data=request.data)
sr.is_valid(raise_exception=True)
vdata = sr.validated_data
res = get_mgroup_goals(vdata['mgroup'], vdata['year'], True)
return Response(res)