from django import forms from django_scopes.forms import SafeModelChoiceField, SafeModelMultipleChoiceField from i18nfield.forms import I18nModelForm from .models import MusicrateSettings class MusicrateSettingsForm(I18nModelForm): def __init__(self, *args, event=None, **kwargs): self.instance, _ = MusicrateSettings.objects.get_or_create(event=event) super().__init__(*args, **kwargs, instance=self.instance, locales=event.locales) if not event.submission_types.count(): self.fields.pop("submission_types") else: self.fields["submission_types"].queryset = event.submission_types.all() self.fields["genre_question"].queryset = ( event.questions.exclude(pk=self.instance.origin_question.pk) if self.instance.origin_question else event.questions.all() ) self.fields["origin_question"].queryset = ( event.questions.exclude(pk=self.instance.genre_question.pk) if self.instance.genre_question else event.questions.all() ) class Meta: model = MusicrateSettings fields = ( "submission_types", "genre_question", "origin_question", "advance_threshold", ) widgets = { "submission_types": forms.SelectMultiple(attrs={"class": "select2"}), "genre_question": forms.Select(attrs={"class": "select2"}), "origin_question": forms.Select(attrs={"class": "select2"}), } field_classes = { "submission_types": SafeModelMultipleChoiceField, "genre_question": SafeModelChoiceField, "origin_question": SafeModelChoiceField, }