Skip to content

Commit a7d1f2a

Browse files
committed
Update documentation for 0.54.0.
1 parent 8c25809 commit a7d1f2a

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

docs/source/actions.md

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PassingArgsView(UnicornView):
5959

6060
### Argument types
6161

62-
Arguments can be most basic Python types, including `string`, `int`, `float`, `bool`, `list`, `tuple`, `dictionary`, `set`, `datetime`, and `UUID4`.
62+
Most basic Python types, including `string`, `int`, `float`, `bool`, `list`, `tuple`, `dictionary`, and `set`, are supported by default.
6363

6464
```html
6565
<!-- argument-types.html -->
@@ -71,19 +71,100 @@ Arguments can be most basic Python types, including `string`, `int`, `float`, `b
7171
<button unicorn:click="update([1, 2, 3])">Pass list</button>
7272
<button unicorn:click="update((1, 2, 3))">Pass tuple</button>
7373
<button unicorn:click="update({1, 2, 3})">Pass set</button>
74-
<button unicorn:click="update(2020-09-12T01:01:01)">Pass datetime</button>
75-
<button unicorn:click="update(90144cb9-fc47-476d-b124-d543b0cff091)">
76-
Pass UUID
77-
</button>
7874
</div>
7975
```
8076

77+
#### Coerced types
78+
79+
Arguments with the types of `datetime`, `date`, `time`, `timedelta`, and `UUID` can be coerced by using a type annotation in the action method.
80+
81+
```{note}
82+
[Django's `dateparse` methods](https://docs.djangoproject.com/en/stable/ref/utils/#module-django.utils.dateparse) are used to convert strings to the date-related type.
83+
```
84+
8185
```{note}
82-
Strings will be converted to `datetime` if they are successfully parsed by Django's [`parse_datetime`](https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.dateparse.parse_datetime) method.
86+
Both integer and float epochs can also be coerced into `datetime` or `date` objects.
87+
```
88+
89+
```python
90+
# argument_type_hints.py
91+
from django_unicorn.components import UnicornView
92+
from datetime import date, datetime
93+
from uuid import UUID
94+
95+
class ArgumentTypeHintsView(UnicornView):
96+
def is_datetime(self, obj: datetime):
97+
assert type(obj) is datetime
98+
99+
def is_uuid(self, obj: UUID):
100+
assert type(obj) is UUID
101+
102+
def is_date(self, _date: date = None):
103+
assert type(obj) is _date
104+
```
105+
106+
```html
107+
<!-- argument-type-hints.html -->
108+
<div>
109+
<button unicorn:click="is_datetime('2020-09-12T01:01:01')">Check datetime with string</button>
110+
<button unicorn:click="is_datetime(1691499534)">Check datetime with epoch</button>
111+
<button unicorn:click="is_uuid('90144cb9-fc47-476d-b124-d543b0cff091')">Check UUID</button>
112+
<button unicorn:click="is_date(date='2020-09-12')">Check date</button>
113+
</div>
114+
```
115+
116+
#### Django models
117+
118+
Django models can be instantiated as an argument or with a `pk` kwarg and a type annotation.
119+
120+
```python
121+
# argument_model.py
122+
from django_unicorn.components import UnicornView
123+
from django.contrib.auth.models import User
124+
125+
class ArgumentModelView(UnicornView):
126+
def is_user_via_arg(self, obj: User):
127+
assert type(obj) is User
128+
129+
def is_user_via_kwarg(self, pk: User=None):
130+
assert type(obj) is User
83131
```
84132

85-
````{note}
86-
Enums themselves cannot be passed as an argument, but the enum _value_ can be since that is a Python primitive. In the component's view, use the enum's constructor to convert the primitive back into the enum if needed.
133+
```html
134+
<!-- argument-model.html -->
135+
<div>
136+
<button unicorn:click="is_user_via_arg(1)">Gets user with pk of 1</button>
137+
<button unicorn:click="is_user_via_kwarg(pk=2)">Gets user with pk of 2</button>
138+
</div>
139+
```
140+
141+
#### Custom types
142+
143+
Custom objects can also be used as a type annotation and `Unicorn` will attempt to instantiate the object with the value that is passed in as the argument.
144+
145+
```python
146+
# argument_custom_type.py
147+
from django_unicorn.components import UnicornView
148+
149+
class CustomType:
150+
def __init__(self, custom_type_id: int):
151+
self.custom_type_id = custom_type_id
152+
153+
class ArgumentCustomTypeView(UnicornView):
154+
def is_custom_type(self, obj: CustomType):
155+
assert type(obj) is CustomType
156+
```
157+
158+
```html
159+
<!-- argument-custom-type.html -->
160+
<div>
161+
<button unicorn:click="is_custom_type(1)">Gets custom type</button>
162+
</div>
163+
```
164+
165+
#### Enums
166+
167+
Enums themselves cannot be passed as an argument, but the enum _value_ can be since that is a Python primitive. Use the enum as a type annotation to coerce the value into the specified enum.
87168

88169
```python
89170
# enum.py
@@ -100,8 +181,8 @@ class EnumView(UnicornView):
100181
color = Color.RED
101182
purple_color = Color.PURPLE
102183

103-
def set_color(self, color_int: int):
104-
self.color = Color(color_int)
184+
def set_color(self, color: Color):
185+
self.color = color
105186
```
106187

107188
```html
@@ -121,8 +202,6 @@ class EnumView(UnicornView):
121202
</div>
122203
```
123204

124-
````
125-
126205
## Set shortcut
127206

128207
Actions can also set properties without requiring an explicit method.
@@ -309,7 +388,7 @@ Sometimes you need to trigger a method on a component from regular JavaScript. T
309388
</button>
310389
```
311390

312-
Passing arguments the method call is also supported.
391+
Passing arguments to the method call is also supported.
313392

314393
```html
315394
<!-- index.html -->

docs/source/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.54.0-dev
4+
5+
- Coerce type annotated arguments in an action method to the specified type ([#571](https://github.com/adamghill/django-unicorn/pull/571)).
6+
- Fix: Dictionary fields would sometimes create checksum errors ([#572](https://github.com/adamghill/django-unicorn/pull/572)).
7+
38
## 0.53.0
49

510
- Support passing arguments into a component ([#560](https://github.com/adamghill/django-unicorn/pull/560)).

0 commit comments

Comments
 (0)