16 from xml.dom.minidom import parse, parseString
17 from forum.base import get_database_engine
18 from forum.models import Question, User
19 from forum.settings import APP_URL, SVN_REVISION, APP_TITLE, APP_DESCRIPTION
20 from django import VERSION as DJANGO_VERSION
21 from django.utils import simplejson
22 from django.utils.html import escape
23 from django.utils.encoding import smart_unicode
24 from django.conf import settings as django_settings
25 from django.utils.translation import ugettext as _
28 def generate_installation_key():
29 gen = lambda length: "".join( [random.choice(string.digits+string.letters) for i in xrange(length)])
30 return '%s-%s-%s-%s' % (gen(4), gen(4), gen(4), gen(4))
32 # To get the site views count we get the SUM of all questions views.
36 # Go through all questions and increase the views count
37 for question in Question.objects.all():
38 views += question.view_count
42 # Gets the active users count since the last visit
43 def get_active_users():
47 if settings.LATEST_UPDATE_DATETIME:
48 users_count = User.objects.filter(last_login__gt=settings.LATEST_UPDATE_DATETIME).count()
54 def get_server_name():
59 request = urllib2.Request(url)
60 response = urllib2.urlopen(request)
62 # Get the response information
63 response_info = response.info()
65 server_name = re.findall("Server: (?P<server_name>.*)$", str(response_info))[0]
66 server_name = ''.join(server_name.splitlines())
72 def get_admin_emails():
75 for user in User.objects.filter(is_superuser=True):
76 emails.append(user.email)
80 def check_for_updates():
81 # Get the SVN Revision
83 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
85 # Here we'll have to find another way of getting the SVN revision
88 admin_emails_xml = '<emails>'
89 for email in get_admin_emails():
90 admin_emails_xml += '<email value="%s" />' % email
91 admin_emails_xml += '</emails>'
93 database_type = get_database_engine()
95 statistics = """<check>
96 <key value="%(site_key)s" />
97 <app_url value="%(app_url)s" />
98 <app_title value="%(app_title)s" />
99 <app_description value="%(app_description)s" />
100 <svn_revision value="%(svn_revision)d" />
101 <views value="%(site_views)d" />
102 <active_users value="%(active_users)d" />
103 <server value="%(server_name)s" />
104 <python_version value="%(python_version)s" />
105 <django_version value="%(django_version)s" />
106 <database value="%(database)s" />
107 <os value="%(os)s" />
110 'site_key' : settings.SITE_KEY,
112 'app_title' : escape(APP_TITLE.value),
113 'app_description' : escape(APP_DESCRIPTION.value),
114 'svn_revision' : svn_revision,
115 'site_views' : get_site_views(),
116 'server_name' : get_server_name(),
117 'active_users' : get_active_users(),
118 'python_version' : ''.join(sys.version.splitlines()),
119 'django_version' : str(DJANGO_VERSION),
120 'database' : database_type,
121 'os' : str(platform.uname()),
122 'emails' : admin_emails_xml,
125 # Compress the statistics XML dump
126 statistics_compressed = bz2.compress(statistics)
128 # Pass the compressed statistics to the update server
130 'statistics' : binascii.b2a_base64(statistics_compressed),
132 data = urllib.urlencode(post_data)
134 # We simulate some browser, otherwise the server can return 403 response
135 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'
136 headers={ 'User-Agent' : user_agent,}
139 check_request = urllib2.Request('%s%s' % (settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
140 check_response = urllib2.urlopen(check_request)
141 content = check_response.read()
142 except urllib2.HTTPError, error:
143 content = error.read()
145 return _("Wasn't able to check to the update server.")
147 # Read the messages from the Update Server
149 messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
150 messages_request = urllib2.Request(messages_xml_url, headers=headers)
151 messages_response = urllib2.urlopen(messages_request)
152 messages_xml = messages_response.read()
154 return _("Wasn't able to retreive the update messages.")
156 # Store the messages XML in a Setting object
157 settings.UPDATE_MESSAGES_XML.set_value(messages_xml)
159 messages_dom = parseString(messages_xml)
160 messages_count = len(messages_dom.getElementsByTagName('message'))
162 # Set the latest update datetime to now.
163 now = datetime.datetime.now()
164 settings.LATEST_UPDATE_DATETIME.set_value(now)
166 return _('%d update messages have been downloaded.') % messages_count
168 def update_trigger():
169 # Trigger the update process
170 now = datetime.datetime.now()
171 if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1):
172 update_status = check_for_updates()
174 logging.error(smart_unicode("Update process has been triggered: %s" % update_status))
175 settings.LATEST_UPDATE_DATETIME.set_value(now)