Skip to content

Commit

Permalink
Note report effective time, BCA timestamps no tz. (bcgov#1977)
Browse files Browse the repository at this point in the history
* Note report effective time, BCA timestamps no tz.

Signed-off-by: Doug Lovett <[email protected]>

* BCA timestamps no tz.

Signed-off-by: Doug Lovett <[email protected]>

---------

Signed-off-by: Doug Lovett <[email protected]>
  • Loading branch information
doug-lovett authored Jul 12, 2024
1 parent 0a93c28 commit ff8e6bd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mhr_api/report-templates/unitNoteV2.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<tr>
<td>
{% if note is defined and note.documentType is defined and note.documentType in ('NCAN', 'NRED') %}
Cancelled Date:
Cancelled Date and Time:
{% else %}
Effective Date:
Effective Date and Time:
{% endif %}
</td>
<td>
Expand Down
11 changes: 9 additions & 2 deletions mhr_api/src/mhr_api/models/batch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ def get_batch_registration_data(start_ts: str = None, end_ts: str = None):
query = text(query_s)
result = None
if start_ts and end_ts:
start: str = start_ts[:19].replace('T', ' ')
end: str = end_ts[:19].replace('T', ' ')
start: str = get_query_ts(start_ts)
end: str = get_query_ts(end_ts)
current_app.logger.debug(f'start={start} end={end}')
result = db.session.execute(query, {'query_val1': start, 'query_val2': end})
else:
Expand All @@ -445,3 +445,10 @@ def get_batch_registration_data(start_ts: str = None, end_ts: str = None):
else:
current_app.logger.debug('No batch registrations found within the timestamp range.')
return results_json


def get_query_ts(request_ts: str):
"""Get a query timestamp as UTC in the DB format."""
ts = model_utils.ts_from_iso_format_no_tz(request_ts)
query_ts: str = model_utils.format_ts(ts)
return query_ts[:19].replace('T', ' ')
10 changes: 10 additions & 0 deletions mhr_api/src/mhr_api/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ def now_ts_offset(offset_days: int = 1, add: bool = False):
return now - timedelta(days=offset_days)


def ts_from_iso_format_no_tz(timestamp_iso: str):
"""Create a datetime object from a timestamp string in the ISO format using the local time zone."""
if len(timestamp_iso) > 19:
return ts_from_iso_format(timestamp_iso)
ts: _datetime = _datetime.fromisoformat(timestamp_iso)
local_ts = LOCAL_TZ.localize(ts, is_dst=True)
# Return as UTC
return local_ts.astimezone(timezone.utc)


def today_ts_offset(offset_days: int = 1, add: bool = False):
"""Create a timestamp representing the current date at 00:00:00 adjusted by offset number of days."""
today = date.today()
Expand Down
2 changes: 1 addition & 1 deletion mhr_api/src/mhr_api/reports/v2/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def _set_note(self):
elif note.get('expiryDateTime'):
note['expiryDateTime'] = Report._to_report_datetime(note.get('expiryDateTime'), False)
if note.get('effectiveDateTime'):
note['effectiveDateTime'] = Report._to_report_datetime(note.get('effectiveDateTime'), False)
note['effectiveDateTime'] = Report._to_report_datetime(note.get('effectiveDateTime'), True)
if note.get('cancelledDateTime'):
note['cancelledDateTime'] = Report._to_report_datetime(note.get('cancelledDateTime'), True)
if note.get('givingNoticeParty') and note['givingNoticeParty'].get('phoneNumber'):
Expand Down
2 changes: 1 addition & 1 deletion mhr_api/src/mhr_api/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
Development release segment: .devN
"""

__version__ = '1.8.16' # pylint: disable=invalid-name
__version__ = '1.8.17' # pylint: disable=invalid-name
2 changes: 2 additions & 0 deletions mhr_api/tests/unit/models/test_batch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
# testdata pattern is ({start_ts}, {end_ts})
TEST_DATA_BATCH_REGISTRATION = [
('2023-12-15T08:01:00+00:00', '2023-12-22T08:01:00+00:00'),
('2023-12-15T00:01:00-08:00', '2023-12-22T00:01:00-00:00'),
('2023-12-15T00:01:00', '2023-12-22T00:01:00'),
(None, None)
]

Expand Down
25 changes: 25 additions & 0 deletions mhr_api/tests/unit/models/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mhr_api.models import utils as model_utils
from mhr_api.models.db2.search_utils import get_search_serial_number_key


DB2_IND_NAME_MIDDLE = 'DANYLUK LEONARD MICHAEL '
DB2_IND_NAME = 'KING MARDI '
DB2_IND_NAME_MAX = 'M.BELLERIVE-MAXIMILLIAN-JCHARLES-OLIVIERGUILLAUME-JEAN-CLAUDE-VAN-DAMN'
Expand Down Expand Up @@ -80,6 +81,15 @@
(False, '2022-09-01T07:01:00+00:00', '2021-09-01T07:01:00+00:00'),
(False, '2022-09-01T07:01:00+00:00', None)
]
# testdata pattern is ({test_ts}, {expected_ts})
TEST_DATA_TS_NO_TZ = [
('2024-06-01T08:00:00', '2024-06-01T15:00:00+00:00'),
('2024-09-01T21:00:00', '2024-09-02T04:00:00+00:00'),
('2024-12-01T21:00:00', '2024-12-02T05:00:00+00:00'),
('2024-06-01T08:00:00-07:00', '2024-06-01T15:00:00+00:00'),
('2024-09-01T21:00:00-07:00', '2024-09-02T04:00:00+00:00'),
('2024-12-01T21:00:00-08:00', '2024-12-02T05:00:00+00:00')
]


@pytest.mark.parametrize('last, first, middle, db2_name', TEST_DATA_LEGACY_NAME)
Expand Down Expand Up @@ -133,5 +143,20 @@ def test_tax_cert_date(session, valid, registration_ts, tax_cert_ts):
def test_permit_expiry_days(session):
"""Assert that setting and computing expiry days works as expected."""
expiry_ts = model_utils.compute_permit_expiry()
current_app.logger.debug(model_utils.format_ts(model_utils.now_ts()))
current_app.logger.debug(model_utils.format_ts(expiry_ts))
expiry_days = model_utils.expiry_ts_days(expiry_ts)
assert expiry_days == 30


@pytest.mark.parametrize('registration_ts,expected_ts', TEST_DATA_TS_NO_TZ)
def test_ts_from_iso_format_no_tz(session, registration_ts, expected_ts):
"""Assert that converting an ISO timestamp with no time zone works as expected."""
reg_ts = model_utils.ts_from_iso_format_no_tz(registration_ts)
test_ts = model_utils.format_ts(reg_ts)
current_app.logger.debug(f'In={registration_ts} out={test_ts}')
assert test_ts == expected_ts
if len(registration_ts) > 19:
reg_ts = model_utils.ts_from_iso_format(registration_ts)
test_ts = model_utils.format_ts(reg_ts)
assert test_ts == expected_ts

0 comments on commit ff8e6bd

Please sign in to comment.