|
# -*- coding:utf-8 -*- |
|
|
|
# Standard library imports |
|
from __future__ import unicode_literals |
|
|
|
from django.core.management.base import BaseCommand |
|
from django.db import transaction |
|
from djangocms_picture.models import get_templates as get_picture_templates |
|
from djangocms_file.models import get_templates as get_file_templates |
|
from djangocms_link.models import get_templates as get_link_templates |
|
from django.db import connection |
|
from django.db.migrations.recorder import MigrationRecorder |
|
|
|
|
|
DOMAIN_URL = "SET THIS TO YOUR DOMAIN URL" |
|
|
|
try: |
|
from cmsplugin_filer_file.models import FilerFile as CMSPluginFilerFile |
|
from djangocms_file.models import File as DjangoCMSFileFile |
|
except ImportError: |
|
print("Warning: Cant import cmsplugin_filer_file or djangocms_file models") |
|
CMSPluginFilerFile = None |
|
DjangoCMSFileFile = None |
|
|
|
try: |
|
from cmsplugin_filer_folder.models import FilerFolder as CMSPluginFilerFolder |
|
from djangocms_file.models import Folder as DjangoCMSFileFolder |
|
except ImportError: |
|
print("Warning: Cant import cmsplugin_filer_folder or djangocms_file models") |
|
CMSPluginFilerFolder = None |
|
DjangoCMSFileFolder = None |
|
|
|
try: |
|
from cmsplugin_filer_image.models import FilerImage as CMSPluginFilerImage |
|
from djangocms_picture.models import Picture as DjangoCMSPicture |
|
except ImportError: |
|
print("Warning: Cant import cmsplugin_filer_image or djangocms_picture models") |
|
CMSPluginFilerImage = None |
|
DjangoCMSPicture = None |
|
|
|
try: |
|
from cmsplugin_filer_link.models import FilerLinkPlugin as CMSPluginFilerLink |
|
from djangocms_link.models import Link as DjangoCMSLink |
|
except ImportError: |
|
print("Warning: Cant import cmsplugin_filer_link or djangocms_link models") |
|
CMSPluginFilerLink = None |
|
DjangoCMSLink = None |
|
|
|
|
|
class Command(BaseCommand): |
|
args = "<directory>" |
|
help = "Migrate from cmsplugin_filer to djangocms_file, djangocms_folder, djangocms_picture." |
|
|
|
def handle(self, *args, **options): |
|
with transaction.atomic(): |
|
self.forwards_filer_file() |
|
self.forwards_filer_folder() |
|
self.forwards_filer_image() |
|
self.forwards_filer_link() |
|
self.delete_old_tables() |
|
self.delete_old_migrations() |
|
|
|
def forwards_filer_file(self): |
|
print("Processing filer file") |
|
|
|
if CMSPluginFilerFile: |
|
for old_object in CMSPluginFilerFile.objects.all(): |
|
default_template = get_file_templates()[0][0] |
|
|
|
print( |
|
"Processing plugin {}, inheriting from CMSPLugin {}".format( |
|
old_object.id, old_object.cmsplugin_ptr |
|
) |
|
) |
|
|
|
old_cmsplugin_ptr = old_object.cmsplugin_ptr |
|
old_object |
|
old_cmsplugin_ptr.plugin_type = "FilePlugin" |
|
old_cmsplugin_ptr.save() |
|
|
|
new_object = DjangoCMSFileFile( |
|
cmsplugin_ptr_id=old_cmsplugin_ptr.id, |
|
file_name=old_object.title or "", |
|
file_src=old_object.file, |
|
link_target="_blank" if old_object.target_blank else "", |
|
link_title=old_object.title or "", |
|
template=old_object.style or default_template, |
|
attributes=old_object.link_attributes, |
|
# defaults for fields that don't exist in the old_object |
|
show_file_size=False, |
|
# fields for the cms_cmsplugin table |
|
plugin_type="FilePlugin", |
|
position=old_cmsplugin_ptr.position, |
|
language=old_cmsplugin_ptr.language, |
|
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, |
|
) |
|
|
|
new_object.save() |
|
print( |
|
"Created new plungin {}, with parent {}".format( |
|
new_object.id, new_object.cmsplugin_ptr.id |
|
) |
|
) |
|
|
|
def forwards_filer_folder(self): |
|
print("Processing filer folder") |
|
|
|
if CMSPluginFilerFolder: |
|
default_template = get_file_templates()[0][0] |
|
|
|
for old_object in CMSPluginFilerFolder.objects.all(): |
|
print( |
|
"Processing plugin {}, inheriting from CMSPLugin {}".format( |
|
old_object.id, old_object.cmsplugin_ptr |
|
) |
|
) |
|
|
|
old_cmsplugin_ptr = old_object.cmsplugin_ptr |
|
old_cmsplugin_ptr.plugin_type = "FolderPlugin" |
|
old_cmsplugin_ptr.save() |
|
|
|
# Props lost from old object: |
|
# - title |
|
new_object = DjangoCMSFileFolder( |
|
cmsplugin_ptr_id=old_cmsplugin_ptr.id, |
|
folder_src=old_object.folder, |
|
template=old_object.style or default_template, |
|
# defaults for fields that don't exist in the old_object |
|
link_target="", |
|
show_file_size=False, |
|
# fields for the cms_cmsplugin table |
|
plugin_type="FolderPlugin", |
|
position=old_cmsplugin_ptr.position, |
|
language=old_cmsplugin_ptr.language, |
|
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, |
|
) |
|
|
|
new_object.save() |
|
print( |
|
"Created new plungin {}, with parent {}".format( |
|
new_object.id, new_object.cmsplugin_ptr.id |
|
) |
|
) |
|
|
|
def forwards_filer_image(self): |
|
print("Processing filer images") |
|
|
|
if CMSPluginFilerImage: |
|
default_template = get_picture_templates()[0][0] |
|
for old_object in CMSPluginFilerImage.objects.all(): |
|
print( |
|
"Processing plugin {}, inheriting from CMSPLugin {}".format( |
|
old_object.id, old_object.cmsplugin_ptr |
|
) |
|
) |
|
|
|
old_cmsplugin_ptr = old_object.cmsplugin_ptr |
|
old_object |
|
old_cmsplugin_ptr.plugin_type = "PicturePlugin" |
|
old_cmsplugin_ptr.save() |
|
|
|
attributes = {} |
|
if old_object.alt_text: |
|
attributes.update({"alt": old_object.alt_text}) |
|
|
|
if old_object.thumbnail_option is not None: |
|
use_crop = False |
|
use_upscale = False |
|
else: |
|
use_crop = old_object.crop |
|
use_upscale = old_object.upscale |
|
|
|
width = old_object.width |
|
height = old_object.height |
|
|
|
if ( |
|
old_object.image |
|
and old_object.image.width |
|
and old_object.image.height |
|
): |
|
if not height and width: |
|
height = int( |
|
width * old_object.image.height / old_object.image.width |
|
) |
|
elif not width and height: |
|
width = int( |
|
height * old_object.image.width / old_object.image.height |
|
) |
|
|
|
# Props lost from old object: |
|
# - original_link -> OK |
|
# - description -> OK |
|
# - file_link_id : recreate links to files by hand (don't remember what this comment is all about :/) |
|
|
|
new_object = DjangoCMSPicture( |
|
cmsplugin_ptr_id=old_cmsplugin_ptr.id, |
|
caption_text=old_object.caption_text, |
|
external_picture=old_object.image_url or "", |
|
use_automatic_scaling=old_object.use_autoscale, |
|
width=width, |
|
height=height, |
|
use_crop=use_crop, |
|
use_upscale=use_upscale, |
|
alignment=old_object.alignment or "", |
|
picture=old_object.image, |
|
thumbnail_options=old_object.thumbnail_option, |
|
attributes=attributes, |
|
link_url=old_object.free_link, |
|
link_page_id=old_object.page_link_id, |
|
link_attributes=old_object.link_attributes, |
|
use_no_cropping=old_object.use_original_image, |
|
template=old_object.style or default_template, |
|
# fields for the cms_cmsplugin table |
|
plugin_type="PicturePlugin", |
|
position=old_cmsplugin_ptr.position, |
|
language=old_cmsplugin_ptr.language, |
|
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, |
|
) |
|
|
|
if old_object.target_blank: |
|
new_object.link_target = "_blank" |
|
|
|
new_object.save() |
|
print( |
|
"Created new plungin {}, with parent {}".format( |
|
new_object.id, new_object.cmsplugin_ptr.id |
|
) |
|
) |
|
|
|
def forwards_filer_link(self): |
|
print("Processing filer links") |
|
|
|
if CMSPluginFilerLink: |
|
default_template = get_link_templates()[0][0] |
|
for old_object in CMSPluginFilerLink.objects.all(): |
|
print( |
|
"Processing plugin {}, inheriting from CMSPLugin {}".format( |
|
old_object.id, old_object.cmsplugin_ptr |
|
) |
|
) |
|
|
|
old_cmsplugin_ptr = old_object.cmsplugin_ptr |
|
old_object |
|
old_cmsplugin_ptr.plugin_type = "LinkPlugin" |
|
old_cmsplugin_ptr.save() |
|
|
|
external_link = "" |
|
|
|
if old_object.url not in [None, ""]: |
|
external_link = old_object.url |
|
|
|
if external_link.startswith("/"): |
|
external_link = DOMAIN_URL + external_link |
|
|
|
new_object = DjangoCMSLink( |
|
cmsplugin_ptr_id=old_cmsplugin_ptr.id, |
|
template=old_object.link_style.strip() or default_template, |
|
name=old_object.name, |
|
external_link=external_link, |
|
internal_link_id=old_object.page_link_id, |
|
file_link_id=old_object.file_id, |
|
mailto=old_object.mailto or "", |
|
attributes=old_object.link_attributes, |
|
# defaults for fields that don't exist in the old_object |
|
anchor="", |
|
phone="", |
|
# fields for the cms_cmsplugin table |
|
plugin_type="LinkPlugin", |
|
position=old_cmsplugin_ptr.position, |
|
language=old_cmsplugin_ptr.language, |
|
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, |
|
) |
|
|
|
if old_object.new_window: |
|
new_object.target = "_blank" |
|
|
|
new_object.save() |
|
print( |
|
"Created new plungin {}, with parent {}".format( |
|
new_object.id, new_object.cmsplugin_ptr.id |
|
) |
|
) |
|
|
|
def delete_old_tables(self): |
|
old_tables = [ |
|
"cmsplugin_filer_file_filerfile", |
|
"cmsplugin_filer_folder_filerfolder", |
|
"cmsplugin_filer_image_filerimage", |
|
"cmsplugin_filer_video_filervideo", |
|
"cmsplugin_filerfile", |
|
"cmsplugin_filerfolder", |
|
"cmsplugin_filerimage", |
|
] |
|
cursor = connection.cursor() |
|
for old_table in old_tables: |
|
cursor.execute("DROP TABLE IF EXISTS {}".format(old_table)) |
|
|
|
def delete_old_migrations(self): |
|
old_apps = [ |
|
"cmsplugin_filer_folder", |
|
"cmsplugin_filer_video", |
|
"cmsplugin_filer_image", |
|
"cmsplugin_filer_file", |
|
] |
|
|
|
MigrationRecorder.Migration.objects.filter(app__in=old_apps).delete() |