2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Creates the default Site object.
|
|
|
|
"""
|
|
|
|
|
2013-12-28 03:55:58 +08:00
|
|
|
from django.apps import apps
|
2014-12-01 01:03:40 +08:00
|
|
|
from django.conf import settings
|
2012-01-07 16:53:36 +08:00
|
|
|
from django.core.management.color import no_style
|
2013-12-28 03:55:58 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections, router
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-12-27 22:06:38 +08:00
|
|
|
def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
|
2013-12-28 03:55:58 +08:00
|
|
|
try:
|
|
|
|
Site = apps.get_model('sites', 'Site')
|
|
|
|
except LookupError:
|
|
|
|
return
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
if not router.allow_migrate_model(using, Site):
|
2013-12-28 03:55:58 +08:00
|
|
|
return
|
|
|
|
|
2015-02-13 02:58:37 +08:00
|
|
|
if not Site.objects.using(using).exists():
|
2011-09-22 06:46:48 +08:00
|
|
|
# The default settings set SITE_ID = 1, and some tests in Django's test
|
|
|
|
# suite rely on this value. However, if database sequences are reused
|
|
|
|
# (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
|
|
|
|
# the next id will be 1, so we coerce it. See #15573 and #16353. This
|
|
|
|
# can also crop up outside of tests - see #15346.
|
2012-01-07 16:53:36 +08:00
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Creating example.com Site object")
|
2015-03-16 06:54:20 +08:00
|
|
|
Site(pk=getattr(settings, 'SITE_ID', 1), domain="example.com", name="example.com").save(using=using)
|
2012-01-07 16:53:36 +08:00
|
|
|
|
|
|
|
# We set an explicit pk instead of relying on auto-incrementation,
|
|
|
|
# so we need to reset the database sequence. See #17415.
|
2014-12-27 22:06:38 +08:00
|
|
|
sequence_sql = connections[using].ops.sequence_reset_sql(no_style(), [Site])
|
2012-01-07 16:53:36 +08:00
|
|
|
if sequence_sql:
|
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Resetting sequence")
|
2014-12-27 22:06:38 +08:00
|
|
|
with connections[using].cursor() as cursor:
|
2014-01-09 23:05:15 +08:00
|
|
|
for command in sequence_sql:
|
|
|
|
cursor.execute(command)
|