crud.py
unknown
python
a year ago
8.0 kB
3
Indexable
from sqlalchemy.orm import Session from fastapi import HTTPException from . import models, schemas def create_standalone_configurable_product(db: Session, product: schemas.ProductCreate): if product.itemType != "standalone_configurable": raise HTTPException(status_code=400, detail="Item type must be 'standalone_configurable'") if product.steps: raise HTTPException(status_code=400, detail="Standalone configurable product cannot have steps") 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) db.commit() return db_product def update_standalone_configurable_product(db: Session, product_id: int, product: schemas.ProductCreate): if product.itemType != "standalone_configurable": raise HTTPException(status_code=400, detail="Item type must be 'standalone_configurable'") if product.steps: raise HTTPException(status_code=400, detail="Standalone configurable product cannot have steps") 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", "variants"]: 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) db.commit() db.refresh(db_product) return db_product
Editor is loading...
Leave a Comment