feat: 英文名自动Title Case + 自动翻译未翻译配方
All checks were successful
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 4s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 16s
Test / e2e-test (push) Successful in 48s

- 所有 en_name 写入时自动 Title Case(pain relief → Pain Relief)
- 新建/采纳配方到公共库时自动生成英文名
- 启动时自动补全未翻译的配方英文名
- 新增 translate.py 关键词翻译字典(100+ 中医/美容/精油词条)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 09:50:41 +00:00
parent 1ade1c0eaa
commit 1af9e02e92
2 changed files with 137 additions and 4 deletions

View File

@@ -6,12 +6,18 @@ import json
import os
from backend.database import get_db, init_db, seed_defaults, log_audit
from backend.translate import auto_translate
import hashlib
import secrets as _secrets
app = FastAPI(title="Essential Oil Formula Calculator API")
def title_case(s: str) -> str:
"""Convert to title case: 'pain relief''Pain Relief'"""
return s.strip().title() if s else s
# ── Password hashing (PBKDF2-SHA256, stdlib) ─────────
def hash_password(password: str) -> str:
salt = _secrets.token_hex(16)
@@ -693,7 +699,7 @@ def upsert_oil(oil: OilIn, user=Depends(require_role("admin", "senior_editor")))
"ON CONFLICT(name) DO UPDATE SET bottle_price=excluded.bottle_price, drop_count=excluded.drop_count, "
"retail_price=excluded.retail_price, en_name=COALESCE(excluded.en_name, oils.en_name), "
"is_active=COALESCE(excluded.is_active, oils.is_active)",
(oil.name, oil.bottle_price, oil.drop_count, oil.retail_price, oil.en_name, oil.is_active),
(oil.name, oil.bottle_price, oil.drop_count, oil.retail_price, title_case(oil.en_name) if oil.en_name else oil.en_name, oil.is_active),
)
log_audit(conn, user["id"], "upsert_oil", "oil", oil.name, oil.name,
json.dumps({"bottle_price": oil.bottle_price, "drop_count": oil.drop_count}))
@@ -789,8 +795,9 @@ def create_recipe(recipe: RecipeIn, user=Depends(get_current_user)):
admin = c.execute("SELECT id FROM users WHERE role = 'admin' LIMIT 1").fetchone()
if admin:
owner_id = admin["id"]
c.execute("INSERT INTO recipes (name, note, owner_id) VALUES (?, ?, ?)",
(recipe.name, recipe.note, owner_id))
en_name = title_case(recipe.en_name) if recipe.en_name else auto_translate(recipe.name)
c.execute("INSERT INTO recipes (name, note, owner_id, en_name) VALUES (?, ?, ?, ?)",
(recipe.name, recipe.note, owner_id, en_name))
rid = c.lastrowid
for ing in recipe.ingredients:
c.execute(
@@ -853,7 +860,7 @@ def update_recipe(recipe_id: int, update: RecipeUpdate, user=Depends(get_current
if update.note is not None:
c.execute("UPDATE recipes SET note = ? WHERE id = ?", (update.note, recipe_id))
if update.en_name is not None:
c.execute("UPDATE recipes SET en_name = ? WHERE id = ?", (update.en_name, recipe_id))
c.execute("UPDATE recipes SET en_name = ? WHERE id = ?", (title_case(update.en_name), recipe_id))
if update.ingredients is not None:
c.execute("DELETE FROM recipe_ingredients WHERE recipe_id = ?", (recipe_id,))
for ing in update.ingredients:
@@ -916,6 +923,10 @@ def adopt_recipe(recipe_id: int, user=Depends(require_role("admin"))):
return {"ok": True, "msg": "already owned"}
old_owner = conn.execute("SELECT id, role, display_name, username FROM users WHERE id = ?", (row["owner_id"],)).fetchone()
old_name = (old_owner["display_name"] or old_owner["username"]) if old_owner else "unknown"
# Auto-fill en_name if missing
existing_en = conn.execute("SELECT en_name FROM recipes WHERE id = ?", (recipe_id,)).fetchone()
if not existing_en["en_name"]:
conn.execute("UPDATE recipes SET en_name = ? WHERE id = ?", (auto_translate(row["name"]), recipe_id))
conn.execute("UPDATE recipes SET owner_id = ?, updated_by = ? WHERE id = ?", (user["id"], user["id"], recipe_id))
log_audit(conn, user["id"], "adopt_recipe", "recipe", recipe_id, row["name"],
json.dumps({"from_user": old_name}))
@@ -1811,6 +1822,16 @@ def startup():
data = json.load(f)
seed_defaults(data["oils_meta"], data["recipes"])
# Auto-fill missing en_name for existing recipes
conn = get_db()
missing = conn.execute("SELECT id, name FROM recipes WHERE en_name IS NULL OR en_name = ''").fetchall()
for row in missing:
conn.execute("UPDATE recipes SET en_name = ? WHERE id = ?", (auto_translate(row["name"]), row["id"]))
if missing:
conn.commit()
print(f"[INIT] Auto-translated {len(missing)} recipe names to English")
conn.close()
if os.path.isdir(FRONTEND_DIR):
# Serve static assets (js/css/images) directly
app.mount("/assets", StaticFiles(directory=os.path.join(FRONTEND_DIR, "assets")), name="assets")