<commit_before>from django.contrib import messages
from django.db.models import Count, Q
from django.http import JsonResponse
from django.shortcuts import redirect
from django.utils.timezone import now
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView, View

from pretix.base.models import Quota, Voucher
from pretix.base.services.cart import (
    CartError, add_items_to_cart, remove_items_from_cart,
)
from pretix.multidomain.urlreverse import eventreverse
from pretix.presale.views import EventViewMixin
from pretix.presale.views.async import AsyncAction
from pretix.presale.views.event import item_group_by_category


class CartActionMixin:

    def get_next_url(self):
        if "next" in self.request.GET and '://' not in self.request.GET.get('next'):
            return self.request.GET.get('next')
        else:
            return eventreverse(self.request.event, 'presale:event.index')

    def get_success_url(self, value=None):
        return self.get_next_url()

    def get_error_url(self):
        return self.get_next_url()

    def _items_from_post_data(self):
        """
        Parses the POST data and returns a list of tuples in the
        form (item id, variation id or None, number)
        """

        # Compatibility patch that makes the frontend code a lot easier
        req_items = list(self.request.POST.items())
        if '_voucher_item' in self.request.POST and '_voucher_code' in self.request.POST:
            req_items.append((
                '%s_voucher' % self.request.POST['_voucher_item'], self.request.POST['_voucher_code']
            ))
            pass

        items = []
        for key, value in req_items:
            if value.strip() == '' or '_' not in key:
                continue

            parts = key.split("_")
            if parts[-1] == "voucher":
                voucher = value
                value = 1
                parts = parts[:-1]
            else:
                voucher = None
            price = self.request.POST.get('price_' + "_".join(parts[1:]), "")
            if key.startswith('item_'):
                try:
                    items.append({
                        'item': int(parts[1]),
                        'variation': None,
                        'count': int(value),
                        'price': price,
                        'voucher': voucher
                    })
                except ValueError:
                    messages.error(self.request, _('Please enter numbers only.'))
                    return []
            elif key.startswith('variation_'):
                try:
                    items.append({
                        'item': int(parts[1]),
                        'variation': int(parts[2]),
                        'count': int(value),
                        'price': price,
                        'voucher': voucher
                    })
                except ValueError:
                    messages.error(self.request, _('Please enter numbers only.'))
                    return []
        if len(items) == 0:<commit_msg>Assert that the item count is a positive integer (#221)<commit_after>from django.contrib import messages
from django.db.models import Count, Q
from django.http import JsonResponse
from django.shortcuts import redirect
from django.utils.timezone import now
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView, View

from pretix.base.models import Quota, Voucher
from pretix.base.services.cart import (
    CartError, add_items_to_cart, remove_items_from_cart,
)
from pretix.multidomain.urlreverse import eventreverse
from pretix.presale.views import EventViewMixin
from pretix.presale.views.async import AsyncAction
from pretix.presale.views.event import item_group_by_category


class CartActionMixin:

    def get_next_url(self):
        if "next" in self.request.GET and '://' not in self.request.GET.get('next'):
            return self.request.GET.get('next')
        else:
            return eventreverse(self.request.event, 'presale:event.index')

    def get_success_url(self, value=None):
        return self.get_next_url()

    def get_error_url(self):
        return self.get_next_url()

    def _items_from_post_data(self):
        """
        Parses the POST data and returns a list of tuples in the
        form (item id, variation id or None, number)
        """

        # Compatibility patch that makes the frontend code a lot easier
        req_items = list(self.request.POST.items())
        if '_voucher_item' in self.request.POST and '_voucher_code' in self.request.POST:
            req_items.append((
                '%s_voucher' % self.request.POST['_voucher_item'], self.request.POST['_voucher_code']
            ))
            pass

        items = []
        for key, value in req_items:
            if value.strip() == '' or '_' not in key:
                continue

            parts = key.split("_")
            if parts[-1] == "voucher":
                voucher = value
                value = 1
                parts = parts[:-1]
            else:
                voucher = None

            try:
                amount = int(value)
            except ValueError:
                messages.error(self.request, _('Please enter numbers only.'))
                return []
            if amount <= 0:
                messages.error(self.request, _('Please enter positive numbers only.'))
                return []

            price = self.request.POST.get('price_' + "_".join(parts[1:]), "")
            if key.startswith('item_'):
                try:
                    items.append({
                        'item': int(parts[1]),
                        'variation': None,
                        'count': amount,
                        'price': price,
                        'voucher': voucher
                    })
                except ValueError:
                    messages.error(self.request, _('Please enter numbers only.'))
                    return []
            elif key.startswith('variation_'):
                try:
                    items.append({
                        'item': int(parts[1]),
                        'variation': int(parts[2]),
                        'count': amount,
                        'price': price,
                        'voucher': voucher
                    })
                except ValueError:
                    messages.error(self.request, _('Please enter numbers only.'))
                    return []
        if len(items) == 0:
{'fraction_non_alphanumeric': 0.09879738164104125, 'fraction_numerical': 0.0030446034404018876, 'mean_word_length': 1.6631536278881232, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3289151', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1696, 'n_words': 447}