Generate a bootstrap's fixture metadata on save not only when dumping it, add comments

This commit is contained in:
Roberto Rosario
2012-10-08 11:28:41 -04:00
parent a430a1ce73
commit 2b706f3f4a
5 changed files with 42 additions and 21 deletions

View File

@@ -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: