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.
 
 
 
 
 
 

68 lines
2.0 KiB

  1. import logging
  2. from django.dispatch import receiver
  3. from django.utils.module_loading import import_string
  4. from django.conf import settings
  5. from django.db import models
  6. from core.models import Image
  7. from django_images.models import Thumbnail
  8. _plugins = getattr(settings, "ENABLED_PLUGINS", [])
  9. _plugin_instances = []
  10. def _load_plugins():
  11. for plugin_path in _plugins:
  12. plugin_cls = import_string(plugin_path)
  13. _plugin_instances.append(plugin_cls())
  14. @receiver(models.signals.pre_save, sender=Image)
  15. def process_image_pre_creation(sender, instance: Image, **kwargs):
  16. # FIXME(winkidney): May have issue on determining if it
  17. # is created or not
  18. if instance.pk is not None:
  19. return
  20. for plugin in _plugin_instances:
  21. process_fn = getattr(plugin, "process_image_pre_creation", None)
  22. if process_fn is None:
  23. continue
  24. try:
  25. process_fn(
  26. django_settings=settings,
  27. image_instance=instance,
  28. )
  29. except Exception:
  30. logging.exception(
  31. "Error occurs while trying to access plugin's pin_pre_save "
  32. "for plugin %s" % plugin
  33. )
  34. @receiver(models.signals.pre_save, sender=Thumbnail)
  35. def process_thumbnail_pre_creation(sender, instance: Thumbnail, **kwargs):
  36. # FIXME(winkidney): May have issue on determining if it
  37. # is created or not
  38. if instance.pk is not None:
  39. return
  40. for plugin in _plugin_instances:
  41. process_fn = getattr(plugin, "process_thumbnail_pre_creation", None)
  42. if process_fn is None:
  43. continue
  44. try:
  45. process_fn(
  46. django_settings=settings,
  47. thumbnail_instance=instance,
  48. )
  49. except Exception:
  50. logging.exception(
  51. "Error occurs while trying to access plugin's process_thumbnail_pre_creation "
  52. "for plugin %s" % plugin
  53. )
  54. def init():
  55. _load_plugins()