Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new custom django command for us ⚠️ #728

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions taccsite_cms/management/commands/find_pages_with_plugins.py
Original file line number Diff line number Diff line change
@@ -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 <plugin_instance_id>")
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}")
54 changes: 54 additions & 0 deletions taccsite_cms/management/commands/find_plugins_in_text.py
Original file line number Diff line number Diff line change
@@ -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 []