remove data migration from 0010, create 0014 with optional data migration (does nothing if already migrated)
This commit is contained in:
parent
18ea7bbd88
commit
678bd1ab9b
2 changed files with 43 additions and 29 deletions
|
@ -4,37 +4,9 @@ from __future__ import unicode_literals
|
|||
import aldryn_apphooks_config.fields
|
||||
import app_data.fields
|
||||
import djangocms_text_ckeditor.fields
|
||||
from cms.models import Page
|
||||
from cms.utils.i18n import get_language_list
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
BlogConfig = apps.get_model('djangocms_blog', 'BlogConfig')
|
||||
BlogConfigTranslation = apps.get_model('djangocms_blog', 'BlogConfigTranslation')
|
||||
Post = apps.get_model('djangocms_blog', 'Post')
|
||||
BlogCategory = apps.get_model('djangocms_blog', 'BlogCategory')
|
||||
GenericBlogPlugin = apps.get_model('djangocms_blog', 'GenericBlogPlugin')
|
||||
LatestPostsPlugin = apps.get_model('djangocms_blog', 'LatestPostsPlugin')
|
||||
AuthorEntriesPlugin = apps.get_model('djangocms_blog', 'AuthorEntriesPlugin')
|
||||
config = None
|
||||
for page in Page.objects.drafts().filter(application_urls='BlogApp'):
|
||||
config = BlogConfig.objects.create(namespace=page.application_namespace)
|
||||
for lang in get_language_list():
|
||||
title = page.get_title(lang)
|
||||
translation = BlogConfigTranslation.objects.create(language_code=lang, master_id=config.pk, app_title=title)
|
||||
if config:
|
||||
for model in (Post, BlogCategory, GenericBlogPlugin, LatestPostsPlugin, AuthorEntriesPlugin):
|
||||
for item in model.objects.all():
|
||||
item.app_config = config
|
||||
item.save()
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
# No need for backward data migration
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
|
@ -120,5 +92,4 @@ class Migration(migrations.Migration):
|
|||
name='sites',
|
||||
field=models.ManyToManyField(to='sites.Site', help_text='Select sites in which to show the post. If none is set it will be visible in all the configured sites.', blank=True, verbose_name='Site(s)'),
|
||||
),
|
||||
migrations.RunPython(forwards, backwards)
|
||||
]
|
||||
|
|
43
djangocms_blog/migrations/0014_auto_20160215_1331.py
Normal file
43
djangocms_blog/migrations/0014_auto_20160215_1331.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from cms.models import Page
|
||||
from cms.utils.i18n import get_language_list
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
BlogConfig = apps.get_model('djangocms_blog', 'BlogConfig')
|
||||
BlogConfigTranslation = apps.get_model('djangocms_blog', 'BlogConfigTranslation')
|
||||
Post = apps.get_model('djangocms_blog', 'Post')
|
||||
BlogCategory = apps.get_model('djangocms_blog', 'BlogCategory')
|
||||
GenericBlogPlugin = apps.get_model('djangocms_blog', 'GenericBlogPlugin')
|
||||
LatestPostsPlugin = apps.get_model('djangocms_blog', 'LatestPostsPlugin')
|
||||
AuthorEntriesPlugin = apps.get_model('djangocms_blog', 'AuthorEntriesPlugin')
|
||||
config = None
|
||||
for page in Page.objects.drafts().filter(application_urls='BlogApp'):
|
||||
config, created = BlogConfig.objects.get_or_create(namespace=page.application_namespace)
|
||||
if not BlogConfigTranslation.objects.exists():
|
||||
for lang in get_language_list():
|
||||
title = page.get_title(lang)
|
||||
translation = BlogConfigTranslation.objects.create(language_code=lang, master_id=config.pk, app_title=title)
|
||||
if config:
|
||||
for model in (Post, BlogCategory, GenericBlogPlugin, LatestPostsPlugin, AuthorEntriesPlugin):
|
||||
for item in model.objects.filter(app_config__isnull=True):
|
||||
item.app_config = config
|
||||
item.save()
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
# No need for backward data migration
|
||||
pass
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('djangocms_blog', '0013_auto_20160201_2235'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forwards, backwards),
|
||||
]
|
Loading…
Reference in a new issue