]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/updates/startup.py
f49d10697696c91c059b23d14572d247411b186b
[osqa.git] / forum_modules / updates / startup.py
1 import datetime
2 import views
3 import logging
4 import settings
5
6 from xml.dom.minidom import parse, parseString
7 from xml.parsers.expat import ExpatError
8 from forum.modules import ui, decorate
9 from forum.settings import SVN_REVISION
10 from django.contrib.auth.middleware import AuthenticationMiddleware
11 from django.core.exceptions import ObjectDoesNotExist
12 from django.utils.encoding import smart_str
13
14 from base import update_trigger
15
16 # Update the user messages
17 @decorate.result(AuthenticationMiddleware.process_request, needs_params=True)
18 def process_request(result, self, request):
19     # Call the update trigger on every request
20     update_trigger()
21
22     try:
23         messages_dom = parseString(smart_str(settings.UPDATE_MESSAGES_XML.value))
24         messages = messages_dom.getElementsByTagName('message')
25
26         for message in messages:
27             # Get the SVN Revision
28             try:
29                 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
30             except ValueError:
31                 # Here we'll have to find another way of getting the SVN revision
32                 svn_revision = 0
33
34             message_body = message.getElementsByTagName('body')[0].firstChild.nodeValue
35             message_revision = int(message.getElementsByTagName('revision')[0].firstChild.nodeValue)
36
37             # Add the message to the user messages set only if the Message Revision number is greater than the
38             # current installation SVN Revision number and only if the current user is a super user.
39             if message_revision >= svn_revision and request.user.is_superuser:
40                 # We do not want to repeat ourselves. If the message already exists in the message list, we're not going to
41                 # add it. That's why first of all we're going the check if it is there.
42                 try:
43                     # If the message doesn't exist in the RelatedManager ObjectsDoesNotExist is going to be raised.
44                     request.user.message_set.all().get(message=message_body)
45                 except ObjectDoesNotExist:
46                     # Let's create the message.
47                     request.user.message_set.create(message=message_body)
48                 except:
49                     pass
50     except ExpatError:
51         pass
52
53     return result