Convert database bootstrap setups from class based to database stored fixtures
This commit is contained in:
7
apps/bootstrap/admin.py
Normal file
7
apps/bootstrap/admin.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import BootstrapSetup
|
||||
|
||||
admin.site.register(BootstrapSetup)
|
||||
18
apps/bootstrap/classes.py
Normal file
18
apps/bootstrap/classes.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
|
||||
class Cleanup(object):
|
||||
"""
|
||||
Class to store all the registered cleanup functions in one place
|
||||
"""
|
||||
_registry = {}
|
||||
|
||||
@classmethod
|
||||
def execute_all(cls):
|
||||
for cleanup in cls._registry.values():
|
||||
cleanup.function()
|
||||
|
||||
def __init__(self, name, function):
|
||||
self.name = name
|
||||
self.function = function
|
||||
self.__class__._registry[self.name] = self
|
||||
20
apps/bootstrap/literals.py
Normal file
20
apps/bootstrap/literals.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
FIXTURE_TYPE_JSON = 'json'
|
||||
FIXTURE_TYPE_YAML = 'yaml'
|
||||
FIXTURE_TYPE_XML = 'xml'
|
||||
|
||||
FIXTURE_TYPES_CHOICES = (
|
||||
(FIXTURE_TYPE_JSON, _(u'JSON')),
|
||||
(FIXTURE_TYPE_YAML, _(u'YAML')),
|
||||
(FIXTURE_TYPE_XML, _(u'XML')),
|
||||
)
|
||||
|
||||
FIXTURE_FILE_TYPE = {
|
||||
FIXTURE_TYPE_JSON: 'json',
|
||||
FIXTURE_TYPE_YAML: 'yaml',
|
||||
FIXTURE_TYPE_XML: 'xml',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
# Adding field 'BootstrapSetup.type'
|
||||
db.add_column('bootstrap_bootstrapsetup', 'type',
|
||||
self.gf('django.db.models.fields.CharField')(default=0, max_length=16),
|
||||
keep_default=False)
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
# Deleting field 'BootstrapSetup.type'
|
||||
db.delete_column('bootstrap_bootstrapsetup', 'type')
|
||||
|
||||
|
||||
models = {
|
||||
'bootstrap.bootstrapsetup': {
|
||||
'Meta': {'object_name': 'BootstrapSetup'},
|
||||
'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
|
||||
'fixture': ('django.db.models.fields.TextField', [], {}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
|
||||
'type': ('django.db.models.fields.CharField', [], {'max_length': '16'})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['bootstrap']
|
||||
@@ -1,7 +1,13 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core import management
|
||||
|
||||
from .literals import FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE
|
||||
|
||||
|
||||
class BootstrapSetup(models.Model):
|
||||
@@ -11,10 +17,27 @@ class BootstrapSetup(models.Model):
|
||||
name = models.CharField(max_length=128, verbose_name=_(u'name'), unique=True)
|
||||
description = models.TextField(verbose_name=_(u'description'), blank=True)
|
||||
fixture = models.TextField(verbose_name=_(u'fixture'))
|
||||
type = models.CharField(max_length=16, verbose_name=_(u'type'), choices=FIXTURE_TYPES_CHOICES)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
def get_extension(self):
|
||||
return FIXTURE_FILE_TYPE[self.type]
|
||||
|
||||
def execute(self):
|
||||
handle, filepath = tempfile.mkstemp()
|
||||
# Just need the filepath, close the file description
|
||||
os.close(handle)
|
||||
|
||||
filepath = os.path.extsep.join([filepath, self.get_extension()])
|
||||
|
||||
with open(filepath, 'w') as file_handle:
|
||||
file_handle.write(self.fixture)
|
||||
|
||||
management.call_command('loaddata', filepath, verbosity=0)
|
||||
os.unlink(filepath)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _(u'bootstrap setup')
|
||||
verbose_name_plural = _(u'bootstrap setups')
|
||||
|
||||
@@ -46,7 +46,7 @@ def bootstrap_execute(request, bootstrap_setup_pk):
|
||||
except Exception, exc:
|
||||
messages.error(request, _(u'Error executing bootstrap setup; %s') % exc)
|
||||
else:
|
||||
messages.success(request, _(u'Bootstrap setup "%s" executed successfully.') % bootstrap)
|
||||
messages.success(request, _(u'Bootstrap setup "%s" executed successfully.') % bootstrap_setup)
|
||||
return HttpResponseRedirect(next)
|
||||
|
||||
context = {
|
||||
@@ -55,10 +55,10 @@ def bootstrap_execute(request, bootstrap_setup_pk):
|
||||
'previous': previous,
|
||||
'next': next,
|
||||
'form_icon': icon_bootstrap_execute,
|
||||
'object': bootstrap,
|
||||
'object': bootstrap_setup,
|
||||
}
|
||||
|
||||
context['title'] = _(u'Are you sure you wish to execute the database bootstrap named: %s?') % bootstrap.label
|
||||
context['title'] = _(u'Are you sure you wish to execute the database bootstrap named: %s?') % bootstrap_setup
|
||||
|
||||
return render_to_response('generic_confirm.html', context,
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
Reference in New Issue
Block a user