From 5c448696472355c155a182d7b9656e962f4b496f Mon Sep 17 00:00:00 2001 From: winkidney Date: Thu, 7 May 2020 23:11:51 +0800 Subject: [PATCH] Feature: Add naive plugin support for pinry --- pinry/settings/base.py | 1 + pinry_plugins/__init__.py | 0 pinry_plugins/admin.py | 3 ++ pinry_plugins/apps.py | 9 +++++ pinry_plugins/batteries/__init__.py | 0 pinry_plugins/batteries/plugin_example.py | 10 +++++ pinry_plugins/builder/__init__.py | 5 +++ pinry_plugins/builder/_loader.py | 63 +++++++++++++++++++++++++++++++ pinry_plugins/migrations/__init__.py | 0 pinry_plugins/models.py | 3 ++ pinry_plugins/tests.py | 3 ++ pinry_plugins/views.py | 3 ++ 12 files changed, 100 insertions(+) create mode 100644 pinry_plugins/__init__.py create mode 100644 pinry_plugins/admin.py create mode 100644 pinry_plugins/apps.py create mode 100644 pinry_plugins/batteries/__init__.py create mode 100644 pinry_plugins/batteries/plugin_example.py create mode 100644 pinry_plugins/builder/__init__.py create mode 100644 pinry_plugins/builder/_loader.py create mode 100644 pinry_plugins/migrations/__init__.py create mode 100644 pinry_plugins/models.py create mode 100644 pinry_plugins/tests.py create mode 100644 pinry_plugins/views.py diff --git a/pinry/settings/base.py b/pinry/settings/base.py index 39aba9e..9b9c313 100644 --- a/pinry/settings/base.py +++ b/pinry/settings/base.py @@ -21,6 +21,7 @@ INSTALLED_APPS = [ 'django_images', 'core', 'users', + 'pinry_plugins.apps.PinryPluginsConfig', ] ROOT_URLCONF = 'pinry.urls' diff --git a/pinry_plugins/__init__.py b/pinry_plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pinry_plugins/admin.py b/pinry_plugins/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/pinry_plugins/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/pinry_plugins/apps.py b/pinry_plugins/apps.py new file mode 100644 index 0000000..fbb8684 --- /dev/null +++ b/pinry_plugins/apps.py @@ -0,0 +1,9 @@ +from django.apps import AppConfig + + +class PinryPluginsConfig(AppConfig): + name = 'pinry_plugins' + + def ready(self): + from pinry_plugins import builder # noqa + builder.init() diff --git a/pinry_plugins/batteries/__init__.py b/pinry_plugins/batteries/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pinry_plugins/batteries/plugin_example.py b/pinry_plugins/batteries/plugin_example.py new file mode 100644 index 0000000..8b2d46b --- /dev/null +++ b/pinry_plugins/batteries/plugin_example.py @@ -0,0 +1,10 @@ +from core.models import Image +from django_images.models import Thumbnail + + +class Plugin: + def process_image_pre_creation(self, django_settings, image_instance: Image): + pass + + def process_thumbnail_pre_creation(self, django_settings, thumbnail_instance: Thumbnail): + pass diff --git a/pinry_plugins/builder/__init__.py b/pinry_plugins/builder/__init__.py new file mode 100644 index 0000000..69d536d --- /dev/null +++ b/pinry_plugins/builder/__init__.py @@ -0,0 +1,5 @@ +from . import _loader + + +def init(): + _loader.init() diff --git a/pinry_plugins/builder/_loader.py b/pinry_plugins/builder/_loader.py new file mode 100644 index 0000000..5621fbe --- /dev/null +++ b/pinry_plugins/builder/_loader.py @@ -0,0 +1,63 @@ +import logging + +from django.dispatch import receiver +from django.utils.module_loading import import_string +from django.conf import settings +from django.db import models + +from core.models import Image +from django_images.models import Thumbnail + +_plugins = getattr(settings, "ENABLED_PLUGINS", []) +_plugin_instances = [] + + +def _load_plugins(): + for plugin_path in _plugins: + plugin_cls = import_string(plugin_path) + _plugin_instances.append(plugin_cls()) + + +@receiver(models.signals.pre_save, sender=Image) +def process_image_pre_creation(sender, instance: Image, **kwargs): + # FIXME(winkidney): May have issue on determining if it + # is created or not + if instance.pk is not None: + return + for plugin in _plugin_instances: + process_fn = getattr(plugin, "process_image_pre_creation") + try: + process_fn( + django_settings=settings, + image_instance=instance, + ) + except Exception: + logging.exception( + "Error occurs while trying to access plugin's pin_pre_save " + "for plugin %s" % plugin + ) + + +@receiver(models.signals.pre_save, sender=Thumbnail) +def process_thumbnail_pre_creation(sender, instance: Thumbnail, **kwargs): + # FIXME(winkidney): May have issue on determining if it + # is created or not + if instance.pk is not None: + return + + for plugin in _plugin_instances: + process_fn = getattr(plugin, "process_thumbnail_pre_creation") + try: + process_fn( + django_settings=settings, + thumbnail_instance=instance, + ) + except Exception: + logging.exception( + "Error occurs while trying to access plugin's process_thumbnail_pre_creation " + "for plugin %s" % plugin + ) + + +def init(): + _load_plugins() diff --git a/pinry_plugins/migrations/__init__.py b/pinry_plugins/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pinry_plugins/models.py b/pinry_plugins/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/pinry_plugins/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/pinry_plugins/tests.py b/pinry_plugins/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/pinry_plugins/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/pinry_plugins/views.py b/pinry_plugins/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/pinry_plugins/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.