feat: implement Phase 2 (Pulse, Admin, RC2)
This commit is contained in:
130
app/utils/seed_articles.py
Normal file
130
app/utils/seed_articles.py
Normal file
@@ -0,0 +1,130 @@
|
||||
import os
|
||||
import json
|
||||
import pymysql
|
||||
from pymysql.cursors import DictCursor
|
||||
|
||||
# DB Connection Config
|
||||
DB_HOST = os.getenv("MARIADB_HOST", "db")
|
||||
DB_USER = os.getenv("MARIADB_USER", "crumb")
|
||||
DB_PASS = os.getenv("MARIADB_PASSWORD", "secret")
|
||||
DB_NAME = os.getenv("MARIADB_DATABASE", "crumbcrm")
|
||||
|
||||
def get_db():
|
||||
print(f"🔌 Connecting to {DB_HOST}...")
|
||||
return pymysql.connect(
|
||||
host=DB_HOST,
|
||||
user=DB_USER,
|
||||
password=DB_PASS,
|
||||
database=DB_NAME,
|
||||
autocommit=True,
|
||||
charset="utf8mb4",
|
||||
cursorclass=DictCursor,
|
||||
)
|
||||
|
||||
DUMMY_POSTS = [
|
||||
{
|
||||
"title": "Was ist RAG? 🤖",
|
||||
"slug": "was-ist-rag",
|
||||
"locale": "de",
|
||||
"excerpt": "Retrieval Augmented Generation einfach erklärt. Warum unser Wald ein Gedächtnis braucht.",
|
||||
"tags": ["RAG", "Tech", "Grundlagen"],
|
||||
"author": "System",
|
||||
"body_md": """# Was ist RAG?
|
||||
|
||||
RAG steht für **Retrieval Augmented Generation**.
|
||||
|
||||
Stell dir vor, du schreibst eine Prüfung.
|
||||
* **Ohne RAG**: Du musst alles auswendig wissen (wie ein LLM, das nur sein Training kennt).
|
||||
* **Mit RAG**: Du darfst in einem offenen Lehrbuch nachschlagen.
|
||||
|
||||
## Im Crumbforest
|
||||
Unser "Lehrbuch" ist die Vektordatenbank (Qdrant).
|
||||
Wenn du fragst *"Was steht im Tagebuch von Schnippsi?"*, suchen wir erst die passenden Seiten und geben sie der KI.
|
||||
|
||||
So halluziniert die KI weniger und kennt deine privaten Daten!"""
|
||||
},
|
||||
{
|
||||
"title": "Die Eule erklärt: JSON in MariaDB 🦉",
|
||||
"slug": "eule-json-mariadb",
|
||||
"locale": "de",
|
||||
"excerpt": "Wussten Sie, dass SQL und NoSQL Freunde sein können? Professor Eule klärt auf.",
|
||||
"tags": ["Database", "SQL", "Eule"],
|
||||
"author": "Professor Eule",
|
||||
"body_md": """# JSON Spalten in SQL
|
||||
|
||||
Huhu! 🦉
|
||||
|
||||
Viele denken, man muss sich entscheiden: SQL (strikt) oder NoSQL (flexibel).
|
||||
Aber **MariaDB** erlaubt uns beides!
|
||||
|
||||
Wir speichern Tags einfach als JSON:
|
||||
```json
|
||||
["RAG", "SQL", "Eule"]
|
||||
```
|
||||
|
||||
## Warum?
|
||||
* Es ist schnell.
|
||||
* Wir brauchen keine extra `tags` Tabelle mit komplizierten JOINS für einfache Listen.
|
||||
* Wir können trotzdem darauf suchen: `JSON_CONTAINS(tags, '"Eule"')`.
|
||||
|
||||
Clever, oder? Hu-hu!"""
|
||||
},
|
||||
{
|
||||
"title": "Neu im Wald: Der Blog ist da! 📰",
|
||||
"slug": "neu-im-wald-blog",
|
||||
"locale": "de",
|
||||
"excerpt": "Endlich können wir Neuigkeiten teilen. Willkommen im 'Pulse' Bereich.",
|
||||
"tags": ["News", "Community", "Update"],
|
||||
"author": "Krümel",
|
||||
"body_md": """# Willkommen beim Pulse
|
||||
|
||||
Das ist der neue Blog-Bereich ("Pulse").
|
||||
Hier landen:
|
||||
* Updates zum System
|
||||
* Tutorials
|
||||
* Gedanken aus dem Wald
|
||||
|
||||
## Tech-Stack
|
||||
Diese Seite nutzt:
|
||||
* **Jinja2** für HTML
|
||||
* **PicoCSS** für das Design
|
||||
* **MariaDB** als Content Store
|
||||
|
||||
Viel Spaß beim Lesen!"""
|
||||
}
|
||||
]
|
||||
|
||||
def seed():
|
||||
conn = get_db()
|
||||
with conn.cursor() as cur:
|
||||
print("🌱 Seeding Posts...")
|
||||
for post in DUMMY_POSTS:
|
||||
# Check exist
|
||||
cur.execute("SELECT id FROM posts WHERE slug=%s AND locale=%s", (post['slug'], post['locale']))
|
||||
if cur.fetchone():
|
||||
print(f" ⚠️ Skipping '{post['title']}' (already exists)")
|
||||
continue
|
||||
|
||||
# Insert
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO posts (title, slug, locale, excerpt, tags, author, body_md, is_published)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, 1)
|
||||
""",
|
||||
(
|
||||
post['title'],
|
||||
post['slug'],
|
||||
post['locale'],
|
||||
post['excerpt'],
|
||||
json.dumps(post['tags']),
|
||||
post['author'],
|
||||
post['body_md']
|
||||
)
|
||||
)
|
||||
print(f" ✅ Created '{post['title']}'")
|
||||
|
||||
conn.close()
|
||||
print("✨ Seeding complete.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
seed()
|
||||
Reference in New Issue
Block a user