1 from django.utils.translation import ugettext as _
2 from django.core.urlresolvers import reverse
3 from django.core.exceptions import ObjectDoesNotExist
4 from django.contrib import messages
6 from forum.settings import EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, \
9 class AdminMessagesMiddleware(object):
10 def process_request(self, request):
11 # Check if the email settings are configured
12 self.check_email_settings(request)
14 # Check if the APP_URL setting is configured
15 self.check_app_url(request)
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 == '':
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'})))
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 if msg not in [m.message for m in messages.api.get_messages(request)]:
33 messages.info(request, msg)
35 def check_app_url(self, request):
36 # We consider the APP_URL setting not configured if it contains only the protocol
37 # name or if it's shorter than 7 characters.
38 if request.user.is_authenticated and request.user.is_staff and request.user.is_superuser and \
39 APP_URL == 'http://' or APP_URL == 'https://' or len(APP_URL) < 7:
42 Please, configure your APP_URL setting from the local settings file.
45 # We do not want to repeat ourselves. If the message already exists in the message list, we're not going to
46 # add it. That's why first of all we're going the check if it is there.
47 if msg not in [m.message for m in messages.api.get_messages(request)]:
48 messages.info(request, msg)