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
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 statistics = """<check>
38 <key value="%(site_key)s" />
39 <app_url value="%(app_url)s" />
40 <svn_revision value="%(svn_revision)d" />
41 <views value="%(site_views)d" />
42 <active_users value="11959" />
43 <server value="%(server_name)s" />
44 <python_version value="%(python_version)s" />
45 <django_version value="%(django_version)s" />
46 <database value="%(database)s" />
49 'site_key' : SITE_KEY,
51 'svn_revision' : svn_revision,
52 'site_views' : get_site_views(),
53 'server_name' : get_server_name(),
54 'python_version' : ''.join(sys.version.splitlines()),
55 'django_version' : str(DJANGO_VERSION),
56 'database' : settings.DATABASE_ENGINE,
57 'os' : str(os.uname()),
60 # Compress the statistics XML dump
61 statistics_compressed = bz2.compress(statistics)
63 # Pass the compressed statistics to the update server
65 'statistics' : binascii.b2a_base64(statistics_compressed),
67 data = urllib.urlencode(post_data)
69 # We simulate some browser, otherwise the server can return 403 response
70 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'
71 headers={ 'User-Agent' : user_agent,}
74 check_request = urllib2.Request('%s%s' % (UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
75 check_response = urllib2.urlopen(check_request)
76 content = check_response.read()
77 except urllib2.HTTPError, error:
78 content = error.read()
80 # Read the messages from the Update Server
81 messages_xml_url = '%s%s' % (UPDATE_SERVER_URL, '/messages/xml/')
82 messages_request = urllib2.Request(messages_xml_url, headers=headers)
83 messages_response = urllib2.urlopen(messages_request)
84 messages_xml = messages_response.read()
86 messages_dom = parseString(messages_xml)
87 messages_count = len(messages_dom.getElementsByTagName('message'))
89 return HttpResponse(_('%d update messages have been downloaded') % messages_count, mimetype='text/html')