Skip to content

Commit

Permalink
fix: handle any missing date for last_updated
Browse files Browse the repository at this point in the history
it's rare, but some child blocks are missing created and/or edited

handle that by just taking all dates as an untrusted pile, test by
adding a bunch of empty dates into the test case

also have to update the test with a timezone because they must be
compatible with the default date which must be compatible with real
system dates
  • Loading branch information
ashultz0 committed Oct 12, 2023
1 parent 32575c0 commit 106fd28
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Change Log
Unreleased
**********

3.6.2 — 2023-10-12
**********************************************

* Handle rare blocks missing dates when calculating last updated
* Remove log of expected "not here" exception during config

3.6.1 — 2023-10-10
**********************************************

Expand Down
2 changes: 1 addition & 1 deletion ai_aside/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
A plugin containing xblocks and apps supporting GPT and other LLM use on edX.
"""

__version__ = '3.6.1'
__version__ = '3.6.2'

default_app_config = "ai_aside.apps.AiAsideConfig"
23 changes: 16 additions & 7 deletions ai_aside/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from datetime import datetime

import pytz
from django.conf import settings
from django.template import Context, Template
from web_fragments.fragment import Fragment
Expand Down Expand Up @@ -135,6 +136,17 @@ def _check_summarizable(block):
return False


def _latest_block_date(maybe_dates):
"""
Find the latest a set of possibly null dates sourced from blocks.
"""
latest = datetime(2012, 5, 1, 0, 0, 0, 0, pytz.UTC) # no blocks predate edx
for d in maybe_dates:
if d is not None and d > latest:
latest = d
return latest


def _render_hook_fragment(user_role_string, handler_url, block, summary_items):
"""
Create hook Fragment from block and summarized children.
Expand All @@ -147,18 +159,15 @@ def _render_hook_fragment(user_role_string, handler_url, block, summary_items):
usage_id = block.scope_ids.usage_id
course_key = usage_id.course_key

all_interesting_dates = [last_published, last_edited]
for item in summary_items:
published = item['published_on']
edited = item['edited_on']
if published and published > last_published:
last_published = published
if edited and edited > last_edited:
last_edited = edited
all_interesting_dates.append(published)
all_interesting_dates.append(edited)

# we only need to know when the last time was that anything happened
last_updated = last_published
if last_edited > last_published:
last_updated = last_edited
last_updated = _latest_block_date(all_interesting_dates)

fragment = Fragment('')
fragment.add_content(
Expand Down
15 changes: 11 additions & 4 deletions tests/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from textwrap import dedent
from unittest.mock import MagicMock, Mock, call, patch

import pytz
from django.test import TestCase, override_settings
from opaque_keys.edx.keys import UsageKey

Expand All @@ -17,8 +18,8 @@
)

fake_transcript = 'This is the text version from the transcript'
date1 = datetime(2023, 1, 2, 3, 4, 5)
date2 = datetime(2023, 6, 7, 8, 9, 10)
date1 = datetime(2023, 1, 2, 3, 4, 5, 0, pytz.UTC)
date2 = datetime(2023, 6, 7, 8, 9, 10, 0, pytz.UTC)


def fake_get_transcript(child, lang=None, output_format='SRT', youtube_id=None): # pylint: disable=unused-argument
Expand Down Expand Up @@ -83,7 +84,7 @@ def setUp(self):

def test_format_date(self):
formatted_date = _format_date(date1)
self.assertEqual(formatted_date, '2023-01-02T03:04:05')
self.assertEqual(formatted_date, '2023-01-02T03:04:05+00:00')

def test_format_date_with_invalid_input(self):
invalid_date = '2023-05-01'
Expand Down Expand Up @@ -291,8 +292,14 @@ def test_parse_children_contents_with_invalid_children(self):
def test_render_hook_fragment(self):
block = FakeBlock([])
items = [{
'published_on': None,
'edited_on': None,
}, {
'published_on': date1,
'edited_on': date1,
}, {
'published_on': None,
'edited_on': None,
}, {
'published_on': date2,
'edited_on': date1,
Expand All @@ -306,7 +313,7 @@ def test_render_hook_fragment(self):
data-course-id="course-v1:edX+A+B"
data-content-id="block-v1:edX+A+B+type@vertical+block@verticalD"
data-handler-url="http://handler.url"
data-last-updated="2023-06-07T08:09:10"
data-last-updated="2023-06-07T08:09:10+00:00"
data-user-role="user role string"
>
</div>
Expand Down

0 comments on commit 106fd28

Please sign in to comment.