Skip to content

Commit 8d7f087

Browse files
committed
docs: Improve docs
1 parent 77d1664 commit 8d7f087

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

docs/advanced.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,27 @@ class UserAccount(LifecycleModel):
2424

2525
@hook(AFTER_UPDATE)
2626
def on_name_change_heck_on_marital_status(self):
27-
if self.has_changed('last_name') and not self.has_changed('marital_status'):
28-
send_mail(to=self.email, "Has your marital status changed recently?")
27+
if (
28+
self.has_changed('last_name')
29+
and not self.has_changed('marital_status')
30+
):
31+
send_mail(
32+
to=self.email,
33+
"Has your marital status changed recently?"
34+
)
2935

3036
```
3137

3238
## Custom conditions <a id="custom-conditions"></a>
3339
Custom conditions can be created as long as they respect condition signature
3440
```python
35-
def changes_to_ned_flanders(instance, update_fields=None):
36-
...
41+
def changes_to_ned_flanders(instance, update_fields=None) -> bool:
42+
return (
43+
instance.has_changed("first_name")
44+
and instance.has_changed("last_name")
45+
and instance.first_name == "Ned"
46+
and instance.last_name == "Flanders"
47+
)
3748
```
3849

3950
To allow your custom conditions to be chainable, create a class based condition inheriting `ChainableCondition`.
@@ -45,12 +56,19 @@ from django_lifecycle.conditions.base import ChainableCondition
4556

4657
class IsNedFlanders(ChainableCondition):
4758
def __call__(self, instance, update_fields=None):
48-
return instance.first_name == "Ned" and instance.last_name == "Flanders"
59+
return (
60+
instance.first_name == "Ned"
61+
and instance.last_name == "Flanders"
62+
)
4963

5064

5165
@hook(
5266
BEFORE_SAVE,
53-
condition=WhenFieldHasChanged("first_name") & WhenFieldHasChanged("last_name") & IsNedFlanders()
67+
condition=(
68+
WhenFieldHasChanged("first_name")
69+
& WhenFieldHasChanged("last_name")
70+
& IsNedFlanders()
71+
)
5472
)
5573
def foo():
5674
...

docs/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class Article(LifecycleModel):
2020
status = models.ChoiceField(choices=['draft', 'published'])
2121
editor = models.ForeignKey(AuthUser)
2222

23-
@hook(BEFORE_UPDATE, condition=WhenFieldHasChanged('contents', has_changed=True))
23+
@hook(
24+
BEFORE_UPDATE,
25+
condition=WhenFieldHasChanged('contents', has_changed=True),
26+
)
2427
def on_content_change(self):
2528
self.updated_at = timezone.now()
2629

0 commit comments

Comments
 (0)