Get rid of dumpdata and serialize document programatically, add support for model specific pk sanitation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user