diff --git a/wafer/schedule/models.py b/wafer/schedule/models.py index da40dc46..b2c6b3e9 100644 --- a/wafer/schedule/models.py +++ b/wafer/schedule/models.py @@ -333,26 +333,24 @@ def __str__(self): def get_duration(self): """Return the total duration of the item. - This is the sum of all the slot durations.""" - # This is intended for the pentabarf xml file - # It will do the wrong thing if the slots aren't - # contigious, which we should address sometime. - slots = list(self.slots.all()) - result = {'hours': 0, 'minutes': 0} - if slots: - for slot in slots: - dur = slot.get_duration() - result['hours'] += dur['hours'] - result['minutes'] += dur['minutes'] - # Normalise again - hours, result['minutes'] = divmod(result['minutes'], 60) - result['hours'] += hours - return result + This is the time from start to end, including any discontinuities + between slots.""" + minutes = self.get_duration_minutes() + hours, minutes = divmod(minutes, 60) + return { + 'hours': hours, + 'minutes': minutes, + } def get_duration_minutes(self): """Return the duration in total number of minutes.""" - duration = self.get_duration() - return int(duration['hours'] * 60 + duration['minutes']) + slots = list(self.slots.all()) + if not slots: + return 0 + start = slots[0].get_start_time() + end = slots[-1].end_time + duration = end - start + return duration.total_seconds() // 60 @property def guid(self): diff --git a/wafer/schedule/tests/test_models.py b/wafer/schedule/tests/test_models.py index 56bfa34d..2d3375b8 100644 --- a/wafer/schedule/tests/test_models.py +++ b/wafer/schedule/tests/test_models.py @@ -377,6 +377,45 @@ def test_invalid_slot_block(self): self.assertRaises(ValidationError, slot4.clean) +class ScheduleItemTests(TestCase): + def setUp(self): + utc = timezone.utc + self.blocks = [ + ScheduleBlock.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 19, 0, 0, tzinfo=utc)), + ] + self.slots = [ + Slot.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 9, 20, 0, tzinfo=utc)), + Slot.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 30, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 9, 50, 0, tzinfo=utc)), + ] + self.venues = [ + Venue.objects.create(order=1, name='Venue 1'), + ] + self.pages = make_pages(1) + + def test_duration_minutes(self): + """Test events spanning multiple slots with a gap in between""" + item = make_items(self.venues, self.pages)[0] + # Single Slot + item.slots.add(self.slots[0]) + self.assertEqual(item.get_duration_minutes(), 20) + # Spanning 2 slots with a gap in-between + item.slots.add(self.slots[1]) + self.assertEqual(item.get_duration_minutes(), 50) + + def test_get_duration(self): + """Test ScheduleItem.get_duration()""" + item = make_items(self.venues, self.pages)[0] + # Single Slot + item.slots.add(self.slots[0]) + self.assertEqual(item.get_duration(), {'minutes': 20, 'hours': 0}) + + class LastUpdateTests(TestCase): def setUp(self):