<commit_before>from django import forms
from django.db import models

from organisations.models import Organisation
from organisations.models import OrganisationDivisionSet

from .models import ElectionSubType, ElectionType

from dc_theme import forms as dc_forms


#
# Forms:
#   ElectionDateForm
#   ElectionTypeForm
#   ElectionOrganisationForm


class ElectionSourceForm(forms.Form):
    source = forms.CharField(
        required=True,
        max_length=1000,
        label="Where did you find out about this election?",
    )
    document = forms.URLField(
        required=False, max_length=1000, label="Link to 'Notice of Election' Document"
    )


class ElectionDateForm(forms.Form):
    date = dc_forms.DCDateField(help_text="The date that polls open")


class ElectionTypeForm(forms.Form):
    election_type = forms.ModelChoiceField(
        queryset=ElectionType.objects.exclude(organisation=None),
        widget=forms.RadioSelect,
        to_field_name="election_type",
        empty_label=None,
    )


class ElectionSubTypeForm(forms.Form):
    def __init__(self, *args, **kwargs):
        election_type = kwargs.pop("election_type", None)
        kwargs.pop("election_date", None)
        super().__init__(*args, **kwargs)
        if election_type:
            qs = self.fields["election_subtype"].queryset
            qs = qs.filter(election_type__election_type=election_type)
            self.fields["election_subtype"].queryset = qs

    election_subtype = forms.ModelMultipleChoiceField(
        queryset=ElectionSubType.objects.all(), widget=forms.CheckboxSelectMultiple
    )


class ElectionOrganisationForm(forms.Form):
    def __init__(self, *args, **kwargs):
        election_type = kwargs.pop("election_type", None)
        election_date = kwargs.pop("election_date", None)
        super().__init__(*args, **kwargs)
        if election_type:
            qs = self.fields["election_organisation"].queryset
            qs = qs.filter(election_types__election_type=election_type).filter_by_date(
                election_date
            )

            if len(qs) == 0:
                self.fields["election_organisation"] = forms.CharField(
                    widget=forms.TextInput(attrs={"class": "hide"}),
                    required=False,
                    label="No organisations available for this poll date.",
                )
            else:
                self.fields["election_organisation"].queryset = qs

    election_organisation = forms.ModelMultipleChoiceField(<commit_msg>fix organisation names in wizard<commit_after>from django import forms
from django.db import models

from organisations.models import Organisation
from organisations.models import OrganisationDivisionSet

from .models import ElectionSubType, ElectionType

from dc_theme import forms as dc_forms


#
# Forms:
#   ElectionDateForm
#   ElectionTypeForm
#   ElectionOrganisationForm


class ElectionSourceForm(forms.Form):
    source = forms.CharField(
        required=True,
        max_length=1000,
        label="Where did you find out about this election?",
    )
    document = forms.URLField(
        required=False, max_length=1000, label="Link to 'Notice of Election' Document"
    )


class ElectionDateForm(forms.Form):
    date = dc_forms.DCDateField(help_text="The date that polls open")


class ElectionTypeForm(forms.Form):
    election_type = forms.ModelChoiceField(
        queryset=ElectionType.objects.exclude(organisation=None),
        widget=forms.RadioSelect,
        to_field_name="election_type",
        empty_label=None,
    )


class ElectionSubTypeForm(forms.Form):
    def __init__(self, *args, **kwargs):
        election_type = kwargs.pop("election_type", None)
        kwargs.pop("election_date", None)
        super().__init__(*args, **kwargs)
        if election_type:
            qs = self.fields["election_subtype"].queryset
            qs = qs.filter(election_type__election_type=election_type)
            self.fields["election_subtype"].queryset = qs

    election_subtype = forms.ModelMultipleChoiceField(
        queryset=ElectionSubType.objects.all(), widget=forms.CheckboxSelectMultiple
    )


class ElectionOrganisationField(forms.ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.name


class ElectionOrganisationForm(forms.Form):
    def __init__(self, *args, **kwargs):
        election_type = kwargs.pop("election_type", None)
        election_date = kwargs.pop("election_date", None)
        super().__init__(*args, **kwargs)
        if election_type:
            qs = self.fields["election_organisation"].queryset
            qs = qs.filter(election_types__election_type=election_type).filter_by_date(
                election_date
            )

            if len(qs) == 0:
                self.fields["election_organisation"] = forms.CharField(
                    widget=forms.TextInput(attrs={"class": "hide"}),
                    required=False,
                    label="No organisations available for this poll date.",
                )
            else:
                self.fields["election_organisation"].queryset = qs

    election_organisation = ElectionOrganisationField(
{'fraction_non_alphanumeric': 0.09432146294513956, 'fraction_numerical': 0.0034648700673724736, 'mean_word_length': 3.0123552123552124, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749999', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1389, 'n_words': 284}