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

management command lang_copy #971

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions cms/management/commands/lang_copy.py
@@ -0,0 +1,61 @@
from copy import deepcopy
from optparse import make_option

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

from cms.models.pluginmodel import CMSPlugin
from cms.models.titlemodels import Title

class Command(BaseCommand):
args = '<language_from language_to>'
help = 'dupplicate the cms content from one lang to another (to boot a new lang)'

option_list = BaseCommand.option_list + (
make_option('--skipattrs', action='store_true', dest='skipattrs', default=False,
help='Tells django-cms to NOT copy page attributes (like title, slug, id, plugin app, etc..). '),
)

def handle(self, *args, **kwargs):
verbosity = kwargs.get('verbosity', 1)
skip_attributes = kwargs.get('skipattrs', False)

#test both langs
try:
assert len(args) == 2

from_lang = args[0]
to_lang = args[1]

assert from_lang != to_lang
except AssertionError:
print 'available LANGUAGES :'+str(settings.LANGUAGES)
raise CommandError("Error: bad arguments -- Usage: manage.py lang_copy en de")

try:
assert list(k for k,v in settings.LANGUAGES if k == from_lang)
assert list(k for k,v in settings.LANGUAGES if k == to_lang)
except AssertionError:
raise CommandError("Both languages have to be present in settings.LANGUAGES")

for plugin in CMSPlugin.objects.filter(language=from_lang):
#copying content of the page
if not CMSPlugin.objects.filter(language=to_lang, placeholder=plugin.placeholder, position=plugin.position).exists():
if verbosity == "2":
print 'copying plugin from '+str(plugin)
plugin.copy_plugin(plugin.placeholder, to_lang, [])

if not skip_attributes:
#copying attributes of the page
for title in Title.objects.filter(language=from_lang):
if not Title.objects.filter(page=title.page, language=to_lang).exists():
if verbosity == "2":
print 'copying title from '+str(title)
title.id = None
title.language = to_lang
title.save()
elif verbosity == '2':
print 'skipping attributes'

if verbosity == '2':
print 'DONE'