7 from xml.dom.minidom import parse, parseString
9 from django import VERSION as DJANGO_VERSION
10 from django.http import HttpResponse
11 from django.utils.translation import ugettext as _
12 from django.utils import simplejson
13 from django.conf import settings
15 from base import get_site_views, get_server_name, get_admin_emails
16 from settings import SITE_KEY, UPDATE_SERVER_URL
17 from forum.settings import APP_URL, SVN_REVISION
18 from forum.views.admin import admin_tools_page, admin_page
20 @admin_tools_page(_('updater'), _('Update Checker'))
21 def updater_index(request):
23 'modules/updater/index.html',
29 def updater_check(request):
30 # Get the SVN Revision
32 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
34 # Here we'll have to find another way of getting the SVN revision
37 admin_emails_xml = '<emails>'
38 for email in get_admin_emails():
39 admin_emails_xml += '<email value="%s" />' % email
40 admin_emails_xml += '</emails>'
42 statistics = """<check>
43 <key value="%(site_key)s" />
44 <app_url value="%(app_url)s" />
45 <svn_revision value="%(svn_revision)d" />
46 <views value="%(site_views)d" />
47 <active_users value="11959" />
48 <server value="%(server_name)s" />
49 <python_version value="%(python_version)s" />
50 <django_version value="%(django_version)s" />
51 <database value="%(database)s" />
55 'site_key' : SITE_KEY,
57 'svn_revision' : svn_revision,
58 'site_views' : get_site_views(),
59 'server_name' : get_server_name(),
60 'python_version' : ''.join(sys.version.splitlines()),
61 'django_version' : str(DJANGO_VERSION),
62 'database' : settings.DATABASE_ENGINE,
63 'os' : str(os.uname()),
64 'emails' : admin_emails_xml,
67 # Compress the statistics XML dump
68 statistics_compressed = bz2.compress(statistics)
70 # Pass the compressed statistics to the update server
72 'statistics' : binascii.b2a_base64(statistics_compressed),
74 data = urllib.urlencode(post_data)
76 # We simulate some browser, otherwise the server can return 403 response
77 user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/5'
78 headers={ 'User-Agent' : user_agent,}
81 check_request = urllib2.Request('%s%s' % (UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
82 check_response = urllib2.urlopen(check_request)
83 content = check_response.read()
84 except urllib2.HTTPError, error:
85 content = error.read()
87 # Read the messages from the Update Server
88 messages_xml_url = '%s%s' % (UPDATE_SERVER_URL, '/messages/xml/')
89 messages_request = urllib2.Request(messages_xml_url, headers=headers)
90 messages_response = urllib2.urlopen(messages_request)
91 messages_xml = messages_response.read()
93 messages_dom = parseString(messages_xml)
94 messages_count = len(messages_dom.getElementsByTagName('message'))
96 return HttpResponse(_('%d update messages have been downloaded') % messages_count, mimetype='text/html')