<commit_before>from django import forms

from ..cart.forms import AddToCartForm
from .models import Bag, BagVariant, Shirt, ShirtVariant, Color


class BagForm(AddToCartForm):

    def get_variant(self, clean_data):
        return BagVariant.objects.get(product__color=self.product.color)


class ShirtForm(AddToCartForm):

    size = forms.ChoiceField(choices=ShirtVariant.SIZE_CHOICES,
                             widget=forms.RadioSelect())

    def get_variant(self, clean_data):
        size = clean_data.get('size')
        return ShirtVariant.objects.get(size=size,
                                        product__color=self.product.color)


def get_form_class_for_product(product):
    if type(product) is Shirt:
        return ShirtForm
    if type(product) is Bag:
        return BagForm
    raise NotImplemented()<commit_msg>Limit size choices in ShirtForm only to available sizes<commit_after>from django import forms

from ..cart.forms import AddToCartForm
from .models import Bag, BagVariant, Shirt, ShirtVariant, Color


class BagForm(AddToCartForm):

    def get_variant(self, clean_data):
        return BagVariant.objects.get(product__color=self.product.color)


class ShirtForm(AddToCartForm):

    size = forms.ChoiceField(choices=ShirtVariant.SIZE_CHOICES,
                             widget=forms.RadioSelect())

    def __init__(self, *args, **kwargs):
        super(ShirtForm, self).__init__(*args, **kwargs)
        available_sizes = [
            (p.size, p.get_size_display) for p in self.product.variants.all()
        ]
        self.fields['size'].choices = available_sizes

    def get_variant(self, clean_data):
        size = clean_data.get('size')
        return ShirtVariant.objects.get(size=size,
                                        product__color=self.product.color)


def get_form_class_for_product(product):
    if type(product) is Shirt:
        return ShirtForm
    if type(product) is Bag:
        return BagForm
    raise NotImplemented()
{'fraction_non_alphanumeric': 0.08857574232511324, 'fraction_numerical': 0.0, 'mean_word_length': 2.8980392156862744, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7522342', 'n_tokens_mistral': 580, 'n_tokens_neox': 564, 'n_words': 127}