2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-01 21:16:46 +00:00

Plugin static files (#7763)

* add plugin_static template tag

* don't load plugins for collectstatic anymore as they are now collected seperatly

* fix clear plugin staic files bug with nested folder path
This commit is contained in:
Lukas 2024-07-30 12:51:27 +02:00 committed by GitHub
parent e3ccd3a682
commit 648cb12b5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 5 deletions

View File

@ -115,6 +115,7 @@ def canAppAccessDatabase(
'makemessages', 'makemessages',
'compilemessages', 'compilemessages',
'spectactular', 'spectactular',
'collectstatic',
] ]
if not allow_shell: if not allow_shell:
@ -125,7 +126,7 @@ def canAppAccessDatabase(
excluded_commands.append('test') excluded_commands.append('test')
if not allow_plugins: if not allow_plugins:
excluded_commands.extend(['collectstatic', 'collectplugins']) excluded_commands.extend(['collectplugins'])
for cmd in excluded_commands: for cmd in excluded_commands:
if cmd in sys.argv: if cmd in sys.argv:

View File

@ -1,7 +1,6 @@
"""Static files management for InvenTree plugins.""" """Static files management for InvenTree plugins."""
import logging import logging
from pathlib import Path
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
@ -23,12 +22,15 @@ def clear_static_dir(path, recursive=True):
dirs, files = staticfiles_storage.listdir(path) dirs, files = staticfiles_storage.listdir(path)
for f in files: for f in files:
staticfiles_storage.delete(f'{path}/{f}') staticfiles_storage.delete(f'{path}{f}')
if recursive: if recursive:
for d in dirs: for d in dirs:
clear_static_dir(f'{path}/{d}', recursive=True) clear_static_dir(f'{path}{d}/', recursive=True)
staticfiles_storage.delete(d) staticfiles_storage.delete(f'{path}{d}')
# Finally, delete the directory itself to remove orphan folders when uninstalling a plugin
staticfiles_storage.delete(path)
def collect_plugins_static_files(): def collect_plugins_static_files():

View File

@ -2,6 +2,7 @@
from django import template from django import template
from django.conf import settings as djangosettings from django.conf import settings as djangosettings
from django.templatetags.static import static
from django.urls import reverse from django.urls import reverse
from common.notifications import storage from common.notifications import storage
@ -96,3 +97,27 @@ def notification_list(context, *args, **kwargs):
} }
for a in storage.liste for a in storage.liste
] ]
@register.simple_tag(takes_context=True)
def plugin_static(context, file: str, **kwargs):
"""Return the URL for a static file within a plugin.
Arguments:
file: The path to the file within the plugin static directory
Keyword Arguments:
plugin: The plugin slug (optional, will be inferred from the context if not provided)
"""
plugin = context.get('plugin', None)
if plugin:
plugin = plugin.slug
else:
plugin = kwargs.get('plugin', None)
if not plugin:
return file
return static(f'plugins/{plugin}/{file}')