diff --git a/apps/bootstrap/admin.py b/apps/bootstrap/admin.py new file mode 100644 index 0000000000..86013694b3 --- /dev/null +++ b/apps/bootstrap/admin.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import + +from django.contrib import admin + +from .models import BootstrapSetup + +admin.site.register(BootstrapSetup) diff --git a/apps/bootstrap/classes.py b/apps/bootstrap/classes.py new file mode 100644 index 0000000000..c56133bfa1 --- /dev/null +++ b/apps/bootstrap/classes.py @@ -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 diff --git a/apps/bootstrap/literals.py b/apps/bootstrap/literals.py new file mode 100644 index 0000000000..f341e2cb0b --- /dev/null +++ b/apps/bootstrap/literals.py @@ -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', +} + diff --git a/apps/bootstrap/migrations/0002_auto__add_field_bootstrapsetup_type.py b/apps/bootstrap/migrations/0002_auto__add_field_bootstrapsetup_type.py new file mode 100644 index 0000000000..112fa76a28 --- /dev/null +++ b/apps/bootstrap/migrations/0002_auto__add_field_bootstrapsetup_type.py @@ -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'] \ No newline at end of file diff --git a/apps/bootstrap/models.py b/apps/bootstrap/models.py index e86eafe888..1d70b2d030 100644 --- a/apps/bootstrap/models.py +++ b/apps/bootstrap/models.py @@ -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') diff --git a/apps/bootstrap/views.py b/apps/bootstrap/views.py index 68d9423f6b..6dfbf834a5 100644 --- a/apps/bootstrap/views.py +++ b/apps/bootstrap/views.py @@ -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))