# -*- coding: utf-8 -*- """ 复聊话术 API(需要登录): - GET /api/scripts -> 查询所有话术 - POST /api/scripts -> 创建话术 - GET /api/scripts/{id} -> 查询单个话术 - PUT /api/scripts/{id} -> 更新话术 - DELETE /api/scripts/{id} -> 删除话术 """ from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response from server.models import ChatScript from server.serializers import ChatScriptSerializer @api_view(["GET", "POST"]) def script_list(request): if request.method == "GET": position = request.query_params.get("position") script_type = request.query_params.get("script_type") qs = ChatScript.objects.all().order_by("-updated_at") if position: qs = qs.filter(position=position) if script_type: qs = qs.filter(script_type=script_type) return Response(ChatScriptSerializer(qs, many=True).data) ser = ChatScriptSerializer(data=request.data) ser.is_valid(raise_exception=True) ser.save() return Response(ser.data, status=status.HTTP_201_CREATED) @api_view(["GET", "PUT", "DELETE"]) def script_detail(request, pk): try: obj = ChatScript.objects.get(pk=pk) except ChatScript.DoesNotExist: return Response({"detail": "话术不存在"}, status=status.HTTP_404_NOT_FOUND) if request.method == "GET": return Response(ChatScriptSerializer(obj).data) if request.method == "PUT": ser = ChatScriptSerializer(obj, data=request.data, partial=True) ser.is_valid(raise_exception=True) ser.save() return Response(ser.data) obj.delete() return Response({"message": "话术已删除"})