haha
This commit is contained in:
@@ -159,6 +159,7 @@ class ProductAdminOut(Schema):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
image: Optional[str] = None
|
||||
images: List[str] = []
|
||||
category_id: int
|
||||
category_name: Optional[str] = None
|
||||
status: str
|
||||
@@ -186,6 +187,12 @@ class ProductReviewIn(Schema):
|
||||
reject_reason: Optional[str] = None
|
||||
|
||||
|
||||
class ProductImagesIn(Schema):
|
||||
"""Product images update input schema."""
|
||||
images: List[str] = []
|
||||
image: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/products/pending/", response=List[ProductAdminOut], auth=JWTAuth())
|
||||
@paginate(PageNumberPagination, page_size=20)
|
||||
def list_pending_products(request):
|
||||
@@ -233,3 +240,25 @@ def review_product(request, product_id: int, data: ProductReviewIn):
|
||||
product.save()
|
||||
|
||||
return product
|
||||
|
||||
|
||||
@router.put("/products/{product_id}/images/", response=ProductAdminOut, auth=JWTAuth())
|
||||
def update_product_images(request, product_id: int, data: ProductImagesIn):
|
||||
require_admin(request.auth)
|
||||
try:
|
||||
product = Product.objects.select_related("category", "submitted_by").get(id=product_id)
|
||||
except Product.DoesNotExist:
|
||||
raise HttpError(404, "商品不存在")
|
||||
|
||||
max_images = 6
|
||||
images = [url.strip() for url in (data.images or []) if url and url.strip()]
|
||||
image = (data.image or "").strip() or (images[0] if images else None)
|
||||
if image and image not in images:
|
||||
images.insert(0, image)
|
||||
if len(images) > max_images:
|
||||
raise HttpError(400, f"最多上传{max_images}张图片")
|
||||
|
||||
product.image = image or None
|
||||
product.images = images
|
||||
product.save(update_fields=["image", "images", "updated_at"])
|
||||
return product
|
||||
|
||||
Reference in New Issue
Block a user