mirror of
https://github.com/pinry/pinry.git
synced 2026-01-15 03:32:05 +01:00
Merge pull request #194 from pinry/feature/plugin-system
Feature: Add naive plugin support for pinry
This commit is contained in:
20
docs/src/plugin-system.md
Normal file
20
docs/src/plugin-system.md
Normal file
@@ -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!
|
||||||
@@ -19,6 +19,7 @@ nav:
|
|||||||
- Theories: 'theories.md'
|
- Theories: 'theories.md'
|
||||||
- Install with Docker: 'install-with-docker.md'
|
- Install with Docker: 'install-with-docker.md'
|
||||||
- Development: 'development.md'
|
- Development: 'development.md'
|
||||||
|
- PluginSystem: 'plugin-system.md'
|
||||||
- Docs: 'docs.md'
|
- Docs: 'docs.md'
|
||||||
- Passwords: 'passwords.md'
|
- Passwords: 'passwords.md'
|
||||||
- Assets: 'assets.md'
|
- Assets: 'assets.md'
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ INSTALLED_APPS = [
|
|||||||
'django_images',
|
'django_images',
|
||||||
'core',
|
'core',
|
||||||
'users',
|
'users',
|
||||||
|
'pinry_plugins.apps.PinryPluginsConfig',
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'pinry.urls'
|
ROOT_URLCONF = 'pinry.urls'
|
||||||
|
|||||||
@@ -42,3 +42,7 @@ IMAGE_SIZES = {
|
|||||||
|
|
||||||
# Whether people can view pins without login
|
# Whether people can view pins without login
|
||||||
PUBLIC = True
|
PUBLIC = True
|
||||||
|
|
||||||
|
ENABLED_PLUGINS = [
|
||||||
|
'pinry_plugins.batteries.plugin_example.Plugin',
|
||||||
|
]
|
||||||
|
|||||||
0
pinry_plugins/__init__.py
Normal file
0
pinry_plugins/__init__.py
Normal file
0
pinry_plugins/admin.py
Normal file
0
pinry_plugins/admin.py
Normal file
9
pinry_plugins/apps.py
Normal file
9
pinry_plugins/apps.py
Normal file
@@ -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()
|
||||||
0
pinry_plugins/batteries/__init__.py
Normal file
0
pinry_plugins/batteries/__init__.py
Normal file
10
pinry_plugins/batteries/plugin_example.py
Normal file
10
pinry_plugins/batteries/plugin_example.py
Normal file
@@ -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
|
||||||
5
pinry_plugins/builder/__init__.py
Normal file
5
pinry_plugins/builder/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from . import _loader
|
||||||
|
|
||||||
|
|
||||||
|
def init():
|
||||||
|
_loader.init()
|
||||||
63
pinry_plugins/builder/_loader.py
Normal file
63
pinry_plugins/builder/_loader.py
Normal file
@@ -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()
|
||||||
0
pinry_plugins/migrations/__init__.py
Normal file
0
pinry_plugins/migrations/__init__.py
Normal file
0
pinry_plugins/models.py
Normal file
0
pinry_plugins/models.py
Normal file
0
pinry_plugins/tests.py
Normal file
0
pinry_plugins/tests.py
Normal file
0
pinry_plugins/views.py
Normal file
0
pinry_plugins/views.py
Normal file
Reference in New Issue
Block a user