Exemple #1
0
    def _item_from_post_value(self, key, value, voucher=None):
        if value.strip() == '' or '_' not in key:
            return

        if not key.startswith('item_') and not key.startswith(
                'variation_') and not key.startswith('seat_'):
            return

        parts = key.split("_")
        price = self.request.POST.get('price_' + "_".join(parts[1:]), "")

        if key.startswith('seat_'):
            try:
                return {
                    'item': int(parts[1]),
                    'variation': int(parts[2]) if len(parts) > 2 else None,
                    'count': 1,
                    'seat': value,
                    'price': price,
                    'voucher': voucher,
                    'subevent': self.request.POST.get("subevent")
                }
            except ValueError:
                raise CartError(_('Please enter numbers only.'))

        try:
            amount = int(value)
        except ValueError:
            raise CartError(_('Please enter numbers only.'))
        if amount < 0:
            raise CartError(_('Please enter positive numbers only.'))
        elif amount == 0:
            return

        if key.startswith('item_'):
            try:
                return {
                    'item': int(parts[1]),
                    'variation': None,
                    'count': amount,
                    'price': price,
                    'voucher': voucher,
                    'subevent': self.request.POST.get("subevent")
                }
            except ValueError:
                raise CartError(_('Please enter numbers only.'))
        elif key.startswith('variation_'):
            try:
                return {
                    'item': int(parts[1]),
                    'variation': int(parts[2]),
                    'count': amount,
                    'price': price,
                    'voucher': voucher,
                    'subevent': self.request.POST.get("subevent")
                }
            except ValueError:
                raise CartError(_('Please enter numbers only.'))
Exemple #2
0
def validate_cart(sender, positions=None, **kwargs):
    if not sender.has_subevents or not positions:
        return
    if not sender.settings.get('block_multisubevent_checkout', as_type=bool):
        return
    subevent = positions[0].subevent
    for pos in positions[1:]:
        if subevent != pos.subevent:
            raise CartError(
                _("Sorry, you can only choose one event per order. "
                  "Please create multiple orders to participate on multiple dates."
                  ))
    def create_cart(self, sc, expires):
        with transaction.atomic():
            with self.request.event.lock():
                positions = []
                quotas = Counter()

                for form in self.formset.forms:
                    if '-' in form.cleaned_data['itemvar']:
                        itemid, varid = form.cleaned_data['itemvar'].split('-')
                    else:
                        itemid, varid = form.cleaned_data['itemvar'], None

                    item = Item.objects.get(pk=itemid,
                                            event=self.request.event)
                    variation = ItemVariation.objects.get(
                        pk=varid, item=item) if varid else None
                    price = form.cleaned_data['price']
                    if not price:
                        price = (variation.default_price if variation
                                 and variation.default_price is not None else
                                 item.default_price)

                    for quota in item.quotas.all():
                        quotas[quota] += form.cleaned_data['count']

                    for i in range(form.cleaned_data['count']):
                        positions.append(
                            CartPosition(item=item,
                                         variation=variation,
                                         event=self.request.event,
                                         cart_id=sc.cart_id,
                                         expires=expires,
                                         price=price))

                for quota, diff in quotas.items():
                    avail = quota.availability()
                    if avail[0] != Quota.AVAILABILITY_OK or (
                            avail[1] is not None and avail[1] < diff):
                        raise CartError(self.error_messages['quota'].format(
                            name=quota.name))

                sc.expires = expires
                sc.event = self.request.event
                sc.total = sum([p.price for p in positions])
                sc.save()
                CartPosition.objects.bulk_create(positions)