forked from erp-dev/erp
feat: backfill external order image upgrade to beat schedule by 1 hour
This commit is contained in:
@@ -13,6 +13,7 @@ from flower.viewsets import LimitedLimitOffsetPagination
|
||||
from shipment.models import Shipment
|
||||
|
||||
from .serializers import (
|
||||
SalesItemDetailSerializer,
|
||||
SalesItemSerializer,
|
||||
ShipmentSerializer,
|
||||
ShipmentCreateNormalSerializer,
|
||||
@@ -35,18 +36,39 @@ def _build_sales_item_serializer_context(items):
|
||||
)
|
||||
|
||||
printing_order_map = {}
|
||||
external_order_id_map = {}
|
||||
product_image_map = {}
|
||||
if printing_job_ids:
|
||||
from printing.models import PrintingJob
|
||||
|
||||
printing_order_map = dict(
|
||||
PrintingJob.objects.filter(id__in=printing_job_ids).values_list(
|
||||
"id", "printing_order_id"
|
||||
)
|
||||
printing_jobs = list(
|
||||
PrintingJob.objects.filter(id__in=printing_job_ids)
|
||||
.select_related("printing_order", "product")
|
||||
)
|
||||
printing_order_map = {
|
||||
job.id: job.printing_order_id for job in printing_jobs
|
||||
}
|
||||
external_order_id_map = {
|
||||
job.id: getattr(job.printing_order, "external_order_id", None)
|
||||
for job in printing_jobs
|
||||
}
|
||||
for job in printing_jobs:
|
||||
if not getattr(job, "product", None):
|
||||
product_image_map[job.id] = None
|
||||
continue
|
||||
primary_url = job.product.get_primary_image_url()
|
||||
if primary_url:
|
||||
product_image_map[job.id] = primary_url
|
||||
elif job.product.image:
|
||||
product_image_map[job.id] = job.product.image.url
|
||||
else:
|
||||
product_image_map[job.id] = None
|
||||
|
||||
return {
|
||||
"customer_name_map": customer_name_map,
|
||||
"printing_order_map": printing_order_map,
|
||||
"external_order_id_map": external_order_id_map,
|
||||
"product_image_map": product_image_map,
|
||||
}
|
||||
|
||||
|
||||
@@ -444,6 +466,7 @@ class SalesItemByCustomerView(GenericAPIView):
|
||||
request.query_params.get("include_already_has_shipment", "false").lower()
|
||||
== "true"
|
||||
)
|
||||
external_order_id = (request.query_params.get("external_order_id") or "").strip()
|
||||
|
||||
from shipment.services import get_sales_items_by_customer
|
||||
|
||||
@@ -451,6 +474,7 @@ class SalesItemByCustomerView(GenericAPIView):
|
||||
merchant=customer.merchant,
|
||||
customer_id=customer.id,
|
||||
include_already_has_shipment=include_already_has_shipment,
|
||||
external_order_id=external_order_id or None,
|
||||
)
|
||||
page = self.paginate_queryset(queryset)
|
||||
items = list(page) if page is not None else list(queryset)
|
||||
@@ -529,5 +553,48 @@ class SalesItemCreateView(APIView):
|
||||
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 返回创建的销售品
|
||||
response_serializer = SalesItemSerializer(sales_item)
|
||||
response_serializer = SalesItemSerializer(
|
||||
sales_item,
|
||||
context=_build_sales_item_serializer_context([sales_item]),
|
||||
)
|
||||
return Response(response_serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
class SalesItemDetailView(GenericAPIView):
|
||||
"""
|
||||
销售品详情。
|
||||
|
||||
GET /api/v1/shipment/sales-items/<id>/
|
||||
"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = SalesItemDetailSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
from shipment.models import SalesItem
|
||||
|
||||
qs = SalesItem.objects.select_related("shipment", "created_by").order_by("id")
|
||||
|
||||
user = self.request.user
|
||||
if getattr(user, "is_superuser", False):
|
||||
return qs
|
||||
|
||||
emp = getattr(user, "employee", None)
|
||||
merchant = getattr(emp, "merchant", None) if emp else None
|
||||
if not merchant:
|
||||
return SalesItem.objects.none()
|
||||
return qs.filter(merchant=merchant)
|
||||
|
||||
def get(self, request, pk: int):
|
||||
sales_item = self.get_queryset().filter(id=pk).first()
|
||||
if sales_item is None:
|
||||
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
serializer = self.get_serializer(
|
||||
sales_item,
|
||||
context={
|
||||
**_build_sales_item_serializer_context([sales_item]),
|
||||
"request": request,
|
||||
},
|
||||
)
|
||||
return Response(serializer.data)
|
||||
|
||||
Reference in New Issue
Block a user