Untitled
unknown
python
a year ago
12 kB
11
Indexable
from sqlalchemy.orm import Session
from fastapi import HTTPException
from . import models, schemas
def create_bundle_product(db: Session, product: schemas.ProductCreate):
if product.itemType != "bundle":
raise HTTPException(status_code=400, detail="Item type must be 'bundle'")
db_product = models.Product(
titleEn=product.titleEn,
titleAr=product.titleAr,
descriptionEn=product.descriptionEn,
descriptionAr=product.descriptionAr,
customizable=product.customizable,
sequenceOrder=product.sequenceOrder,
price=product.price,
specialPrice=product.specialPrice,
strikeOutPrice=product.strikeOutPrice,
discountPrecentage=product.discountPrecentage,
selectedItemId=product.selectedItemId,
type=product.type,
displayDateConfig=product.displayDateConfig,
selectedValue=product.selectedValue,
itemType=product.itemType,
limited_offer=product.limited_offer,
spicyOutConfig=product.spicyOutConfig,
taxClassId=product.taxClassId,
lucky_day=product.lucky_day,
lucky_date_everymonth=product.lucky_date_everymonth,
last_weak_everymonth=product.last_weak_everymonth,
lucky_type=product.lucky_type,
status=product.status,
qrcode=product.qrcode
)
db.add(db_product)
db.commit()
db.refresh(db_product)
# Assign the product to multiple categories
for category_id in product.categoryIds:
category_product = models.CategoryProduct(category_id=category_id, product_id=db_product.id)
db.add(category_product)
# Add services
if product.services:
for service_name, active in product.services.items():
service_entry = models.Service(name=service_name, active=active, productId=db_product.id)
db.add(service_entry)
# Add display days and time ranges if limited_offer is set
if product.limited_offer and product.display_day:
for day, time_ranges in product.display_day.items():
display_day_entry = db.query(models.DisplayDay).filter(models.DisplayDay.day == day).first()
if not display_day_entry:
display_day_entry = models.DisplayDay(day=day)
db.add(display_day_entry)
db.commit()
db.refresh(display_day_entry)
for time_range in time_ranges:
display_time_range_entry = models.DisplayTimeRange(
displayDayId=display_day_entry.id,
startTime=time_range['from'],
endTime=time_range['to'],
productId=db_product.id
)
db.add(display_time_range_entry)
# Add steps
if product.steps:
for step in product.steps:
step_entry = models.Step(
productId=db_product.id,
titleEn=step.titleEn,
titleAr=step.titleAr,
compId=step.compId,
subtitleEn=step.subtitleEn,
subtitleAr=step.subtitleAr,
displayType=step.displayType,
sequenceOrder=step.sequenceOrder,
parseVg=step.parseVg
)
db.add(step_entry)
db.commit()
db.refresh(step_entry)
for option in step.options:
step_option_entry = models.StepOption(
stepId=step_entry.id,
nameEn=option.nameEn,
nameAr=option.nameAr,
price=option.price,
selected=option.selected,
displayType=option.displayType,
sequence=option.sequence,
modGroupId=option.modGroupId
)
db.add(step_option_entry)
db.commit()
db.refresh(step_option_entry)
for modifier in option.modifiers:
modifier_entry = models.Modifier(
stepOptionId=step_option_entry.id,
isAddOn=modifier.isAddOn,
titleEn=modifier.titleEn,
titleAr=modifier.titleAr,
subtitleEn=modifier.subtitleEn,
subtitleAr=modifier.subtitleAr,
maximum=modifier.maximum,
minimum=modifier.minimum,
displayType=modifier.displayType,
itemStyle=modifier.itemStyle,
ingredient=modifier.ingredient
)
db.add(modifier_entry)
db.commit()
db.refresh(modifier_entry)
for mod_option in modifier.options:
mod_option_entry = models.ModifierOption(
modifierId=modifier_entry.id,
nameEn=mod_option.nameEn,
nameAr=mod_option.nameAr,
price=mod_option.price,
default=mod_option.default
)
db.add(mod_option_entry)
db.commit()
return db_product
def update_bundle_product(db: Session, product_id: int, product: schemas.ProductCreate):
if product.itemType != "bundle":
raise HTTPException(status_code=400, detail="Item type must be 'bundle'")
db_product = db.query(models.Product).filter(models.Product.id == product_id).first()
if db_product:
for key, value in product.dict().items():
if key not in ["categoryIds", "services", "display_day", "steps"]:
setattr(db_product, key, value)
# Clear existing category assignments
db.query(models.CategoryProduct).filter(models.CategoryProduct.product_id == product_id).delete()
# Re-assign the product to multiple categories
for category_id in product.categoryIds:
category_product = models.CategoryProduct(category_id=category_id, product_id=product_id)
db.add(category_product)
# Clear existing services
db.query(models.Service).filter(models.Service.productId == product_id).delete()
# Add services
if product.services:
for service_name, active in product.services.items():
service_entry = models.Service(name=service_name, active=active, productId=product_id)
db.add(service_entry)
# Clear existing display time ranges if limited_offer is set
if product.limited_offer:
db.query(models.DisplayTimeRange).filter(models.DisplayTimeRange.productId == product_id).delete()
# Add display days and time ranges
if product.display_day:
for day, time_ranges in product.display_day.items():
display_day_entry = db.query(models.DisplayDay).filter(models.DisplayDay.day == day).first()
if not display_day_entry:
display_day_entry = models.DisplayDay(day=day)
db.add(display_day_entry)
db.commit()
db.refresh(display_day_entry)
for time_range in time_ranges:
display_time_range_entry = models.DisplayTimeRange(
displayDayId=display_day_entry.id,
startTime=time_range['from'],
endTime=time_range['to'],
productId=product_id
)
db.add(display_time_range_entry)
# Clear existing steps, options, modifiers, and modifier options
db.query(models.ModifierOption).filter(models.ModifierOption.modifierId.in_(
db.query(models.Modifier.id).filter(models.Modifier.stepOptionId.in_(
db.query(models.StepOption.id).filter(models.StepOption.stepId.in_(
db.query(models.Step.id).filter(models.Step.productId == product_id)
))
))
)).delete()
db.query(models.Modifier).filter(models.Modifier.stepOptionId.in_(
db.query(models.StepOption.id).filter(models.StepOption.stepId.in_(
db.query(models.Step.id).filter(models.Step.productId == product_id)
))
)).delete()
db.query(models.StepOption).filter(models.StepOption.stepId.in_(
db.query(models.Step.id).filter(models.Step.productId == product_id)
)).delete()
db.query(models.Step).filter(models.Step.productId == product_id).delete()
# Add steps
if product.steps:
for step in product.steps:
step_entry = models.Step(
productId=product_id,
titleEn=step.titleEn,
titleAr=step.titleAr,
compId=step.compId,
subtitleEn=step.subtitleEn,
subtitleAr=step.subtitleAr,
displayType=step.displayType,
sequenceOrder=step.sequenceOrder,
parseVg=step.parseVg
)
db.add(step_entry)
db.commit()
db.refresh(step_entry)
for option in step.options:
step_option_entry = models.StepOption(
stepId=step_entry.id,
nameEn=option.nameEn,
nameAr=option.nameAr,
price=option.price,
selected=option.selected,
displayType=option.displayType,
sequence=option.sequence,
modGroupId=option.modGroupId
)
db.add(step_option_entry)
db.commit()
db.refresh(step_option_entry)
for modifier in option.modifiers:
modifier_entry = models.Modifier(
stepOptionId=step_option_entry.id,
isAddOn=modifier.isAddOn,
titleEn=modifier.titleEn,
titleAr=modifier.titleAr,
subtitleEn=modifier.subtitleEn,
subtitleAr=modifier.subtitleAr,
maximum=modifier.maximum,
minimum=modifier.minimum,
displayType=modifier.displayType,
itemStyle=modifier.itemStyle,
ingredient=modifier.ingredient
)
db.add(modifier_entry)
db.commit()
db.refresh(modifier_entry)
for mod_option in modifier.options:
mod_option_entry = models.ModifierOption(
modifierId=modifier_entry.id,
nameEn=mod_option.nameEn,
nameAr=mod_option.nameAr,
price=mod_option.price,
default=mod_option.default
)
db.add(mod_option_entry)
db.commit()
db.refresh(db_product)
return db_product
Editor is loading...
Leave a Comment