]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/updater/base.py
Jira OSQA-712, migrating to the new Databases setting format
[osqa.git] / forum_modules / updater / base.py
index 75e1a212a07af42f045d4b2a9d8153bc8b5ab24b..c5bac16e01273d214add90e4e61f36f38a57be2b 100644 (file)
@@ -13,10 +13,12 @@ import logging
 
 
 from xml.dom.minidom import parse, parseString
+from forum.startup import get_database_engine
 from forum.models import Question, User
-from forum.settings import APP_URL, SVN_REVISION
+from forum.settings import APP_URL, SVN_REVISION, APP_TITLE, APP_DESCRIPTION
 from django import VERSION as DJANGO_VERSION
 from django.utils import simplejson
+from django.utils.html import escape
 from django.utils.encoding import smart_unicode
 from django.conf import settings as django_settings
 from django.utils.translation import ugettext as _
@@ -36,6 +38,18 @@ def get_site_views():
 
     return views
 
+# Gets the active users count since the last visit
+def get_active_users():
+    users_count = 0
+
+    try:
+        if settings.LATEST_UPDATE_DATETIME:
+            users_count = User.objects.filter(last_login__gt=settings.LATEST_UPDATE_DATETIME).count()
+    except:
+        pass
+
+    return users_count
+
 def get_server_name():
     url = '%s/' % APP_URL
 
@@ -75,12 +89,16 @@ def check_for_updates():
         admin_emails_xml += '<email value="%s" />' % email
     admin_emails_xml += '</emails>'
 
+    database_type = get_database_engine()
+
     statistics = """<check>
     <key value="%(site_key)s" />
     <app_url value="%(app_url)s" />
+    <app_title value="%(app_title)s" />
+    <app_description value="%(app_description)s" />
     <svn_revision value="%(svn_revision)d" />
     <views value="%(site_views)d" />
-    <active_users value="11959" />
+    <active_users value="%(active_users)d" />
     <server value="%(server_name)s" />
     <python_version value="%(python_version)s" />
     <django_version value="%(django_version)s" />
@@ -90,12 +108,15 @@ def check_for_updates():
 </check> """ % {
         'site_key' : settings.SITE_KEY,
         'app_url' : APP_URL,
+        'app_title' : escape(APP_TITLE.value),
+        'app_description' : escape(APP_DESCRIPTION.value),
         'svn_revision' : svn_revision,
         'site_views' : get_site_views(),
         'server_name' : get_server_name(),
+        'active_users' : get_active_users(),
         'python_version' : ''.join(sys.version.splitlines()),
         'django_version' : str(DJANGO_VERSION),
-        'database' : django_settings.DATABASE_ENGINE,
+        'database' : database_type,
         'os' : str(os.uname()),
         'emails' : admin_emails_xml,
     }
@@ -119,12 +140,17 @@ def check_for_updates():
         content = check_response.read()
     except urllib2.HTTPError, error:
         content = error.read()
+    except:
+        return _("Wasn't able to check to the update server.")
 
     # Read the messages from the Update Server
-    messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
-    messages_request = urllib2.Request(messages_xml_url, headers=headers)
-    messages_response = urllib2.urlopen(messages_request)
-    messages_xml = messages_response.read()
+    try:
+        messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
+        messages_request = urllib2.Request(messages_xml_url, headers=headers)
+        messages_response = urllib2.urlopen(messages_request)
+        messages_xml = messages_response.read()
+    except:
+        return _("Wasn't able to retreive the update messages.")
 
     # Store the messages XML in a Setting object
     settings.UPDATE_MESSAGES_XML.set_value(messages_xml)
@@ -132,7 +158,11 @@ def check_for_updates():
     messages_dom = parseString(messages_xml)
     messages_count = len(messages_dom.getElementsByTagName('message'))
 
-    return _('%d update messages have been downloaded') % messages_count
+    # Set the latest update datetime to now.
+    now = datetime.datetime.now()
+    settings.LATEST_UPDATE_DATETIME.set_value(now)
+
+    return _('%d update messages have been downloaded.') % messages_count
 
 def update_trigger():
     # Trigger the update process
@@ -141,6 +171,4 @@ def update_trigger():
         update_status = check_for_updates()
 
         logging.error(smart_unicode("Update process has been triggered: %s" % update_status))
-
-        # Set the latest update datetime to now.
         settings.LATEST_UPDATE_DATETIME.set_value(now)