-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (30 loc) · 1.49 KB
/
utils.py
File metadata and controls
40 lines (30 loc) · 1.49 KB
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
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from django.forms import BooleanField, Form, ModelForm, ModelMultipleChoiceField, MultipleChoiceField, NullBooleanField
if TYPE_CHECKING:
from collections.abc import Sequence
from reactpy import Ref
def convert_form_fields(data: dict[str, Any], initialized_form: Form | ModelForm) -> None:
for field_name, field in initialized_form.fields.items():
value = data.get(field_name)
if isinstance(field, (MultipleChoiceField, ModelMultipleChoiceField)) and value is not None:
data[field_name] = value if isinstance(value, list) else [value]
elif isinstance(field, BooleanField) and not isinstance(field, NullBooleanField):
data[field_name] = field_name in data
def validate_form_args(
top_children: Sequence,
top_children_count: Ref[int],
bottom_children: Sequence,
bottom_children_count: Ref[int],
form: type[Form | ModelForm],
) -> None:
# Validate the provided arguments
if len(top_children) != top_children_count.current or len(bottom_children) != bottom_children_count.current:
msg = "Dynamically changing the number of top or bottom children is not allowed."
raise ValueError(msg)
if not isinstance(form, (type(Form), type(ModelForm))):
msg = (
"The provided form must be an uninitialized Django Form. "
"Do NOT initialize your form by calling it (ex. `MyForm()`)."
)
raise TypeError(msg)