add aurweb.time module

This module includes timezone-based utilities for a FastAPI request.
This commit introduces use of the AURTZ cookie within get_request_timezone.
This cookie should be set to the user or session's timezone.

* `make_context` has been modified to parse the request's timezone
  and include the "timezone" and "timezones" variables, along with
  a timezone specified "now" date.
+ Added `Timezone` attribute to aurweb.testing.requests.Request.user.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-01-25 16:52:14 -08:00
parent 07d5907ecd
commit 9052688ed2
4 changed files with 104 additions and 4 deletions

View file

@ -1,5 +1,6 @@
import copy
import os
import zoneinfo
from datetime import datetime
from http import HTTPStatus
@ -11,7 +12,7 @@ from fastapi.responses import HTMLResponse
import aurweb.config
from aurweb import l10n
from aurweb import l10n, time
# Prepare jinja2 objects.
loader = jinja2.FileSystemLoader(os.path.join(
@ -26,14 +27,15 @@ env.filters["tr"] = l10n.tr
def make_context(request: Request, title: str, next: str = None):
""" Create a context for a jinja2 TemplateResponse. """
timezone = time.get_request_timezone(request)
return {
"request": request,
"language": l10n.get_request_language(request),
"languages": l10n.SUPPORTED_LANGUAGES,
"timezone": timezone,
"timezones": time.SUPPORTED_TIMEZONES,
"title": title,
# The 'now' context variable will not show proper datetimes
# until we've implemented timezone support here.
"now": datetime.now(),
"now": datetime.now(tz=zoneinfo.ZoneInfo(timezone)),
"config": aurweb.config,
"next": next if next else request.url.path
}
@ -60,4 +62,5 @@ def render_template(request: Request,
response = HTMLResponse(rendered, status_code=status_code)
response.set_cookie("AURLANG", context.get("language"))
response.set_cookie("AURTZ", context.get("timezone"))
return response