Skip to content

Commit

Permalink
use safe time_from_iso to parse lineitem start/end times
Browse files Browse the repository at this point in the history
This is an improvement on commit 0cdfa43
  • Loading branch information
christianp committed Nov 21, 2024
1 parent 07baa02 commit 8c2298e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 3 additions & 8 deletions numbas_lti/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .groups import group_for_attempt, group_for_resource_stats, group_for_resource
from .report_outcome import report_outcome, report_outcome_for_attempt, ReportOutcomeException
from .diff import make_diff, apply_diff
from .util import parse_scorm_timeinterval, iso_time
from .util import parse_scorm_timeinterval, iso_time, time_from_iso
from .examparser import numbasobject

requests = requests_session.get_session()
Expand Down Expand Up @@ -848,14 +848,9 @@ def get_lti_13_lineitem(self, create = False):

saved_lineitem = LineItem(saved_lineitem)

def parse_isoformat(time: str):
if time is None:
return None
return datetime.fromisoformat(time)

if (saved_lineitem.get_score_maximum() != lineitem.get_score_maximum()
or parse_isoformat(saved_lineitem.get_start_date_time()) != parse_isoformat(lineitem.get_start_date_time())
or parse_isoformat(saved_lineitem.get_end_date_time()) != parse_isoformat(lineitem.get_end_date_time())
or time_from_iso(saved_lineitem.get_start_date_time()) != time_from_iso(lineitem.get_start_date_time())
or time_from_iso(saved_lineitem.get_end_date_time()) != time_from_iso(lineitem.get_end_date_time())
or saved_lineitem.get_tag() != lineitem.get_tag()
):
max_score = lineitem.get_score_maximum()
Expand Down
6 changes: 6 additions & 0 deletions numbas_lti/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ def iso_time(time: datetime) -> str:
Convert a datetime to an ISO format string if it's not None, otherwise return None.
"""
return time.isoformat() if time else None

def time_from_iso(time: str) -> datetime:
"""
Parse an ISO format datetime string if the argument is not None, otherwise return None.
"""
return datetime.fromisoformat(time) if time is not None else None

0 comments on commit 8c2298e

Please sign in to comment.