Пример #1
0
    def check_restrictions(self):
        """
        This method is used to determine whether this ItemVariation is restricted
        in sale by any restriction plugins.

        :returns:

            * ``False``, if the item is unavailable
            * the item's price, otherwise
        """
        from pretix.base.signals import determine_availability

        responses = determine_availability.send(
            self.item.event,
            item=self.item,
            variations=[self.to_variation_dict()],
            context=None,
            cache=self.item.event.get_cache())
        price = self.default_price if self.default_price is not None else self.item.default_price
        for rec, response in responses:
            if 'available' in response[0] and not response[0]['available']:
                return False
            elif 'price' in response[0] and response[0][
                    'price'] is not None and response[0]['price'] < price:
                price = response[0]['price']
        return price
Пример #2
0
    def check_restrictions(self):
        """
        This method is used to determine whether this ItemVariation is restricted
        in sale by any restriction plugins.

        :returns:

            * ``False``, if the item is unavailable
            * the item's price, otherwise

        :raises ValueError: if you call this on an item which has properties associated with it.
                            Please use the method on the ItemVariation object you are interested in.
        """
        if self.properties.count() > 0:  # NOQA
            raise ValueError('Do not call this directly on items which have properties '
                             'but call this on their ItemVariation objects')
        from pretix.base.signals import determine_availability

        vd = VariationDict()
        responses = determine_availability.send(
            self.event, item=self,
            variations=[vd], context=None,
            cache=self.event.get_cache()
        )
        price = self.default_price
        for rec, response in responses:
            if 'available' in response[0] and not response[0]['available']:
                return False
            elif 'price' in response[0] and response[0]['price'] is not None and response[0]['price'] < price:
                price = response[0]['price']
        return price
Пример #3
0
 def test_one_plugin_active(self):
     self.event.plugins = 'tests.testdummy'
     self.event.save()
     payload = {'foo': 'bar'}
     responses = determine_availability.send(self.event, **payload)
     self.assertEqual(len(responses), 1)
     self.assertIn('tests.testdummy.signals', [r[0].__module__ for r in responses])
Пример #4
0
    def check_restrictions(self):
        """
        This method is used to determine whether this ItemVariation is restricted
        in sale by any restriction plugins.

        :returns:

            * ``False``, if the item is unavailable
            * the item's price, otherwise

        :raises ValueError: if you call this on an item which has properties associated with it.
                            Please use the method on the ItemVariation object you are interested in.
        """
        if self.properties.count() > 0:  # NOQA
            raise ValueError(
                'Do not call this directly on items which have properties '
                'but call this on their ItemVariation objects')
        from pretix.base.signals import determine_availability

        vd = VariationDict()
        responses = determine_availability.send(self.event,
                                                item=self,
                                                variations=[vd],
                                                context=None,
                                                cache=self.event.get_cache())
        price = self.default_price
        for rec, response in responses:
            if 'available' in response[0] and not response[0]['available']:
                return False
            elif 'price' in response[0] and response[0][
                    'price'] is not None and response[0]['price'] < price:
                price = response[0]['price']
        return price
Пример #5
0
    def get_all_available_variations(self, use_cache: bool = False):
        """
        This method returns a list of all variations which are theoretically
        possible for sale. It DOES call all activated restriction plugins, and it
        DOES only return variations which DO have an ItemVariation object, as all
        variations without one CAN NOT be part of a Quota and therefore can never
        be available for sale. The only exception is the empty variation
        for items without properties, which never has an ItemVariation object.

        This DOES NOT take into account quotas itself. Use ``is_available`` on the
        ItemVariation objects (or the Item it self, if it does not have variations) to
        determine availability by the terms of quotas.

        It is recommended to call::

            .prefetch_related('properties', 'variations__values__prop')

        when retrieving Item objects you are going to use this method on.
        """
        if use_cache and hasattr(self, '_get_all_available_variations_cache'):
            return self._get_all_available_variations_cache

        from pretix.base.signals import determine_availability

        variations = self._get_all_generated_variations()
        responses = determine_availability.send(self.event,
                                                item=self,
                                                variations=variations,
                                                context=None,
                                                cache=self.event.get_cache())

        for i, var in enumerate(variations):
            var['available'] = var[
                'variation'].active if 'variation' in var else True
            if 'variation' in var:
                if var['variation'].default_price:
                    var['price'] = var['variation'].default_price
                else:
                    var['price'] = self.default_price
            else:
                var['price'] = self.default_price

            # It is possible, that *multiple* restriction plugins change the default price.
            # In this case, the cheapest one wins. As soon as there is a restriction
            # that changes the price, the default price has no effect.

            newprice = None
            for rec, response in responses:
                if 'available' in response[i] and not response[i]['available']:
                    var['available'] = False
                    break
                if 'price' in response[i] and response[i]['price'] is not None \
                        and (newprice is None or response[i]['price'] < newprice):
                    newprice = response[i]['price']
            var['price'] = newprice or var['price']

        variations = [var for var in variations if var['available']]

        self._get_all_available_variations_cache = variations
        return variations
