From 35a03218d86e69b9dea7e3672ac674b4dbd8c05a Mon Sep 17 00:00:00 2001 From: colaftc Date: Wed, 22 Apr 2026 15:36:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=87=BA=E8=B4=A7=E5=8D=95=E5=AE=A1?= =?UTF-8?q?=E6=A0=B8=E5=90=8E=E4=B9=9F=E5=8F=AF=E4=BB=A5=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86(=E6=9C=AA=E7=BB=91=E5=AE=9A=E9=80=81=E8=B4=A7?= =?UTF-8?q?=E5=8D=95=E9=83=BD=E5=8F=AF=E4=BB=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api_v1/views/shipment/test_api.py | 33 ++++++++++++++++++++++++++++--- docs/shipment_api.md | 6 +++--- shipment/services.py | 6 +++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/api_v1/views/shipment/test_api.py b/api_v1/views/shipment/test_api.py index c546944..b5eb23c 100644 --- a/api_v1/views/shipment/test_api.py +++ b/api_v1/views/shipment/test_api.py @@ -1876,9 +1876,9 @@ class ShipmentQueryAPITestCase(TestCase): self.assertEqual(self.shipment1.contact_name, "王五") self.assertEqual(self.shipment1.contact_phone, "13700137000") - def test_patch_shipment_rejects_non_draft(self): + def test_patch_shipment_allows_approved_shipment_without_delivery(self): """ - 非草稿状态的出货单不允许修改业务数据。 + 已审核但未关联送货单的出货单允许修改业务数据。 """ from shipment.services import modify_status @@ -1887,6 +1887,33 @@ class ShipmentQueryAPITestCase(TestCase): target_status=shipment_models.ShipmentStatus.PUBLISHED, operator=self.user1, ) + modify_status( + self.shipment1, + target_status=shipment_models.ShipmentStatus.APPROVED, + operator=self.user1, + approved_by=self.user1, + ) + + resp = self.client.patch( + f"/api/v1/shipment/shipments/{self.shipment1.id}/", + data={"area": "审核后可修改"}, + format="json", + ) + self.assertEqual(resp.status_code, status.HTTP_200_OK) + self.assertEqual(resp.json()["area"], "审核后可修改") + + self.shipment1.refresh_from_db() + self.assertEqual(self.shipment1.area, "审核后可修改") + + def test_patch_shipment_rejects_when_bound_to_delivery(self): + delivery = shipment_models.ShipmentDelivery.objects.create( + merchant=self.merchant1, + driver_name="绑定司机", + vehicle_trip="LOCK-001", + created_by=self.user1, + ) + self.shipment1.delivery = delivery + self.shipment1.save(update_fields=["delivery", "updated_at"]) resp = self.client.patch( f"/api/v1/shipment/shipments/{self.shipment1.id}/", @@ -1894,7 +1921,7 @@ class ShipmentQueryAPITestCase(TestCase): format="json", ) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) - self.assertIn("仅草稿状态", resp.json()["detail"]) + self.assertIn("已关联送货单", resp.json()["detail"]) self.shipment1.refresh_from_db() self.assertEqual(self.shipment1.area, "A1") diff --git a/docs/shipment_api.md b/docs/shipment_api.md index 0641eb4..58c5f9d 100644 --- a/docs/shipment_api.md +++ b/docs/shipment_api.md @@ -113,8 +113,8 @@ - 仅允许修改业务数据字段:`customer`、`shipment_date`、`address`、`contact_name`、`contact_phone`、`area`、`remark`、`external_id` - 该接口不允许修改 `status` -- 仅 `草稿(未发布)` 状态的出货单允许修改 -- 非草稿状态会返回 `400 Bad Request` +- 仅“未关联送货单”的出货单允许修改 +- 一旦出货单已关联送货单,会返回 `400 Bad Request` 请求体示例: @@ -132,7 +132,7 @@ ```json { - "detail": "仅草稿状态的出货单允许修改" + "detail": "已关联送货单的出货单不允许修改" } ``` diff --git a/shipment/services.py b/shipment/services.py index 53e5434..87814ab 100644 --- a/shipment/services.py +++ b/shipment/services.py @@ -708,13 +708,13 @@ def update_shipment( 更新出货单业务数据。 说明: - - 仅允许在草稿状态下修改 + - 仅允许修改未关联送货单的出货单 - 不处理状态变更 """ from basic_info.models import Customer - if shipment.status != ShipmentStatus.DRAFT: - raise ValueError("仅草稿状态的出货单允许修改") + if shipment.delivery_id is not None: + raise ValueError("已关联送货单的出货单不允许修改") if customer_id is not None: customer = Customer.objects.filter(id=customer_id).first()