82 lines
2.8 KiB
Python
Raw Normal View History

2026-05-11 07:02:06 +02:00
import logging
2026-05-14 04:12:19 +02:00
from datetime import datetime, timezone
2026-05-11 07:02:06 +02:00
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, RedirectResponse
2026-05-13 21:17:57 +02:00
from devplacepy.database import get_table, load_comments
from devplacepy.attachments import get_attachments_batch, link_attachments
2026-05-11 07:02:06 +02:00
from devplacepy.templating import templates
2026-05-12 13:08:38 +02:00
from devplacepy.utils import generate_uid, require_user, time_ago, get_current_user, create_mention_notifications
2026-05-11 07:02:06 +02:00
from devplacepy.seo import base_seo_context
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("", response_class=HTMLResponse)
async def bugs_page(request: Request):
user = get_current_user(request)
bugs_table = get_table("bug_reports")
all_bugs = list(bugs_table.find(order_by=["-created_at"]))
from devplacepy.database import get_users_by_uids
uids = [b["user_uid"] for b in all_bugs]
users_map = get_users_by_uids(uids)
bug_list = []
2026-05-12 15:07:34 +02:00
bug_uids = [b["uid"] for b in all_bugs]
2026-05-13 21:17:57 +02:00
attachments_map = get_attachments_batch("bug", bug_uids) if bug_uids else {}
2026-05-11 07:02:06 +02:00
for b in all_bugs:
bug_list.append({
"bug": b,
"author": users_map.get(b["user_uid"]),
"time_ago": time_ago(b["created_at"]),
2026-05-11 20:49:45 +02:00
"comments": load_comments("bug", b["uid"]),
2026-05-12 15:07:34 +02:00
"attachments": attachments_map.get(b["uid"], []),
2026-05-11 07:02:06 +02:00
})
seo_ctx = base_seo_context(
request,
title="Bug Reports",
description="Report bugs and issues on DevPlace.",
2026-05-12 15:07:34 +02:00
breadcrumbs=[
{"name": "Home", "url": "/feed"},
{"name": "Bug Reports", "url": "/bugs"},
],
2026-05-11 07:02:06 +02:00
)
2026-05-13 23:03:06 +02:00
return templates.TemplateResponse(request, "bugs.html", {
2026-05-11 07:02:06 +02:00
**seo_ctx,
"request": request,
"user": user,
"bugs": bug_list,
})
@router.post("/create")
async def create_bug(request: Request):
user = require_user(request)
form = await request.form()
title = form.get("title", "").strip()
description = form.get("description", "").strip()
if not title or not description:
return RedirectResponse(url="/bugs", status_code=302)
bugs_table = get_table("bug_reports")
2026-05-12 13:08:38 +02:00
bug_uid = generate_uid()
2026-05-11 07:02:06 +02:00
bugs_table.insert({
2026-05-12 13:08:38 +02:00
"uid": bug_uid,
2026-05-11 07:02:06 +02:00
"user_uid": user["uid"],
"title": title,
"description": description,
"status": "open",
2026-05-14 04:12:19 +02:00
"created_at": datetime.now(timezone.utc).isoformat(),
2026-05-11 07:02:06 +02:00
})
2026-05-12 15:07:34 +02:00
attachment_uids = form.getlist("attachment_uids") if hasattr(form, "getlist") else []
2026-05-13 21:17:57 +02:00
if attachment_uids:
link_attachments(attachment_uids, "bug", bug_uid)
2026-05-12 15:07:34 +02:00
2026-05-12 13:08:38 +02:00
create_mention_notifications(description, user["uid"], f"/bugs?highlight={bug_uid}")
2026-05-11 07:02:06 +02:00
logger.info(f"Bug report created by {user['username']}: {title}")
return RedirectResponse(url="/bugs", status_code=302)