diff --git a/apps/bootstrap/classes.py b/apps/bootstrap/classes.py index a520484845..779bffe3b0 100644 --- a/apps/bootstrap/classes.py +++ b/apps/bootstrap/classes.py @@ -14,7 +14,7 @@ logger = logging.getLogger(__name__) class Cleanup(object): """ - Class to store all the registered cleanup functions in one place + Class to store all the registered cleanup functions in one place. """ _registry = {} @@ -31,7 +31,7 @@ class Cleanup(object): class BootstrapModel(object): """ Class used to keep track of all the models to be dumped to create a - bootstrap setup from the current setup in use + bootstrap setup from the current setup in use. """ _registry = SortedDict() @@ -76,6 +76,11 @@ class BootstrapModel(object): class FixtureMetadata(object): + """ + Class to automatically create and extract metadata from a bootstrap + fixture. + """ + _registry = {} @classmethod diff --git a/apps/bootstrap/exceptions.py b/apps/bootstrap/exceptions.py index 933ee9c84a..bb709a7a5f 100644 --- a/apps/bootstrap/exceptions.py +++ b/apps/bootstrap/exceptions.py @@ -5,6 +5,6 @@ class ExistingData(Exception): """ Raised when an attempt to execute a bootstrap setup is made and there is existing data that would be corrupted or damaged by the loading the - bootstrap's fixture + bootstrap's fixture. """ pass diff --git a/apps/bootstrap/managers.py b/apps/bootstrap/managers.py index fcf97b2153..8c023b7c13 100644 --- a/apps/bootstrap/managers.py +++ b/apps/bootstrap/managers.py @@ -5,7 +5,7 @@ import logging from django.db import models from django.core import serializers -from .classes import BootstrapModel, FixtureMetadata +from .classes import BootstrapModel from .literals import FIXTURE_TYPE_FIXTURE_PROCESS, FIXTURE_TYPE_EMPTY_FIXTURE logger = logging.getLogger(__name__) @@ -19,19 +19,14 @@ class BootstrapSetupManager(models.Manager): """ pass - def dump(self, serialization_format, instance): - metadata_text = [] - # Add fixture metadata - metadata_text.append(FixtureMetadata.generate_all(instance)) - metadata_text.append('\n') - + def dump(self, serialization_format): + """ + Get the current setup of Mayan in bootstrap format fixture + """ result = [] for bootstrap_model in BootstrapModel.get_all(): model_fixture = bootstrap_model.dump(serialization_format) # Only add non empty model fixtures if not FIXTURE_TYPE_EMPTY_FIXTURE[serialization_format](model_fixture): result.append(model_fixture) - return '%s\n%s' % ( - '\n'.join(metadata_text), - FIXTURE_TYPE_FIXTURE_PROCESS[serialization_format]('\n'.join(result)) - ) + return FIXTURE_TYPE_FIXTURE_PROCESS[serialization_format]('\n'.join(result)) diff --git a/apps/bootstrap/models.py b/apps/bootstrap/models.py index cb02b8d3a9..7d32a1477a 100644 --- a/apps/bootstrap/models.py +++ b/apps/bootstrap/models.py @@ -2,6 +2,7 @@ from __future__ import absolute_import import os import tempfile +import re try: from cStringIO import StringIO @@ -14,7 +15,7 @@ from django.core import management from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE, COMMAND_LOADDATA) from .managers import BootstrapSetupManager -from .classes import BootstrapModel +from .classes import BootstrapModel, FixtureMetadata class BootstrapSetup(models.Model): @@ -32,9 +33,16 @@ class BootstrapSetup(models.Model): return self.name def get_extension(self): + """ + Return the fixture file extension based on the fixture type. + """ return FIXTURE_FILE_TYPE[self.type] def execute(self): + """ + Read a bootstrap's fixture and create the corresponding model + instances based on it. + """ BootstrapModel.check_for_data() handle, filepath = tempfile.mkstemp() # Just need the filepath, close the file description @@ -43,11 +51,7 @@ class BootstrapSetup(models.Model): filepath = os.path.extsep.join([filepath, self.get_extension()]) with open(filepath, 'w') as file_handle: - for line in self.fixture.splitlines(True): - if line.startswith('#'): - pass - else: - file_handle.write(line) + file_handle.write(self.cleaned_fixture) content = StringIO() management.call_command(COMMAND_LOADDATA, filepath, verbosity=0, stderr=content) @@ -65,7 +69,24 @@ class BootstrapSetup(models.Model): """ return '' + @property + def cleaned_fixture(self): + """ + Return the bootstrap setup's fixture without comments. + """ + return re.sub(re.compile('#.*?\n'), '', self.fixture) + + def get_metadata_string(self): + """ + Return all the metadata for the current bootstrap fixture. + """ + return FixtureMetadata.generate_all(self) + def save(self, *args, **kwargs): + self.fixture = '%s\n\n%s' % ( + self.get_metadata_string(), + self.cleaned_fixture + ) return super(BootstrapSetup, self).save(*args, **kwargs) class Meta: diff --git a/apps/bootstrap/views.py b/apps/bootstrap/views.py index a84fd4fa4d..41b0281eea 100644 --- a/apps/bootstrap/views.py +++ b/apps/bootstrap/views.py @@ -187,7 +187,7 @@ def bootstrap_setup_dump(request): if form.is_valid(): bootstrap = form.save(commit=False) try: - bootstrap.fixture = BootstrapSetup.objects.dump(serialization_format=bootstrap.type, instance=bootstrap) + bootstrap.fixture = BootstrapSetup.objects.dump(serialization_format=bootstrap.type) except Exception as exception: messages.error(request, _(u'Error dumping bootstrap setup; %s') % exception) raise