<commit_before>
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.forms.widgets import Input
from django.utils.translation import ugettext as _

from selvbetjening.core.events.models.options import AutoSelectChoiceOption, DiscountOption, DiscountCode
from selvbetjening.core.events.models import Selection
from selvbetjening.core.events.options.scope import SCOPE


class BaseWidget(object):

    def __init__(self, scope, option):
        self.scope = scope
        self.option = option
        self.required = self.option.required if self.scope != SCOPE.SADMIN else False
        self.use_native_required = self.required and self.option.depends_on is None  # if we have a dependency, then disable native required checks

    def is_editable(self, attendee):
        return True

    def clean_callback(self, value):

        if (value in validators.EMPTY_VALUES or not value) and self.required:
            raise ValidationError(_('This field is required.'))

        return value if value is not None else False


class BooleanWidget(BaseWidget):

    def get_field(self, attrs=None):

        field = forms.BooleanField(
            label=self.option.name if self.option.price == 0 else '%s (%s,-)' % (self.option.name, self.option.price),
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.CheckboxInput(attrs=attrs))
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and value:
            Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )
        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def initial_value(self, selection):
        return True


class TextWidget(BaseWidget):

    def get_field(self, attrs=None):

        field = forms.CharField(
            label=self.option.name,
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.TextInput(attrs=attrs))
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and len(value.strip()) > 0:
            selection, created = Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )

            selection.text = value.strip()
            selection.save()
        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def initial_value(self, selection):
        return selection.text


class ChoiceWidget(BaseWidget):

    def __init__(self, scope, option):
        super(ChoiceWidget, self).__init__(scope, option)

        self.choices = [('', '')] + [('suboption_%s' % suboption.pk, self._label(suboption))
                                     for suboption in self.option.suboptions.all()]

    def get_field(self, attrs=None):

        field = forms.ChoiceField(
            label=self.option.name,
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.Select(attrs=attrs),
            choices=self.choices)
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and len(value) > 0:
            selection, created = Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )

            _, pk = value.split('_')

            selection.suboption_id = pk
            selection.save()

        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def clean_callback(self, value):
        value = super(ChoiceWidget, self).clean_callback(value)

        if value is None:
            return None

        for choice in self.choices:
            if value == choice[0]:
                return value

        raise ValidationError(_('Choice %s is not allowed') % value)

    def initial_value(self, selection):
        if selection.suboption is not None:
            return 'suboption_%s' % selection.suboption.pk
        else:
            return None

    def _label(self, suboption):
        price = suboption.real_price
        return suboption.name if price == 0 else '%s (%s)' % (suboption.name, price)


class AutoSelectBooleanWidget(BooleanWidget):

    def get_field(self, attrs=None):
        return forms.BooleanField(
            required=False,
            widget=forms.HiddenInput(attrs=None))

    def clean_callback(self, value):
        return True

    def initial_value(self, selection):
        return True


class AutoChoiceDisplay(Input):

    CANT_DISABLE = True

    def render(self, name, value, attrs=None):
        value = self.suboption.name if self.suboption.real_price == 0 else '%s (%s,-)' % (self.suboption.name, self.suboption.real_price)
        return '<p class="form-control-static">%s</p>' % value<commit_msg>Fix error in initialization of auto select choice<commit_after>
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.forms.widgets import Input
from django.utils.translation import ugettext as _

from selvbetjening.core.events.models.options import AutoSelectChoiceOption, DiscountOption, DiscountCode
from selvbetjening.core.events.models import Selection
from selvbetjening.core.events.options.scope import SCOPE


class BaseWidget(object):

    def __init__(self, scope, option):
        self.scope = scope
        self.option = option
        self.required = self.option.required if self.scope != SCOPE.SADMIN else False
        self.use_native_required = self.required and self.option.depends_on is None  # if we have a dependency, then disable native required checks

    def is_editable(self, attendee):
        return True

    def clean_callback(self, value):

        if (value in validators.EMPTY_VALUES or not value) and self.required:
            raise ValidationError(_('This field is required.'))

        return value if value is not None else False


class BooleanWidget(BaseWidget):

    def get_field(self, attrs=None):

        field = forms.BooleanField(
            label=self.option.name if self.option.price == 0 else '%s (%s,-)' % (self.option.name, self.option.price),
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.CheckboxInput(attrs=attrs))
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and value:
            Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )
        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def initial_value(self, selection):
        return True


class TextWidget(BaseWidget):

    def get_field(self, attrs=None):

        field = forms.CharField(
            label=self.option.name,
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.TextInput(attrs=attrs))
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and len(value.strip()) > 0:
            selection, created = Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )

            selection.text = value.strip()
            selection.save()
        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def initial_value(self, selection):
        return selection.text


class ChoiceWidget(BaseWidget):

    def __init__(self, scope, option):
        super(ChoiceWidget, self).__init__(scope, option)

        self.choices = [('', '')] + [('suboption_%s' % suboption.pk, self._label(suboption))
                                     for suboption in self.option.suboptions.all()]

    def get_field(self, attrs=None):

        field = forms.ChoiceField(
            label=self.option.name,
            required=self.use_native_required,
            help_text=self.option.description,
            widget=forms.Select(attrs=attrs),
            choices=self.choices)
        field.render_as_required = self.required
        return field

    def save_callback(self, attendee, value):

        if value is not None and len(value) > 0:
            selection, created = Selection.objects.get_or_create(
                option_id=self.option.pk,
                attendee=attendee
            )

            _, pk = value.split('_')

            selection.suboption_id = pk
            selection.save()

        else:
            Selection.objects.filter(
                option_id=self.option.pk,
                attendee=attendee
            ).delete()

    def clean_callback(self, value):
        value = super(ChoiceWidget, self).clean_callback(value)

        if value is None:
            return None

        for choice in self.choices:
            if value == choice[0]:
                return value

        raise ValidationError(_('Choice %s is not allowed') % value)

    def initial_value(self, selection):
        if selection.suboption is not None:
            return 'suboption_%s' % selection.suboption.pk
        else:
            return None

    def _label(self, suboption):
        price = suboption.real_price
        return suboption.name if price == 0 else '%s (%s)' % (suboption.name, price)


class AutoSelectBooleanWidget(BooleanWidget):

    def get_field(self, attrs=None):
        return forms.BooleanField(
            required=False,
            widget=forms.HiddenInput(attrs=None))

    def clean_callback(self, value):
        return True

    def initial_value(self, selection):
        return True


class AutoChoiceDisplay(Input):

    CANT_DISABLE = True

    def __init__(self, suboption, *args, **kwargs):
        self.suboption = suboption
        super(AutoChoiceDisplay, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs=None):
        value = self.suboption.name if self.suboption.real_price == 0 else '%s (%s,-)' % (self.suboption.name, self.suboption.real_price)
        return '<p class="form-control-static">%s</p>' % value
{'fraction_non_alphanumeric': 0.09149307148756539, 'fraction_numerical': 0.0011012205194090117, 'mean_word_length': 2.2550776583034646, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '243331', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2924, 'n_words': 714}