forked from seanwiseman/coar-notify-inbox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (63 loc) · 2.38 KB
/
app.py
File metadata and controls
80 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from datetime import datetime
import os
import json
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import Query
from config import get_settings, PAGE_LIMIT
from db.notifications import get_notifications, get_notifications_collection
from routers import (
inbox_router, notification_state_router, subscription_router
)
templates = Jinja2Templates(directory="templates")
templates.env.globals['max'] = max
templates.env.globals['min'] = min
def create_app() -> FastAPI:
_app = FastAPI()
_app.add_middleware(
CORSMiddleware,
allow_origins=get_settings().allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
_app.mount("/static",
StaticFiles(directory=os.path.join(os.path.dirname(__file__),
"static")), name="static")
_app.include_router(inbox_router)
_app.include_router(notification_state_router)
_app.include_router(subscription_router)
@_app.get("/health", status_code=200)
async def health_check():
return {"status": "ok"}
@_app.get("/")
async def home(request: Request, page: int = Query(1, ge=1),
page_size: int = Query(PAGE_LIMIT, ge=1)):
def ppjson(value, indent=2):
return json.dumps(
{**value, "updated": value["updated"].isoformat()},
indent=indent,
ensure_ascii=False
)
def dateparse(value):
return datetime.fromisoformat(value)
templates.env.filters['dateparse'] = dateparse
templates.env.filters["tojson_pretty"] = ppjson
notifications = await get_notifications(page=page, page_size=page_size)
collection = await get_notifications_collection()
total_notifications = await collection.count_documents({})
total_pages = (total_notifications + page_size - 1) // page_size
return templates.TemplateResponse(
"index.html",
{
"request": request,
"notifications": notifications,
"page": page,
"page_size": page_size,
"total_pages": total_pages
}
)
return _app
app = create_app()