Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #333 from aldryn/feature/default-newsblog-config
Browse files Browse the repository at this point in the history
Default NewsblogConfig migrations
  • Loading branch information
mkoistinen committed Jan 6, 2016
2 parents d3707fe + 31b1435 commit 09a745a
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 27 deletions.
2 changes: 1 addition & 1 deletion aldryn_newsblog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('type', models.CharField(max_length=100, verbose_name='type')),
('namespace', models.CharField(default=None, unique=True, max_length=100, verbose_name='instance namespace')),
('app_data', app_data.fields.AppDataField(default=b'{}', editable=False)),
('app_data', app_data.fields.AppDataField(default=dict, editable=False)),
('permalink_type', models.CharField(default='slug', help_text='Choose the style of urls to use from the examples. (Note, all types are relative to apphook)', max_length=8, verbose_name='permalink type', choices=[('s', 'the-eagle-has-landed/'), ('ys', '1969/the-eagle-has-landed/'), ('yms', '1969/07/the-eagle-has-landed/'), ('ymds', '1969/07/16/the-eagle-has-landed/'), ('ymdi', '1969/07/16/11/')])),
('non_permalink_handling', models.SmallIntegerField(default=302, help_text='How to handle non-permalink urls?', verbose_name='non-permalink handling', choices=[(200, 'Allow'), (302, 'Redirect to permalink (default)'), (301, 'Permanent redirect to permalink'), (404, 'Return 404: Not Found')])),
('paginate_by', models.PositiveIntegerField(default=5, help_text='When paginating list views, how many articles per page?', verbose_name='Paginate size')),
Expand Down
88 changes: 88 additions & 0 deletions aldryn_newsblog/migrations/0007_default_newsblog_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings
from django.db import models, migrations, transaction
from django.db.models import get_model
from django.db.utils import ProgrammingError, OperationalError


def noop(apps, schema_editor):
pass


def get_config_count_count(model_class):
with transaction.atomic():
count = model_class.objects.count()
return count


def create_default_newsblog_config(apps, schema_editor):
import cms.models.fields
from cms.models import Placeholder
NewsBlogConfig = apps.get_model('aldryn_newsblog', 'NewsBlogConfig')

# if we try to execute this migration after cms migrations were migrated
# to latest - we would get an exception because apps.get_model
# contains cms models in the last known state (which is the dependency
# migration state). If that is the case we need to import the real model.
try:
# to avoid the following error:
# django.db.utils.InternalError: current transaction is aborted,
# commands ignored until end of transaction block
# we need to cleanup or avoid that by making transaction atomic.
count = get_config_count_count(NewsBlogConfig)
except (ProgrammingError, OperationalError):
NewsBlogConfig = get_model('aldryn_newsblog.NewsBlogConfig')
count = get_config_count_count(NewsBlogConfig)

if not count == 0:
return
# create only if there is no configs because user may already have
# existing and configured config.
app_config = NewsBlogConfig(namespace='aldryn_newsblog_default')
# usually generated in aldryn_apphooks_config.models.AppHookConfig
# but in migrations we don't have real class with correct parents.
app_config.type = 'aldryn_newsblog.cms_appconfig.NewsBlogConfig'
# placeholders
# cms checks if instance.pk is set, and if it isn't cms creates a new
# placeholder but it does that with real models, and fields on instance
# are faked models. To prevent that we need to manually set instance pk.
app_config.pk = 1

for field in app_config._meta.fields:
if not field.__class__ == cms.models.fields.PlaceholderField:
# skip other fields.
continue
placeholder_name = field.name
placeholder_id_name = '{0}_id'.format(placeholder_name)
placeholder_id = getattr(app_config, placeholder_id_name, None)
if placeholder_id is not None:
# do not process if it has a reference to placeholder field.
continue
# since there is no placeholder - create it, we cannot use
# get_or_create because it can get placeholder from other config
new_placeholder = Placeholder.objects.create(
slot=placeholder_name)
setattr(app_config, placeholder_id_name, new_placeholder.pk)
# after we process all placeholder fields - save config,
# so that django can pick up them.
app_config.save()

# translations
app_config_translation = app_config.translations.create()
app_config_translation.language_code = settings.LANGUAGES[0][0]
app_config_translation.app_title = 'Default NewsBlog'
app_config_translation.save()


class Migration(migrations.Migration):

dependencies = [
('cms', '0003_auto_20140926_2347'),
('aldryn_newsblog', '0006_auto_20160105_1013'),
]

operations = [
migrations.RunPython(create_default_newsblog_config, noop)
]
45 changes: 34 additions & 11 deletions aldryn_newsblog/south_migrations/0014_create_default_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models, connection
from django.db.transaction import set_autocommit

from aldryn_newsblog.utils.migration import rename_tables_old_to_new, rename_tables_new_to_old
from django.conf import settings
from django.db import models, connection, transaction


class Migration(DataMigration):

def forwards(self, orm):
"""
Used to create a default namespace, but we no longer want that.
"""
if connection.vendor == 'sqlite':
transaction.set_autocommit(True)
NewsBlogConfig = orm.NewsBlogConfig
Article = orm.Article
LatestEntriesPlugin = orm.LatestEntriesPlugin
models_list = [
Article,
LatestEntriesPlugin,
]
app_config, created = NewsBlogConfig.objects.get_or_create(
namespace='aldryn_newsblog_default',
)
if created:
app_config_translation = app_config.translations.create()
app_config_translation.language_code = settings.LANGUAGES[0][0]
app_config_translation.app_title = 'Default NewsBlog'
app_config_translation.save()

for model in models_list:
for entry in model.objects.filter(namespace__isnull=True):
entry.namespace = app_config
entry.save()

def backwards(self, orm):
pass
Expand Down Expand Up @@ -126,19 +143,25 @@ def backwards(self, orm):
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'cms.cmsplugin': {
# NOTE: We've commented out a couple of fields below. This will
# allow this data migration to actually work under 3.0 and 3.1
# (where we switched to Treebeard). Manual testing shows that the
# migration still works fine. Please do not un-comment these lines.
# Further note that all of this will disappear once we stop
# supporting Django 1.6.
'Meta': {'object_name': 'CMSPlugin'},
'changed_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'language': ('django.db.models.fields.CharField', [], {'max_length': '15', 'db_index': 'True'}),
'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
# 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
# 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
'parent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.CMSPlugin']", 'null': 'True', 'blank': 'True'}),
'placeholder': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Placeholder']", 'null': 'True'}),
'plugin_type': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}),
'position': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True', 'blank': 'True'}),
'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
# 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
# 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
},
'cms.placeholder': {
'Meta': {'object_name': 'Placeholder'},
Expand Down

0 comments on commit 09a745a

Please sign in to comment.