Skip to content

Commit 91177ee

Browse files
committed
fix: Fix imports and use relative imports
1 parent b2d8815 commit 91177ee

File tree

8 files changed

+48
-106
lines changed

8 files changed

+48
-106
lines changed

django_lifecycle/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__author__ = "Robert Singer"
33
__author_email__ = "[email protected]"
44

5+
from .constants import NotSet
56
from .decorators import hook
67
from .hooks import AFTER_CREATE
78
from .hooks import AFTER_DELETE
@@ -14,11 +15,6 @@
1415
from .mixins import LifecycleModelMixin
1516
from .models import LifecycleModel
1617

17-
18-
class NotSet(object):
19-
pass
20-
21-
2218
__all__ = [
2319
"hook",
2420
"LifecycleModelMixin",

django_lifecycle/conditions/__init__.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from typing import Iterable
66
from typing import Union
77

8-
from django_lifecycle import NotSet
9-
from django_lifecycle.conditions.base import ChainableCondition
8+
from ..constants import NotSet
9+
from ..conditions.base import ChainableCondition
1010

1111

1212
__all__ = [
@@ -25,9 +25,7 @@ class WhenFieldValueWas(ChainableCondition):
2525
field_name: str
2626
value: Any = "*"
2727

28-
def __call__(
29-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
30-
) -> bool:
28+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
3129
return self.value in (instance.initial_value(self.field_name), "*")
3230

3331

@@ -36,9 +34,7 @@ class WhenFieldValueIs(ChainableCondition):
3634
field_name: str
3735
value: Any = "*"
3836

39-
def __call__(
40-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
41-
) -> bool:
37+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
4238
return self.value in (instance._current_value(self.field_name), "*")
4339

4440

@@ -47,73 +43,47 @@ class WhenFieldHasChanged(ChainableCondition):
4743
field_name: str
4844
has_changed: bool | None = None
4945

50-
def __call__(
51-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
52-
) -> bool:
46+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
5347
is_partial_fields_update = update_fields is not None
54-
is_synced = (
55-
is_partial_fields_update is False or self.field_name in update_fields
56-
)
48+
is_synced = is_partial_fields_update is False or self.field_name in update_fields
5749
if not is_synced:
5850
return False
5951

60-
return self.has_changed is None or self.has_changed == instance.has_changed(
61-
self.field_name
62-
)
52+
return self.has_changed is None or self.has_changed == instance.has_changed(self.field_name)
6353

6454

6555
@dataclass
6656
class WhenFieldValueIsNot(ChainableCondition):
6757
field_name: str
6858
value: Any = NotSet
6959

70-
def __call__(
71-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
72-
) -> bool:
73-
return (
74-
self.value is NotSet
75-
or instance._current_value(self.field_name) != self.value
76-
)
60+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
61+
return self.value is NotSet or instance._current_value(self.field_name) != self.value
7762

7863

7964
@dataclass
8065
class WhenFieldValueWasNot(ChainableCondition):
8166
field_name: str
8267
value: Any = NotSet
8368

84-
def __call__(
85-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
86-
) -> bool:
87-
return (
88-
self.value is NotSet
89-
or instance.initial_value(self.field_name) != self.value
90-
)
69+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
70+
return self.value is NotSet or instance.initial_value(self.field_name) != self.value
9171

9272

9373
@dataclass
9474
class WhenFieldValueChangesTo(ChainableCondition):
9575
field_name: str
9676
value: Any = NotSet
9777

98-
def __call__(
99-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
100-
) -> bool:
78+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
10179
is_partial_fields_update = update_fields is not None
102-
is_synced = (
103-
is_partial_fields_update is False or self.field_name in update_fields
104-
)
80+
is_synced = is_partial_fields_update is False or self.field_name in update_fields
10581
if not is_synced:
10682
return False
10783

108-
value_has_changed = bool(
109-
instance.initial_value(self.field_name) != self.value
110-
)
111-
new_value_is_the_expected = bool(
112-
instance._current_value(self.field_name) == self.value
113-
)
114-
return self.value is NotSet or (
115-
value_has_changed and new_value_is_the_expected
116-
)
84+
value_has_changed = bool(instance.initial_value(self.field_name) != self.value)
85+
new_value_is_the_expected = bool(instance._current_value(self.field_name) == self.value)
86+
return self.value is NotSet or (value_has_changed and new_value_is_the_expected)
11787

11888

11989
class Always:

django_lifecycle/conditions/base.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Iterable
88
from typing import Union
99

10-
from django_lifecycle import types
10+
from .. import types
1111

1212

1313
@dataclass
@@ -28,9 +28,7 @@ def __and__(self, other):
2828
def __or__(self, other):
2929
return ChainedCondition(self, other, operator=operator.or_)
3030

31-
def __call__(
32-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
33-
) -> bool:
31+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
3432
left_result = self.left(instance, update_fields)
3533
right_result = self.right(instance, update_fields)
3634
return self.operator(left_result, right_result)
@@ -45,7 +43,5 @@ def __and__(self, other) -> ChainedCondition:
4543
def __or__(self, other) -> ChainedCondition:
4644
return ChainedCondition(self, other, operator=operator.or_)
4745

48-
def __call__(
49-
self, instance: Any, update_fields: Union[Iterable[str], None] = None
50-
) -> bool:
46+
def __call__(self, instance: Any, update_fields: Union[Iterable[str], None] = None) -> bool:
5147
...

django_lifecycle/conditions/legacy.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from typing import List
66
from typing import Optional
77

8-
from django_lifecycle import NotSet
9-
from django_lifecycle.conditions.base import ChainableCondition
10-
from django_lifecycle.conditions import WhenFieldValueChangesTo
11-
from django_lifecycle.conditions import WhenFieldHasChanged
12-
from django_lifecycle.conditions import WhenFieldValueIsNot
13-
from django_lifecycle.conditions import WhenFieldValueIs
14-
from django_lifecycle.conditions import WhenFieldValueWas
15-
from django_lifecycle.conditions import WhenFieldValueWasNot
8+
from ..constants import NotSet
9+
from ..conditions.base import ChainableCondition
10+
from ..conditions import WhenFieldValueChangesTo
11+
from ..conditions import WhenFieldHasChanged
12+
from ..conditions import WhenFieldValueIsNot
13+
from ..conditions import WhenFieldValueIs
14+
from ..conditions import WhenFieldValueWas
15+
from ..conditions import WhenFieldValueWasNot
1616

1717

1818
@dataclass
@@ -26,9 +26,7 @@ class When(ChainableCondition):
2626
changes_to: Any = NotSet
2727

2828
def __call__(self, instance: Any, update_fields=None) -> bool:
29-
has_changed_condition = WhenFieldHasChanged(
30-
self.when, has_changed=self.has_changed
31-
)
29+
has_changed_condition = WhenFieldHasChanged(self.when, has_changed=self.has_changed)
3230
if not has_changed_condition(instance, update_fields=update_fields):
3331
return False
3432

@@ -78,6 +76,4 @@ def __call__(self, instance: Any, update_fields=None) -> bool:
7876
)
7977
for field in self.when_any
8078
)
81-
return any(
82-
condition(instance, update_fields=update_fields) for condition in conditions
83-
)
79+
return any(condition(instance, update_fields=update_fields) for condition in conditions)

