2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +00:00

Adds new "PluginSetting" class

- Adds settings which are unique to a particular plugin
This commit is contained in:
Oliver
2022-01-01 22:00:43 +11:00
parent c6330608e5
commit 547db3322f
3 changed files with 136 additions and 96 deletions

View File

@ -35,12 +35,20 @@ def plugin_deactivate(modeladmin, request, queryset):
plugin_update(queryset, False)
class PluginSettingInline(admin.TabularInline):
"""
Inline admin class for PluginSetting
"""
model = models.PluginSetting
class PluginConfigAdmin(admin.ModelAdmin):
"""Custom admin with restricted id fields"""
readonly_fields = ["key", "name", ]
list_display = ['active', '__str__', 'key', 'name', ]
list_display = ['name', 'key', '__str__', 'active', ]
list_filter = ['active']
actions = [plugin_activate, plugin_deactivate, ]
inlines = [PluginSettingInline,]
admin.site.register(models.PluginConfig, PluginConfigAdmin)

View File

@ -8,16 +8,17 @@ from __future__ import unicode_literals
from django.utils.translation import gettext_lazy as _
from django.db import models
import common.models
from plugin import plugin_reg
class PluginConfig(models.Model):
""" A PluginConfig object holds settings for plugins.
It is used to designate a Part as 'subscribed' for a given User.
"""
A PluginConfig object holds settings for plugins.
Attributes:
key: slug of the plugin - must be unique
key: slug of the plugin (this must be unique across all installed plugins!)
name: PluginName of the plugin - serves for a manual double check if the right plugin is used
active: Should the plugin be loaded?
"""
@ -63,7 +64,10 @@ class PluginConfig(models.Model):
# functions
def __init__(self, *args, **kwargs):
"""override to set original state of"""
"""
Override to set original state of the plugin-config instance
"""
super().__init__(*args, **kwargs)
self.__org_active = self.active
@ -82,7 +86,9 @@ class PluginConfig(models.Model):
}
def save(self, force_insert=False, force_update=False, *args, **kwargs):
"""extend save method to reload plugins if the 'active' status changes"""
"""
Extend save method to reload plugins if the 'active' status changes
"""
reload = kwargs.pop('no_reload', False) # check if no_reload flag is set
ret = super().save(force_insert, force_update, *args, **kwargs)
@ -95,3 +101,37 @@ class PluginConfig(models.Model):
plugin_reg.reload_plugins()
return ret
class PluginSetting(common.models.BaseInvenTreeSetting):
"""
This model represents settings for individual plugins
"""
class Meta:
unique_together = [
('plugin', 'key'),
]
@classmethod
def get_filters(cls, key, **kwargs):
"""
Override filters method to ensure settings are filtered by plugin id
"""
filters = super().get_filters(key, **kwargs)
plugin = kwargs.get('plugin', None)
if plugin:
filters['plugin'] = plugin
return filters
plugin = models.ForeignKey(
PluginConfig,
related_name='settings',
null=False,
verbose_name=_('Plugin'),
on_delete=models.CASCADE,
)