Spaces:
Sleeping
Sleeping
Update backend with high-quality catalog and fixes
Browse files- app/api/cart.py +7 -0
- app/api/products.py +15 -2
- app/api/seed.py +0 -1
- app/models/settings.py +1 -0
- app/schemas/settings.py +1 -0
app/api/cart.py
CHANGED
|
@@ -179,6 +179,11 @@ def add_item_to_cart(
|
|
| 179 |
db: Session = Depends(get_db)
|
| 180 |
) -> Any:
|
| 181 |
"""Add a product to the cart."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
cart = get_or_create_cart(db, user_id=current_user_id, session_id=x_cart_id)
|
| 183 |
|
| 184 |
# Check product availability
|
|
@@ -203,6 +208,8 @@ def add_item_to_cart(
|
|
| 203 |
final_price += v.get("price_modifier", 0)
|
| 204 |
break
|
| 205 |
|
|
|
|
|
|
|
| 206 |
if cart_item:
|
| 207 |
if cart_item.quantity + item_in.quantity > product.stock:
|
| 208 |
raise HTTPException(status_code=400, detail=f"Not enough stock. Only {product.stock} available.")
|
|
|
|
| 179 |
db: Session = Depends(get_db)
|
| 180 |
) -> Any:
|
| 181 |
"""Add a product to the cart."""
|
| 182 |
+
from app.models.settings import StoreSettings
|
| 183 |
+
settings = db.query(StoreSettings).first()
|
| 184 |
+
global_discount = settings.global_discount if settings else 0
|
| 185 |
+
multiplier = (1 - global_discount / 100)
|
| 186 |
+
|
| 187 |
cart = get_or_create_cart(db, user_id=current_user_id, session_id=x_cart_id)
|
| 188 |
|
| 189 |
# Check product availability
|
|
|
|
| 208 |
final_price += v.get("price_modifier", 0)
|
| 209 |
break
|
| 210 |
|
| 211 |
+
final_price *= multiplier
|
| 212 |
+
|
| 213 |
if cart_item:
|
| 214 |
if cart_item.quantity + item_in.quantity > product.stock:
|
| 215 |
raise HTTPException(status_code=400, detail=f"Not enough stock. Only {product.stock} available.")
|
app/api/products.py
CHANGED
|
@@ -35,6 +35,11 @@ async def list_products(
|
|
| 35 |
page_size: int = Query(12, ge=1, le=48),
|
| 36 |
db: Session = Depends(get_db),
|
| 37 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
query = db.query(Product).filter(
|
| 39 |
Product.is_active == True, # noqa: E712
|
| 40 |
Product.deleted_at == None # noqa: E711
|
|
@@ -93,8 +98,8 @@ async def list_products(
|
|
| 93 |
id=p.id,
|
| 94 |
name_ar=p.name_ar,
|
| 95 |
name_en=p.name_en,
|
| 96 |
-
price=p.price,
|
| 97 |
-
compare_price=p.compare_price,
|
| 98 |
stock=p.stock,
|
| 99 |
category_id=p.category_id,
|
| 100 |
category=p.category,
|
|
@@ -141,7 +146,15 @@ async def get_product(product_id: int, db: Session = Depends(get_db)):
|
|
| 141 |
if not product or product.deleted_at is not None:
|
| 142 |
raise HTTPException(status_code=404, detail="Product not found")
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
detail = ProductDetail.model_validate(product).model_dump()
|
|
|
|
|
|
|
|
|
|
| 145 |
if isinstance(detail.get("created_at"), datetime):
|
| 146 |
detail["created_at"] = detail["created_at"].isoformat()
|
| 147 |
if detail.get("deleted_at") and isinstance(detail["deleted_at"], datetime):
|
|
|
|
| 35 |
page_size: int = Query(12, ge=1, le=48),
|
| 36 |
db: Session = Depends(get_db),
|
| 37 |
):
|
| 38 |
+
from app.models.settings import StoreSettings
|
| 39 |
+
settings = db.query(StoreSettings).first()
|
| 40 |
+
global_discount = settings.global_discount if settings else 0
|
| 41 |
+
multiplier = (1 - global_discount / 100)
|
| 42 |
+
|
| 43 |
query = db.query(Product).filter(
|
| 44 |
Product.is_active == True, # noqa: E712
|
| 45 |
Product.deleted_at == None # noqa: E711
|
|
|
|
| 98 |
id=p.id,
|
| 99 |
name_ar=p.name_ar,
|
| 100 |
name_en=p.name_en,
|
| 101 |
+
price=p.price * multiplier,
|
| 102 |
+
compare_price=p.compare_price * multiplier if p.compare_price else None,
|
| 103 |
stock=p.stock,
|
| 104 |
category_id=p.category_id,
|
| 105 |
category=p.category,
|
|
|
|
| 146 |
if not product or product.deleted_at is not None:
|
| 147 |
raise HTTPException(status_code=404, detail="Product not found")
|
| 148 |
|
| 149 |
+
from app.models.settings import StoreSettings
|
| 150 |
+
settings = db.query(StoreSettings).first()
|
| 151 |
+
global_discount = settings.global_discount if settings else 0
|
| 152 |
+
multiplier = (1 - global_discount / 100)
|
| 153 |
+
|
| 154 |
detail = ProductDetail.model_validate(product).model_dump()
|
| 155 |
+
detail["price"] *= multiplier
|
| 156 |
+
if detail.get("compare_price"):
|
| 157 |
+
detail["compare_price"] *= multiplier
|
| 158 |
if isinstance(detail.get("created_at"), datetime):
|
| 159 |
detail["created_at"] = detail["created_at"].isoformat()
|
| 160 |
if detail.get("deleted_at") and isinstance(detail["deleted_at"], datetime):
|
app/api/seed.py
CHANGED
|
@@ -85,7 +85,6 @@ CATEGORY_HIERARCHY = {
|
|
| 85 |
"subs": [
|
| 86 |
{"en": "Beauty", "ar": "العطور والتجميل"},
|
| 87 |
{"en": "Watches", "ar": "الساعات والإكسسوارات"},
|
| 88 |
-
{"en": "Sports", "ar": "الرياضة واللياقة"},
|
| 89 |
]
|
| 90 |
}
|
| 91 |
}
|
|
|
|
| 85 |
"subs": [
|
| 86 |
{"en": "Beauty", "ar": "العطور والتجميل"},
|
| 87 |
{"en": "Watches", "ar": "الساعات والإكسسوارات"},
|
|
|
|
| 88 |
]
|
| 89 |
}
|
| 90 |
}
|
app/models/settings.py
CHANGED
|
@@ -11,3 +11,4 @@ class StoreSettings(Base):
|
|
| 11 |
logo_url = Column(String, default="/logo.png")
|
| 12 |
contact_email = Column(String, nullable=True)
|
| 13 |
contact_phone = Column(String, nullable=True)
|
|
|
|
|
|
| 11 |
logo_url = Column(String, default="/logo.png")
|
| 12 |
contact_email = Column(String, nullable=True)
|
| 13 |
contact_phone = Column(String, nullable=True)
|
| 14 |
+
global_discount = Column(Integer, default=0) # Percentage to apply globally
|
app/schemas/settings.py
CHANGED
|
@@ -8,6 +8,7 @@ class StoreSettingsBase(BaseModel):
|
|
| 8 |
logo_url: str
|
| 9 |
contact_email: Optional[str] = None
|
| 10 |
contact_phone: Optional[str] = None
|
|
|
|
| 11 |
|
| 12 |
class StoreSettingsUpdate(StoreSettingsBase):
|
| 13 |
pass
|
|
|
|
| 8 |
logo_url: str
|
| 9 |
contact_email: Optional[str] = None
|
| 10 |
contact_phone: Optional[str] = None
|
| 11 |
+
global_discount: int = 0
|
| 12 |
|
| 13 |
class StoreSettingsUpdate(StoreSettingsBase):
|
| 14 |
pass
|