mirror of
https://github.com/django/django.git
synced 2025-09-15 05:29:11 +00:00
magic-removal: django-admin syncdb now installs initial data.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2482 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
809f9e9925
commit
397139f32c
@ -295,9 +295,27 @@ def get_sql_reset(app):
|
|||||||
get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)."
|
get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)."
|
||||||
get_sql_reset.args = APP_ARGS
|
get_sql_reset.args = APP_ARGS
|
||||||
|
|
||||||
|
def get_sql_initial_data_for_model(model):
|
||||||
|
from django.db import models
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
opts = model._meta
|
||||||
|
app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql'))
|
||||||
|
output = []
|
||||||
|
|
||||||
|
# Find custom SQL, if it's available.
|
||||||
|
sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)),
|
||||||
|
os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
|
||||||
|
for sql_file in sql_files:
|
||||||
|
if os.path.exists(sql_file):
|
||||||
|
fp = open(sql_file)
|
||||||
|
output.append(fp.read())
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
def get_sql_initial_data(app):
|
def get_sql_initial_data(app):
|
||||||
"Returns a list of the initial INSERT SQL statements for the given app."
|
"Returns a list of the initial INSERT SQL statements for the given app."
|
||||||
from django.conf import settings
|
|
||||||
from django.db.models import get_models
|
from django.db.models import get_models
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
@ -305,23 +323,8 @@ def get_sql_initial_data(app):
|
|||||||
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
|
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
|
||||||
|
|
||||||
for klass in app_models:
|
for klass in app_models:
|
||||||
opts = klass._meta
|
output.extend(get_sql_initial_data_for_model(klass))
|
||||||
|
|
||||||
# Add custom SQL, if it's available.
|
|
||||||
# TODO: This probably needs changing
|
|
||||||
sql_files = [os.path.join(app_dir, opts.module_name + '.' + settings.DATABASE_ENGINE + '.sql'),
|
|
||||||
os.path.join(app_dir, opts.module_name + '.sql')]
|
|
||||||
for sql_file in sql_files:
|
|
||||||
if os.path.exists(sql_file):
|
|
||||||
fp = open(sql_file)
|
|
||||||
output.append(fp.read())
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
# TODO: This is temporarily commented out until we come up
|
|
||||||
# with a better way of letting people initialize permissions.
|
|
||||||
# # Permissions.
|
|
||||||
# for codename, name in _get_all_permissions(opts):
|
|
||||||
# output.append(_get_permission_insert(name, codename, opts))
|
|
||||||
return output
|
return output
|
||||||
get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
|
get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
|
||||||
get_sql_initial_data.args = APP_ARGS
|
get_sql_initial_data.args = APP_ARGS
|
||||||
@ -365,7 +368,6 @@ def get_sql_all(app):
|
|||||||
get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given model module name(s)."
|
get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given model module name(s)."
|
||||||
get_sql_all.args = APP_ARGS
|
get_sql_all.args = APP_ARGS
|
||||||
|
|
||||||
# TODO: syncdb() should include initial SQL data
|
|
||||||
# TODO: Put "example.com" site in initial SQL data for sites app
|
# TODO: Put "example.com" site in initial SQL data for sites app
|
||||||
def syncdb():
|
def syncdb():
|
||||||
"Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
|
"Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
|
||||||
@ -423,13 +425,32 @@ def syncdb():
|
|||||||
try:
|
try:
|
||||||
installperms(app)
|
installperms(app)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
# stop trying to install permissions
|
||||||
|
install_permissions = False
|
||||||
sys.stderr.write("Permissions will not be installed because it "\
|
sys.stderr.write("Permissions will not be installed because it "\
|
||||||
"appears that you are not using Django's auth framework. "\
|
"appears that you are not using Django's auth framework. "\
|
||||||
"If you want to install them in the future, re-run syncdb."\
|
"If you want to install them in the future, re-run syncdb."\
|
||||||
"\n(The full error was: %s)" % (app_name, app_name, e))
|
"\n(The full error was: %s)" % e)
|
||||||
transaction.rollback_unless_managed()
|
transaction.rollback_unless_managed()
|
||||||
else:
|
else:
|
||||||
transaction.commit_unless_managed()
|
transaction.commit_unless_managed()
|
||||||
|
|
||||||
|
# Install initial data for the app (but only if this is a model we've
|
||||||
|
# just created)
|
||||||
|
for model in model_list:
|
||||||
|
if model in created_models:
|
||||||
|
initial_sql = get_sql_initial_data_for_model(model)
|
||||||
|
if initial_sql:
|
||||||
|
print "Installing initial data for %s model" % model._meta.object_name
|
||||||
|
try:
|
||||||
|
for sql in initial_sql:
|
||||||
|
cursor.execute(sql)
|
||||||
|
except Exception, e:
|
||||||
|
sys.stderr.write("Failed to install initial SQL data for %s model: %s" % \
|
||||||
|
(model._meta.object_name, e))
|
||||||
|
transaction.rollback_unless_managed()
|
||||||
|
else:
|
||||||
|
transaction.commit_unless_managed()
|
||||||
|
|
||||||
syncdb.args = ''
|
syncdb.args = ''
|
||||||
|
|
||||||
@ -460,7 +481,6 @@ get_admin_index.args = APP_ARGS
|
|||||||
def install(app):
|
def install(app):
|
||||||
"Executes the equivalent of 'get_sql_all' in the current database."
|
"Executes the equivalent of 'get_sql_all' in the current database."
|
||||||
from django.db import connection, transaction
|
from django.db import connection, transaction
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
app_name = app.__name__.split('.')[-2]
|
app_name = app.__name__.split('.')[-2]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user