<commit_before>from __future__ import unicode_literals

from django import forms
from django.db import transaction
from django.forms.models import ModelChoiceIterator, inlineformset_factory
from django.utils.translation import pgettext_lazy

from ...product.models import (AttributeChoiceValue, Product, ProductAttribute,
                               ProductImage, ProductVariant, Stock,
                               VariantImage, ProductClass, StockLocation)
from .widgets import ImagePreviewWidget


class ProductClassSelectorForm(forms.Form):
    product_cls = forms.ChoiceField(
        label=pgettext_lazy('Product class form label', 'Product class'),
        widget=forms.RadioSelect,
        choices=[])

    def __init__(self, *args, **kwargs):
        product_classes = kwargs.pop('product_classes', [])
        super(ProductClassSelectorForm, self).__init__(*args, **kwargs)
        self.fields['product_cls'].choices = [(obj.pk, obj.name)
                                              for obj in product_classes]


class StockForm(forms.ModelForm):
    class Meta:
        model = Stock
        exclude = ['quantity_allocated']

    def __init__(self, *args, **kwargs):
        product = kwargs.pop('product')
        super(StockForm, self).__init__(*args, **kwargs)
        self.fields['variant'] = forms.ModelChoiceField(
            queryset=product.variants)


class ProductClassForm(forms.ModelForm):
    class Meta:
        model = ProductClass
        exclude = []

    def clean(self):
        data = super(ProductClassForm, self).clean()
        product_attr = set(self.cleaned_data.get('product_attributes', []))
        variant_attr = set(self.cleaned_data.get('variant_attributes', []))
        if len(product_attr & variant_attr) > 0:
            msg = pgettext_lazy(
                "Product Class Errors",
                "Do not use same attributes in both product and variant.")
            self.add_error('variant_attributes', msg)<commit_msg>Add variant attrs validators to product class<commit_after>from __future__ import unicode_literals

from django import forms
from django.db import transaction
from django.forms.models import ModelChoiceIterator, inlineformset_factory
from django.utils.translation import pgettext_lazy

from ...product.models import (AttributeChoiceValue, Product, ProductAttribute,
                               ProductImage, ProductVariant, Stock,
                               VariantImage, ProductClass, StockLocation)
from .widgets import ImagePreviewWidget


class ProductClassSelectorForm(forms.Form):
    product_cls = forms.ChoiceField(
        label=pgettext_lazy('Product class form label', 'Product class'),
        widget=forms.RadioSelect,
        choices=[])

    def __init__(self, *args, **kwargs):
        product_classes = kwargs.pop('product_classes', [])
        super(ProductClassSelectorForm, self).__init__(*args, **kwargs)
        self.fields['product_cls'].choices = [(obj.pk, obj.name)
                                              for obj in product_classes]


class StockForm(forms.ModelForm):
    class Meta:
        model = Stock
        exclude = ['quantity_allocated']

    def __init__(self, *args, **kwargs):
        product = kwargs.pop('product')
        super(StockForm, self).__init__(*args, **kwargs)
        self.fields['variant'] = forms.ModelChoiceField(
            queryset=product.variants)


class ProductClassForm(forms.ModelForm):
    class Meta:
        model = ProductClass
        exclude = []

    def clean(self):
        data = super(ProductClassForm, self).clean()
        has_variants = self.cleaned_data.get('product_attributes')
        product_attr = set(self.cleaned_data.get('product_attributes', []))
        variant_attr = set(self.cleaned_data.get('variant_attributes', []))
        if not has_variants and len(variant_attr) > 0:
            msg = pgettext_lazy(
                "Product Class Errors",
                "This class has no variants options selected.")
            self.add_error('variant_attributes', msg)
        if len(product_attr & variant_attr) > 0:
            msg = pgettext_lazy(
                "Product Class Errors",
                "Do not use same attributes in both product and variant.")
            self.add_error('variant_attributes', msg)
{'fraction_non_alphanumeric': 0.09774261112404002, 'fraction_numerical': 0.0006981615080288574, 'mean_word_length': 2.7439024390243905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7568481', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1101, 'n_words': 271}