From 78c6409762364366ded2a93db70a3a22150f66f1 Mon Sep 17 00:00:00 2001 From: Wesley Bomar Date: Tue, 10 Oct 2023 12:55:12 -0500 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20command,=20find=20plugins=20within?= =?UTF-8?q?=20TextPlugin=20=E2=9A=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⚠️ INCOMPLETE. DOES NOT WORK. SEE `get_parent_text_plugins`. --- .../commands/find_plugins_in_text.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 taccsite_cms/management/commands/find_plugins_in_text.py diff --git a/taccsite_cms/management/commands/find_plugins_in_text.py b/taccsite_cms/management/commands/find_plugins_in_text.py new file mode 100644 index 000000000..54815c56a --- /dev/null +++ b/taccsite_cms/management/commands/find_plugins_in_text.py @@ -0,0 +1,54 @@ +import os +import logging +import importlib + +from django.core.management import BaseCommand +from django.utils.module_loading import import_string + +from djangocms_text_ckeditor.cms_plugins import TextPlugin + +logger = logging.getLogger('django') + +class Command(BaseCommand): + help = f'Find plugins of a given class that are in Text plugin instance(s)' + + def add_arguments(self, parser): + parser.add_argument('plugin_paths', nargs='+', type=str, help='The Python path(s) to plugin module class(es) e.g. "djangocms_picture.cms_plugins.PicturePlugin"') + + def handle(self, *args, **options): + for plugin_path in options['plugin_paths']: + try: + plugin_class = import_string(plugin_path) + except ImportError: + logger.error( + f'Error importing plugin class from path: {plugin_path}' + ) + continue + plugin_instances_list = self.get_parent_text_plugins(plugin_class) + count = len(plugin_instances_list) + + logger.info( + f'{count} instances of {plugin_class.name} in a `TextPlugin`.' + ) + + for plugin_instance in plugin_instances_list: + logger.info( + f'"{plugin_instance.name}" ({plugin_instance.id})' + ) + + # !!!: This function is not accurate! + # TODO: (A) Rewrite so that it: + # 1. finds all instances of TextPlugin + # 2. finds whether plugin has instances of given plugin + # TODO: (B) Rewrite so that it: + # 1. finds all instances of given plugin + # 2. finds whether plugin instances are children of a TextPlugin + def get_parent_text_plugins(self, plugin_class): + logger.info(f'Plugin class') + + if plugin_class and issubclass(plugin_class, TextPlugin): + plugin_instances_set = plugin_class.objects.filter(text_plugin__isnull=False) + + return list(plugin_instances_set) + else: + return [] From a0b79e5e50bb1684e0ee524397776ee63cc17cd9 Mon Sep 17 00:00:00 2001 From: Wesley Bomar Date: Tue, 10 Oct 2023 13:00:25 -0500 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20command,=20find=20pages=20with=20gi?= =?UTF-8?q?ven=20plugin=20=E2=9A=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⚠️ UNTESTED & UNPOLISHED AS DJANGO COMMAND. --- .../commands/find_pages_with_plugins.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 taccsite_cms/management/commands/find_pages_with_plugins.py diff --git a/taccsite_cms/management/commands/find_pages_with_plugins.py b/taccsite_cms/management/commands/find_pages_with_plugins.py new file mode 100644 index 000000000..1df920549 --- /dev/null +++ b/taccsite_cms/management/commands/find_pages_with_plugins.py @@ -0,0 +1,32 @@ +import os +import sys +import django + +# Configure Django settings to avoid errors +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'taccsite_cms.settings') +django.setup() + +from django.apps import apps + +def find_page_using_plugin_instance(plugin_instance_id): + try: + CMSPlugin = apps.get_model('cms', 'CMSPlugin') + plugin_instance = CMSPlugin.objects.get(id=plugin_instance_id) + page = plugin_instance.placeholder.page if plugin_instance.placeholder else None + return page + except CMSPlugin.DoesNotExist: + return None + +# Usage example: +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python find_plugin_page.py ") + sys.exit(1) + + plugin_instance_id = int(sys.argv[1]) + page = find_page_using_plugin_instance(plugin_instance_id) + + if page: + print(f"The plugin instance with ID {plugin_instance_id} is used on page '{page.get_path()}'") + else: + print(f"No page uses the plugin instance with ID {plugin_instance_id}")