Django Library PyGal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signals.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.db.models.signals import post_save
  2. from django.db.models.signals import post_delete
  3. from django.dispatch import receiver
  4. import logging
  5. from .models import Item, Tag
  6. from .search import load_index, delete_item, update_item
  7. import shutil
  8. from .views.xnail import base_item
  9. # Get a logger instance
  10. try:
  11. from config import APP_NAME as ROOT_LOGGER_NAME
  12. except ImportError:
  13. ROOT_LOGGER_NAME = 'root'
  14. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  15. __pre_remove__ = None
  16. __pre_add__ = None
  17. @receiver(post_delete, sender=Item)
  18. def item_delete(instance, **kwargs):
  19. # delete cached xnails
  20. clogger.info('Deleting Xnails stored in "%s".', base_item.__cache_image_folder__(None, instance.rel_path))
  21. shutil.rmtree(base_item.__cache_image_folder__(None, instance.rel_path), True)
  22. # delete index entry
  23. ix = load_index()
  24. delete_item(ix, instance)
  25. @receiver(post_save, sender=Item)
  26. def item_save(instance, **kwargs):
  27. ix = load_index()
  28. update_item(ix, instance)
  29. @receiver(post_save, sender=Tag)
  30. @receiver(post_delete, sender=Tag)
  31. def tag_change(instance, **kwargs):
  32. ix = load_index()
  33. update_item(ix, instance.item)