forked from erp-dev/erp
feat: log formatted
This commit is contained in:
@@ -369,7 +369,7 @@ class PrintingJobListSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'original_id', 'merchant_id', 'printing_order', 'printing_order_id', 'external_order_id', 'product', 'product_name',
|
||||
'id', 'original_id', 'sub_id', 'merchant_id', 'printing_order', 'printing_order_id', 'external_order_id', 'product', 'product_name',
|
||||
'product_image_url', 'has_started',
|
||||
'quantity', 'billed_quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'work_state', 'work_state_display',
|
||||
@@ -480,7 +480,7 @@ class PrintingJobDetailSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'original_id', 'merchant_id', 'printing_order', 'printing_order_id', 'external_order_id', 'product', 'product_name', 'product_code',
|
||||
'id', 'original_id', 'sub_id', 'merchant_id', 'printing_order', 'printing_order_id', 'external_order_id', 'product', 'product_name', 'product_code',
|
||||
'quantity', 'billed_quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'work_state', 'work_state_display',
|
||||
'status', 'status_id', 'is_completed', 'is_production_completed', 'has_started',
|
||||
@@ -543,7 +543,7 @@ class PrintingJobCreateUpdateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = models.PrintingJob
|
||||
fields = [
|
||||
'id', 'original_id', 'printing_order', 'external_order_id', 'product', 'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'id', 'original_id', 'sub_id', 'printing_order', 'external_order_id', 'product', 'quantity', 'unit', 'size', 'pieces', 'description',
|
||||
'work_state',
|
||||
'batch_advance_records',
|
||||
]
|
||||
|
||||
@@ -248,6 +248,22 @@ class PrintingOrderAPITestCase(TestCase):
|
||||
self.assertEqual(response.data['craft'], '活性印花')
|
||||
self.assertIn('customer_name', response.data)
|
||||
self.assertEqual(response.data['customer_name'], self.customer.name)
|
||||
|
||||
def test_get_printing_order_by_external_order_id(self):
|
||||
"""测试通过 external_order_id 获取订单详情"""
|
||||
order = printing_models.PrintingOrder.objects.create(
|
||||
merchant=self.merchant,
|
||||
customer=self.customer,
|
||||
fabric='通过外部编号查询',
|
||||
width='150cm',
|
||||
external_order_id='KD20410611',
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/printing-orders/by-external-order-id/KD20410611/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['id'], order.id)
|
||||
self.assertEqual(response.data['external_order_id'], 'KD20410611')
|
||||
self.assertEqual(response.data['fabric'], '通过外部编号查询')
|
||||
|
||||
def test_update_printing_order(self):
|
||||
"""测试更新订单"""
|
||||
|
||||
@@ -114,12 +114,14 @@ class PrintingJobAPITestCase(TestCase):
|
||||
'unit': '米',
|
||||
'size': '50*60',
|
||||
'pieces': 10,
|
||||
'sub_id': 7,
|
||||
'description': '测试备注'
|
||||
}
|
||||
|
||||
response = self.client.post('/api/v1/printing-jobs/', data, format='json')
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(response.data['external_order_id'], 'KD20410611')
|
||||
self.assertEqual(response.data['sub_id'], 7)
|
||||
|
||||
# 新增字段:批量推进记录(稳定输出 key,默认空数组)
|
||||
self.assertIn('batch_advance_records', response.data)
|
||||
@@ -135,6 +137,7 @@ class PrintingJobAPITestCase(TestCase):
|
||||
self.assertEqual(job.quantity, 100)
|
||||
self.assertEqual(job.unit, '米')
|
||||
self.assertEqual(job.pieces, 10)
|
||||
self.assertEqual(job.sub_id, 7)
|
||||
self.assertFalse(job.is_production_completed)
|
||||
|
||||
def test_create_printing_job_ignores_is_production_completed(self):
|
||||
@@ -162,7 +165,8 @@ class PrintingJobAPITestCase(TestCase):
|
||||
quantity=100,
|
||||
unit='米',
|
||||
size='50*60',
|
||||
pieces=10
|
||||
pieces=10,
|
||||
sub_id=101
|
||||
)
|
||||
job2 = printing_models.PrintingJob.objects.create(
|
||||
printing_order=self.printing_order,
|
||||
@@ -170,12 +174,16 @@ class PrintingJobAPITestCase(TestCase):
|
||||
quantity=200,
|
||||
unit='米',
|
||||
size='60*70',
|
||||
pieces=20
|
||||
pieces=20,
|
||||
sub_id=202
|
||||
)
|
||||
|
||||
response = self.client.get('/api/v1/printing-jobs/')
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 2)
|
||||
sub_ids_by_id = {item['id']: item['sub_id'] for item in response.data['results']}
|
||||
self.assertEqual(sub_ids_by_id[job1.id], 101)
|
||||
self.assertEqual(sub_ids_by_id[job2.id], 202)
|
||||
|
||||
# 新增字段:批量推进记录(稳定输出 key)
|
||||
for item in response.data['results']:
|
||||
@@ -640,6 +648,7 @@ class PrintingJobAPITestCase(TestCase):
|
||||
'unit': '码',
|
||||
'size': '60*70',
|
||||
'pieces': 20,
|
||||
'sub_id': 8,
|
||||
'description': '更新后的备注'
|
||||
}
|
||||
|
||||
@@ -650,11 +659,13 @@ class PrintingJobAPITestCase(TestCase):
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['external_order_id'], 'KD20410611')
|
||||
self.assertEqual(response.data['sub_id'], 8)
|
||||
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.quantity, 200)
|
||||
self.assertEqual(job.unit, '码')
|
||||
self.assertEqual(job.pieces, 20)
|
||||
self.assertEqual(job.sub_id, 8)
|
||||
self.assertFalse(job.is_production_completed)
|
||||
|
||||
def test_update_printing_job_ignores_is_production_completed(self):
|
||||
@@ -724,7 +735,8 @@ class PrintingJobAPITestCase(TestCase):
|
||||
|
||||
patch_data = {
|
||||
'quantity': 150,
|
||||
'pieces': 15
|
||||
'pieces': 15,
|
||||
'sub_id': None
|
||||
}
|
||||
|
||||
response = self.client.patch(
|
||||
@@ -734,10 +746,12 @@ class PrintingJobAPITestCase(TestCase):
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['external_order_id'], 'KD20410611')
|
||||
self.assertIsNone(response.data['sub_id'])
|
||||
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.quantity, 150)
|
||||
self.assertEqual(job.pieces, 15)
|
||||
self.assertIsNone(job.sub_id)
|
||||
self.assertEqual(job.unit, '米') # 未修改字段保持不变
|
||||
|
||||
def test_delete_printing_job_forbidden(self):
|
||||
|
||||
@@ -301,7 +301,7 @@ class PrintingOrderViewSet(CustomerVisibilityFilterMixin, LimitedModelViewSet):
|
||||
):
|
||||
queryset = queryset.filter(is_invalid=False)
|
||||
|
||||
if self.action in ["list", "retrieve"]:
|
||||
if self.action in ["list", "retrieve", "by_external_order_id"]:
|
||||
queryset = queryset.select_related("customer")
|
||||
# 预取 printing_jobs 及其 business_object,用于状态汇总统计
|
||||
# 同时预取 state_logs 和 process 相关数据以减少 job.status 属性调用时的 N+1 查询
|
||||
@@ -327,6 +327,29 @@ class PrintingOrderViewSet(CustomerVisibilityFilterMixin, LimitedModelViewSet):
|
||||
status=status.HTTP_405_METHOD_NOT_ALLOWED,
|
||||
)
|
||||
|
||||
@action(
|
||||
detail=False,
|
||||
methods=["get"],
|
||||
url_path=r"by-external-order-id/(?P<external_order_id>[^/.]+)",
|
||||
)
|
||||
def by_external_order_id(self, request, external_order_id=None):
|
||||
"""通过 external_order_id 获取印染订单详情"""
|
||||
normalized_external_order_id = str(external_order_id or "").strip()
|
||||
printing_order = (
|
||||
self.get_queryset()
|
||||
.filter(external_order_id=normalized_external_order_id)
|
||||
.order_by("id")
|
||||
.first()
|
||||
)
|
||||
if not printing_order:
|
||||
return Response(
|
||||
{"detail": "未找到该 external_order_id 对应的印染订单"},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(printing_order)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=True, methods=["post"])
|
||||
def invalidate(self, request, pk=None):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user