Get rid of dumpdata and serialize document programatically, add support for model specific pk sanitation
This commit is contained in:
@@ -109,7 +109,7 @@ class App(TranslatableLabelMixin, LiveObjectMixin, models.Model):
|
||||
|
||||
for bootstrap_model in getattr(registration, 'bootstrap_models', []):
|
||||
logger.debug('bootstrap_model: %s' % bootstrap_model)
|
||||
BootstrapModel(model_name=bootstrap_model, app_name=app_name)
|
||||
BootstrapModel(model_name=bootstrap_model.get('name'), app_name=app_name, sanitize=bootstrap_model.get('sanitize', True))
|
||||
|
||||
|
||||
#def set_backup(self, *args, **kwargs):
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.db import models
|
||||
from django.core import serializers
|
||||
from django.utils.datastructures import SortedDict
|
||||
|
||||
from .exceptions import ExistingData
|
||||
from .literals import FIXTURE_TYPE_PK_NULLIFIER
|
||||
|
||||
|
||||
class Cleanup(object):
|
||||
@@ -27,7 +30,7 @@ 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
|
||||
"""
|
||||
_registry = {}
|
||||
_registry = SortedDict()#{}
|
||||
|
||||
@classmethod
|
||||
def check_for_data(cls):
|
||||
@@ -43,7 +46,10 @@ class BootstrapModel(object):
|
||||
def get_fullname(self):
|
||||
return '.'.join([self.app_name, self.model_name])
|
||||
|
||||
def __init__(self, model_name, app_name=None):
|
||||
def get_model_instance(self):
|
||||
return models.get_model(self.app_name, self.model_name)
|
||||
|
||||
def __init__(self, model_name, app_name=None, sanitize=True):
|
||||
app_name_splitted = None
|
||||
if '.' in model_name:
|
||||
app_name_splitted, model_name = model_name.split('.')
|
||||
@@ -53,3 +59,11 @@ class BootstrapModel(object):
|
||||
raise Exception('Pass either a dotted app plus model name or a model name and a separate app name')
|
||||
self.model_name = model_name
|
||||
self.__class__._registry[self.get_fullname()] = self
|
||||
self.sanitize = sanitize
|
||||
|
||||
def dump(self, serialization_format):
|
||||
result = serializers.serialize(serialization_format, self.get_model_instance().objects.all(), indent=4, use_natural_keys=True)
|
||||
if self.sanitize:
|
||||
# Remove primary key values
|
||||
result = FIXTURE_TYPE_PK_NULLIFIER[serialization_format](result)
|
||||
return result
|
||||
|
||||
@@ -28,4 +28,3 @@ FIXTURE_TYPE_PK_NULLIFIER = {
|
||||
}
|
||||
|
||||
COMMAND_LOADDATA = 'loaddata'
|
||||
COMMAND_DUMPDATA = 'dumpdata'
|
||||
|
||||
@@ -2,16 +2,15 @@ from __future__ import absolute_import
|
||||
|
||||
import logging
|
||||
|
||||
try:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
#try:
|
||||
# from cStringIO import StringIO
|
||||
#except ImportError:
|
||||
# from StringIO import StringIO
|
||||
|
||||
from django.db import models
|
||||
from django.core import management
|
||||
from django.core import serializers
|
||||
|
||||
from .classes import BootstrapModel
|
||||
from .literals import COMMAND_DUMPDATA
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -24,12 +23,14 @@ class BootstrapSetupManager(models.Manager):
|
||||
"""
|
||||
pass
|
||||
|
||||
def dump(cls, format):
|
||||
models = [instance.get_fullname() for instance in BootstrapModel.get_all()]
|
||||
logger.debug('models: %s' % models)
|
||||
result = StringIO()
|
||||
options = dict(indent=4, format=format, use_natural_keys=True, interactive=False, verbosity=0, stdout=result)
|
||||
management.call_command(COMMAND_DUMPDATA, *models, **options)
|
||||
result.seek(0)
|
||||
logger.debug('result: %s' % result)
|
||||
return result.read()
|
||||
def dump(self, serialization_format):
|
||||
result = []
|
||||
#models = [instance.get_fullname()
|
||||
for bootstrap_model in BootstrapModel.get_all():
|
||||
#logger.debug('models: %s' % models)
|
||||
#options = dict(indent=4, format=format, use_natural_keys=True, interactive=False, verbosity=0, stdout=result)
|
||||
#management.call_command(COMMAND_DUMPDATA, *models, **options)
|
||||
#logger.debug('result: %s' % result)
|
||||
result.append(bootstrap_model.dump(serialization_format))
|
||||
#return result.read()
|
||||
return '\n'.join(result)
|
||||
|
||||
@@ -7,8 +7,7 @@ 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,
|
||||
FIXTURE_TYPE_PK_NULLIFIER, COMMAND_LOADDATA)
|
||||
from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE, COMMAND_LOADDATA)
|
||||
from .managers import BootstrapSetupManager
|
||||
from .classes import BootstrapModel
|
||||
|
||||
@@ -51,14 +50,7 @@ class BootstrapSetup(models.Model):
|
||||
"""
|
||||
return ''
|
||||
|
||||
def sanitize(self):
|
||||
"""
|
||||
Remove pk values
|
||||
"""
|
||||
self.fixture = FIXTURE_TYPE_PK_NULLIFIER[self.type](self.fixture)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.sanitize()
|
||||
return super(BootstrapSetup, self).save(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
|
||||
1
apps/bootstrap/utils.py
Normal file
1
apps/bootstrap/utils.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -187,9 +187,10 @@ def bootstrap_setup_dump(request):
|
||||
if form.is_valid():
|
||||
bootstrap = form.save(commit=False)
|
||||
try:
|
||||
bootstrap.fixture = BootstrapSetup.objects.dump(format=bootstrap.type)
|
||||
bootstrap.fixture = BootstrapSetup.objects.dump(serialization_format=bootstrap.type)
|
||||
except Exception as exception:
|
||||
messages.error(request, _(u'Error dumping bootstrap setup; %s') % exception)
|
||||
raise
|
||||
else:
|
||||
bootstrap.save()
|
||||
messages.success(request, _(u'Bootstrap created successfully.'))
|
||||
|
||||
Reference in New Issue
Block a user