# retoor import logging from molodetz import config from molodetz.content import LEGACY_SEED_TITLES, seed_placeholder_posts, translate_seeded_posts from molodetz.database import count_users, create_user, db, get_setting, get_user_by_username, set_setting from molodetz.gallery import sync_gallery from molodetz.utils.audit import record_system from molodetz.utils.auth import hash_password logger = logging.getLogger(__name__) ADMIN_BIO = "Starts the roll. Writes about the work, finishing it, and judgement." LEGACY_ADMIN_BIO = "Begint de rol. Schrijft over het werk, het afmaken en het oordeel." LEGACY_SETTINGS = { "site_tagline": ("De kring, niet de reclame.", "The circle, not the commercial."), "maintenance_message": ("Molodetz is even dicht voor onderhoud.", "Molodetz is closed for maintenance. Back shortly."), } LEGACY_NOTIFICATION_SUFFIXES = ((" wil meeschrijven", " wants to write along"),) LEGACY_NOTIFICATION_EXACT = {"Je API-sleutel is vernieuwd.": "Your API key was renewed."} def bootstrap_admin(): if count_users() > 0: return get_user_by_username(config.ADMIN_USERNAME) if not config.ADMIN_PASSWORD: logger.warning("no users and MOLODETZ_ADMIN_PASSWORD unset; skipping admin bootstrap") return None user = create_user(config.ADMIN_USERNAME, config.ADMIN_EMAIL, hash_password(config.ADMIN_PASSWORD), bio=ADMIN_BIO) record_system("admin.bootstrap", payload={"username": user["username"]}, targets=[("user", user["uid"])]) return user def bootstrap_content(): admin = bootstrap_admin() migrate_to_english() if admin and get_setting("seed_posts_done", "0") != "1": created = seed_placeholder_posts(admin) set_setting("seed_posts_done", "1") record_system("post.seed", payload={"count": len(created)}) synced = sync_gallery() logger.info("gallery synced: %d items", synced) def _translate_notification(message): if message in LEGACY_NOTIFICATION_EXACT: return LEGACY_NOTIFICATION_EXACT[message] if message.startswith("Gepubliceerd: "): title = message[len("Gepubliceerd: "):] return "Published: " + LEGACY_SEED_TITLES.get(title, title) if message.startswith("Back-up "): return "Backup " + message[len("Back-up "):].replace(" klaar", " finished").replace(" mislukt", " failed") for dutch, english in LEGACY_NOTIFICATION_SUFFIXES: if message.endswith(dutch): return message[: -len(dutch)] + english return message def migrate_to_english(): if get_setting("content_language", "nl") == "en": return 0 posts = translate_seeded_posts() for key, (dutch, english) in LEGACY_SETTINGS.items(): if get_setting(key) == dutch or ("site_settings" in db.tables and (db["site_settings"].find_one(key=key) or {}).get("value") == dutch): set_setting(key, english) notes = 0 if "notifications" in db.tables: for row in list(db["notifications"].all()): translated = _translate_notification(row.get("message") or "") if translated != row.get("message"): db["notifications"].update({"uid": row["uid"], "message": translated}, ["uid"]) notes += 1 if "users" in db.tables: for row in list(db["users"].find(bio=LEGACY_ADMIN_BIO)): db["users"].update({"uid": row["uid"], "bio": ADMIN_BIO}, ["uid"]) set_setting("content_language", "en") record_system("system.migrate.english", payload={"posts": posts, "notifications": notes}) logger.info("content migrated to english: %d posts, %d notifications", posts, notes) return posts