Skip to content

Commit

Permalink
Returns value in delete method override
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinger86 committed Jan 25, 2021
1 parent 5f83023 commit 2834786
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ Instead of overriding `save` and `__init__` in a clunky way that hurts readabili

# Changelog

## 0.8.1 (January 2021)
* Added missing return to `delete()` method override. Thanks @oaosman84!

## 0.8.0 (October 2020)
* Significant performance improvements. Thanks @dralley!


## 0.7.7 (August 2020)
* Fixes issue with `GenericForeignKey`. Thanks @bmbouter!

Expand Down
2 changes: 1 addition & 1 deletion django_lifecycle/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .django_info import IS_GTE_1_POINT_9

__version__ = "0.8.0"
__version__ = "0.8.1"
__author__ = "Robert Singer"
__author_email__ = "[email protected]"

Expand Down
32 changes: 22 additions & 10 deletions django_lifecycle/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

from . import NotSet
from .hooks import (
BEFORE_CREATE, BEFORE_UPDATE,
BEFORE_SAVE, BEFORE_DELETE,
AFTER_CREATE, AFTER_UPDATE,
AFTER_SAVE, AFTER_DELETE,
BEFORE_CREATE,
BEFORE_UPDATE,
BEFORE_SAVE,
BEFORE_DELETE,
AFTER_CREATE,
AFTER_UPDATE,
AFTER_SAVE,
AFTER_DELETE,
)

from .django_info import DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES
Expand Down Expand Up @@ -138,8 +142,9 @@ def save(self, *args, **kwargs):

def delete(self, *args, **kwargs):
self._run_hooked_methods(BEFORE_DELETE)
super().delete(*args, **kwargs)
value = super().delete(*args, **kwargs)
self._run_hooked_methods(AFTER_DELETE)
return value

@classmethod
@lru_cache(typed=True)
Expand Down Expand Up @@ -253,10 +258,15 @@ def _check_was_not_condition(self, field_name: str, specs: dict) -> bool:

def _check_changes_to_condition(self, field_name: str, specs: dict) -> bool:
changes_to = specs["changes_to"]
return any([
changes_to is NotSet,
(self.initial_value(field_name) != changes_to and self._current_value(field_name) == changes_to)
])
return any(
[
changes_to is NotSet,
(
self.initial_value(field_name) != changes_to
and self._current_value(field_name) == changes_to
),
]
)

@classmethod
def _get_model_property_names(cls) -> List[str]:
Expand All @@ -270,7 +280,9 @@ def _get_model_property_names(cls) -> List[str]:
for name in dir(cls):
attr = getattr(cls, name, None)

if attr and (isinstance(attr, property) or isinstance(attr, cached_property)):
if attr and (
isinstance(attr, property) or isinstance(attr, cached_property)
):
property_names.append(name)

return property_names
Expand Down
11 changes: 11 additions & 0 deletions tests/testapp/tests/test_user_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,14 @@ def test_skip_hooks(self):
account.email = "Homer.Simpson@springfieldnuclear"
account.save(skip_hooks=True)
self.assertEqual(account.email, "Homer.Simpson@springfieldnuclear")

def test_delete_should_return_default_django_value(self):
"""
Hooked method that auto-lowercases email should be skipped.
"""
UserAccount.objects.create(**self.stub_data)
value = UserAccount.objects.all().delete()

self.assertEqual(
value, (1, {"testapp.Locale_users": 0, "testapp.UserAccount": 1})
)

0 comments on commit 2834786

Please sign in to comment.