forked from erp-dev/erp
fix
This commit is contained in:
@@ -281,9 +281,29 @@ class PrintingJobListSerializer(serializers.ModelSerializer):
|
|||||||
return obj.business_object.id if obj.business_object else None
|
return obj.business_object.id if obj.business_object else None
|
||||||
|
|
||||||
def get_product_image_url(self, obj):
|
def get_product_image_url(self, obj):
|
||||||
"""获取产品图片 URL(优先 mdy_image_url,其次从 description 提取)"""
|
"""
|
||||||
if obj.product:
|
获取产品图片 URL
|
||||||
return obj.product.get_primary_image_url()
|
|
||||||
|
优先级:
|
||||||
|
1. mdy_image_url(明道云图片)
|
||||||
|
2. description JSON 中的图片 URL
|
||||||
|
3. image 字段(本地上传的图片)
|
||||||
|
"""
|
||||||
|
if not obj.product:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 先尝试 get_primary_image_url(mdy_image_url 或 description JSON)
|
||||||
|
primary_url = obj.product.get_primary_image_url()
|
||||||
|
if primary_url:
|
||||||
|
return primary_url
|
||||||
|
|
||||||
|
# fallback 到本地上传的 image 字段
|
||||||
|
if obj.product.image:
|
||||||
|
request = self.context.get('request')
|
||||||
|
if request:
|
||||||
|
return request.build_absolute_uri(obj.product.image.url)
|
||||||
|
return obj.product.image.url
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_work_state_display(self, obj):
|
def get_work_state_display(self, obj):
|
||||||
|
|||||||
@@ -1198,3 +1198,32 @@ class PrintingJobAPITestCase(TestCase):
|
|||||||
result = response.data['results'][0]
|
result = response.data['results'][0]
|
||||||
self.assertIn('product_image_url', result)
|
self.assertIn('product_image_url', result)
|
||||||
self.assertIsNone(result['product_image_url'])
|
self.assertIsNone(result['product_image_url'])
|
||||||
|
|
||||||
|
def test_list_printing_jobs_product_image_url_fallback_to_local_image(self):
|
||||||
|
"""测试产品图片 URL fallback 到本地 image 字段"""
|
||||||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
|
|
||||||
|
# 确保 mdy_image_url 和 description 都为空
|
||||||
|
self.product.mdy_image_url = None
|
||||||
|
self.product.description = None
|
||||||
|
# 设置本地图片(模拟已上传的文件路径)
|
||||||
|
self.product.image = 'product_images/test_product.jpg'
|
||||||
|
self.product.save()
|
||||||
|
|
||||||
|
job = printing_models.PrintingJob.objects.create(
|
||||||
|
printing_order=self.printing_order,
|
||||||
|
product=self.product,
|
||||||
|
quantity=100,
|
||||||
|
unit='米',
|
||||||
|
size='50*60',
|
||||||
|
pieces=10
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.get('/api/v1/printing-jobs/')
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
# 验证 product_image_url 字段存在且包含本地图片路径
|
||||||
|
result = response.data['results'][0]
|
||||||
|
self.assertIn('product_image_url', result)
|
||||||
|
self.assertIsNotNone(result['product_image_url'])
|
||||||
|
self.assertIn('product_images/test_product.jpg', result['product_image_url'])
|
||||||
|
|||||||
Reference in New Issue
Block a user