Merge tag '0.10.2'
[ganeti_webmgr.git] / ganeti_web / management.py
blob28879dac7f61ba00f3efaf32f739307028c97fc1
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
18 from django.conf import settings
20 from django.contrib.sites import models as sites_app
21 from django.contrib.sites.management import create_default_site
22 from django.contrib.sites.models import Site
24 from django.db.models.signals import post_syncdb
27 def update_sites_module(sender, **kwargs):
28 """
29 Create a new row in the django_sites table that
30 holds SITE_ID, SITE_NAME and SITE_DOMAIN defined
31 in setting.py
33 If SITE_NAME or SITE_DOMAIN are not defined they
34 will default to 'example.com'
35 """
36 verb = kwargs.get('verbosity', 0)
37 id, name, domain = (1, 'example.com', 'example.com')
38 try:
39 id = settings.SITE_ID
40 except AttributeError, e:
41 print e
42 print 'Using: \'%s\' for site id.' % id
44 try:
45 name = settings.SITE_NAME
46 except AttributeError, e:
47 print e
48 print 'Using: \'%s\' for site name.' % name
50 try:
51 domain = settings.SITE_DOMAIN
52 except AttributeError, e:
53 print e
54 print 'Using: \'%s\' for site domain.' % domain
56 try:
57 site = Site.objects.get(id=id)
58 if site.name != name:
59 if verb >= 1:
60 print "Site name changed from %s to %s." % \
61 (site.name, name)
62 site.name = name
63 if site.domain != domain:
64 if verb >= 1:
65 print "Site domain changed from %s to %s." % \
66 (site.domain, domain)
67 site.domain = domain
68 site.save()
69 except Site.DoesNotExist:
70 if verb >= 1:
71 print "New site: [%s] %s (%s) created in django_site table." % \
72 (id, name, domain)
73 site = Site(id=id, name=name, domain=domain)
74 site.save()
75 # Reconnect create_default_site request for other apps
76 post_syncdb.connect(create_default_site, sender=sites_app)
77 else:
78 if site.name != name:
79 print "A site with the id of %s is already taken. " \
80 "Please change SITE_ID to a different number in your " \
81 "settings.py file." % id