From 823a4dcbdbc04ed9cc21b4adf5e75b195f3196bc Mon Sep 17 00:00:00 2001 From: jordan Date: Sat, 23 Apr 2011 22:47:46 +0000 Subject: [PATCH] display the XML messages set to the site administrators git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1000 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/settings/__init__.py | 5 ++++ forum_modules/updater/settings.py | 8 +++++- forum_modules/updater/startup.py | 48 +++++++++++++++++++++++++++++++ forum_modules/updater/views.py | 11 ++++--- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/forum/settings/__init__.py b/forum/settings/__init__.py index d8d5aa0..4596605 100644 --- a/forum/settings/__init__.py +++ b/forum/settings/__init__.py @@ -7,8 +7,13 @@ from django.conf import settings as djsettings from django.utils.version import get_svn_revision OSQA_VERSION = "Development Build" + SVN_REVISION = get_svn_revision(djsettings.SITE_SRC_ROOT) +# We'll have to keep in mind this variable on every release. +if SVN_REVISION == u'SVN-unknown': + SVN_REVISION = u'SVN-1000' + MAINTAINANCE_MODE = Setting('MAINTAINANCE_MODE', None) SETTINGS_PACK = Setting('SETTINGS_PACK', "default") diff --git a/forum_modules/updater/settings.py b/forum_modules/updater/settings.py index 3faa290..7c3a8c3 100644 --- a/forum_modules/updater/settings.py +++ b/forum_modules/updater/settings.py @@ -1,3 +1,5 @@ +import datetime + from forum.settings.base import Setting, SettingSet from django.utils.translation import ugettext_lazy as _ @@ -14,5 +16,9 @@ required=False)) SITE_KEY = Setting('SITE_KEY', generate_installation_key()) +UPDATE_MESSAGES_XML = Setting('UPDATE_MESSAGES_XML', '') + +LATEST_UPDATE_DATETIME = Setting('LATEST_UPDATES_DATETIME', datetime.datetime.now()) + # Update server. Do not edit. -UPDATE_SERVER_URL = 'http://192.168.0.102:8008' \ No newline at end of file +UPDATE_SERVER_URL = 'http://192.168.0.102:8008' diff --git a/forum_modules/updater/startup.py b/forum_modules/updater/startup.py index 4164248..dec53e2 100644 --- a/forum_modules/updater/startup.py +++ b/forum_modules/updater/startup.py @@ -1 +1,49 @@ +import datetime import views +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 + +# Update the user messages +@decorate.result(AuthenticationMiddleware.process_request, needs_params=True) +def process_request(result, self, request): + messages_dom = parseString(smart_str(settings.UPDATE_MESSAGES_XML.value)) + messages = messages_dom.getElementsByTagName('message') + + for message in messages: + # 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 + + message_body = message.getElementsByTagName('body')[0].firstChild.nodeValue + message_revision = int(message.getElementsByTagName('revision')[0].firstChild.nodeValue) + + # Add the message to the user messages set only if the Message Revision number is greater than the + # current installation SVN Revision number and only if the current user is a super user. + if message_revision >= svn_revision and request.user.is_superuser: + # We do not want to repeat ourselves. If the message already exists in the message list, we're not going to + # add it. That's why first of all we're going the check if it is there. + try: + # If the message doesn't exist in the RelatedManager ObjectsDoesNotExist is going to be raised. + request.user.message_set.all().get(message=message_body) + except ObjectDoesNotExist: + # Let's create the message. + request.user.message_set.create(message=message_body) + except: + pass + return result diff --git a/forum_modules/updater/views.py b/forum_modules/updater/views.py index 083819f..cc46914 100644 --- a/forum_modules/updater/views.py +++ b/forum_modules/updater/views.py @@ -13,7 +13,7 @@ from django.utils import simplejson from django.conf import settings from base import get_site_views, get_server_name, get_admin_emails -from settings import SITE_KEY, UPDATE_SERVER_URL +import settings as updater_settings from forum.settings import APP_URL, SVN_REVISION from forum.views.admin import admin_tools_page, admin_page @@ -52,7 +52,7 @@ def updater_check(request): %(emails)s """ % { - 'site_key' : SITE_KEY, + 'site_key' : updater_settings.SITE_KEY, 'app_url' : APP_URL, 'svn_revision' : svn_revision, 'site_views' : get_site_views(), @@ -78,18 +78,21 @@ def updater_check(request): headers={ 'User-Agent' : user_agent,} try: - check_request = urllib2.Request('%s%s' % (UPDATE_SERVER_URL, '/site_check/'), data, headers=headers) + 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' % (UPDATE_SERVER_URL, '/messages/xml/') + 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')) -- 2.39.5