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', []):
|
for bootstrap_model in getattr(registration, 'bootstrap_models', []):
|
||||||
logger.debug('bootstrap_model: %s' % bootstrap_model)
|
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):
|
#def set_backup(self, *args, **kwargs):
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.core import serializers
|
||||||
|
from django.utils.datastructures import SortedDict
|
||||||
|
|
||||||
from .exceptions import ExistingData
|
from .exceptions import ExistingData
|
||||||
|
from .literals import FIXTURE_TYPE_PK_NULLIFIER
|
||||||
|
|
||||||
|
|
||||||
class Cleanup(object):
|
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
|
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 = {}
|
_registry = SortedDict()#{}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def check_for_data(cls):
|
def check_for_data(cls):
|
||||||
@@ -43,7 +46,10 @@ class BootstrapModel(object):
|
|||||||
def get_fullname(self):
|
def get_fullname(self):
|
||||||
return '.'.join([self.app_name, self.model_name])
|
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
|
app_name_splitted = None
|
||||||
if '.' in model_name:
|
if '.' in model_name:
|
||||||
app_name_splitted, model_name = model_name.split('.')
|
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')
|
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.model_name = model_name
|
||||||
self.__class__._registry[self.get_fullname()] = self
|
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_LOADDATA = 'loaddata'
|
||||||
COMMAND_DUMPDATA = 'dumpdata'
|
|
||||||
|
|||||||
@@ -2,16 +2,15 @@ from __future__ import absolute_import
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
try:
|
#try:
|
||||||
from cStringIO import StringIO
|
# from cStringIO import StringIO
|
||||||
except ImportError:
|
#except ImportError:
|
||||||
from StringIO import StringIO
|
# from StringIO import StringIO
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.core import management
|
from django.core import serializers
|
||||||
|
|
||||||
from .classes import BootstrapModel
|
from .classes import BootstrapModel
|
||||||
from .literals import COMMAND_DUMPDATA
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -24,12 +23,14 @@ class BootstrapSetupManager(models.Manager):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def dump(cls, format):
|
def dump(self, serialization_format):
|
||||||
models = [instance.get_fullname() for instance in BootstrapModel.get_all()]
|
result = []
|
||||||
logger.debug('models: %s' % models)
|
#models = [instance.get_fullname()
|
||||||
result = StringIO()
|
for bootstrap_model in BootstrapModel.get_all():
|
||||||
options = dict(indent=4, format=format, use_natural_keys=True, interactive=False, verbosity=0, stdout=result)
|
#logger.debug('models: %s' % models)
|
||||||
management.call_command(COMMAND_DUMPDATA, *models, **options)
|
#options = dict(indent=4, format=format, use_natural_keys=True, interactive=False, verbosity=0, stdout=result)
|
||||||
result.seek(0)
|
#management.call_command(COMMAND_DUMPDATA, *models, **options)
|
||||||
logger.debug('result: %s' % result)
|
#logger.debug('result: %s' % result)
|
||||||
return result.read()
|
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.utils.translation import ugettext_lazy as _
|
||||||
from django.core import management
|
from django.core import management
|
||||||
|
|
||||||
from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE,
|
from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE, COMMAND_LOADDATA)
|
||||||
FIXTURE_TYPE_PK_NULLIFIER, COMMAND_LOADDATA)
|
|
||||||
from .managers import BootstrapSetupManager
|
from .managers import BootstrapSetupManager
|
||||||
from .classes import BootstrapModel
|
from .classes import BootstrapModel
|
||||||
|
|
||||||
@@ -51,14 +50,7 @@ class BootstrapSetup(models.Model):
|
|||||||
"""
|
"""
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def sanitize(self):
|
|
||||||
"""
|
|
||||||
Remove pk values
|
|
||||||
"""
|
|
||||||
self.fixture = FIXTURE_TYPE_PK_NULLIFIER[self.type](self.fixture)
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
self.sanitize()
|
|
||||||
return super(BootstrapSetup, self).save(*args, **kwargs)
|
return super(BootstrapSetup, self).save(*args, **kwargs)
|
||||||
|
|
||||||
class Meta:
|
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():
|
if form.is_valid():
|
||||||
bootstrap = form.save(commit=False)
|
bootstrap = form.save(commit=False)
|
||||||
try:
|
try:
|
||||||
bootstrap.fixture = BootstrapSetup.objects.dump(format=bootstrap.type)
|
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
|
||||||
else:
|
else:
|
||||||
bootstrap.save()
|
bootstrap.save()
|
||||||
messages.success(request, _(u'Bootstrap created successfully.'))
|
messages.success(request, _(u'Bootstrap created successfully.'))
|
||||||
|
|||||||
Reference in New Issue
Block a user