商品id查询活动列表
This commit is contained in:
parent
265702fb97
commit
34709b4ecd
@ -264,6 +264,46 @@ class CampaignViewSet(viewsets.ModelViewSet):
|
|||||||
return []
|
return []
|
||||||
return super().get_permissions()
|
return super().get_permissions()
|
||||||
|
|
||||||
|
@action(detail=False, methods=['get'], url_path='by-product')
|
||||||
|
def get_campaigns_by_product(self, request):
|
||||||
|
"""获取与特定产品相关的活动列表"""
|
||||||
|
try:
|
||||||
|
product_id = request.query_params.get('product_id')
|
||||||
|
if not product_id:
|
||||||
|
return api_response(code=400, message="缺少必要参数:product_id", data=None)
|
||||||
|
|
||||||
|
# 查询产品是否存在
|
||||||
|
from .models import Product
|
||||||
|
try:
|
||||||
|
product = Product.objects.get(id=product_id, is_active=True)
|
||||||
|
except Product.DoesNotExist:
|
||||||
|
return api_response(code=404, message=f"产品不存在: {product_id}", data=None)
|
||||||
|
|
||||||
|
# 查询与该产品关联的所有活动
|
||||||
|
campaigns = Campaign.objects.filter(
|
||||||
|
link_product=product,
|
||||||
|
is_active=True
|
||||||
|
).distinct()
|
||||||
|
|
||||||
|
# 序列化活动数据
|
||||||
|
serializer = self.get_serializer(campaigns, many=True)
|
||||||
|
|
||||||
|
return api_response(data={
|
||||||
|
'product': {
|
||||||
|
'id': str(product.id),
|
||||||
|
'name': product.name,
|
||||||
|
'brand': product.brand.name if product.brand else None
|
||||||
|
},
|
||||||
|
'campaigns': serializer.data,
|
||||||
|
'total': campaigns.count()
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取产品相关活动列表失败: {str(e)}")
|
||||||
|
import traceback
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
return api_response(code=500, message=f"获取产品相关活动列表失败: {str(e)}", data=None)
|
||||||
|
|
||||||
@action(detail=False, methods=['get'], url_path='token-info')
|
@action(detail=False, methods=['get'], url_path='token-info')
|
||||||
def token_info(self, request):
|
def token_info(self, request):
|
||||||
"""获取当前用户的token信息和WebSocket URL示例"""
|
"""获取当前用户的token信息和WebSocket URL示例"""
|
||||||
|
Loading…
Reference in New Issue
Block a user