/
language_form.py
65 lines (53 loc) · 2.01 KB
/
language_form.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations
from typing import TYPE_CHECKING
from ...models import Language
from ..custom_model_form import CustomModelForm
if TYPE_CHECKING:
from typing import Any
class LanguageForm(CustomModelForm):
"""
Form for creating and modifying language objects
"""
class Meta:
"""
This class contains additional meta configuration of the form class, see the :class:`django.forms.ModelForm`
for more information.
"""
#: The model of this :class:`django.forms.ModelForm`
model = Language
#: The fields of the model which should be handled by this form
fields = [
"slug",
"bcp47_tag",
"english_name",
"native_name",
"text_direction",
"table_of_contents",
"primary_country_code",
"secondary_country_code",
"message_content_not_available",
"message_partial_live_content_not_available",
]
def __init__(self, *args: Any, **kwargs: Any) -> None:
r"""
Initialize language form
:param \*args: The supplied arguments
:param \**kwargs: The supplied keyword arguments
"""
# Instantiate ModelForm
super().__init__(*args, **kwargs)
# Sort countries by translated name
sorted_language_choices = sorted(
self.fields["primary_country_code"].choices, key=lambda x: x[1]
)
self.fields["primary_country_code"].choices = sorted_language_choices
self.fields["secondary_country_code"].choices = sorted_language_choices
# Make left border rounded if no flag is selected yet
if not self.instance.primary_country_code:
self.fields["primary_country_code"].widget.attrs[
"class"
] = "rounded-l border-l"
if not self.instance.secondary_country_code:
self.fields["secondary_country_code"].widget.attrs[
"class"
] = "rounded-l border-l"