forked from retoor/devplacepy
feat: add news module with admin panel, pagination, and service toggle via env flag
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from devplacepy.database import get_table, db
|
||||
from devplacepy.templating import templates
|
||||
from devplacepy.utils import get_current_user, time_ago
|
||||
from devplacepy.seo import base_seo_context, website_schema, site_url
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
NEWS_MAX_AGE_DAYS = 4
|
||||
|
||||
|
||||
@router.get("", response_class=HTMLResponse)
|
||||
async def news_page(request: Request):
|
||||
user = get_current_user(request)
|
||||
cutoff = (datetime.utcnow() - timedelta(days=NEWS_MAX_AGE_DAYS)).isoformat()
|
||||
|
||||
news_table = get_table("news")
|
||||
articles = list(news_table.find(
|
||||
status="published",
|
||||
synced_at={">=": cutoff},
|
||||
order_by=["-grade", "-synced_at"],
|
||||
))
|
||||
|
||||
article_uids = [a["uid"] for a in articles]
|
||||
|
||||
images_by_news = {}
|
||||
if article_uids and "news_images" in db.tables:
|
||||
images_table = get_table("news_images")
|
||||
for uid in article_uids:
|
||||
img = images_table.find_one(news_uid=uid, order_by=["uid"])
|
||||
if img:
|
||||
images_by_news[uid] = img["url"]
|
||||
|
||||
enriched = []
|
||||
for a in articles:
|
||||
enriched.append({
|
||||
"article": a,
|
||||
"time_ago": time_ago(a["synced_at"]),
|
||||
"image_url": images_by_news.get(a["uid"]),
|
||||
"grade": a.get("grade", 0),
|
||||
})
|
||||
|
||||
base = site_url(request)
|
||||
seo_ctx = base_seo_context(
|
||||
request,
|
||||
title="Developer News",
|
||||
description="Curated developer news and industry signals. Stay ahead with hand-picked articles.",
|
||||
breadcrumbs=[{"name": "News", "url": "/news"}],
|
||||
schemas=[website_schema(base)],
|
||||
)
|
||||
|
||||
return templates.TemplateResponse("news.html", {
|
||||
**seo_ctx,
|
||||
"request": request,
|
||||
"user": user,
|
||||
"articles": enriched,
|
||||
})
|
||||
Reference in New Issue
Block a user