15 from xml.dom.minidom import parse, parseString
16 from forum.models import Question, User
17 from forum.settings import APP_URL, SVN_REVISION
18 from django import VERSION as DJANGO_VERSION
19 from django.utils import simplejson
20 from django.utils.encoding import smart_unicode
21 from django.conf import settings as django_settings
22 from django.utils.translation import ugettext as _
25 def generate_installation_key():
26 gen = lambda length: "".join( [random.choice(string.digits+string.letters) for i in xrange(length)])
27 return '%s-%s-%s-%s' % (gen(4), gen(4), gen(4), gen(4))
29 # To get the site views count we get the SUM of all questions views.
33 # Go through all questions and increase the views count
34 for question in Question.objects.all():
35 views += question.view_count
39 def get_server_name():
44 request = urllib2.Request(url)
45 response = urllib2.urlopen(request)
47 # Get the response information
48 response_info = response.info()
50 server_name = re.findall("Server: (?P<server_name>.*)$", str(response_info))[0]
51 server_name = ''.join(server_name.splitlines())
57 def get_admin_emails():
60 for user in User.objects.filter(is_superuser=True):
61 emails.append(user.email)
65 def check_for_updates():
66 # Get the SVN Revision
68 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
70 # Here we'll have to find another way of getting the SVN revision
73 admin_emails_xml = '<emails>'
74 for email in get_admin_emails():
75 admin_emails_xml += '<email value="%s" />' % email
76 admin_emails_xml += '</emails>'
78 statistics = """<check>
79 <key value="%(site_key)s" />
80 <app_url value="%(app_url)s" />
81 <svn_revision value="%(svn_revision)d" />
82 <views value="%(site_views)d" />
83 <active_users value="11959" />
84 <server value="%(server_name)s" />
85 <python_version value="%(python_version)s" />
86 <django_version value="%(django_version)s" />
87 <database value="%(database)s" />
91 'site_key' : settings.SITE_KEY,
93 'svn_revision' : svn_revision,
94 'site_views' : get_site_views(),
95 'server_name' : get_server_name(),
96 'python_version' : ''.join(sys.version.splitlines()),
97 'django_version' : str(DJANGO_VERSION),
98 'database' : django_settings.DATABASE_ENGINE,
99 'os' : str(os.uname()),
100 'emails' : admin_emails_xml,
103 # Compress the statistics XML dump
104 statistics_compressed = bz2.compress(statistics)
106 # Pass the compressed statistics to the update server
108 'statistics' : binascii.b2a_base64(statistics_compressed),
110 data = urllib.urlencode(post_data)
112 # We simulate some browser, otherwise the server can return 403 response
113 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'
114 headers={ 'User-Agent' : user_agent,}
117 check_request = urllib2.Request('%s%s' % (settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
118 check_response = urllib2.urlopen(check_request)
119 content = check_response.read()
120 except urllib2.HTTPError, error:
121 content = error.read()
123 # Read the messages from the Update Server
124 messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
125 messages_request = urllib2.Request(messages_xml_url, headers=headers)
126 messages_response = urllib2.urlopen(messages_request)
127 messages_xml = messages_response.read()
129 # Store the messages XML in a Setting object
130 settings.UPDATE_MESSAGES_XML.set_value(messages_xml)
132 messages_dom = parseString(messages_xml)
133 messages_count = len(messages_dom.getElementsByTagName('message'))
135 return _('%d update messages have been downloaded') % messages_count
137 def update_trigger():
138 # Trigger the update process
139 now = datetime.datetime.now()
140 if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1):
141 update_status = check_for_updates()
143 logging.error(smart_unicode("Update process has been triggered: %s" % update_status))
145 # Set the latest update datetime to now.
146 settings.LATEST_UPDATE_DATETIME.set_value(now)