Skip to content

Instantly share code, notes, and snippets.

@wfehr
Created April 30, 2018 15:54
Show Gist options
  • Save wfehr/86ac31e8e263b872b746cc721662251e to your computer and use it in GitHub Desktop.
Save wfehr/86ac31e8e263b872b746cc721662251e to your computer and use it in GitHub Desktop.
Migration to move deprecated cmsplugin-*-plugins to the newer djangocms-*-plugins
from django.apps import apps as global_apps
from django.db import migrations
from djangocms_file.models import get_templates as get_file_templates
from djangocms_picture.models import get_templates as get_picture_templates
def forwards_filer_file(apps, schema_editor):
try:
CMSPluginFilerFile = apps.get_model('cmsplugin_filer_file',
'FilerFile')
cmsplugin_filer_file = True
except LookupError:
cmsplugin_filer_file = False
if cmsplugin_filer_file:
DjangoCMSFileFile = apps.get_model('djangocms_file', 'File')
for old_object in CMSPluginFilerFile.objects.all():
old_cmsplugin_ptr = old_object.cmsplugin_ptr
new_object = DjangoCMSFileFile(
file_name=old_object.title,
file_src=old_object.file,
# defaults for fields that don't exist in the old_object
link_target='',
template=get_file_templates()[0][0],
show_file_size=1,
# fields for the cms_cmsplugin table
position=old_cmsplugin_ptr.position,
language=old_cmsplugin_ptr.language,
plugin_type='FilePlugin',
creation_date=old_cmsplugin_ptr.creation_date,
changed_date=old_cmsplugin_ptr.changed_date,
parent=old_cmsplugin_ptr.parent,
placeholder=old_cmsplugin_ptr.placeholder,
depth=old_cmsplugin_ptr.depth,
numchild=old_cmsplugin_ptr.numchild,
path=old_cmsplugin_ptr.path,
)
old_object.delete()
new_object.save()
return cmsplugin_filer_file
def forwards_filer_folder(apps, schema_editor):
try:
CMSPluginFilerFolder = apps.get_model('cmsplugin_filer_folder',
'FilerFolder')
cmsplugin_filer_folder = True
except LookupError:
cmsplugin_filer_folder = False
if cmsplugin_filer_folder:
DjangoCMSFileFolder = apps.get_model('djangocms_file', 'Folder')
for old_object in CMSPluginFilerFolder.objects.all():
old_cmsplugin_ptr = old_object.cmsplugin_ptr
new_object = DjangoCMSFileFolder(
folder_src=old_object.folder,
# defaults for fields that don't exist in the old_object
template=get_file_templates()[0][0],
link_target='',
show_file_size=0,
# fields for the cms_cmsplugin table
position=old_cmsplugin_ptr.position,
language=old_cmsplugin_ptr.language,
plugin_type='FolderPlugin',
creation_date=old_cmsplugin_ptr.creation_date,
changed_date=old_cmsplugin_ptr.changed_date,
parent=old_cmsplugin_ptr.parent,
placeholder=old_cmsplugin_ptr.placeholder,
depth=old_cmsplugin_ptr.depth,
numchild=old_cmsplugin_ptr.numchild,
path=old_cmsplugin_ptr.path,
)
old_object.delete()
new_object.save()
return cmsplugin_filer_folder
def forwards_filer_image(apps, schema_editor):
try:
CMSPluginFilerImage = apps.get_model('cmsplugin_filer_image',
'FilerImage')
cmsplugin_filer_image = True
except LookupError:
cmsplugin_filer_image = False
if cmsplugin_filer_image:
DjangoCMSPicture = apps.get_model('djangocms_picture', 'Picture')
for old_object in CMSPluginFilerImage.objects.all():
old_cmsplugin_ptr = old_object.cmsplugin_ptr
attributes = {}
if old_object.alt_text:
attributes.update({'alt': old_object.alt_text})
new_object = DjangoCMSPicture(
caption_text=old_object.caption_text,
external_picture=old_object.image_url
if old_object.image_url else '',
use_automatic_scaling=old_object.use_autoscale,
width=old_object.width,
height=old_object.height,
use_crop=old_object.crop,
use_upscale=old_object.upscale,
alignment=old_object.alignment if old_object.alignment else '',
picture=old_object.image,
thumbnail_options=old_object.thumbnail_option,
attributes=attributes,
link_attributes=old_object.link_attributes,
# defaults for fields that don't exist in the old_object
use_no_cropping=0,
template=get_picture_templates()[0][0],
# fields for the cms_cmsplugin table
position=old_cmsplugin_ptr.position,
language=old_cmsplugin_ptr.language,
plugin_type='PicturePlugin',
creation_date=old_cmsplugin_ptr.creation_date,
changed_date=old_cmsplugin_ptr.changed_date,
parent=old_cmsplugin_ptr.parent,
placeholder=old_cmsplugin_ptr.placeholder,
depth=old_cmsplugin_ptr.depth,
numchild=old_cmsplugin_ptr.numchild,
path=old_cmsplugin_ptr.path,
)
old_object.delete()
new_object.save()
return cmsplugin_filer_image
def forwards_my_app_image(apps, schema_editor):
try:
MyAppImage = apps.get_model('my_app', 'Image')
my_app_image = True
except LookupError:
my_app_image = False
if my_app_image:
MyAppPicture = apps.get_model('my_app', 'Picture')
for old_object in MyAppImage.objects.all():
old_cmsplugin_ptr = old_object.cmsplugin_ptr
attributes = {}
if old_object.alt_text:
attributes.update({'alt': old_object.alt_text})
new_object = MyAppPicture(
enable_zoom=old_object.enable_zoom,
caption_text=old_object.caption_text,
external_picture=old_object.image_url
if old_object.image_url else '',
use_automatic_scaling=old_object.use_autoscale,
width=old_object.width,
height=old_object.height,
use_crop=old_object.crop,
use_upscale=old_object.upscale,
alignment=old_object.alignment if old_object.alignment else '',
picture=old_object.image,
thumbnail_options=old_object.thumbnail_option,
attributes=attributes,
link_attributes=old_object.link_attributes,
# defaults for fields that don't exist in the old_object
use_no_cropping=0,
template=get_picture_templates()[0][0],
# fields for the cms_cmsplugin table
position=old_cmsplugin_ptr.position,
language=old_cmsplugin_ptr.language,
plugin_type='PicturePlugin',
creation_date=old_cmsplugin_ptr.creation_date,
changed_date=old_cmsplugin_ptr.changed_date,
parent=old_cmsplugin_ptr.parent,
placeholder=old_cmsplugin_ptr.placeholder,
depth=old_cmsplugin_ptr.depth,
numchild=old_cmsplugin_ptr.numchild,
path=old_cmsplugin_ptr.path,
)
old_object.delete()
new_object.save()
return my_app_image
def forwards(apps, schema_editor):
cmsplugin_filer_file = forwards_filer_file(apps, schema_editor)
cmsplugin_filer_folder = forwards_filer_folder(apps, schema_editor)
# move all inherited plugins first, then the parents
my_app_image = forwards_my_app_image(apps, schema_editor)
cmsplugin_filer_image = forwards_filer_image(apps, schema_editor)
if not cmsplugin_filer_file and not cmsplugin_filer_folder and not \
cmsplugin_filer_image and not my_app_image:
return
class Migration(migrations.Migration):
'''
Move data from filer-plugins to the new djangocms-*-plugins. Inspiration:
https://docs.djangoproject.com/en/2.0/howto/writing-migrations/
#migrating-data-between-third-party-apps
'''
operations = [
migrations.RunPython(forwards, migrations.RunPython.noop),
]
dependencies = [
('my_app', '00XY_my_app_previous_migration'),
('djangocms_file', '0010_removed_null_fields'),
('djangocms_picture', '0007_fix_alignment'),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment