diff --git a/docs/src/plugin-system.md b/docs/src/plugin-system.md new file mode 100644 index 0000000..8683dba --- /dev/null +++ b/docs/src/plugin-system.md @@ -0,0 +1,20 @@ +# New plugin system for Pinry + +New plugin system is under development. + +Now you could access it via project example plugin file `pinry_plugins/batteries/plugin_example.py`. + +You could create a simple plugin which has a class which owns methods named: + ++ process_image_pre_creation ++ process_thumbnail_pre_creation + +And, add the plugin class to local_settings.py as: + +``` +ENABLED_PLUGINS = [ + 'pinry_plugins.batteries.plugin_example.Plugin', +] +``` + +Now the plugin will work like a charm! diff --git a/mkdocs.yml b/mkdocs.yml index b2d8922..b29318c 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -19,6 +19,7 @@ nav: - Theories: 'theories.md' - Install with Docker: 'install-with-docker.md' - Development: 'development.md' + - PluginSystem: 'plugin-system.md' - Docs: 'docs.md' - Passwords: 'passwords.md' - Assets: 'assets.md' 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/settings/local_settings.example.py b/pinry/settings/local_settings.example.py index 7c71f77..889d0ea 100644 --- a/pinry/settings/local_settings.example.py +++ b/pinry/settings/local_settings.example.py @@ -42,3 +42,7 @@ IMAGE_SIZES = { # Whether people can view pins without login PUBLIC = True + +ENABLED_PLUGINS = [ + 'pinry_plugins.batteries.plugin_example.Plugin', +] 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..e69de29 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..e69de29 diff --git a/pinry_plugins/tests.py b/pinry_plugins/tests.py new file mode 100644 index 0000000..e69de29 diff --git a/pinry_plugins/views.py b/pinry_plugins/views.py new file mode 100644 index 0000000..e69de29