hisham-cmd commited on
Commit
100a310
·
verified ·
1 Parent(s): 2607dc1

Update backend with high-quality catalog and fixes

Browse files
app/api/products.py CHANGED
@@ -171,6 +171,7 @@ async def create_product(
171
  category_id=request.category_id,
172
  is_featured=request.is_featured,
173
  is_active=request.is_active,
 
174
  )
175
  db.add(product)
176
  db.flush()
 
171
  category_id=request.category_id,
172
  is_featured=request.is_featured,
173
  is_active=request.is_active,
174
+ specs=request.specs,
175
  )
176
  db.add(product)
177
  db.flush()
app/api/seed.py CHANGED
@@ -199,29 +199,34 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
199
  if cat_name and cat_name in ENGLISH_TO_ARABIC_CATEGORY:
200
  cat_name = ENGLISH_TO_ARABIC_CATEGORY[cat_name]
201
 
 
202
  if not cat_name or cat_name not in sub_cats:
203
- # Automatic fallback mapping for consistency
204
- if not cat_name:
205
- # Try to use category from specs if available - safely handle if specs is not a dict
206
- specs_val = item.get("specs")
207
- specs_category = None
208
- if isinstance(specs_val, dict):
209
- specs_category = specs_val.get("category")
210
- # Convert English to Arabic if needed
211
- if specs_category and specs_category in ENGLISH_TO_ARABIC_CATEGORY:
212
  specs_category = ENGLISH_TO_ARABIC_CATEGORY[specs_category]
213
- if specs_category and specs_category in sub_cats:
214
- cat_name = specs_category
215
- else:
216
- continue
217
- # Try to find best match by partial matching
218
- matched = False
219
- for sub in sub_cats.keys():
220
- if cat_name and (cat_name in sub or sub in cat_name):
221
- cat_name = sub
222
- matched = True
223
- break
224
- if not matched: continue
 
 
 
 
 
 
 
225
 
226
  product_name_ar = item.get("name_ar", "").strip()
227
  product_name_en = item.get("name_en", "").strip()
 
199
  if cat_name and cat_name in ENGLISH_TO_ARABIC_CATEGORY:
200
  cat_name = ENGLISH_TO_ARABIC_CATEGORY[cat_name]
201
 
202
+ # Fallback: if category still not found, use default
203
  if not cat_name or cat_name not in sub_cats:
204
+ # Try specs category
205
+ specs_val = item.get("specs")
206
+ if isinstance(specs_val, dict):
207
+ specs_category = specs_val.get("category")
208
+ if specs_category:
209
+ if specs_category in ENGLISH_TO_ARABIC_CATEGORY:
 
 
 
210
  specs_category = ENGLISH_TO_ARABIC_CATEGORY[specs_category]
211
+ if specs_category in sub_cats:
212
+ cat_name = specs_category
213
+
214
+ # Final fallback - use first available category if nothing matched
215
+ if not cat_name or cat_name not in sub_cats:
216
+ # Try partial match as last resort
217
+ for sub in sub_cats.keys():
218
+ if cat_name and (cat_name in sub or sub in cat_name or
219
+ (isinstance(cat_name, str) and any(w in sub for w in cat_name.split()))):
220
+ cat_name = sub
221
+ break
222
+
223
+ # If still no match, use a default category
224
+ if not cat_name or cat_name not in sub_cats:
225
+ cat_name = "الهواتف والإلكترونيات" # Default category
226
+
227
+ # Verify cat_name is in sub_cats
228
+ if cat_name not in sub_cats:
229
+ cat_name = "الهواتف والإلكترونيات" # Force default
230
 
231
  product_name_ar = item.get("name_ar", "").strip()
232
  product_name_en = item.get("name_en", "").strip()
app/models/product.py CHANGED
@@ -12,6 +12,7 @@ from app.db.base import Base
12
 
13
  class Product(Base):
14
  __tablename__ = "products"
 
15
 
16
  id = Column(Integer, primary_key=True, autoincrement=True)
17
  name_ar = Column(String(500), nullable=False)
@@ -42,6 +43,7 @@ class Product(Base):
42
 
43
  class ProductImage(Base):
44
  __tablename__ = "product_images"
 
45
 
46
  id = Column(Integer, primary_key=True, autoincrement=True)
47
  product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False)
@@ -54,6 +56,7 @@ class ProductImage(Base):
54
 
55
  class Category(Base):
56
  __tablename__ = "categories"
 
57
 
58
  id = Column(Integer, primary_key=True, autoincrement=True)
59
  name_ar = Column(String(255), nullable=False)
 
12
 
13
  class Product(Base):
14
  __tablename__ = "products"
15
+ __table_args__ = {'extend_existing': True}
16
 
17
  id = Column(Integer, primary_key=True, autoincrement=True)
18
  name_ar = Column(String(500), nullable=False)
 
43
 
44
  class ProductImage(Base):
45
  __tablename__ = "product_images"
46
+ __table_args__ = {'extend_existing': True}
47
 
48
  id = Column(Integer, primary_key=True, autoincrement=True)
49
  product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False)
 
56
 
57
  class Category(Base):
58
  __tablename__ = "categories"
59
+ __table_args__ = {'extend_existing': True}
60
 
61
  id = Column(Integer, primary_key=True, autoincrement=True)
62
  name_ar = Column(String(255), nullable=False)
app/schemas/product.py CHANGED
@@ -55,6 +55,7 @@ class ProductCreate(BaseModel):
55
  is_featured: bool = False
56
  is_active: bool = True
57
  images: List[ProductImageCreate] = []
 
58
 
59
 
60
  class ProductUpdate(BaseModel):
@@ -68,6 +69,7 @@ class ProductUpdate(BaseModel):
68
  category_id: Optional[int] = None
69
  is_featured: Optional[bool] = None
70
  is_active: Optional[bool] = None
 
71
 
72
 
73
  class ProductListItem(BaseModel):
 
55
  is_featured: bool = False
56
  is_active: bool = True
57
  images: List[ProductImageCreate] = []
58
+ specs: Optional[dict] = None
59
 
60
 
61
  class ProductUpdate(BaseModel):
 
69
  category_id: Optional[int] = None
70
  is_featured: Optional[bool] = None
71
  is_active: Optional[bool] = None
72
+ specs: Optional[dict] = None
73
 
74
 
75
  class ProductListItem(BaseModel):