]> git.openstreetmap.org Git - osqa.git/blob - forum/middleware/admin_messages.py
merge notification-config -> trunk, previous commits 695, 751 (email debugging inform...
[osqa.git] / forum / middleware / admin_messages.py
1 from forum.user_messages import create_message
2 from django.utils.translation import ugettext as _
3 from django.core.urlresolvers import reverse
4 from django.core.exceptions import ObjectDoesNotExist
5
6 from forum.settings import EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, \
7         APP_URL
8
9 class AdminMessagesMiddleware(object):
10     def process_request(self, request):
11         # Check if the email settings are configured
12         self.check_email_settings(request)
13
14         # Check if the APP_URL setting is configured
15         self.check_app_url(request)
16
17     def check_email_settings(self, request):
18         # We want to warn only the administrators that the email settings are not configured.
19         # So, first of all we check if we're dealing with the administrators and after that if
20         # the SMTP settings are configured at all. We suppose that the SMTP settings are not configured
21         # if the EMAIL_HOST, the EMAIL_HOST_USER and the EMAIL_HOST_PASSWORD are not set at all.
22         if request.user.is_authenticated and request.user.is_staff and request.user.is_superuser and \
23             EMAIL_HOST == '' and EMAIL_HOST_USER == '' and EMAIL_HOST_PASSWORD == '':
24
25             msg = _("""
26                     The e-mail settings of this community are not configured yet. We strongly recommend you to
27                     do that from the <a href="%(email_settings_url)s">e-mail settings page</a> as soon as possible.
28                     """ % dict(email_settings_url=reverse('admin_set', kwargs={'set_name':'email'})))
29
30             # We do not want to repeat ourselves. If the message already exists in the message list, we're not going to
31             # add it. That's why first of all we're going the check if it is there.
32             try:
33                 # If the message doesn't exist in the RelatedManager ObjectsDoesNotExist is going to be raised.
34                 request.user.message_set.all().get(message=msg)
35             except ObjectDoesNotExist:
36                 # Let's create the message.
37                 request.user.message_set.create(message=msg)
38             except:
39                 pass
40
41     def check_app_url(self, request):
42         # We consider the APP_URL setting not configured if it contains only the protocol
43         # name or if it's shorter than 7 characters.
44         if request.user.is_authenticated and request.user.is_staff and request.user.is_superuser and \
45             APP_URL == 'http://' or APP_URL == 'https://' or len(APP_URL) < 7:
46
47             msg = _("""
48                        Please, configure your APP_URL setting from the local settings file.
49                     """)
50
51             # We do not want to repeat ourselves. If the message already exists in the message list, we're not going to
52             # add it. That's why first of all we're going the check if it is there.
53             try:
54                 # If the message doesn't exist in the RelatedManager ObjectsDoesNotExist is going to be raised.
55                 request.user.message_set.all().get(message=msg)
56             except ObjectDoesNotExist:
57                 # Let's create the message.
58                 request.user.message_set.create(message=msg)
59             except:
60                 pass