Skip to content

Commit

Permalink
Merge pull request #686 from drnlm/bugfix/fix_invalid_timestamp_handling
Browse files Browse the repository at this point in the history
Bugfix/fix invalid timestamp handling
  • Loading branch information
drnlm authored Oct 17, 2023
2 parents 8425337 + e6132cd commit cadbb11
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
47 changes: 47 additions & 0 deletions wafer/schedule/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,53 @@ def validate_current(response):
items[5].get_details(),
'</a></td>']), html=True)

def test_current_view_invalid_timestamp(self):
"""Test that invalid timestamps are treated as no timestamp"""
day1 = ScheduleBlock.objects.create(
start_time=D.datetime.now(tz=D.timezone.utc) - D.timedelta(minutes=120),
end_time=D.datetime.now(tz=D.timezone.utc) + D.timedelta(minutes=120)
)
venue1 = Venue.objects.create(order=1, name='Venue 1')
venue2 = Venue.objects.create(order=2, name='Venue 2')
venue1.blocks.add(day1)
venue2.blocks.add(day1)

start1 = D.datetime.now(tz=D.timezone.utc) - D.timedelta(minutes=45)
start2 = D.datetime.now(tz=D.timezone.utc) + D.timedelta(minutes=30)
end = D.datetime.now(tz=D.timezone.utc) + D.timedelta(minutes=90)

slots = []

slots.append(Slot.objects.create(start_time=start1, end_time=start2))
slots.append(Slot.objects.create(start_time=start2, end_time=end))

pages = make_pages(4)
venues = [venue1, venue2] * 2
items = make_items(venues, pages)

for index, item in enumerate(items):
item.slots.add(slots[index // 2])

c = Client()

default_view = c.get('/schedule/current/')

# Try with complete nonsense
response = c.get('/schedule/current/',
{'timestamp': 'this_is_not_a_timestamp'})
self.assertEqual(len(response.context['slots']), 2)
self.assertEqual(default_view.context['cur_slot'], response.context['cur_slot'])
# We compare the __repr__'s, because of how the objects are created
self.assertEqual(str(default_view.context['slots']), str(response.context['slots']))

# Try with a timestamp that give a ValueError
response = c.get('/schedule/current/',
{'timestamp': '2016-12-32T12:00:00'})
self.assertEqual(len(response.context['slots']), 2)
self.assertEqual(default_view.context['cur_slot'], response.context['cur_slot'])
self.assertEqual(str(default_view.context['slots']), str(response.context['slots']))


def test_current_view_invalid(self):
"""Test that invalid schedules return a inactive current view."""
day1 = ScheduleBlock.objects.create(
Expand Down
14 changes: 9 additions & 5 deletions wafer/schedule/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,24 @@ def get_context_data(self, **kwargs):
class CurrentView(TemplateView):
template_name = 'wafer.schedule/current.html'

def _parse_timestamp(self, timestamp):
def _parse_timestamp(self, given_timestamp):
"""
Parse a user provided timestamp query string parameter.
Return a TZ aware datetime, or None.
"""
if not timestamp:
if not given_timestamp:
return None
try:
timestamp = parse_datetime(timestamp)
timestamp = parse_datetime(given_timestamp)
except ValueError as e:
messages.error(self.request,
'Failed to parse timestamp: %s' % e)
f'Failed to parse timestamp ({given_timestamp}): {e}')
# Short circuit out here
return None
if timestamp is None:
messages.error(self.request, 'Failed to parse timestamp')
# If parse_datetime completely fails to extract anything
# we end up here
messages.error(self.request, f'Failed to parse timestamp {given_timestamp}')
return None
if not timezone.is_aware(timestamp):
timestamp = timezone.make_aware(timestamp)
Expand Down

0 comments on commit cadbb11

Please sign in to comment.