crud.py
unknown
python
a year ago
14 kB
11
Indexable
from sqlalchemy.orm import Session
from fastapi import HTTPException
from . import models, schemas
def create_standalone_group_product(db: Session, product: schemas.ProductCreate):
if product.itemType != "standalone_group":
raise HTTPException(status_code=400, detail="Item type must be 'standalone_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,
spicyOut
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 # Reference to parent 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)
db.commit()
return db_product
def update_standalone_group_product(db: Session, product_id: int, product: schemas.ProductCreate):
if product.itemType != "standalone_group":
raise HTTPException(status_code=400, detail="Item type must be 'standalone_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 variants and their 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,
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=product_id # Reference to parent 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)
db.commit()
db.refresh(db_product)
return db_product
Editor is loading...
Leave a Comment