django_lifecycle/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class NotSet(object):
2+
pass

django_lifecycle/decorators.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from __future__ import annotations
22

33
import operator
4-
from django_lifecycle import types
54
from dataclasses import dataclass
65
from functools import reduce
76
from functools import wraps
87
from typing import Any
98
from typing import Callable
10-
from typing import List, Optional
9+
from typing import List
10+
from typing import Optional
1111

12-
from django_lifecycle import NotSet
12+
from . import types
1313
from .conditions import Always
1414
from .conditions.legacy import When
15+
from .constants import NotSet
1516
from .dataclass_validation import Validations
1617
from .hooks import VALID_HOOKS
1718
from .priority import DEFAULT_PRIORITY
@@ -110,8 +111,7 @@ def validate_when_any(self, value, **kwargs):
110111
return
111112

112113
when_any_error_msg = (
113-
"'when_any' hook param must be a list of strings "
114-
"matching the names of model fields"
114+
"'when_any' hook param must be a list of strings " "matching the names of model fields"
115115
)
116116

117117
if not isinstance(value, list):
@@ -145,9 +145,7 @@ def validate_on_commit(self, value, **kwargs):
145145

146146
def validate_priority(self, value, **kwargs):
147147
if self.priority < 0:
148-
raise DjangoLifeCycleException(
149-
"'priority' hook param must be a positive integer"
150-
)
148+
raise DjangoLifeCycleException("'priority' hook param must be a positive integer")
151149

152150
return value
153151

@@ -159,9 +157,7 @@ def validate_on_commit_only_for_after_hooks(self):
159157

160158
def validate_when_and_when_any(self):
161159
if self.when is not None and self.when_any is not None:
162-
raise DjangoLifeCycleException(
163-
"Can pass either 'when' or 'when_any' but not both"
164-
)
160+
raise DjangoLifeCycleException("Can pass either 'when' or 'when_any' but not both")
165161

