|
1 | | -from datetime import date, datetime |
2 | 1 | from typing import Any, Dict, Tuple, Union |
3 | 2 |
|
4 | 3 | from django.db.models import Model |
5 | 4 |
|
6 | 5 | from django_unicorn.call_method_parser import ( |
7 | | - CASTERS, |
8 | 6 | InvalidKwarg, |
9 | 7 | parse_call_method_name, |
10 | 8 | parse_kwarg, |
11 | 9 | ) |
12 | 10 | from django_unicorn.components import UnicornView |
13 | 11 | from django_unicorn.decorators import timed |
14 | | -from django_unicorn.utils import get_method_arguments, get_type_hints |
| 12 | +from django_unicorn.utils import cast_value, get_method_arguments, get_type_hints |
15 | 13 | from django_unicorn.views.action_parsers.utils import set_property_value |
16 | 14 | from django_unicorn.views.objects import ComponentRequest, Return |
17 | 15 | from django_unicorn.views.utils import set_property_from_data |
18 | 16 |
|
19 | 17 |
|
20 | 18 | try: |
21 | | - from typing import get_args, get_origin |
| 19 | + from typing import get_origin |
22 | 20 | except ImportError: |
23 | | - # Fallback to dunder methods for older versions of Python |
24 | | - def get_args(type_hint): |
25 | | - if hasattr(type_hint, "__args__"): |
26 | | - return type_hint.__args__ |
27 | 21 |
|
28 | 22 | def get_origin(type_hint): |
29 | 23 | if hasattr(type_hint, "__origin__"): |
@@ -107,51 +101,6 @@ def handle(component_request: ComponentRequest, component: UnicornView, payload: |
107 | 101 | ) |
108 | 102 |
|
109 | 103 |
|
110 | | -def _cast_value(type_hint, value): |
111 | | - """ |
112 | | - Try to cast the value based on the type hint and |
113 | | - `django_unicorn.call_method_parser.CASTERS`. |
114 | | -
|
115 | | - Additional features: |
116 | | - - convert `int`/`float` epoch to `datetime` or `date` |
117 | | - - instantiate the `type_hint` class with passed-in value |
118 | | - """ |
119 | | - |
120 | | - type_hints = [] |
121 | | - |
122 | | - if get_origin(type_hint) is Union: |
123 | | - for arg in get_args(type_hint): |
124 | | - type_hints.append(arg) |
125 | | - else: |
126 | | - type_hints.append(type_hint) |
127 | | - |
128 | | - for type_hint in type_hints: |
129 | | - caster = CASTERS.get(type_hint) |
130 | | - |
131 | | - if caster: |
132 | | - try: |
133 | | - value = caster(value) |
134 | | - break |
135 | | - except TypeError: |
136 | | - if (type_hint is datetime or type_hint is date) and ( |
137 | | - isinstance(value, int) or isinstance(value, float) |
138 | | - ): |
139 | | - try: |
140 | | - value = datetime.fromtimestamp(value) |
141 | | - |
142 | | - if type_hint is date: |
143 | | - value = value.date() |
144 | | - |
145 | | - break |
146 | | - except ValueError: |
147 | | - pass |
148 | | - else: |
149 | | - value = type_hint(value) |
150 | | - break |
151 | | - |
152 | | - return value |
153 | | - |
154 | | - |
155 | 104 | @timed |
156 | 105 | def _call_method_name( |
157 | 106 | component: UnicornView, method_name: str, args: Tuple[Any], kwargs: Dict[str, Any] |
@@ -207,9 +156,9 @@ def _call_method_name( |
207 | 156 | parsed_kwargs[argument] = DbModel.objects.get(**{key: value}) |
208 | 157 |
|
209 | 158 | elif argument in kwargs: |
210 | | - parsed_kwargs[argument] = _cast_value(type_hint, kwargs[argument]) |
| 159 | + parsed_kwargs[argument] = cast_value(type_hint, kwargs[argument]) |
211 | 160 | else: |
212 | | - parsed_args.append(_cast_value(type_hint, args[len(parsed_args)])) |
| 161 | + parsed_args.append(cast_value(type_hint, args[len(parsed_args)])) |
213 | 162 | elif argument in kwargs: |
214 | 163 | parsed_kwargs[argument] = kwargs[argument] |
215 | 164 | else: |
|
0 commit comments