Merge branch 'release/0.7' into develop
[ganeti_webmgr.git] / ganeti_web / management.py
blob2474c3920b1f7c3bd9fa4cf5acf7ade23e063482
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
26 def update_sites_module(sender, **kwargs):
27 """
28 Create a new row in the django_sites table that
29 holds SITE_ID, SITE_NAME and SITE_DOMAIN defined
30 in setting.py
32 If SITE_NAME or SITE_DOMAIN are not defined they
33 will default to 'example.com'
34 """
35 verb = kwargs.get('verbosity', 0)
36 id, name, domain = (1, 'example.com', 'example.com')
37 try:
38 id = settings.SITE_ID
39 except AttributeError, e:
40 print e
41 print 'Using: \'%s\' for site id.' % id
43 try:
44 name = settings.SITE_NAME
45 except AttributeError, e:
46 print e
47 print 'Using: \'%s\' for site name.' % name
49 try:
50 domain = settings.SITE_DOMAIN
51 except AttributeError, e:
52 print e
53 print 'Using: \'%s\' for site domain.' % domain
55 try:
56 site = Site.objects.get(id=id)
57 if site.name != name:
58 if verb >= 1:
59 print "Site name changed from %s to %s." % \
60 (site.name, name)
61 site.name = name
62 if site.domain != domain:
63 if verb >= 1:
64 print "Site domain changed from %s to %s." % \
65 (site.domain, domain)
66 site.domain = domain
67 site.save()
68 except Site.DoesNotExist:
69 if verb >= 1:
70 print "New site: [%s] %s (%s) created in django_site table." % \
71 (id, name, domain)
72 site = Site(id=id, name=name, domain=domain)
73 site.save()
74 # Reconnect create_default_site request for other apps
75 post_syncdb.connect(create_default_site, sender=sites_app)
76 else:
77 if site.name != name:
78 print "A site with the id of %s is already taken. " \
79 "Please change SITE_ID to a different number in your " \
80 "settings.py file." % id