Skip to content

Commit

Permalink
Merge branch '54-fix-delete-history' into 'development'
Browse files Browse the repository at this point in the history
fix: History does not delete when item deleted

Closes #54

See merge request nofusscomputing/projects/django_template!25
  • Loading branch information
jon-nfc committed Jun 13, 2024
2 parents 904234c + 7239f57 commit d4c07d0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
44 changes: 43 additions & 1 deletion app/core/mixin/history_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,65 @@ def save(self, *args, **kwargs):
self.save_history(before, after)


def delete_history(self, item_pk, item_class):
""" Delete the objects history
When an object is no longer in the database, delete the objects history and
that of the child objects. Only caveat is that if the history has a parent_pk
the object history is not to be deleted.
Args:
item_pk (int): Primary key of the object to be deleted
item_class (str): Object class of the object to be deleted
"""

object_history = History.objects.filter(
item_pk = item_pk,
item_class = item_class,
item_parent_pk = None,
)

if object_history.exists():

object_history.delete()

child_object_history = History.objects.filter(
item_parent_pk = item_pk,
item_parent_class = item_class,
)

if child_object_history.exists():

child_object_history.delete()


def delete(self, using=None, keep_parents=False):
""" OverRides delete for keeping model history and on parent object ONLY!.
Not a Full-Override as this is just to add to existing.
"""

before = {}
item_pk = self.pk
item_class = self._meta.model_name

try:

before = self.__class__.objects.get(pk=self.pk).__dict__.copy()

except Exception:

pass

# Process the save
# Process the delete
super().delete(using=using, keep_parents=keep_parents)

after = self.__dict__.copy()

if hasattr(self, 'parent_object'):

self.save_history(before, after)

else:

self.delete_history(item_pk, item_class)
9 changes: 9 additions & 0 deletions app/core/tests/test_history/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ def test_history_delete_calls_save_history():
This method saves the delete history to the database for parent objects
"""
pass


@pytest.mark.skip(reason="to be written")
def test_history_delete_calls_delete_history():
""" During model delete, self.delete_history is called
This method deletes the item and child-item history from the database
"""
pass

0 comments on commit d4c07d0

Please sign in to comment.