|
import time
|
|
from fastapi.templating import Jinja2Templates
|
|
from devplacepy.config import TEMPLATES_DIR
|
|
from devplacepy.database import get_table
|
|
from devplacepy.avatar import avatar_url
|
|
from devplacepy.utils import format_date as _format_date
|
|
from devplacepy.seo import (
|
|
site_url, combine,
|
|
website_schema, breadcrumb_schema,
|
|
discussion_forum_posting, profile_page_schema,
|
|
software_application_schema, truncate,
|
|
)
|
|
|
|
templates = Jinja2Templates(directory=str(TEMPLATES_DIR))
|
|
|
|
|
|
_unread_cache = {}
|
|
_UNREAD_CACHE_TTL = 60
|
|
|
|
def jinja_unread_count(user_uid: str) -> int:
|
|
cached = _unread_cache.get(user_uid)
|
|
if cached:
|
|
entry, expiry = cached
|
|
if time.time() < expiry:
|
|
return entry
|
|
_unread_cache.pop(user_uid, None)
|
|
notifs = get_table("notifications")
|
|
count = len(list(notifs.find(user_uid=user_uid, read=False)))
|
|
_unread_cache[user_uid] = (count, time.time() + _UNREAD_CACHE_TTL)
|
|
return count
|
|
|
|
|
|
def jinja_user_projects(user_uid: str) -> list:
|
|
projects = get_table("projects")
|
|
return list(projects.find(user_uid=user_uid))
|
|
|
|
|
|
templates.env.globals["get_unread_count"] = jinja_unread_count
|
|
templates.env.globals["get_user_projects"] = jinja_user_projects
|
|
templates.env.globals["avatar_url"] = avatar_url
|
|
templates.env.globals["format_date"] = _format_date
|