2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Creates the default Site object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db.models import signals
|
2012-01-07 16:53:36 +08:00
|
|
|
from django.db import connections
|
2011-09-22 06:46:48 +08:00
|
|
|
from django.db import router
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.contrib.sites import models as site_app
|
2012-01-07 16:53:36 +08:00
|
|
|
from django.core.management.color import no_style
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def create_default_site(app, created_models, verbosity, db, **kwargs):
|
2011-09-22 06:46:48 +08:00
|
|
|
# Only create the default sites in databases where Django created the table
|
2013-10-27 09:27:42 +08:00
|
|
|
if Site in created_models and router.allow_migrate(db, Site):
|
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")
|
2012-01-07 16:53:36 +08:00
|
|
|
Site(pk=1, domain="example.com", name="example.com").save(using=db)
|
|
|
|
|
|
|
|
# We set an explicit pk instead of relying on auto-incrementation,
|
|
|
|
# so we need to reset the database sequence. See #17415.
|
|
|
|
sequence_sql = connections[db].ops.sequence_reset_sql(no_style(), [Site])
|
|
|
|
if sequence_sql:
|
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Resetting sequence")
|
2012-01-07 16:53:36 +08:00
|
|
|
cursor = connections[db].cursor()
|
|
|
|
for command in sequence_sql:
|
|
|
|
cursor.execute(command)
|
|
|
|
|
2008-06-22 14:34:17 +08:00
|
|
|
Site.objects.clear_cache()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
signals.post_migrate.connect(create_default_site, sender=site_app)
|