From b1dc5a1a7af5bacdee0e0b8be70bb9e903fa209f Mon Sep 17 00:00:00 2001 From: jordan Date: Mon, 25 Apr 2011 14:00:31 +0000 Subject: [PATCH] moving the update block to a separate function in the base package, creating the update process triggerer git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1003 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum_modules/updater/base.py | 104 ++++++++++++++++++++++++++++++- forum_modules/updater/startup.py | 11 ++-- forum_modules/updater/views.py | 87 ++------------------------ 3 files changed, 110 insertions(+), 92 deletions(-) diff --git a/forum_modules/updater/base.py b/forum_modules/updater/base.py index 7635b60..75e1a21 100644 --- a/forum_modules/updater/base.py +++ b/forum_modules/updater/base.py @@ -1,11 +1,26 @@ +import os +import sys +import bz2 +import urllib2, urllib +import binascii import string import random import re - import urllib2 +import settings +import datetime +import logging + +from xml.dom.minidom import parse, parseString from forum.models import Question, User -from forum.settings import APP_URL +from forum.settings import APP_URL, SVN_REVISION +from django import VERSION as DJANGO_VERSION +from django.utils import simplejson +from django.utils.encoding import smart_unicode +from django.conf import settings as django_settings +from django.utils.translation import ugettext as _ + def generate_installation_key(): gen = lambda length: "".join( [random.choice(string.digits+string.letters) for i in xrange(length)]) @@ -45,4 +60,87 @@ def get_admin_emails(): for user in User.objects.filter(is_superuser=True): emails.append(user.email) - return emails \ No newline at end of file + return emails + +def check_for_updates(): + # Get the SVN Revision + try: + svn_revision = int(SVN_REVISION.replace('SVN-', '')) + except ValueError: + # Here we'll have to find another way of getting the SVN revision + svn_revision = 0 + + admin_emails_xml = '' + for email in get_admin_emails(): + admin_emails_xml += '' % email + admin_emails_xml += '' + + statistics = """ + + + + + + + + + + + %(emails)s + """ % { + 'site_key' : settings.SITE_KEY, + 'app_url' : APP_URL, + 'svn_revision' : svn_revision, + 'site_views' : get_site_views(), + 'server_name' : get_server_name(), + 'python_version' : ''.join(sys.version.splitlines()), + 'django_version' : str(DJANGO_VERSION), + 'database' : django_settings.DATABASE_ENGINE, + 'os' : str(os.uname()), + 'emails' : admin_emails_xml, + } + + # Compress the statistics XML dump + statistics_compressed = bz2.compress(statistics) + + # Pass the compressed statistics to the update server + post_data = { + 'statistics' : binascii.b2a_base64(statistics_compressed), + } + data = urllib.urlencode(post_data) + + # We simulate some browser, otherwise the server can return 403 response + 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' + headers={ 'User-Agent' : user_agent,} + + try: + check_request = urllib2.Request('%s%s' % (settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers) + check_response = urllib2.urlopen(check_request) + content = check_response.read() + except urllib2.HTTPError, error: + content = error.read() + + # 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() + + # Store the messages XML in a Setting object + settings.UPDATE_MESSAGES_XML.set_value(messages_xml) + + messages_dom = parseString(messages_xml) + messages_count = len(messages_dom.getElementsByTagName('message')) + + return _('%d update messages have been downloaded') % messages_count + +def update_trigger(): + # Trigger the update process + now = datetime.datetime.now() + if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1): + 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) diff --git a/forum_modules/updater/startup.py b/forum_modules/updater/startup.py index dec53e2..ca32986 100644 --- a/forum_modules/updater/startup.py +++ b/forum_modules/updater/startup.py @@ -4,21 +4,20 @@ import logging import settings from xml.dom.minidom import parse, parseString - from forum.modules import ui, decorate from forum.settings import SVN_REVISION from django.contrib.auth.middleware import AuthenticationMiddleware from django.core.exceptions import ObjectDoesNotExist from django.utils.encoding import smart_str -# Trigger the update process -now = datetime.datetime.now() -if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1): - pass +from base import update_trigger # Update the user messages @decorate.result(AuthenticationMiddleware.process_request, needs_params=True) def process_request(result, self, request): + # Call the update trigger on every request + update_trigger() + messages_dom = parseString(smart_str(settings.UPDATE_MESSAGES_XML.value)) messages = messages_dom.getElementsByTagName('message') @@ -46,4 +45,4 @@ def process_request(result, self, request): request.user.message_set.create(message=message_body) except: pass - return result + return result \ No newline at end of file diff --git a/forum_modules/updater/views.py b/forum_modules/updater/views.py index cc46914..c811364 100644 --- a/forum_modules/updater/views.py +++ b/forum_modules/updater/views.py @@ -1,20 +1,8 @@ -import os -import sys -import bz2 -import urllib2, urllib -import binascii - -from xml.dom.minidom import parse, parseString - -from django import VERSION as DJANGO_VERSION from django.http import HttpResponse from django.utils.translation import ugettext as _ -from django.utils import simplejson -from django.conf import settings -from base import get_site_views, get_server_name, get_admin_emails -import settings as updater_settings -from forum.settings import APP_URL, SVN_REVISION +from base import check_for_updates + from forum.views.admin import admin_tools_page, admin_page @admin_tools_page(_('updater'), _('Update Checker')) @@ -27,73 +15,6 @@ def updater_index(request): ) def updater_check(request): - # Get the SVN Revision - try: - svn_revision = int(SVN_REVISION.replace('SVN-', '')) - except ValueError: - # Here we'll have to find another way of getting the SVN revision - svn_revision = 0 - - admin_emails_xml = '' - for email in get_admin_emails(): - admin_emails_xml += '' % email - admin_emails_xml += '' - - statistics = """ - - - - - - - - - - - %(emails)s - """ % { - 'site_key' : updater_settings.SITE_KEY, - 'app_url' : APP_URL, - 'svn_revision' : svn_revision, - 'site_views' : get_site_views(), - 'server_name' : get_server_name(), - 'python_version' : ''.join(sys.version.splitlines()), - 'django_version' : str(DJANGO_VERSION), - 'database' : settings.DATABASE_ENGINE, - 'os' : str(os.uname()), - 'emails' : admin_emails_xml, - } - - # Compress the statistics XML dump - statistics_compressed = bz2.compress(statistics) - - # Pass the compressed statistics to the update server - post_data = { - 'statistics' : binascii.b2a_base64(statistics_compressed), - } - data = urllib.urlencode(post_data) - - # We simulate some browser, otherwise the server can return 403 response - 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' - headers={ 'User-Agent' : user_agent,} - - try: - check_request = urllib2.Request('%s%s' % (updater_settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers) - check_response = urllib2.urlopen(check_request) - content = check_response.read() - except urllib2.HTTPError, error: - content = error.read() - - # Read the messages from the Update Server - messages_xml_url = '%s%s' % (updater_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() - - # Store the messages XML in a Setting object - updater_settings.UPDATE_MESSAGES_XML.set_value(messages_xml) - - messages_dom = parseString(messages_xml) - messages_count = len(messages_dom.getElementsByTagName('message')) + update_status = check_for_updates() - return HttpResponse(_('%d update messages have been downloaded') % messages_count, mimetype='text/html') \ No newline at end of file + return HttpResponse(update_status, mimetype='text/html') -- 2.45.2