# coding=utf-8 from rest_framework.views import APIView from django.db.models import Q from django.utils import timezone from rest_framework.decorators import action from utils.permission import isLogin, permission_required from utils.custom_modelviewset import CustomModelViewSet from utils import response_ok, response_error from utils.exceptions import CustomError from apps.log.models import BizLog from apps.option.models import Option from apps.option.serializers import OptionComboboxSerializer from .models import ReportCustomer from .serializers import ReportCustomerSerializer from .filters import ReportCustomerFilter class ReportCustomerViewSet(CustomModelViewSet): permission_classes = [isLogin] queryset = ReportCustomer.objects.filter() serializer_class = ReportCustomerSerializer @permission_required('customer.view_report_customer') def filter_queryset(self, queryset): queryset = queryset.filter() f = ReportCustomerFilter(self.request.GET, queryset=queryset) return f.qs @permission_required('customer.add_report_customer') def perform_create(self, serializer): super(ReportCustomerViewSet, self).perform_create(serializer) instance = serializer.instance validated_data = serializer.validated_data BizLog.objects.addnew(self.request.user, BizLog.INSERT, u'添加客户报备[%s],id=%d' % (instance.name, instance.id), validated_data) @permission_required('customer.add_report_customer') def perform_update(self, serializer): super(ReportCustomerViewSet, self).perform_update(serializer) instance = serializer.instance validated_data = serializer.validated_data BizLog.objects.addnew(self.request.user, BizLog.UPDATE, u'修改客户报备[%s],id=%d' % (instance.name, instance.id), validated_data) @permission_required('customer.delete_report_customer') def perform_destroy(self, instance): BizLog.objects.addnew(self.request.user, BizLog.DELETE, u'删除客户报备[%s],id=%d' % (instance.name, instance.id)) super(ReportCustomerViewSet, self).perform_destroy(instance) @permission_required('customer.check_report_customer') @action(methods=['post'], detail=False) def check(self, request, pk): # 审核 report_status = request.POST.get('report_status') try: instance = ReportCustomer.objects.filter(id=pk).first() if not instance: raise CustomError('当前客户报备信息有误!') if report_status == ReportCustomer.REPEAT_REPORT: raise CustomError('当前客户报备状态为重复报备!') if instance.report_status == ReportCustomer.CHECKED and report_status == ReportCustomer.CHECKED: # 撞单 instance.check_user = self.request.user instance.report_status = ReportCustomer.REPEAT_REPORT instance.check_time = timezone.now() instance.save() if instance.report_status == ReportCustomer.NOT_CHECKED and report_status == ReportCustomer.CHECKED: instance.check_user = self.request.user instance.report_status = ReportCustomer.CHECKED instance.check_time = timezone.now() instance.save() BizLog.objects.addnew(None, request.user, BizLog.INSERT, u'审核客户报备[%s]状态为[%s],id=%d' % (instance.name, report_status, instance.id)) except CustomError as e: return response_error(e.get_error_msg()) except Exception as e: return response_error(str(e)) return response_ok('审核完成!') class ReportCustomerDictView(APIView): permission_classes = [isLogin] def get(self, request): scouce = Option.objects.filter(type=Option.CUSTOMER_SOURCE , enable=True) project = Option.objects.filter(type=Option.CATEGORY, enable=True) serializer_scouce = OptionComboboxSerializer(scouce, many=True) serializer_project = OptionComboboxSerializer(project, many=True) return response_ok({ 'source': serializer_scouce.data, 'project': serializer_project.data, })