feat(reviews,catalog): add product details to review moderation and smart related products selection

This commit is contained in:
iFixKart Dev 2026-11-17 13:45:36 +05:30
parent 9cb74b3d10
commit 352830a66d
3 changed files with 57 additions and 4 deletions

View file

@ -1134,7 +1134,8 @@ def get_product_detail_by_slug(slug: str, db: Session = Depends(get_db)):
for r in reviews for r in reviews
] ]
related = db.execute( # Find related products prioritizing same brand and products with verified images on disk
related_stmt = (
select(Product) select(Product)
.options( .options(
selectinload(Product.variants).selectinload(ProductVariant.attributes), selectinload(Product.variants).selectinload(ProductVariant.attributes),
@ -1143,8 +1144,30 @@ def get_product_detail_by_slug(slug: str, db: Session = Depends(get_db)):
selectinload(Product.brand), selectinload(Product.brand),
) )
.where(Product.category_id == product.category_id, Product.product_id != product.product_id) .where(Product.category_id == product.category_id, Product.product_id != product.product_id)
.limit(4) )
).scalars().all() if product.brand_id:
related_stmt = related_stmt.order_by((Product.brand_id == product.brand_id).desc(), Product.created_at.desc())
else:
related_stmt = related_stmt.order_by(Product.created_at.desc())
candidates = db.execute(related_stmt.limit(20)).scalars().all()
with_valid_img = []
without_img = []
for cp in candidates:
has_file = False
if cp.images:
for pi in cp.images:
if pi.image_url:
rel = pi.image_url.strip('/')
if not rel.startswith('uploads/') or os.path.exists(os.path.join(os.getcwd(), rel)):
has_file = True
break
if has_file:
with_valid_img.append(cp)
else:
without_img.append(cp)
related = (with_valid_img + without_img)[:5]
related_serialized = [product_response_with_stock(p, db).model_dump(mode="json") for p in related] related_serialized = [product_response_with_stock(p, db).model_dump(mode="json") for p in related]
compatibles = [] compatibles = []

View file

@ -188,6 +188,9 @@ class CategoryCmsUpdate(BaseModel):
class ReviewModerationResponse(BaseModel): class ReviewModerationResponse(BaseModel):
review_id: str review_id: str
product_id: str product_id: str
product_name: Optional[str] = None
product_slug: Optional[str] = None
product_thumbnail: Optional[str] = None
author_name: str author_name: str
author_email: Optional[str] = None author_email: Optional[str] = None
rating: int rating: int

View file

@ -4,6 +4,7 @@
Footer Info, Store Settings, Mega Menu, Catalog Filters, and Review moderation. Footer Info, Store Settings, Mega Menu, Catalog Filters, and Review moderation.
""" """
from __future__ import annotations from __future__ import annotations
import os
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import select from sqlalchemy import select
@ -15,6 +16,7 @@ from app.models.StorefrontCmsModel import (
StorefrontFooterInfo, StorefrontFooterInfo,
) )
from app.models.CategoryModel import Category from app.models.CategoryModel import Category
from app.models.ProductModel import Product
from app.models.ProductReviewModel import ProductReview from app.models.ProductReviewModel import ProductReview
from app.schemas.StorefrontCmsSchema import ( from app.schemas.StorefrontCmsSchema import (
FooterInfoResponse, FooterInfoResponse,
@ -323,9 +325,34 @@ class StorefrontCmsService:
return self._map_review(review) return self._map_review(review)
def _map_review(self, r: ProductReview) -> ReviewModerationResponse: def _map_review(self, r: ProductReview) -> ReviewModerationResponse:
prod = self.db.get(Product, r.product_id)
prod_name = prod.name if prod else None
prod_slug = prod.slug if prod else None
prod_thumb = None
if prod and prod.images:
for pi in prod.images:
if pi.image_url:
rel = pi.image_url.strip('/')
if not rel.startswith('uploads/') or os.path.exists(os.path.join(os.getcwd(), rel)):
prod_thumb = pi.image_url
break
if not prod_thumb and prod.images:
prod_thumb = prod.images[0].image_url
valid_images = []
if r.images:
for img in r.images:
if img.is_approved and img.image_url:
rel = img.image_url.strip('/')
if img.image_url.startswith(('http://', 'https://')) or os.path.exists(os.path.join(os.getcwd(), rel)):
valid_images.append(img.image_url)
return ReviewModerationResponse( return ReviewModerationResponse(
review_id=r.review_id, review_id=r.review_id,
product_id=r.product_id, product_id=r.product_id,
product_name=prod_name,
product_slug=prod_slug,
product_thumbnail=prod_thumb,
author_name=r.author_name, author_name=r.author_name,
author_email=getattr(r, "author_email", None), author_email=getattr(r, "author_email", None),
rating=r.rating, rating=r.rating,
@ -336,5 +363,5 @@ class StorefrontCmsService:
helpful_count=r.helpful_count, helpful_count=r.helpful_count,
admin_reply=r.admin_reply, admin_reply=r.admin_reply,
review_date=r.created_at.strftime("%Y-%m-%d") if r.created_at else None, review_date=r.created_at.strftime("%Y-%m-%d") if r.created_at else None,
images=[img.image_url for img in r.images if img.is_approved] if r.images else [], images=valid_images,
) )