Skip to content

Commit

Permalink
feat: backend - book delete
Browse files Browse the repository at this point in the history
signals folder created to hold every signals

on delete book, a signal delete the book file, cover image physically from the file directory
  • Loading branch information
rkshaon committed Sep 8, 2024
1 parent aad6c5e commit 7a773d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/book_api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
class BookApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'book_api'

def ready(self) -> None:
import book_api.signals # noqa
return super().ready()
7 changes: 7 additions & 0 deletions backend/book_api/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# book_api/signals/__init__.py
from book_api.signals.book_delete import delete_book_files


__all__ = [
delete_book_files,
]
14 changes: 14 additions & 0 deletions backend/book_api/signals/book_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db.models.signals import post_delete
from django.dispatch import receiver

from book_api.models import Book


@receiver(post_delete, sender=Book)
def delete_book_files(sender, instance, *args, **kwargs):
print(instance)
if instance.book:
instance.book.delete(save=False)

if instance.cover_image:
instance.cover_image.delete(save=False)

0 comments on commit 7a773d0

Please sign in to comment.