From bca0863952b18dac6928a7596074023c5ddb2d9e Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Thu, 28 Mar 2024 17:09:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=80=90=E6=A0=87=E7=AD=BE=E3=80=91?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E7=BB=91=E5=AE=9A=E8=B5=84=E6=BA=90api?= =?UTF-8?q?=E9=9C=80=E6=A0=A1=E9=AA=8C=E6=AD=A3=E7=A1=AE=E7=9A=84uuid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/labels/api.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/apps/labels/api.py b/apps/labels/api.py index ff373cd7d..eb34d3621 100644 --- a/apps/labels/api.py +++ b/apps/labels/api.py @@ -1,4 +1,7 @@ +from django.core.exceptions import ValidationError from django.shortcuts import get_object_or_404 +from django.utils.translation import gettext_lazy as _ +from rest_framework import status from rest_framework.decorators import action from rest_framework.response import Response @@ -79,6 +82,19 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet): queryset = content_type.filter_queryset(queryset, keyword) return queryset + @staticmethod + def validate_res_ids(content_type: ContentType, res_ids: list): + model_cls = content_type.model_class() + pk_field = model_cls._meta.pk + pk_python_type = pk_field.to_python + invalid_ids = [] + for _id in res_ids: + try: + pk_python_type(_id) + except ValidationError: + invalid_ids.append(_id) + return invalid_ids + def put(self, request, *args, **kwargs): label_pk = self.kwargs.get('label') res_type = self.kwargs.get('res_type') @@ -86,6 +102,13 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet): label = get_object_or_404(Label, pk=label_pk) res_ids = request.data.get('res_ids', []) + invalid_ids = self.validate_res_ids(content_type, res_ids) + if invalid_ids: + error = f'{_("Invalid data")}: {", ".join(invalid_ids)}' + return Response({ + "code": 'invalid_data', "detail": error, + }, status=status.HTTP_400_BAD_REQUEST) + LabeledResource.objects \ .filter(res_type=content_type, label=label) \ .exclude(res_id__in=res_ids).delete()