Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.management.base import NoArgsCommand
from cms.models import CMSPlugin, Page
class Command(NoArgsCommand):
def get_corrupt_parent_plugins(self):
plugins = CMSPlugin.objects.prefetch_related('cmsplugin_set')
corrupt_parents = []
for plugin in plugins:
children_with_language = plugin.cmsplugin_set.filter(language=plugin.language)
children_with_language_count = children_with_language.count()
# Total plugins - plugins whose language match their parent language
difference = plugin.cmsplugin_set.count() - children_with_language_count
if difference >= 1:
self.stdout.write('Plugin %d is corrupted.' % plugin.pk)
self.stdout.write('It has %d plugins in a different language.\n\n' % difference)
corrupt_parents.append(plugin.pk)
return corrupt_parents
def handle_noargs(self, **options):
corrupt_plugins = self.get_corrupt_parent_plugins()
plugins = CMSPlugin.objects.filter(pk__in=corrupt_plugins).select_related('placeholder')
pages = [plugin.page.pk for plugin in plugins if plugin.page]
page_ids = set(pages)
public_pages = Page.objects.public().filter(pk__in=page_ids)
public_pages_ids = public_pages.values_list('pk', flat=True)
non_public_page_ids = [page_id for page_id in page_ids if page_id not in public_pages_ids]
non_public_pages = Page.objects.filter(
pk__in=non_public_page_ids
).values_list('pk', flat=True)
non_public_pages = list(non_public_pages)
languages = [language[0] for language in settings.LANGUAGES]
published_pages = []
unpublished_pages = []
for page in public_pages:
is_published = page.is_published
_is_published = [is_published(language) for language in languages]
if any(_is_published):
published_pages.append(page.pk)
else:
non_public_pages.append(page.pk)
self.stdout.write("Found %d corrupt plugins.\n" % len(corrupt_plugins))
self.stdout.write("Found %d corrupt public pages.\n" % len(published_pages))
self.stdout.write("Found %d corrupt non public pages.\n" % len(unpublished_pages))
self.stdout.write("Public Pages: %s\n" % published_pages)
self.stdout.write("Non public Pages: %s\n" % unpublished_pages)
self.stdout.write("Plugins: %s\n" % corrupt_plugins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.