Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

64 wiersze
1.9 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")
  22. try:
  23. process_fn(
  24. django_settings=settings,
  25. image_instance=instance,
  26. )
  27. except Exception:
  28. logging.exception(
  29. "Error occurs while trying to access plugin's pin_pre_save "
  30. "for plugin %s" % plugin
  31. )
  32. @receiver(models.signals.pre_save, sender=Thumbnail)
  33. def process_thumbnail_pre_creation(sender, instance: Thumbnail, **kwargs):
  34. # FIXME(winkidney): May have issue on determining if it
  35. # is created or not
  36. if instance.pk is not None:
  37. return
  38. for plugin in _plugin_instances:
  39. process_fn = getattr(plugin, "process_thumbnail_pre_creation")
  40. try:
  41. process_fn(
  42. django_settings=settings,
  43. thumbnail_instance=instance,
  44. )
  45. except Exception:
  46. logging.exception(
  47. "Error occurs while trying to access plugin's process_thumbnail_pre_creation "
  48. "for plugin %s" % plugin
  49. )
  50. def init():
  51. _load_plugins()