feat: add campaign model and API integration with price and tags
This commit is contained in:
@@ -81,6 +81,16 @@ SITE_CONFIG_MODULES = {
|
||||
"none_to_empty": {"image"},
|
||||
"create_defaults": {"image": ""},
|
||||
},
|
||||
"campaigns": {
|
||||
"model": Campaign,
|
||||
"entity": "campaign",
|
||||
"primary": "title",
|
||||
"fields": {"title", "slug", "description", "coverImage", "priceAmount", "priceUnit", "tags", "status", "startsAt", "endsAt"},
|
||||
"none_to_empty": set(),
|
||||
"empty_to_none": {"description", "coverImage", "priceUnit", "startsAt", "endsAt"},
|
||||
"create_defaults": {"description": None, "coverImage": None, "priceAmount": None, "priceUnit": "起/人", "tags": [], "status": "draft", "startsAt": None, "endsAt": None},
|
||||
"ordered": False,
|
||||
},
|
||||
"ctaBanners": {
|
||||
"model": CtaBanner,
|
||||
"entity": "cta_banner",
|
||||
@@ -91,6 +101,8 @@ SITE_CONFIG_MODULES = {
|
||||
},
|
||||
}
|
||||
|
||||
CAMPAIGN_STATUSES = {"draft", "published"}
|
||||
|
||||
|
||||
def media_error(status_code: int, message: str, code: str, details: dict | None = None) -> None:
|
||||
raise HTTPException(status_code=status_code, detail={"message": message, "code": code, "details": details or {}})
|
||||
@@ -280,13 +292,33 @@ def clean_site_value(value):
|
||||
return value.strip() if isinstance(value, str) else value
|
||||
|
||||
|
||||
def normalize_campaign_tags(value) -> list[str]:
|
||||
if value is None:
|
||||
return []
|
||||
tags = [item.strip() for item in value if isinstance(item, str) and item.strip()]
|
||||
if len(tags) > 3:
|
||||
site_config_error(422, "campaign tags cannot exceed 3", "MODULE_CONFIG_VALIDATION_ERROR", {"field": "tags", "max": 3})
|
||||
return tags
|
||||
|
||||
|
||||
def site_field_value(config: dict, field: str, value):
|
||||
if field == "tags":
|
||||
return normalize_campaign_tags(value)
|
||||
value = clean_site_value(value)
|
||||
if value == "" and field in config.get("empty_to_none", set()):
|
||||
return None
|
||||
if value is None and field in config["none_to_empty"]:
|
||||
return ""
|
||||
return value
|
||||
|
||||
|
||||
def validate_campaign_status(value):
|
||||
status_value = clean_site_value(value)
|
||||
if status_value not in CAMPAIGN_STATUSES:
|
||||
site_config_error(422, "status must be draft or published", "MODULE_CONFIG_VALIDATION_ERROR", {"field": "status"})
|
||||
return status_value
|
||||
|
||||
|
||||
def validate_site_primary(config: dict, body: SiteConfigPatchIn) -> str:
|
||||
primary = config["primary"]
|
||||
value = clean_site_value(getattr(body, primary))
|
||||
@@ -356,12 +388,21 @@ def site_create_payload(module: str, config: dict, body: SiteConfigPatchIn, db:
|
||||
payload[config["primary"]] = validate_site_primary(config, body)
|
||||
for field, value in config["create_defaults"].items():
|
||||
payload.setdefault(field, value)
|
||||
payload["isActive"] = payload.get("isActive", True)
|
||||
if "isActive" in config["fields"]:
|
||||
payload["isActive"] = payload.get("isActive", True)
|
||||
if config.get("ordered", True) and payload.get("sortOrder") is None:
|
||||
payload["sortOrder"] = next_site_sort_order(db, config["model"])
|
||||
if module == "destinations":
|
||||
slug = clean_site_value(payload.get("slug"))
|
||||
payload["slug"] = slug or site_slugify(payload["name"])
|
||||
if module == "campaigns":
|
||||
slug = clean_site_value(payload.get("slug"))
|
||||
if not slug:
|
||||
site_config_error(422, "required field is empty", "MODULE_CONFIG_VALIDATION_ERROR", {"field": "slug"})
|
||||
payload["slug"] = slug
|
||||
if not payload.get("priceUnit"):
|
||||
payload["priceUnit"] = "起/人"
|
||||
payload["status"] = validate_campaign_status(payload.get("status", "draft"))
|
||||
return payload
|
||||
|
||||
|
||||
@@ -375,6 +416,10 @@ def apply_site_patch(module: str, config: dict, item, body: SiteConfigPatchIn) -
|
||||
site_config_error(422, "必填字段不能为空", "MODULE_CONFIG_VALIDATION_ERROR", {"field": field})
|
||||
if module == "destinations" and field == "slug" and not value:
|
||||
site_config_error(422, "必填字段不能为空", "MODULE_CONFIG_VALIDATION_ERROR", {"field": field})
|
||||
if module == "campaigns" and field == "slug" and not value:
|
||||
site_config_error(422, "required field is empty", "MODULE_CONFIG_VALIDATION_ERROR", {"field": field})
|
||||
if module == "campaigns" and field == "status":
|
||||
value = validate_campaign_status(value)
|
||||
setattr(item, field, value)
|
||||
|
||||
|
||||
@@ -532,6 +577,10 @@ def admin_site_config(_user: AdminUser = Depends(require_admin), db: Session = D
|
||||
model_dict(item)
|
||||
for item in db.scalars(select(CtaBanner).order_by(CtaBanner.sortOrder.asc())).all()
|
||||
],
|
||||
"campaigns": [
|
||||
model_dict(item)
|
||||
for item in db.scalars(select(Campaign).order_by(Campaign.updatedAt.desc())).all()
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user