Untitled
unknown
python
a year ago
27 kB
10
Indexable
from sqlalchemy.orm import Session
from fastapi import HTTPException
from . import models, schemas
def create_bundle_group_product(db: Session, product: schemas.ProductCreate):
if product.itemType != "bundle_group":
raise HTTPException(status_code=400, detail="Item type must be 'bundle_group'")
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 variants
if product.variants:
for variant in product.variants:
variant_entry = models.Variant(
productId=db_product.id,
titleEn=variant.titleEn,
titleAr=variant.titleAr,
selIndex=variant.selIndex,
subtitleEn=variant.subtitleEn,
subtitleAr=variant.subtitleAr
)
db.add(variant_entry)
db.commit()
db.refresh(variant_entry)
for option in variant.options:
variant_option_entry = models.VariantOption(
variantId=variant_entry.id,
titleEn=option.titleEn,
titleAr=option.titleAr,
isSelected=option.isSelected
)
db.add(variant_option_entry)
# Add sub-products
if product.products:
for sub_product in product.products:
sub_product_entry = models.Product(
titleEn=sub_product.titleEn,
titleAr=sub_product.titleAr,
descriptionEn=sub_product.descriptionEn,
descriptionAr=sub_product.descriptionAr,
customizable=sub_product.customizable,
sequenceOrder=sub_product.sequenceOrder,
price=sub_product.price,
specialPrice=sub_product.specialPrice,
strikeOutPrice=sub_product.strikeOutPrice,
discountPrecentage=sub_product.discountPrecentage,
selectedItemId=sub_product.selectedItemId,
type=sub_product.type,
displayDateConfig=sub_product.displayDateConfig,
selectedValue=sub_product.selectedValue,
itemType=sub_product.itemType,
limited_offer=sub_product.limited_offer,
spicyOutConfig=sub_product.spicyOutConfig,
taxClassId=sub_product.taxClassId,
lucky_day=sub_product.lucky_day,
lucky_date_everymonth=sub_product.lucky_date_everymonth,
last_weak_everymonth=sub_product.last_weak_everymonth,
lucky_type=sub_product.lucky_type,
status=sub_product.status,
qrcode=sub_product.qrcode,
parent_product_id=db_product.id # Link sub-product to the parent bundle group product
)
db.add(sub_product_entry)
db.commit()
db.refresh(sub_product_entry)
# Add services for sub-products
if sub_product.services:
for service_name, active in sub_product.services.items():
service_entry = models.Service(name=service_name, active=active, productId=sub_product_entry.id)
db.add(service_entry)
# Add display days and time ranges for sub-products if limited_offer is set
if sub_product.limited_offer and sub_product.display_day:
for day, time_ranges in sub_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=sub_product_entry.id
)
db.add(display_time_range_entry)
# Add steps for sub-products
if sub_product.steps:
for step in sub_product.steps:
step_entry = models.Step(
productId=sub_product_entry.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)
# Add variants for sub-products
if sub_product.variants:
for variant in sub_product.variants:
variant_entry = models.Variant(
productId=sub_product_entry.id,
titleEn=variant.titleEn,
titleAr=variant.titleAr,
selIndex=variant.selIndex,
subtitleEn=variant.subtitleEn,
subtitleAr=variant.subtitleAr
)
db.add(variant_entry)
db.commit()
db.refresh(variant_entry)
for option in variant.options:
variant_option_entry = models.VariantOption(
variantId=variant_entry.id,
titleEn=option.titleEn,
titleAr=option.titleAr,
isSelected=option.isSelected
)
db.add(variant_option_entry)
db.commit()
return db_product
def update_bundle_group_product(db: Session, product_id: int, product: schemas.ProductCreate):
if product.itemType != "bundle_group":
raise HTTPException(status_code=400, detail="Item type must be 'bundle_group'")
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", "variants", "products"]:
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)
# Clear existing variants and variant options
db.query(models.VariantOption).filter(models.VariantOption.variantId.in_(
db.query(models.Variant.id).filter(models.Variant.productId == product_id)
)).delete()
db.query(models.Variant).filter(models.Variant.productId == product_id).delete()
# Add variants
if product.variants:
for variant in product.variants:
variant_entry = models.Variant(
productId=product_id,
titleEn=variant
titleEn=variant.titleEn,
titleAr=variant.titleAr,
selIndex=variant.selIndex,
subtitleEn=variant.subtitleEn,
subtitleAr=variant.subtitleAr
)
db.add(variant_entry)
db.commit()
db.refresh(variant_entry)
for option in variant.options:
variant_option_entry = models.VariantOption(
variantId=variant_entry.id,
titleEn=option.titleEn,
titleAr=option.titleAr,
isSelected=option.isSelected
)
db.add(variant_option_entry)
# Clear existing sub-products
db.query(models.Product).filter(models.Product.parent_product_id == product_id).delete()
# Add sub-products
if product.products:
for sub_product in product.products:
sub_product_entry = models.Product(
titleEn=sub_product.titleEn,
titleAr=sub_product.titleAr,
descriptionEn=sub_product.descriptionEn,
descriptionAr=sub_product.descriptionAr,
customizable=sub_product.customizable,
sequenceOrder=sub_product.sequenceOrder,
price=sub_product.price,
specialPrice=sub_product.specialPrice,
strikeOutPrice=sub_product.strikeOutPrice,
discountPrecentage=sub_product.discountPrecentage,
selectedItemId=sub_product.selectedItemId,
type=sub_product.type,
displayDateConfig=sub_product.displayDateConfig,
selectedValue=sub_product.selectedValue,
itemType=sub_product.itemType,
limited_offer=sub_product.limited_offer,
spicyOutConfig=sub_product.spicyOutConfig,
taxClassId=sub_product.taxClassId,
lucky_day=sub_product.lucky_day,
lucky_date_everymonth=sub_product.lucky_date_everymonth,
last_weak_everymonth=sub_product.last_weak_everymonth,
lucky_type=sub_product.lucky_type,
status=sub_product.status,
qrcode=sub_product.qrcode,
parent_product_id=db_product.id # Link sub-product to the parent bundle group product
)
db.add(sub_product_entry)
db.commit()
db.refresh(sub_product_entry)
# Add services for sub-products
if sub_product.services:
for service_name, active in sub_product.services.items():
service_entry = models.Service(name=service_name, active=active, productId=sub_product_entry.id)
db.add(service_entry)
# Add display days and time ranges for sub-products if limited_offer is set
if sub_product.limited_offer and sub_product.display_day:
for day, time_ranges in sub_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=sub_product_entry.id
)
db.add(display_time_range_entry)
# Add steps for sub-products
if sub_product.steps:
for step in sub_product.steps:
step_entry = models.Step(
productId=sub_product_entry.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)
# Add variants for sub-products
if sub_product.variants:
for variant in sub_product.variants:
variant_entry = models.Variant(
productId=sub_product_entry.id,
titleEn=variant.titleEn,
titleAr=variant.titleAr,
selIndex=variant.selIndex,
subtitleEn=variant.subtitleEn,
subtitleAr=variant.subtitleAr
)
db.add(variant_entry)
db.commit()
db.refresh(variant_entry)
for option in variant.options:
variant_option_entry = models.VariantOption(
variantId=variant_entry.id,
titleEn=option.titleEn,
titleAr=option.titleAr,
isSelected=option.isSelected
)
db.add(variant_option_entry)
db.commit()
db.refresh(db_product)
return db_product
Editor is loading...
Leave a Comment