Changeset - 3b985d40ac0b
[Not reviewed]
0 1 0
Christopher Neugebauer - 8 years ago 2017-01-07 22:47:54
chrisjrn@gmail.com
Missing line
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
registrasion/forms.py
Show inline comments
...
 
@@ -257,208 +257,208 @@ class _ItemQuantityProductsForm(_ProductsForm):
 
        if not category.required:
 
            choices.append((0, "---"))
 

	
 
        for product in products:
 
            choice_text = "%s -- $%d each" % (product.name, product.price)
 
            choices.append((product.id, choice_text))
 

	
 
        cls.base_fields[cls.CHOICE_FIELD] = forms.TypedChoiceField(
 
            label=category.name,
 
            widget=forms.Select,
 
            choices=choices,
 
            initial=0,
 
            empty_value=0,
 
            coerce=int,
 
        )
 

	
 
        cls.base_fields[cls.QUANTITY_FIELD] = forms.IntegerField(
 
            label="Quantity",  # TODO: internationalise
 
            min_value=0,
 
            max_value=500,  # Issue #19. We should figure out real limit.
 
        )
 

	
 
    @classmethod
 
    def initial_data(cls, product_quantities):
 
        initial = {}
 

	
 
        for product, quantity in product_quantities:
 
            if quantity > 0:
 
                initial[cls.CHOICE_FIELD] = product.id
 
                initial[cls.QUANTITY_FIELD] = quantity
 
                break
 

	
 
        return initial
 

	
 
    def product_quantities(self):
 
        our_choice = self.cleaned_data[self.CHOICE_FIELD]
 
        our_quantity = self.cleaned_data[self.QUANTITY_FIELD]
 
        choices = self.fields[self.CHOICE_FIELD].choices
 
        for choice_value, choice_display in choices:
 
            if choice_value == 0:
 
                continue
 
            yield (
 
                choice_value,
 
                our_quantity if our_choice == choice_value else 0,
 
            )
 

	
 
    def add_product_error(self, product, error):
 
        if self.CHOICE_FIELD not in self.cleaned_data:
 
            return
 

	
 
        if product.id == self.cleaned_data[self.CHOICE_FIELD]:
 
            self.add_error(self.CHOICE_FIELD, error)
 
            self.add_error(self.QUANTITY_FIELD, error)
 

	
 

	
 
class _ItemQuantityProductsFormSet(_HasProductsFields, forms.BaseFormSet):
 

	
 
    @classmethod
 
    def set_fields(cls, category, products):
 
        raise ValueError("set_fields must be called on the underlying Form")
 

	
 
    @classmethod
 
    def initial_data(cls, product_quantities):
 
        ''' Prepares initial data for an instance of this form.
 
        product_quantities is a sequence of (product,quantity) tuples '''
 

	
 
        f = [
 
            {
 
                _ItemQuantityProductsForm.CHOICE_FIELD: product.id,
 
                _ItemQuantityProductsForm.QUANTITY_FIELD: quantity,
 
            }
 
            for product, quantity in product_quantities
 
            if quantity > 0
 
        ]
 
        return f
 

	
 
    def product_quantities(self):
 
        ''' Yields a sequence of (product, quantity) tuples from the
 
        cleaned form data. '''
 

	
 
        products = set()
 
        # Track everything so that we can yield some zeroes
 
        all_products = set()
 

	
 
        for form in self:
 
            if form.empty_permitted and not form.cleaned_data:
 
                # This is the magical empty form at the end of the list.
 
                continue
 

	
 
            for product, quantity in form.product_quantities():
 
                all_products.add(product)
 
                if quantity == 0:
 
                    continue
 
                if product in products:
 
                    form.add_error(
 
                        _ItemQuantityProductsForm.CHOICE_FIELD,
 
                        "You may only choose each product type once.",
 
                    )
 
                    form.add_error(
 
                        _ItemQuantityProductsForm.QUANTITY_FIELD,
 
                        "You may only choose each product type once.",
 
                    )
 
                products.add(product)
 
                yield product, quantity
 

	
 
        for product in (all_products - products):
 
            yield product, 0
 

	
 
    def add_product_error(self, product, error):
 
        for form in self.forms:
 
            form.add_product_error(product, error)
 

	
 
    @property
 
    def errors(self):
 
        _errors = super(_ItemQuantityProductsFormSet, self).errors
 
        if False not in [not form.errors for form in self.forms]:
 
            return []
 
        else:
 
            return _errors
 

	
 

	
 
class VoucherForm(forms.Form):
 
    voucher = forms.CharField(
 
        label="Voucher code",
 
        help_text="If you have a voucher code, enter it here",
 
        required=False,
 
    )
 

	
 

	
 
def staff_products_form_factory(user):
 
    ''' Creates a StaffProductsForm that restricts the available products to
 
    those that are available to a user. '''
 

	
 
    products = inventory.Product.objects.all()
 
    products = ProductController.available_products(user, products=products)
 

	
 
    product_ids = [product.id for product in products]
 
    product_set = inventory.Product.objects.filter(id__in=product_ids)
 

	
 
    class StaffProductsForm(forms.Form):
 
        ''' Form for allowing staff to add an item to a user's cart. '''
 

	
 
        product = forms.ModelChoiceField(
 
            widget=forms.Select,
 
            queryset=product_set,
 
        )
 

	
 
        quantity = forms.IntegerField(
 
            min_value=0,
 
        )
 

	
 
    return StaffProductsForm
 

	
 
def staff_products_formset_factory(user):
 
    ''' Creates a formset of StaffProductsForm for the given user. '''
 
    form_type = staff_products_form_factory(user)
 
    return forms.formset_factory(form_type)
 

	
 

	
 
class InvoiceEmailForm(forms.Form):
 

	
 
    ACTION_PREVIEW = 1
 
    ACTION_SEND = 2
 

	
 
    ACTION_CHOICES = (
 
        (ACTION_PREVIEW, "Preview"),
 
        (ACTION_SEND, "Send emails"),
 
    )
 

	
 
    invoice = forms.ModelMultipleChoiceField(
 
        widget=forms.CheckboxSelectMultiple,
 
        queryset=commerce.Invoice.objects.all(),
 
    )
 
    from_email = forms.CharField()
 
    subject = forms.CharField()
 
    body = forms.CharField(
 
        widget=forms.Textarea,
 
    )
 
    action = forms.TypedChoiceField(
 
        widget=forms.RadioSelect,
 
        coerce=int,
 
        choices=ACTION_CHOICES,
 
        initial=ACTION_PREVIEW,
 
    )
 

	
 
    def __init__(self, *a, **k):
 
        category = k.pop('category', None) or []
 
        product = k.pop('product', None) or []
 

	
 
        category = [int(i) for i in category]
 
        product = [int(i) for i in product]
 

	
 
        super(InvoiceNagForm, self).__init__(*a, **k)
 
        super(InvoiceEmailForm, self).__init__(*a, **k)
 

	
 
        qs = commerce.Invoice.objects.filter(
 
            status=commerce.Invoice.STATUS_UNPAID,
 
        ).filter(
 
            Q(lineitem__product__category__in=category) |
 
            Q(lineitem__product__in=product)
 
        )
 

	
 
        # Uniqify
 
        qs = commerce.Invoice.objects.filter(
 
            id__in=qs,
 
        )
 

	
 
        self.fields['invoice'].queryset = qs
 
        self.fields['invoice'].initial = [i.id for i in qs]
0 comments (0 inline, 0 general)