Add code to check if there is existing data before executing a bootstrap setup
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from .exceptions import ExistingData
|
||||||
|
|
||||||
|
|
||||||
class Cleanup(object):
|
class Cleanup(object):
|
||||||
"""
|
"""
|
||||||
@@ -25,6 +29,13 @@ class BootstrapModel(object):
|
|||||||
"""
|
"""
|
||||||
_registry = {}
|
_registry = {}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def check_for_data(cls):
|
||||||
|
for model in cls.get_all():
|
||||||
|
model_instance = models.get_model(model.app_name, model.model_name)
|
||||||
|
if model_instance.objects.all().count():
|
||||||
|
raise ExistingData
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all(cls):
|
def get_all(cls):
|
||||||
return cls._registry.values()
|
return cls._registry.values()
|
||||||
|
|||||||
10
apps/bootstrap/exceptions.py
Normal file
10
apps/bootstrap/exceptions.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
|
||||||
|
class ExistingData(Exception):
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
bootstrap's fixture
|
||||||
|
"""
|
||||||
|
pass
|
||||||
@@ -10,6 +10,7 @@ from django.core import management
|
|||||||
from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE,
|
from .literals import (FIXTURE_TYPES_CHOICES, FIXTURE_FILE_TYPE,
|
||||||
FIXTURE_TYPE_PK_NULLIFIER, COMMAND_LOADDATA)
|
FIXTURE_TYPE_PK_NULLIFIER, COMMAND_LOADDATA)
|
||||||
from .managers import BootstrapSetupManager
|
from .managers import BootstrapSetupManager
|
||||||
|
from .classes import BootstrapModel
|
||||||
|
|
||||||
|
|
||||||
class BootstrapSetup(models.Model):
|
class BootstrapSetup(models.Model):
|
||||||
@@ -30,6 +31,7 @@ class BootstrapSetup(models.Model):
|
|||||||
return FIXTURE_FILE_TYPE[self.type]
|
return FIXTURE_FILE_TYPE[self.type]
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
|
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
|
||||||
os.close(handle)
|
os.close(handle)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from .permissions import (PERMISSION_BOOTSTRAP_VIEW, PERMISSION_BOOTSTRAP_CREATE
|
|||||||
PERMISSION_BOOTSTRAP_EXECUTE, PERMISSION_NUKE_DATABASE, PERMISSION_BOOTSTRAP_DUMP)
|
PERMISSION_BOOTSTRAP_EXECUTE, PERMISSION_NUKE_DATABASE, PERMISSION_BOOTSTRAP_DUMP)
|
||||||
from .icons import icon_bootstrap_setup_execute, icon_nuke_database, icon_bootstrap_setup_delete
|
from .icons import icon_bootstrap_setup_execute, icon_nuke_database, icon_bootstrap_setup_delete
|
||||||
from .forms import BootstrapSetupForm, BootstrapSetupForm_view, BootstrapSetupForm_dump
|
from .forms import BootstrapSetupForm, BootstrapSetupForm_view, BootstrapSetupForm_dump
|
||||||
|
from .exceptions import ExistingData
|
||||||
|
|
||||||
|
|
||||||
def bootstrap_setup_list(request):
|
def bootstrap_setup_list(request):
|
||||||
@@ -155,6 +156,8 @@ def bootstrap_setup_execute(request, bootstrap_setup_pk):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
try:
|
try:
|
||||||
bootstrap_setup.execute()
|
bootstrap_setup.execute()
|
||||||
|
except ExistingData:
|
||||||
|
messages.error(request, _(u'Cannot execute bootstrap setup, there is existing data. Erase database and try again.'))
|
||||||
except Exception, exc:
|
except Exception, exc:
|
||||||
messages.error(request, _(u'Error executing bootstrap setup; %s') % exc)
|
messages.error(request, _(u'Error executing bootstrap setup; %s') % exc)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user