Generate a bootstrap's fixture metadata on save not only when dumping it, add comments
This commit is contained in:
@@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class Cleanup(object):
|
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 = {}
|
_registry = {}
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ class Cleanup(object):
|
|||||||
class BootstrapModel(object):
|
class BootstrapModel(object):
|
||||||
"""
|
"""
|
||||||
Class used to keep track of all the models to be dumped to create a
|
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()
|
_registry = SortedDict()
|
||||||
|
|
||||||
@@ -76,6 +76,11 @@ class BootstrapModel(object):
|
|||||||
|
|
||||||
|
|
||||||
class FixtureMetadata(object):
|
class FixtureMetadata(object):
|
||||||
|
"""
|
||||||
|
Class to automatically create and extract metadata from a bootstrap
|
||||||
|
fixture.
|
||||||
|
"""
|
||||||
|
|
||||||
_registry = {}
|
_registry = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ class ExistingData(Exception):
|
|||||||
"""
|
"""
|
||||||
Raised when an attempt to execute a bootstrap setup is made and there is
|
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
|
existing data that would be corrupted or damaged by the loading the
|
||||||
bootstrap's fixture
|
bootstrap's fixture.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import logging
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.core import serializers
|
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
|
from .literals import FIXTURE_TYPE_FIXTURE_PROCESS, FIXTURE_TYPE_EMPTY_FIXTURE
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -19,19 +19,14 @@ class BootstrapSetupManager(models.Manager):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def dump(self, serialization_format, instance):
|
def dump(self, serialization_format):
|
||||||
metadata_text = []
|
"""
|
||||||
# Add fixture metadata
|
Get the current setup of Mayan in bootstrap format fixture
|
||||||
metadata_text.append(FixtureMetadata.generate_all(instance))
|
"""
|
||||||
metadata_text.append('\n')
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for bootstrap_model in BootstrapModel.get_all():
|
for bootstrap_model in BootstrapModel.get_all():
|
||||||
model_fixture = bootstrap_model.dump(serialization_format)
|
model_fixture = bootstrap_model.dump(serialization_format)
|
||||||
# Only add non empty model fixtures
|
# Only add non empty model fixtures
|
||||||
if not FIXTURE_TYPE_EMPTY_FIXTURE[serialization_format](model_fixture):
|
if not FIXTURE_TYPE_EMPTY_FIXTURE[serialization_format](model_fixture):
|
||||||
result.append(model_fixture)
|
result.append(model_fixture)
|
||||||
return '%s\n%s' % (
|
return FIXTURE_TYPE_FIXTURE_PROCESS[serialization_format]('\n'.join(result))
|
||||||
'\n'.join(metadata_text),
|
|
||||||
FIXTURE_TYPE_FIXTURE_PROCESS[serialization_format]('\n'.join(result))
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from __future__ import absolute_import
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import re
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cStringIO import StringIO
|
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 .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE, COMMAND_LOADDATA)
|
||||||
from .managers import BootstrapSetupManager
|
from .managers import BootstrapSetupManager
|
||||||
from .classes import BootstrapModel
|
from .classes import BootstrapModel, FixtureMetadata
|
||||||
|
|
||||||
|
|
||||||
class BootstrapSetup(models.Model):
|
class BootstrapSetup(models.Model):
|
||||||
@@ -32,9 +33,16 @@ class BootstrapSetup(models.Model):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_extension(self):
|
def get_extension(self):
|
||||||
|
"""
|
||||||
|
Return the fixture file extension based on the fixture type.
|
||||||
|
"""
|
||||||
return FIXTURE_FILE_TYPE[self.type]
|
return FIXTURE_FILE_TYPE[self.type]
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
|
"""
|
||||||
|
Read a bootstrap's fixture and create the corresponding model
|
||||||
|
instances based on it.
|
||||||
|
"""
|
||||||
BootstrapModel.check_for_data()
|
BootstrapModel.check_for_data()
|
||||||
handle, filepath = tempfile.mkstemp()
|
handle, filepath = tempfile.mkstemp()
|
||||||
# Just need the filepath, close the file description
|
# 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()])
|
filepath = os.path.extsep.join([filepath, self.get_extension()])
|
||||||
|
|
||||||
with open(filepath, 'w') as file_handle:
|
with open(filepath, 'w') as file_handle:
|
||||||
for line in self.fixture.splitlines(True):
|
file_handle.write(self.cleaned_fixture)
|
||||||
if line.startswith('#'):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
file_handle.write(line)
|
|
||||||
|
|
||||||
content = StringIO()
|
content = StringIO()
|
||||||
management.call_command(COMMAND_LOADDATA, filepath, verbosity=0, stderr=content)
|
management.call_command(COMMAND_LOADDATA, filepath, verbosity=0, stderr=content)
|
||||||
@@ -65,7 +69,24 @@ class BootstrapSetup(models.Model):
|
|||||||
"""
|
"""
|
||||||
return ''
|
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):
|
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)
|
return super(BootstrapSetup, self).save(*args, **kwargs)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ def bootstrap_setup_dump(request):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
bootstrap = form.save(commit=False)
|
bootstrap = form.save(commit=False)
|
||||||
try:
|
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:
|
except Exception as exception:
|
||||||
messages.error(request, _(u'Error dumping bootstrap setup; %s') % exception)
|
messages.error(request, _(u'Error dumping bootstrap setup; %s') % exception)
|
||||||
raise
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user