Пример #6
0
 def test_one_plugin_active(self):
     self.event.plugins = 'tests.testdummy'
     self.event.save()
     payload = {'foo': 'bar'}
     responses = determine_availability.send(self.event, **payload)
     self.assertEqual(len(responses), 1)
     self.assertIn('tests.testdummy.signals', [r[0].__module__ for r in responses])
Пример #7
0
    def get_all_available_variations(self, use_cache: bool=False):
        """
        This method returns a list of all variations which are theoretically
        possible for sale. It DOES call all activated restriction plugins, and it
        DOES only return variations which DO have an ItemVariation object, as all
        variations without one CAN NOT be part of a Quota and therefore can never
        be available for sale. The only exception is the empty variation
        for items without properties, which never has an ItemVariation object.

        This DOES NOT take into account quotas itself. Use ``is_available`` on the
        ItemVariation objects (or the Item it self, if it does not have variations) to
        determine availability by the terms of quotas.

        It is recommended to call::

            .prefetch_related('properties', 'variations__values__prop')

        when retrieving Item objects you are going to use this method on.
        """
        if use_cache and hasattr(self, '_get_all_available_variations_cache'):
            return self._get_all_available_variations_cache

        from pretix.base.signals import determine_availability

        variations = self._get_all_generated_variations()
        responses = determine_availability.send(
            self.event, item=self,
            variations=variations, context=None,
            cache=self.event.get_cache()
        )

        for i, var in enumerate(variations):
            var['available'] = var['variation'].active if 'variation' in var else True
            if 'variation' in var:
                if var['variation'].default_price:
                    var['price'] = var['variation'].default_price
                else:
                    var['price'] = self.default_price
            else:
                var['price'] = self.default_price

            # It is possible, that *multiple* restriction plugins change the default price.
            # In this case, the cheapest one wins. As soon as there is a restriction
            # that changes the price, the default price has no effect.

            newprice = None
            for rec, response in responses:
                if 'available' in response[i] and not response[i]['available']:
                    var['available'] = False
                    break
                if 'price' in response[i] and response[i]['price'] is not None \
                        and (newprice is None or response[i]['price'] < newprice):
                    newprice = response[i]['price']
            var['price'] = newprice or var['price']

        variations = [var for var in variations if var['available']]

        self._get_all_available_variations_cache = variations
        return variations
Пример #8
0
    def check_restrictions(self):
        """
        This method is used to determine whether this ItemVariation is restricted
        in sale by any restriction plugins.

        :returns:

            * ``False``, if the item is unavailable
            * the item's price, otherwise
        """
        from pretix.base.signals import determine_availability

        responses = determine_availability.send(
            self.item.event, item=self.item,
            variations=[self.to_variation_dict()], context=None,
            cache=self.item.event.get_cache()
        )
        price = self.default_price if self.default_price is not None else self.item.default_price
        for rec, response in responses:
            if 'available' in response[0] and not response[0]['available']:
                return False
            elif 'price' in response[0] and response[0]['price'] is not None and response[0]['price'] < price:
                price = response[0]['price']
        return price
Пример #9
0
 def test_no_plugins_active(self):
     self.event.plugins = ''
     self.event.save()
     responses = determine_availability.send(self.event)
     self.assertEqual(len(responses), 0)
Пример #10
0
 def test_no_plugins_active(self):
     self.event.plugins = ''
     self.event.save()
     responses = determine_availability.send(self.event)
     self.assertEqual(len(responses), 0)