from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from apps.inm.models import FIFO, Inventory, MaterialBatch
from apps.mtm.models import Material
from apps.pm.models import ProductionPlan
from apps.sam.models import Order
from apps.wf.models import Ticket
from apps.wpm.models import Operation
# Create your views here.

class CleanDataView(APIView):
    permission_classes = [IsAdminUser]

    def post(self, request, format=None):
        """
        清空数据库
        """
        Order.objects.all().delete(soft=False)
        ProductionPlan.objects.all().delete(soft=False)
        FIFO.objects.all().delete(soft=False)
        Material.objects.filter(type__in=[Material.MA_TYPE_GOOD, Material.MA_TYPE_HALFGOOD]).update(count=0)
        MaterialBatch.objects.filter(material__type__in=[Material.MA_TYPE_GOOD, Material.MA_TYPE_HALFGOOD]).delete()
        Inventory.objects.filter(material__type__in=[Material.MA_TYPE_GOOD, Material.MA_TYPE_HALFGOOD]).delete()
        Ticket.objects.all().delete(soft=False)
        Operation.objects.all().delete()
        return Response()