diff --git a/doc/development/api/restriction.rst b/doc/development/api/restriction.rst index ff3870ee7..6155c01a1 100644 --- a/doc/development/api/restriction.rst +++ b/doc/development/api/restriction.rst @@ -178,9 +178,10 @@ In our example, the implementation could look like this:: for restriction in restrictions: applied_to = list(restriction.variations.all()) - # Only take this restriction into consideration if it either - # is directly applied to this variation - if 'variation' not in v or v['variation'] not in applied_to: + # Only take this restriction into consideration if it + # is directly applied to this variation or if the item + # has no variations + if len(v) != 0 and ('variation' not in v or v['variation'] not in applied_to): continue if restriction.timeframe_from <= now() <= restriction.timeframe_to: diff --git a/src/tixlplugins/timerestriction/signals.py b/src/tixlplugins/timerestriction/signals.py index 78a22e4ad..6e286e75d 100644 --- a/src/tixlplugins/timerestriction/signals.py +++ b/src/tixlplugins/timerestriction/signals.py @@ -82,8 +82,9 @@ def availability_handler(sender, **kwargs): applied_to = list(restriction.variations.current.all()) # Only take this restriction into consideration if it - # is directly applied to this variation - if 'variation' not in v or v['variation'] not in applied_to: + # is directly applied to this variation or if the item + # has no variations + if len(v) != 0 and ('variation' not in v or v['variation'] not in applied_to): continue if restriction.timeframe_from <= now() <= restriction.timeframe_to: diff --git a/src/tixlplugins/timerestriction/tests.py b/src/tixlplugins/timerestriction/tests.py index 6bb4f76cd..c612d4667 100644 --- a/src/tixlplugins/timerestriction/tests.py +++ b/src/tixlplugins/timerestriction/tests.py @@ -29,6 +29,12 @@ class TimeRestrictionTest(TestCase): self.value1 = PropertyValue.objects.create(prop=self.property, value='S') self.value2 = PropertyValue.objects.create(prop=self.property, value='M') self.value3 = PropertyValue.objects.create(prop=self.property, value='L') + self.variation1 = ItemVariation.objects.create(item=self.item) + self.variation1.values.add(self.value1) + self.variation2 = ItemVariation.objects.create(item=self.item) + self.variation2.values.add(self.value2) + self.variation3 = ItemVariation.objects.create(item=self.item) + self.variation3.values.add(self.value3) def test_nothing(self): result = signals.availability_handler( @@ -213,10 +219,6 @@ class TimeRestrictionTest(TestCase): def test_variation_specific(self): self.item.properties.add(self.property) - v1 = ItemVariation.objects.create(item=self.item) - v1.values.add(self.value1) - v2 = ItemVariation.objects.create(item=self.item) - v2.values.add(self.value2) r1 = TimeRestriction.objects.create( timeframe_from=now() - timedelta(days=5), @@ -226,7 +228,7 @@ class TimeRestrictionTest(TestCase): ) r1.item = self.item r1.save() - r1.variations.add(v1) + r1.variations.add(self.variation1) result = signals.availability_handler( self.event, item=self.item, variations=self.item.get_all_variations(), @@ -234,65 +236,14 @@ class TimeRestrictionTest(TestCase): ) self.assertEqual(len(result), 3) for v in result: - if 'variation' in v and v['variation'].pk == v1.pk: + if 'variation' in v and v['variation'].pk == self.variation1.pk: self.assertTrue(v['available']) self.assertEqual(v['price'], 12) else: self.assertFalse(v['available']) - def test_variation_specific_and_general(self): - self.item.properties.add(self.property) - v1 = ItemVariation.objects.create(item=self.item) - v1.values.add(self.value1) - v2 = ItemVariation.objects.create(item=self.item) - v2.values.add(self.value2) - - r1 = TimeRestriction.objects.create( - timeframe_from=now() - timedelta(days=5), - timeframe_to=now() + timedelta(days=1), - event=self.event, - price=12 - ) - r1.item = self.item - r1.save() - r2 = TimeRestriction.objects.create( - timeframe_from=now() - timedelta(days=5), - timeframe_to=now() + timedelta(days=1), - event=self.event, - price=8 - ) - r2.item = self.item - r2.save() - r2.variations.add(v1) - r3 = TimeRestriction.objects.create( - timeframe_from=now() - timedelta(days=5), - timeframe_to=now() - timedelta(days=1), - event=self.event, - price=10 - ) - r3.item = self.item - r3.save() - r3.variations.add(v2) - result = signals.availability_handler( - self.event, item=self.item, - variations=self.item.get_all_variations(), - context=None, cache=self.event.get_cache() - ) - self.assertEqual(len(result), 3) - for v in result: - if 'variation' in v and v['variation'].pk == v1.pk: - self.assertTrue(v['available']) - self.assertEqual(v['price'], 8) - else: - self.assertTrue(v['available']) - self.assertEqual(v['price'], 12) - def test_variation_specifics(self): self.item.properties.add(self.property) - v1 = ItemVariation.objects.create(item=self.item) - v1.values.add(self.value1) - v2 = ItemVariation.objects.create(item=self.item) - v2.values.add(self.value2) r1 = TimeRestriction.objects.create( timeframe_from=now() - timedelta(days=5), @@ -302,7 +253,7 @@ class TimeRestrictionTest(TestCase): ) r1.item = self.item r1.save() - r1.variations.add(v1) + r1.variations.add(self.variation1) r2 = TimeRestriction.objects.create( timeframe_from=now() - timedelta(days=5), timeframe_to=now() + timedelta(days=1), @@ -311,7 +262,7 @@ class TimeRestrictionTest(TestCase): ) r2.item = self.item r2.save() - r2.variations.add(v1) + r2.variations.add(self.variation1) r3 = TimeRestriction.objects.create( timeframe_from=now() - timedelta(days=5), timeframe_to=now() - timedelta(days=1), @@ -320,7 +271,7 @@ class TimeRestrictionTest(TestCase): ) r3.item = self.item r3.save() - r3.variations.add(v2) + r3.variations.add(self.variation3) result = signals.availability_handler( self.event, item=self.item, variations=self.item.get_all_variations(), @@ -328,7 +279,7 @@ class TimeRestrictionTest(TestCase): ) self.assertEqual(len(result), 3) for v in result: - if 'variation' in v and v['variation'].pk == v1.pk: + if 'variation' in v and v['variation'].pk == self.variation1.pk: self.assertTrue(v['available']) self.assertEqual(v['price'], 8) else: