|
@@ -0,0 +1,64 @@
|
|
|
+# coding=utf-8
|
|
|
+from rest_framework.decorators import action
|
|
|
+from rest_framework import permissions
|
|
|
+from django.utils import timezone
|
|
|
+
|
|
|
+from utils.custom_modelviewset import CustomModelViewSet
|
|
|
+from utils.permission import IsAdministratorUser,IsTenantUser
|
|
|
+from utils.exceptions import CustomError
|
|
|
+from utils import response_ok, response_error
|
|
|
+from .models import Customer
|
|
|
+from .serializers import CustomerSerializer
|
|
|
+from .filters import CustomerFilter
|
|
|
+
|
|
|
+
|
|
|
+class CustomerViewSet(CustomModelViewSet):
|
|
|
+ permission_classes = []
|
|
|
+ queryset = Customer.objects.filter()
|
|
|
+ serializer_class = CustomerSerializer
|
|
|
+
|
|
|
+ def filter_queryset(self, queryset):
|
|
|
+ f = CustomerFilter(self.request.GET, queryset=queryset)
|
|
|
+ return f.qs
|
|
|
+
|
|
|
+ def perform_create(self, serializer):
|
|
|
+ super(CustomerViewSet, self).perform_create(serializer)
|
|
|
+ instance = serializer.instance
|
|
|
+ validated_data = serializer.validated_data
|
|
|
+
|
|
|
+ def perform_update(self, serializer):
|
|
|
+ super(CustomerViewSet, self).perform_update(serializer)
|
|
|
+ instance = serializer.instance
|
|
|
+ validated_data = serializer.validated_data
|
|
|
+
|
|
|
+ def destroy(self, request, *args, **kwargs):
|
|
|
+ instance = self.get_object()
|
|
|
+ # if instance.tenant != request.user.employee.tenant:
|
|
|
+ # raise CustomError(u'禁止跨企业操作!')
|
|
|
+ super(CustomerViewSet, self).destroy(self, request, *args, **kwargs)
|
|
|
+ return response_ok()
|
|
|
+
|
|
|
+ @action(methods=['post'], detail=True)
|
|
|
+ def check(self, request, pk):
|
|
|
+ # 审核
|
|
|
+ report_status = request.POST.get('report_status')
|
|
|
+ try:
|
|
|
+ instance = Customer.objects.filter(id=pk).first()
|
|
|
+ if not instance:
|
|
|
+ raise CustomError('当前客户报备有误!')
|
|
|
+ if instance.report_status == Customer.CHECKED:
|
|
|
+ instance.report_status = Customer.REPEAT_REPORT
|
|
|
+ instance.save()
|
|
|
+ return response_ok('当前客户已审核!')
|
|
|
+ if instance.report_status == Customer.REPEAT_REPORT:
|
|
|
+ raise CustomError('当前用户已重复报备!')
|
|
|
+
|
|
|
+ instance.report_status = report_status
|
|
|
+ instance.check_time = timezone.now()
|
|
|
+ instance.save()
|
|
|
+
|
|
|
+ except CustomError as e:
|
|
|
+ return response_error(e.get_error_msg())
|
|
|
+ except Exception as e:
|
|
|
+ return response_error(str(e))
|
|
|
+ return response_ok('审核完成!')
|