"""Seed demo content: authors, categories and published articles. Usage (inside the backend container or with the backend venv):: python seed_content.py Idempotent — safe to re-run. Existing authors (matched by email), categories (matched by slug) and articles (matched by title) are left untouched; only missing rows are created. The seeded dataset mirrors the front end's sample data (``frontend/src/data/sample.ts``) so the site has real content to serve until the admin area is used. """ from __future__ import annotations from datetime import datetime from db.session import SessionLocal from models import Article, Author, Category AUTHORS: list[dict] = [ { "name": "Ada Lovelace", "email": "ada@example.com", "bio": "Writes about technology and computation.", }, { "name": "Grace Hopper", "email": "grace@example.com", "bio": "Covers science and engineering.", }, { "name": "Alan Turing", "email": "alan@example.com", "bio": "Follows sports and culture.", }, ] CATEGORIES: list[dict] = [ {"name": "Technology", "slug": "technology"}, {"name": "Science", "slug": "science"}, {"name": "Sports", "slug": "sports"}, {"name": "Culture", "slug": "culture"}, ] # (title, body, author_email, category_slug, created_at, published) ARTICLES: list[tuple[str, str, str, str, str, bool]] = [ ( "Inside the new wave of open-source language models", 'Open-source language models are closing the gap with proprietary systems, ' "and the ecosystem around them is growing fast. Researchers now release " "pretrained weights alongside detailed training recipes, letting smaller " 'teams fine-tune models for narrow tasks that once required large budgets. ' 'The result is a new layer of the AI stack: community-maintained tooling ' 'for evaluation, deployment, and safety testing that moves at a pace big ' 'vendors struggle to match. As hardware costs keep falling, expect the line ' 'between "research model" and "production model" to blur further this year.', "ada@example.com", "technology", "2025-06-02T09:30:00Z", True, ), ( "Fusion milestone: net energy gain repeated for the third time", "Scientists have repeated a net-energy-gain fusion reaction for the third " "consecutive time, confirming that last months breakthrough was not a fluke. " "The latest run sustained the plasma for a record duration and produced " "energy output within a few percent of the original measurement. Engineers " "now say the remaining challenges are engineering, not physics: materials " "that survive repeated shots and power systems that deliver pulses on " "demand. A commercial demonstration plant could enter the design phase as " "early as next decade.", "grace@example.com", "science", "2025-05-28T14:10:00Z", True, ), ( "City marathon sets record turnout as first-timers flood the course", "More than 40,000 runners started this weekend city marathon, a record that " "organizers attributed to a wave of first-time participants. The 21-kilometer " "fun run sold out in under an hour, and volunteers reported the most positive " "start-line atmosphere in the events history. Pacing groups, music zones, and " "a free post-race breakfast kept the mood festive even in the rain. Officials " "are already planning capacity expansions for next year, with an eye on adding " "a night race.", "alan@example.com", "sports", "2025-05-25T08:00:00Z", True, ), ( "The quiet return of the neighborhood library", "After years of budget cuts, the neighborhood library is staging an unlikely " "comeback. Community volunteers now staff the Saturday makerspace, where 3D " "printing, sewing machines, and a small recording studio are booked weeks in " "advance. Librarians report that circulation is up, but the real story is " "programming: repair cafés, language tables, and civic workshops that have " "made the building a second living room for the district. City planners are " "taking note, and two more branches are slated for renovation this fall.", "ada@example.com", "culture", "2025-05-20T17:45:00Z", True, ), ( "Rust keeps climbing the systems programming charts", "Rust has moved up again in the annual systems programming survey, now the " "most-wanted language for kernel and infrastructure work among respondents. " "Adoption is no longer limited to startups: several large platforms report " "that new storage and networking services are written in Rust by default. The " "ecosystems memory-safety guarantees, combined with a mature async story, " "continue to draw engineers from C and C++. Tooling improvements in debugging " "and profiling have been the most cited reasons for teams crossing over this " "year.", "grace@example.com", "technology", "2025-05-15T11:20:00Z", True, ), ( "Ocean robots map the deepest trenches in record time", "A fleet of autonomous underwater vehicles has completed the most detailed " "survey of the ocean trench yet, mapping 80 percent of the basin floor in six " "weeks. The robots, equipped with multibeam sonar and high-resolution " "cameras, collected terabytes of data that reveal previously unknown " "seamounts and hydrothermal vent fields. Biologists have already flagged " "three candidate species from the first batch of imagery. The team plans to " "return next winter with sampling landers to study the vent communities up " "close.", "alan@example.com", "science", "2025-05-10T19:05:00Z", True, ), ] def main() -> int: db = SessionLocal() try: authors: dict[str, Author] = {} for spec in AUTHORS: existing = ( db.query(Author).filter(Author.email == spec["email"]).first() ) if existing is not None: authors[spec["email"]] = existing continue author = Author(**spec) db.add(author) db.flush() authors[spec["email"]] = author print(f"Created author '{spec['name']}'.") categories: dict[str, Category] = {} for spec in CATEGORIES: existing = ( db.query(Category).filter(Category.slug == spec["slug"]).first() ) if existing is not None: categories[spec["slug"]] = existing continue category = Category(**spec) db.add(category) db.flush() categories[spec["slug"]] = category print(f"Created category '{spec['name']}'.") for title, content, email, slug, created, published in ARTICLES: if db.query(Article).filter(Article.title == title).first() is not None: continue article = Article( title=title, content=content, author_id=authors[email].id, category_id=categories[slug].id, created_at=datetime.fromisoformat(created.replace("Z", "+00:00")), published=published, ) db.add(article) print(f"Created article '{title}'.") db.commit() print( "Done. " f"{db.query(Author).count()} authors, " f"{db.query(Category).count()} categories, " f"{db.query(Article).count()} articles." ) return 0 finally: db.close() if __name__ == "__main__": raise SystemExit(main())