1
1
from __future__ import annotations
2
2
3
- import asyncio
4
3
from pathlib import Path
5
4
from typing import TYPE_CHECKING , Any , Callable , Union , cast
6
5
from uuid import uuid4
20
19
)
21
20
from reactpy_django .forms .utils import convert_boolean_fields , convert_multiple_choice_fields
22
21
from reactpy_django .types import AsyncFormEvent , FormEventData , SyncFormEvent
22
+ from reactpy_django .utils import ensure_async
23
23
24
24
if TYPE_CHECKING :
25
25
from collections .abc import Sequence
@@ -80,15 +80,9 @@ async def render_form():
80
80
await database_sync_to_async (initialized_form .full_clean )()
81
81
success = not initialized_form .errors .as_data ()
82
82
if success and on_success :
83
- if asyncio .iscoroutinefunction (on_success ):
84
- await on_success (form_event )
85
- else :
86
- on_success (form_event )
83
+ await ensure_async (on_success )(form_event )
87
84
if not success and on_error :
88
- if asyncio .iscoroutinefunction (on_error ):
89
- await on_error (form_event )
90
- else :
91
- on_error (form_event )
85
+ await ensure_async (on_error )(form_event )
92
86
if success and auto_save and isinstance (initialized_form , ModelForm ):
93
87
await database_sync_to_async (initialized_form .save )()
94
88
set_submitted_data (None )
@@ -109,21 +103,15 @@ async def on_submit_callback(new_data: dict[str, Any]):
109
103
new_form_event = FormEventData (
110
104
form = initialized_form , submitted_data = new_data , set_submitted_data = set_submitted_data
111
105
)
112
- if asyncio .iscoroutinefunction (on_receive_data ):
113
- await on_receive_data (new_form_event )
114
- else :
115
- on_receive_data (new_form_event )
106
+ await ensure_async (on_receive_data )(new_form_event )
116
107
117
108
if submitted_data != new_data :
118
109
set_submitted_data (new_data )
119
110
120
111
async def _on_change (_event ):
121
112
"""Event that exist solely to allow the user to detect form changes."""
122
113
if on_change :
123
- if asyncio .iscoroutinefunction (on_change ):
124
- await on_change (form_event )
125
- else :
126
- on_change (form_event )
114
+ await ensure_async (on_change )(form_event )
127
115
128
116
if not rendered_form :
129
117
return None
0 commit comments