<commit_before>from django import forms
from django.utils.translation import ugettext_lazy as _
from django.forms import ModelForm

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout

from selvbetjening.core.translation.utility import translate_model
from selvbetjening.core.invoice.models import Payment

from selvbetjening.viewbase.forms.helpers import SFieldset

from models import AttendState

class OptionGroupForm(forms.Form):
    def __init__(self, optiongroup, *args,  **kwargs):
        self.optiongroup = translate_model(optiongroup)
        self.selected_total = 0
        self.selected_initally = False
        self.save_options = []
        self.attendee = kwargs.pop('attendee', None)

        if self.attendee is not None:
            selections = [selection for selection in self.attendee.selections if selection.option.group == optiongroup]
        else:
            selections = []

        kwargs['initial'] = {}
        for selection in selections:
            kwargs['initial'][self._get_id(selection.option)] = True
            if selection.suboption:
                kwargs['initial'][self._get_sub_id(selection.option)] = selection.suboption.id

        super(OptionGroupForm, self).__init__(*args, **kwargs)

        self.selected_option_pks = [selection.option.pk for selection in selections]

        display_params = None

        # package controls
        if optiongroup.package_solution:
            self.fields[self._get_package_id(optiongroup)] = \
                forms.BooleanField(label=_(u'Package'), required=False,
                                   widget=forms.CheckboxInput(attrs={'class': 'package'}))

            display_params = {'class': 'in_package in_package_%s' % optiongroup.pk}

        for option in optiongroup.options:
            translate_model(option)

            selected = option.pk in self.selected_option_pks

            disabled = self.attendee is not None and \
                self.optiongroup.lock_selections_on_acceptance == True and \
                self.attendee.state != AttendState.waiting
            disabled = disabled or (option.max_attendees_reached() and not selected)
            disabled = disabled or option.is_frozen()

            suboptions = option.suboptions

            if self._should_save(option, suboptions, disabled):
                self.save_options.append((option, suboptions))

            self._display_option(option, disabled, suboptions, display_params=display_params)
            self._register_clean_function(option, selected, disabled)

        # setup display related settings

        fields = [self.optiongroup.name,] + [field_id for field_id in self.fields]
        options = {'help_text': self.optiongroup.description,
                   'large_hints': True}

        layout = Layout(SFieldset(*fields, **options))

        self.helper = FormHelper()

        self.helper.add_layout(layout)
        self.helper.form_tag = False
        self.helper.use_csrf_protection = True

    def _should_save(self, option, suboptions, disabled):
        return True

    def _display_option(self, option, disabled, suboptions, display_params=None):
        if display_params is None:
            display_params = {}

        #if len(suboptions) > 0:
        #    display_params['children'] = (self._get_sub_id(option),)

        if option.price == 0:
            label = option.name
        else:
            label = '%s (%s,-)' % (option.name, option.price)

        self.fields[self._get_id(option)] = forms.BooleanField(label=label,
                                                               required=False,
                                                               help_text=option.description,
                                                               widget=forms.CheckboxInput(attrs=display_params))

        if len(suboptions) > 0:
            choices = [(suboption.id, suboption.name) for suboption in suboptions]
            self.fields[self._get_sub_id(option)] = forms.ChoiceField(label=_('Choices'),
                                                                      choices=choices,
                                                                      required=False)

    def _register_clean_function(self, option, selected_initially, disabled):<commit_msg>Remove decimals from price if not a decimal number<commit_after>from django import forms
from django.utils.translation import ugettext_lazy as _
from django.forms import ModelForm

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout

from selvbetjening.core.translation.utility import translate_model
from selvbetjening.core.invoice.models import Payment

from selvbetjening.viewbase.forms.helpers import SFieldset

from models import AttendState

class OptionGroupForm(forms.Form):
    def __init__(self, optiongroup, *args,  **kwargs):
        self.optiongroup = translate_model(optiongroup)
        self.selected_total = 0
        self.selected_initally = False
        self.save_options = []
        self.attendee = kwargs.pop('attendee', None)

        if self.attendee is not None:
            selections = [selection for selection in self.attendee.selections if selection.option.group == optiongroup]
        else:
            selections = []

        kwargs['initial'] = {}
        for selection in selections:
            kwargs['initial'][self._get_id(selection.option)] = True
            if selection.suboption:
                kwargs['initial'][self._get_sub_id(selection.option)] = selection.suboption.id

        super(OptionGroupForm, self).__init__(*args, **kwargs)

        self.selected_option_pks = [selection.option.pk for selection in selections]

        display_params = None

        # package controls
        if optiongroup.package_solution:
            self.fields[self._get_package_id(optiongroup)] = \
                forms.BooleanField(label=_(u'Package'), required=False,
                                   widget=forms.CheckboxInput(attrs={'class': 'package'}))

            display_params = {'class': 'in_package in_package_%s' % optiongroup.pk}

        for option in optiongroup.options:
            translate_model(option)

            selected = option.pk in self.selected_option_pks

            disabled = self.attendee is not None and \
                self.optiongroup.lock_selections_on_acceptance == True and \
                self.attendee.state != AttendState.waiting
            disabled = disabled or (option.max_attendees_reached() and not selected)
            disabled = disabled or option.is_frozen()

            suboptions = option.suboptions

            if self._should_save(option, suboptions, disabled):
                self.save_options.append((option, suboptions))

            self._display_option(option, disabled, suboptions, display_params=display_params)
            self._register_clean_function(option, selected, disabled)

        # setup display related settings

        fields = [self.optiongroup.name,] + [field_id for field_id in self.fields]
        options = {'help_text': self.optiongroup.description,
                   'large_hints': True}

        layout = Layout(SFieldset(*fields, **options))

        self.helper = FormHelper()

        self.helper.add_layout(layout)
        self.helper.form_tag = False
        self.helper.use_csrf_protection = True

    def _should_save(self, option, suboptions, disabled):
        return True

    def _display_option(self, option, disabled, suboptions, display_params=None):
        if display_params is None:
            display_params = {}

        #if len(suboptions) > 0:
        #    display_params['children'] = (self._get_sub_id(option),)

        if option.price == 0:
            label = option.name
        else:
            price = option.price

            if float(price).is_integer():
                price = int(price)

            label = '%s (%s,-)' % (option.name, price)

        self.fields[self._get_id(option)] = forms.BooleanField(label=label,
                                                               required=False,
                                                               help_text=option.description,
                                                               widget=forms.CheckboxInput(attrs=display_params))

        if len(suboptions) > 0:
            choices = [(suboption.id, suboption.name) for suboption in suboptions]
            self.fields[self._get_sub_id(option)] = forms.ChoiceField(label=_('Choices'),
                                                                      choices=choices,
                                                                      required=False)

    def _register_clean_function(self, option, selected_initially, disabled):
{'fraction_non_alphanumeric': 0.08778712758699113, 'fraction_numerical': 0.0009097111667045713, 'mean_word_length': 2.118794326241135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2957647', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2193, 'n_words': 487}