]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/updater/views.py
we compress the XML dump, convert the binary data to ASCII base64 encoded and send...
[osqa.git] / forum_modules / updater / views.py
1 import os
2 import sys
3 import bz2
4 import urllib2, urllib
5 import binascii
6
7 from django import VERSION as DJANGO_VERSION
8 from django.http import HttpResponse
9 from django.utils.translation import ugettext as _
10 from django.utils import simplejson
11 from django.conf import settings
12
13 from base import get_site_views, get_server_name
14 from settings import SITE_KEY, UPDATE_SERVER_URL
15 from forum.settings import APP_URL, SVN_REVISION
16 from forum.views.admin import admin_tools_page, admin_page
17
18 @admin_tools_page(_('updater'), _('Update Checker'))
19 def updater_index(request):
20     return (
21         'modules/updater/index.html',
22         {
23
24         },
25     )
26
27 def updater_check(request):
28     # Get the SVN Revision
29     try:
30         svn_revision = int(SVN_REVISION.replace('SVN-', ''))
31     except ValueError:
32         # Here we'll have to find another way of getting the SVN revision
33         svn_revision = 0
34
35     statistics = """<check>
36     <key value="%(site_key)s" />
37     <app_url value="%(app_url)s" />
38     <svn_revision value="%(svn_revision)d" />
39     <views value="%(site_views)d" />
40     <active_users value="11959" />
41     <server value="%(server_name)s" />
42     <python_version value="%(python_version)s" />
43     <django_version value="%(django_version)s" />
44     <database value="%(database)s" />
45     <os value="%(os)s" />
46 </check> """ % {
47         'site_key' : SITE_KEY,
48         'app_url' : APP_URL,
49         'svn_revision' : svn_revision,
50         'site_views' : get_site_views(),
51         'server_name' : get_server_name(),
52         'python_version' : ''.join(sys.version.splitlines()),
53         'django_version' : str(DJANGO_VERSION),
54         'database' : settings.DATABASE_ENGINE,
55         'os' : str(os.uname()),
56     }
57
58     # Compress the statistics XML dump
59     statistics_compressed = bz2.compress(statistics)
60
61     # Pass the compressed statistics to the update server
62     post_data = {
63         'statistics' : binascii.b2a_base64(statistics_compressed),
64     }
65     data = urllib.urlencode(post_data)
66
67     # We simulate some browser, otherwise the server can return 403 response
68     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'
69     headers={ 'User-Agent' : user_agent,}
70
71     try:
72         check_request = urllib2.Request('%s%s' % (UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
73         check_response = urllib2.urlopen(check_request)
74         content = check_response.read()
75     except urllib2.HTTPError, error:
76         content = error.read()
77
78     json = simplejson.dumps({})
79     return HttpResponse(content, mimetype='text/html')