Permalink
Browse files

Merge remote-tracking branch 'chrischambers/master'

  • Loading branch information...
2 parents 4831aa0 + 1f0db53 commit e8cb9cddddd55f762207da5151df046fa223ffa2 Shaun Sephton committed Sep 9, 2011
Showing with 51 additions and 0 deletions.
  1. +3 −0 AUTHORS.rst
  2. +16 −0 README.rst
  3. +31 −0 snippetscream/_2536.py
  4. +1 −0 snippetscream/__init__.py
View
@@ -36,3 +36,6 @@ Maintained By
--------------------
#. stringify
+2536. Configurable defaults for contrib.sites default Site during syncdb
+------------------------------------------------------------------------
+#. chrischambers
View
@@ -103,3 +103,19 @@ Example::
>>> from django.core import serializers
>>> csvdata = serializers.serialize('csv', Foo.objects.all())
+2536. Configurable defaults for contrib.sites default Site during syncdb
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Modelled after #1875, this provides a more sensible default for the ``Site``
+object created during the first pass of ``syncdb`` (default domain of
+``localhost:8000``). It means that the admin's "view on site" button will work
+automagically, amongst other things.
+
+Original Snippet - http://djangosnippets.org/snippets/2536/
+
+To enable, simply add ``snippetscream`` to your ``INSTALLED_APPS`` settings.
+If you'd like to customise the default ``Site`` yourself, you can specify:
+
+* settings parameters (``DEFAULT_SITE_DOMAIN`` and ``DEFAULT_SITE_NAME``) or
+* ``kwargs`` for the ``create_default_site`` function [#]_
+
+.. [#] ``kwargs`` take precedence over the settings parameters.
View
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+# Adapted From Http://stackoverflow.com/questions/1466827/ --
+from django.conf import settings
+from django.db.models import signals
+from django.contrib.sites.models import Site
+from django.contrib.sites import models as site_app
+from django.contrib.sites.management import create_default_site as orig_default_site
+
+signals.post_syncdb.disconnect(orig_default_site, sender=site_app)
+
+# Configure default Site creation with better defaults, and provide
+# overrides for those defaults via settings and kwargs:
+def create_default_site(app, created_models, verbosity, db, **kwargs):
+ name = kwargs.pop('name', None)
+ domain = kwargs.pop('domain', None)
+
+ if not name:
+ name = getattr(settings, 'DEFAULT_SITE_NAME', 'example.com')
+ if not domain:
+ domain = getattr(settings, 'DEFAULT_SITE_DOMAIN', 'localhost:8000')
+
+ if Site in created_models:
+ if verbosity >= 2:
+ print "Creating example.com Site object"
+ s = Site(domain=domain, name=name)
+ s.save(using=db)
+ Site.objects.clear_cache()
+
+signals.post_syncdb.connect(create_default_site, sender=site_app)
@@ -3,3 +3,4 @@
from _1031 import *
from _1378 import *
import _1875
+import _2536

0 comments on commit e8cb9cd

Please sign in to comment.