Revised the calculation of "today" to use the client's timezone instead of the server's timezone.

This commit is contained in:
2024-06-04 08:23:15 +08:00
parent 6ee3ee76ea
commit 80ae4bd91c
9 changed files with 113 additions and 22 deletions

View File

@ -1,7 +1,7 @@
# The Mia! Accounting Demonstration Website.
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/13
# Copyright (c) 2023 imacat.
# Copyright (c) 2023-2024 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ from typing import Any
import sqlalchemy as sa
from flask import Flask
from accounting.utils.timezone import get_tz_today
from . import db
from .auth import User
@ -44,6 +45,17 @@ class Accounts:
MEAL: str = "6272-001"
def get_today() -> dt.date:
"""Returns today, based on the context.
:return: Today.
"""
try:
return get_tz_today()
except RuntimeError:
return dt.date.today()
class JournalEntryLineItemData:
"""The journal entry line item data."""
@ -183,7 +195,7 @@ class JournalEntryData:
:param is_update: True for an update operation, or False otherwise
:return: The journal entry as a form.
"""
date: dt.date = dt.date.today() - dt.timedelta(days=self.days)
date: dt.date = get_today() - dt.timedelta(days=self.days)
form: dict[str, str] = {"csrf_token": csrf_token,
"next": encoded_next_uri,
"date": date.isoformat()}
@ -260,8 +272,7 @@ class BaseTestData(ABC):
existing_j_id: set[int] = {x["id"] for x in self.__journal_entries}
existing_l_id: set[int] = {x["id"] for x in self.__line_items}
journal_entry_data.id = self.__new_id(existing_j_id)
date: dt.date \
= dt.date.today() - dt.timedelta(days=journal_entry_data.days)
date: dt.date = get_today() - dt.timedelta(days=journal_entry_data.days)
self.__journal_entries.append(
{"id": journal_entry_data.id,
"date": date,

View File

@ -1,7 +1,7 @@
# The Mia! Accounting Demonstration Website.
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/4/12
# Copyright (c) 2023 imacat.
# Copyright (c) 2023-2024 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ from flask import Flask, Blueprint, url_for, flash, redirect, session, \
render_template, current_app
from flask_babel import lazy_gettext
from accounting.utils.timezone import get_tz_today
from . import db
from .auth import admin_required
from .lib import Accounts, JournalEntryLineItemData, JournalEntryData, \
@ -117,7 +118,7 @@ class SampleData(BaseTestData):
:return: None.
"""
today: dt.date = dt.date.today()
today: dt.date = get_tz_today()
days: int
year: int
month: int
@ -160,7 +161,7 @@ class SampleData(BaseTestData):
:return: None.
"""
today: dt.date = dt.date.today()
today: dt.date = get_tz_today()
year: int = today.year - 5
month: int = today.month