99 lines
4.3 KiB
Python
99 lines
4.3 KiB
Python
"""初始化码表:城市(GB/T 2260 三级)+ 12 星座。
|
||
|
||
用法(在 server/ 下):
|
||
../.venv/bin/python scripts/seed_catalog.py # 只补缺
|
||
../.venv/bin/python scripts/seed_catalog.py --force # 覆盖为内置值
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import asyncio
|
||
import json
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
|
||
from sqlalchemy import select # noqa: E402
|
||
|
||
from app.core.config import settings # noqa: E402
|
||
from app.db.session import SessionLocal, dispose_engine # noqa: E402
|
||
from app.models import Region, ZodiacSign # noqa: E402
|
||
|
||
DATA = settings.data_dir / "regions.json"
|
||
|
||
ZODIACS = [
|
||
("aries", "白羊座", "Aries", "♈", "火", 3, 21, 4, 19, "直接、行动力强,喜欢把想法立刻变成行动。"),
|
||
("taurus", "金牛座", "Taurus", "♉", "土", 4, 20, 5, 20, "稳定、务实,重视感官体验与确定感。"),
|
||
("gemini", "双子座", "Gemini", "♊", "风", 5, 21, 6, 21, "好奇、灵活,靠信息和交流理解世界。"),
|
||
("cancer", "巨蟹座", "Cancer", "♋", "水", 6, 22, 7, 22, "敏感、护家,用情感记忆把人和人连起来。"),
|
||
("leo", "狮子座", "Leo", "♌", "火", 7, 23, 8, 22, "热情、慷慨,需要被看见也愿意照亮别人。"),
|
||
("virgo", "处女座", "Virgo", "♍", "土", 8, 23, 9, 22, "细致、讲方法,习惯把混乱整理成秩序。"),
|
||
("libra", "天秤座", "Libra", "♎", "风", 9, 23, 10, 23, "讲平衡、重关系,天生在找那个刚刚好的点。"),
|
||
("scorpio", "天蝎座", "Scorpio", "♏", "水", 10, 24, 11, 22, "专注、深刻,一旦投入就要看到底。"),
|
||
("sagittarius", "射手座", "Sagittarius", "♐", "火", 11, 23, 12, 21, "乐观、向外,永远在去更远处的路上。"),
|
||
("capricorn", "摩羯座", "Capricorn", "♑", "土", 12, 22, 1, 19, "克制、有耐心,用时间换成果。"),
|
||
("aquarius", "水瓶座", "Aquarius", "♒", "风", 1, 20, 2, 18, "独立、理性,习惯站在局外看规则。"),
|
||
("pisces", "双鱼座", "Pisces", "♓", "水", 2, 19, 3, 20, "共情、想象丰富,容易接收别人的情绪。"),
|
||
]
|
||
|
||
|
||
async def seed_regions(force: bool) -> tuple[int, int]:
|
||
rows = json.loads(DATA.read_text("utf-8"))
|
||
added = updated = 0
|
||
async with SessionLocal() as db:
|
||
existing = {r.code: r for r in (await db.execute(select(Region))).scalars().all()}
|
||
for row in rows:
|
||
cur = existing.get(row["code"])
|
||
if cur is None:
|
||
db.add(Region(**row))
|
||
added += 1
|
||
elif force:
|
||
cur.name = row["name"]
|
||
cur.level = row["level"]
|
||
cur.parent_code = row["parent_code"]
|
||
cur.timezone = row["timezone"]
|
||
cur.sort_order = row["sort_order"]
|
||
updated += 1
|
||
await db.commit()
|
||
return added, updated
|
||
|
||
|
||
async def seed_zodiacs(force: bool) -> tuple[int, int]:
|
||
added = updated = 0
|
||
async with SessionLocal() as db:
|
||
existing = {z.code: z for z in (await db.execute(select(ZodiacSign))).scalars().all()}
|
||
for order, (code, cn, en, sym, el, sm, sd, em, ed, intro) in enumerate(ZODIACS):
|
||
cur = existing.get(code)
|
||
if cur is None:
|
||
db.add(ZodiacSign(
|
||
code=code, name_cn=cn, name_en=en, symbol=sym, element=el,
|
||
start_month=sm, start_day=sd, end_month=em, end_day=ed,
|
||
intro=intro, sort_order=order, enabled=True,
|
||
))
|
||
added += 1
|
||
elif force:
|
||
cur.name_cn, cur.name_en, cur.symbol, cur.element = cn, en, sym, el
|
||
cur.start_month, cur.start_day, cur.end_month, cur.end_day = sm, sd, em, ed
|
||
cur.intro, cur.sort_order = intro, order
|
||
updated += 1
|
||
await db.commit()
|
||
return added, updated
|
||
|
||
|
||
async def main() -> None:
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--force", action="store_true", help="覆盖已有记录的展示字段")
|
||
args = ap.parse_args()
|
||
|
||
a1, u1 = await seed_regions(args.force)
|
||
a2, u2 = await seed_zodiacs(args.force)
|
||
print(f"regions: +{a1} ~{u1}")
|
||
print(f"zodiacs: +{a2} ~{u2}")
|
||
await dispose_engine()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|