# -*- coding: utf-8 -*- # @Author : Sherlock # @Time : 2025/2/21 17:53 # @Description : 数据连接 from rest_framework.permissions import IsAuthenticated from rest_framework_jwt.authentication import JSONWebTokenAuthentication from django_filters.rest_framework import DjangoFilterBackend from rest_framework.decorators import action from rest_framework.filters import SearchFilter, OrderingFilter class ConnectorViewSet(CustomViewBase): ''' 字典详情树 ''' perms_map = ( {'*': 'admin'}, {'*': 'connector_all'}, {'get': 'connector_list'}, {'post': 'connector_create'}, {'put': 'connector_edit'}, {'delete': 'connector_delete'} ) queryset = Connection.objects.all() serializer_class = ConnectionSerializer filter_backends = (SearchFilter, OrderingFilter) ordering_fields = ('id',) authentication_classes = (JSONWebTokenAuthentication,) permission_classes = (IsAuthenticated,) def list(self, request, *args, **kwargs): ''' 重写list方法 ''' companyMid = request.GET.get("companyMid", None) is_enabled = request.GET.get("is_enabled") label = request.GET.get("label") order = request.GET.get("order") sort = request.GET.get("ordering") order_by = 'id' try: queryRows = Connection.objects.filter(companyMid=companyMid).order_by(order_by) for item in queryRows: item.__dict__.pop('_state') results.append(item.__dict__) result = [] return CCAIResponse(results) else: logger.error("connector list failed: \n%s" % traceback.format_exc()) return CCAIResponse(PARAMS_ERR, SERVER_ERROR) def create(self, request, *args, **kwargs): '''添加数据POST''' try: data = req_operate_by_user(request) # data['dict_detail_id'] = uuid.uuid4().__str__() serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) return CCAIResponse(data="success") except Exception as e: logger.error("connector create failed: \n%s" % traceback.format_exc()) return CCAIResponse("connector create failed", SERVER_ERROR) def destroy(self, request, *args, **kwargs): try: instance = self.get_object() # 查询是否存在关联 queryRows = Connection.objects.filter(id=instance.id) if len(queryRows > 0): # return CCAIResponse("该数据存在关联,请先删除关联数据", BAD) # is_association = self.check_association(deptId=instance.id) # if is_association: # return CCAIResponse(is_association, BAD) self.perform_destroy(instance) return CCAIResponse(data="delete connector success") else: logger.error("connector id failed: \n%s" % traceback.format_exc()) return CCAIResponse("delete connector id inexistence ", BAD) except Exception as e: logger.error("user: %s, delete dept failed: \n%s" % (request.user.id, traceback.format_exc())) return CCAIResponse("delete dept failed", SERVER_ERROR) def update(self, request, *args, **kwargs): from rest_framework.exceptions import ValidationError try: companyMid = request.query_params.get('companyMid', None) data = req_operate_by_user(request) data['companyMid'] = companyMid partial = kwargs.pop('partial', False) # True:所有字段全部更新, False:仅更新提供的字段 instance = self.get_object() serializer = self.get_serializer(instance, data=data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return CCAIResponse(data="success") except Exception as e: logger.error("user: %s, update connector failed: \n%s" % (request.user.id, traceback.format_exc())) return CCAIResponse("update dept failed", SERVER_ERROR)