fix: normalize unicode escape sequences and reformat multi-line expressions across codebase

This commit is contained in:
2026-06-09 16:48:08 +00:00
parent 66dfda88bc
commit c4f2937415
175 changed files with 12660 additions and 4175 deletions
+195 -49
View File
@@ -10,14 +10,58 @@ from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.exceptions import RequestValidationError
from devplacepy.config import STATIC_DIR, PORT, SERVICE_LOCK_FILE, INIT_LOCK_FILE
from devplacepy.database import init_db, get_table, db, refresh_snapshot, get_users_by_uids, get_comment_counts_by_post_uids, get_vote_counts, get_news_images_by_uids, get_setting, get_int_setting
from devplacepy.database import (
init_db,
get_table,
db,
refresh_snapshot,
get_users_by_uids,
get_comment_counts_by_post_uids,
get_vote_counts,
get_news_images_by_uids,
get_setting,
get_int_setting,
)
from devplacepy.templating import templates
from devplacepy.responses import wants_json, json_error
from devplacepy.schemas import ValidationErrorOut
from fastapi.responses import JSONResponse
from devplacepy.utils import get_current_user, time_ago
from devplacepy.seo import base_seo_context, site_url, website_schema
from devplacepy.routers import auth, feed, posts, comments, projects, project_files, profile, messages, notifications, votes, avatar, follow, admin, seo, bugs, news, gists, services as services_router, uploads, push, leaderboard, reactions, bookmarks, polls, docs, openai_gateway, devii, zips, forks, containers, containers_admin, proxy
from devplacepy.routers import (
auth,
feed,
posts,
comments,
projects,
project_files,
profile,
messages,
notifications,
votes,
avatar,
follow,
admin,
seo,
bugs,
news,
gists,
services as services_router,
uploads,
push,
leaderboard,
reactions,
bookmarks,
polls,
docs,
openai_gateway,
devii,
zips,
forks,
containers,
containers_admin,
proxy,
)
from devplacepy.services.manager import service_manager
from devplacepy.services.news import NewsService
from devplacepy.services.bot import BotsService
@@ -68,22 +112,47 @@ def acquire_service_lock() -> bool:
_service_lock_handle = handle
return True
INLINE_MEDIA_EXTENSIONS = {
".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tiff",
".mp4", ".webm", ".ogv", ".mov", ".m4v", ".mp3",
".jpg",
".jpeg",
".png",
".gif",
".webp",
".bmp",
".tiff",
".mp4",
".webm",
".ogv",
".mov",
".m4v",
".mp3",
}
class UploadStaticFiles(StaticFiles):
async def get_response(self, path, scope):
response = await super().get_response(path, scope)
disposition = "inline" if Path(path).suffix.lower() in INLINE_MEDIA_EXTENSIONS else "attachment"
disposition = (
"inline"
if Path(path).suffix.lower() in INLINE_MEDIA_EXTENSIONS
else "attachment"
)
response.headers["Content-Disposition"] = disposition
return response
app = FastAPI(title="DevPlace", docs_url="/swagger", redoc_url="/redoc", openapi_url="/openapi.json")
app.mount("/static/uploads", UploadStaticFiles(directory=str(STATIC_DIR / "uploads"), check_dir=False), name="uploads")
app = FastAPI(
title="DevPlace",
docs_url="/swagger",
redoc_url="/redoc",
openapi_url="/openapi.json",
)
app.mount(
"/static/uploads",
UploadStaticFiles(directory=str(STATIC_DIR / "uploads"), check_dir=False),
name="uploads",
)
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
@@ -91,8 +160,23 @@ app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
async def not_found(request: Request, exc):
if wants_json(request):
return json_error(404, "Not found")
seo_ctx = base_seo_context(request, title="Not Found - DevPlace", description="The page you requested does not exist.", robots="noindex")
return templates.TemplateResponse(request, "error.html", {**seo_ctx, "request": request, "error_code": 404, "error_message": "Page not found"}, status_code=404)
seo_ctx = base_seo_context(
request,
title="Not Found - DevPlace",
description="The page you requested does not exist.",
robots="noindex",
)
return templates.TemplateResponse(
request,
"error.html",
{
**seo_ctx,
"request": request,
"error_code": 404,
"error_message": "Page not found",
},
status_code=404,
)
@app.exception_handler(500)
@@ -100,8 +184,23 @@ async def server_error(request: Request, exc):
logger.exception("500 error on %s %s", request.method, request.url.path)
if wants_json(request):
return json_error(500, "Internal server error")
seo_ctx = base_seo_context(request, title="Server Error - DevPlace", description="Something went wrong.", robots="noindex")
return templates.TemplateResponse(request, "error.html", {**seo_ctx, "request": request, "error_code": 500, "error_message": "Internal server error"}, status_code=500)
seo_ctx = base_seo_context(
request,
title="Server Error - DevPlace",
description="Something went wrong.",
robots="noindex",
)
return templates.TemplateResponse(
request,
"error.html",
{
**seo_ctx,
"request": request,
"error_code": 500,
"error_message": "Internal server error",
},
status_code=500,
)
_AUTH_FORM_PAGES = {
@@ -124,7 +223,7 @@ def _friendly_error(err):
return _FRIENDLY_ERRORS[key]
msg = err.get("msg", "Invalid input")
prefix = "Value error, "
return msg[len(prefix):] if msg.startswith(prefix) else msg
return msg[len(prefix) :] if msg.startswith(prefix) else msg
@app.exception_handler(RequestValidationError)
@@ -135,14 +234,21 @@ async def on_validation_error(request: Request, exc: RequestValidationError):
for raw, friendly in zip(exc.errors(), errors):
name = raw["loc"][-1] if raw.get("loc") else "_"
fields.setdefault(str(name), []).append(friendly)
return JSONResponse(ValidationErrorOut(fields=fields, messages=errors).model_dump(mode="json"), status_code=422)
return JSONResponse(
ValidationErrorOut(fields=fields, messages=errors).model_dump(mode="json"),
status_code=422,
)
path = request.url.path
page = _AUTH_FORM_PAGES.get(path)
if page is None and path.startswith("/auth/reset-password/"):
page = ("reset_password.html", "Set New Password")
if page:
template_name, title = page
context = {**base_seo_context(request, title=title, robots="noindex,nofollow"), "request": request, "errors": errors}
context = {
**base_seo_context(request, title=title, robots="noindex,nofollow"),
"request": request,
"errors": errors,
}
try:
form = await request.form()
context.update({k: v for k, v in form.items() if isinstance(v, str)})
@@ -150,10 +256,13 @@ async def on_validation_error(request: Request, exc: RequestValidationError):
pass
if "token" in request.path_params:
context["token"] = request.path_params["token"]
return templates.TemplateResponse(request, template_name, context, status_code=400)
return templates.TemplateResponse(
request, template_name, context, status_code=400
)
referer = request.headers.get("referer") or "/feed"
return RedirectResponse(url=referer, status_code=303)
app.include_router(auth.router, prefix="/auth")
app.include_router(feed.router, prefix="/feed")
app.include_router(posts.router, prefix="/posts")
@@ -205,10 +314,19 @@ async def add_security_headers(request: Request, call_next):
@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
if request.method in ("POST", "PUT", "DELETE", "PATCH") and not request.url.path.startswith("/openai"):
limit = _worker_rate_limit(max(1, get_int_setting("rate_limit_per_minute", RATE_LIMIT)))
if request.method in (
"POST",
"PUT",
"DELETE",
"PATCH",
) and not request.url.path.startswith("/openai"):
limit = _worker_rate_limit(
max(1, get_int_setting("rate_limit_per_minute", RATE_LIMIT))
)
window = max(1, get_int_setting("rate_limit_window_seconds", RATE_WINDOW))
ip = request.headers.get("X-Real-IP") or (request.client.host if request.client else "unknown")
ip = request.headers.get("X-Real-IP") or (
request.client.host if request.client else "unknown"
)
now = time.time()
window_start = now - window
_rate_limit_store[ip] = [t for t in _rate_limit_store[ip] if t > window_start]
@@ -218,7 +336,11 @@ async def rate_limit_middleware(request: Request, call_next):
response = json_error(429, "Rate limit exceeded. Try again later.")
response.headers["Retry-After"] = str(window)
return response
return HTMLResponse("Rate limit exceeded. Try again later.", status_code=429, headers=retry_after)
return HTMLResponse(
"Rate limit exceeded. Try again later.",
status_code=429,
headers=retry_after,
)
_rate_limit_store[ip].append(now)
return await call_next(request)
@@ -235,11 +357,21 @@ async def maintenance_middleware(request: Request, call_next):
user = get_current_user(request)
if user and user.get("role") == "Admin":
return await call_next(request)
message = get_setting("maintenance_message", "DevPlace is undergoing scheduled maintenance. Please check back shortly.")
message = get_setting(
"maintenance_message",
"DevPlace is undergoing scheduled maintenance. Please check back shortly.",
)
if wants_json(request):
return json_error(503, message)
seo_ctx = base_seo_context(request, title="Maintenance - DevPlace", description=message, robots="noindex")
return templates.TemplateResponse(request, "error.html", {**seo_ctx, "request": request, "error_code": 503, "error_message": message}, status_code=503)
seo_ctx = base_seo_context(
request, title="Maintenance - DevPlace", description=message, robots="noindex"
)
return templates.TemplateResponse(
request,
"error.html",
{**seo_ctx, "request": request, "error_code": 503, "error_message": message},
status_code=503,
)
@app.on_event("startup")
@@ -247,6 +379,7 @@ async def startup():
with init_lock():
init_db()
from devplacepy.push import ensure_certificates
ensure_certificates()
service_manager.register(NewsService())
service_manager.register(BotsService())
@@ -261,7 +394,9 @@ async def startup():
service_manager.supervise()
logger.info(f"Background services started in worker pid {os.getpid()}")
else:
logger.info(f"Worker pid {os.getpid()} declined service lock; another worker owns background services")
logger.info(
f"Worker pid {os.getpid()} declined service lock; another worker owns background services"
)
logger.info(f"DevPlace started on port {PORT}")
@@ -280,21 +415,25 @@ async def landing(request: Request):
landing_articles = []
if "news" in db.tables:
news_table = get_table("news")
raw = list(news_table.find(show_on_landing=1, order_by=["-synced_at"], _limit=6))
raw = list(
news_table.find(show_on_landing=1, order_by=["-synced_at"], _limit=6)
)
images_by_news = get_news_images_by_uids([a["uid"] for a in raw])
for a in raw:
landing_articles.append({
"uid": a["uid"],
"slug": a.get("slug", ""),
"title": a.get("title", ""),
"description": (a.get("description", "") or "")[:250],
"url": a.get("url", ""),
"source_name": a.get("source_name", ""),
"grade": a.get("grade", 0),
"synced_at": a.get("synced_at", "") or "",
"time_ago": time_ago(a["synced_at"]),
"image_url": images_by_news.get(a["uid"], ""),
})
landing_articles.append(
{
"uid": a["uid"],
"slug": a.get("slug", ""),
"title": a.get("title", ""),
"description": (a.get("description", "") or "")[:250],
"url": a.get("url", ""),
"source_name": a.get("source_name", ""),
"grade": a.get("grade", 0),
"synced_at": a.get("synced_at", "") or "",
"time_ago": time_ago(a["synced_at"]),
"image_url": images_by_news.get(a["uid"], ""),
}
)
landing_posts = []
if "posts" in db.tables:
@@ -307,14 +446,16 @@ async def landing(request: Request):
comment_counts = get_comment_counts_by_post_uids(post_uids)
upvotes, downvotes = get_vote_counts(post_uids)
for p in raw_posts:
landing_posts.append({
"post": p,
"author": authors.get(p["user_uid"]),
"time_ago": time_ago(p["created_at"]),
"comment_count": comment_counts.get(p["uid"], 0),
"stars": upvotes.get(p["uid"], 0) - downvotes.get(p["uid"], 0),
"slug": p.get("slug", "") or p["uid"],
})
landing_posts.append(
{
"post": p,
"author": authors.get(p["user_uid"]),
"time_ago": time_ago(p["created_at"]),
"comment_count": comment_counts.get(p["uid"], 0),
"stars": upvotes.get(p["uid"], 0) - downvotes.get(p["uid"], 0),
"slug": p.get("slug", "") or p["uid"],
}
)
base = site_url(request)
seo_ctx = base_seo_context(
@@ -324,8 +465,13 @@ async def landing(request: Request):
breadcrumbs=[],
schemas=[website_schema(base)],
)
return templates.TemplateResponse(request, "landing.html", {
**seo_ctx, "request": request,
"landing_articles": landing_articles,
"landing_posts": landing_posts,
})
return templates.TemplateResponse(
request,
"landing.html",
{
**seo_ctx,
"request": request,
"landing_articles": landing_articles,
"landing_posts": landing_posts,
},
)