166162
def validate_condition_and_legacy_parameters_are_not_combined(self):
167163
if self.condition is not None and self._legacy_parameters_have_been_passed():

tests/testapp/tests/test_conditions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.test import TestCase
22

3-
from django_lifecycle import NotSet
3+
from django_lifecycle.constants import NotSet
44
from django_lifecycle.conditions import WhenFieldValueChangesTo
55
from django_lifecycle.conditions import WhenFieldHasChanged
66
from django_lifecycle.conditions import WhenFieldValueIsNot

tests/testapp/tests/test_mixin.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import django
44
from django.test import TestCase
55

6-
from django_lifecycle import NotSet
6+
from django_lifecycle.constants import NotSet
77
from django_lifecycle.decorators import HookConfig
88
from django_lifecycle.priority import DEFAULT_PRIORITY
99
from tests.testapp.models import CannotRename
@@ -66,9 +66,7 @@ def test_initial_value_for_fk_model_field(self):
6666
)
6767

6868
user_account = UserAccount.objects.get()
69-
self.assertEqual(
70-
user_account.initial_value("organization.name"), "Dunder Mifflin"
71-
)
69+
self.assertEqual(user_account.initial_value("organization.name"), "Dunder Mifflin")
7270

7371
def test_initial_value_if_field_has_changed(self):
7472
data = self.stub_data
@@ -100,17 +98,13 @@ def test_current_value_for_watched_fk_model_field(self):
10098
UserAccount.objects.create(**self.stub_data, organization=org)
10199
user_account = UserAccount.objects.get()
102100

103-
self.assertEqual(
104-
user_account._current_value("organization.name"), "Dunder Mifflin"
105-
)
101+
self.assertEqual(user_account._current_value("organization.name"), "Dunder Mifflin")
106102

107103
org.name = "Dwight's Paper Empire"
108104
org.save()
109105
user_account._clear_watched_fk_model_cache()
110106

111-
self.assertEqual(
112-
user_account._current_value("organization.name"), "Dwight's Paper Empire"
113-
)
107+
self.assertEqual(user_account._current_value("organization.name"), "Dwight's Paper Empire")
114108

115109
def test_run_hooked_methods_for_when(self):
116110
instance = UserAccount(first_name="Bob")
@@ -338,19 +332,15 @@ def test_changes_to_condition_should_pass(self):
338332
data = self.stub_data
339333
UserAccount.objects.create(**data)
340334
user_account = UserAccount.objects.get()
341-
with self.assertRaises(
342-
CannotRename, msg="Oh, not Flanders. Anybody but Flanders."
343-
):
335+
with self.assertRaises(CannotRename, msg="Oh, not Flanders. Anybody but Flanders."):
344336
user_account.last_name = "Flanders"
345337
user_account.save()
346338

347339
def test_changes_to_condition_included_in_update_fields_should_fire_hook(self):
348340
user_account = UserAccount.objects.create(**self.stub_data)
349341
user_account.first_name = "Flanders"
350342
user_account.last_name = "Flanders"
351-
with self.assertRaises(
352-
CannotRename, msg="Oh, not Flanders. Anybody but Flanders."
353-
):
343+
with self.assertRaises(CannotRename, msg="Oh, not Flanders. Anybody but Flanders."):
354344
user_account.last_name = "Flanders"
355345
user_account.save(update_fields=["last_name"])
356346

@@ -360,9 +350,7 @@ def test_changes_to_condition_not_included_in_update_fields_should_not_fire_hook
360350
user_account = UserAccount.objects.create(**self.stub_data)
361351
user_account.first_name = "Flanders"
362352
user_account.last_name = "Flanders"
363-
user_account.save(
364-
update_fields=["first_name"]
365-
) # `CannotRename` exception is not raised
353+
user_account.save(update_fields=["first_name"]) # `CannotRename` exception is not raised
366354

367355
user_account.refresh_from_db()
368356
self.assertEqual(user_account.first_name, "Flanders")
@@ -486,9 +474,7 @@ def test_run_hooked_methods_for_on_commit(self):
486474
),
487475
MagicMock(
488476
__name__="after_save_method_that_fires_if_changed_on_commit",
489-
_hooked=[
490-
HookConfig(hook="after_save", has_changed=True, on_commit=True)
491-
],
477+
_hooked=[HookConfig(hook="after_save", has_changed=True, on_commit=True)],
492478
),
493479
]
494480
)

0 commit comments

Comments
 (0)