1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
"""tests to be performed
in the beginning of models/__init__.py
the purpose of this module is to validate deployment of askbot
question: why not run these from askbot/__init__.py?
the main function is run_startup_tests
"""
from django.db import transaction
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from askbot.models import badges
#todo:
#
# *validate emails in settings.py
def test_askbot_url():
"""Tests the ASKBOT_URL setting for the
well-formedness and raises the ImproperlyConfigured
exception, if the setting is not good.
"""
url = settings.ASKBOT_URL
if url != '':
if isinstance(url, str) or isinstance(url, unicode):
pass
else:
msg = 'setting ASKBOT_URL must be of string or unicode type'
raise ImproperlyConfigured(msg)
if url == '/':
msg = 'value "/" for ASKBOT_URL is invalid. '+ \
'Please, either make ASKBOT_URL an empty string ' + \
'or a non-empty path, ending with "/" but not ' + \
'starting with "/", for example: "forum/"'
raise ImproperlyConfigured(msg)
else:
try:
assert(url.endswith('/'))
except AssertionError:
msg = 'if ASKBOT_URL setting is not empty, ' + \
'it must end with /'
raise ImproperlyConfigured(msg)
try:
assert(not url.startswith('/'))
except AssertionError:
msg = 'if ASKBOT_URL setting is not empty, ' + \
'it must not start with /'
def test_middleware():
"""Checks that all required middleware classes are
installed in the django settings.py file. If that is not the
case - raises an ImproperlyConfigured exception.
"""
required_middleware = (
'askbot.middleware.anon_user.ConnectToSessionMessagesMiddleware',
'askbot.middleware.pagesize.QuestionsPageSizeMiddleware',
'askbot.middleware.cancel.CancelActionMiddleware',
'askbot.deps.recaptcha_django.middleware.ReCaptchaMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'askbot.middleware.view_log.ViewLogMiddleware',
)
#'debug_toolbar.middleware.DebugToolbarMiddleware',
missing_middleware = list()
for middleware in required_middleware:
if middleware not in settings.MIDDLEWARE_CLASSES:
missing_middleware.append(middleware)
debug_toolbar_middleware = 'debug_toolbar.middleware.DebugToolbarMiddleware'
if 'debug_toolbar' in settings.INSTALLED_APPS:
if debug_toolbar_middleware not in settings.MIDDLEWARE_CLASSES:
missing_middleware.append(debug_toolbar_middleware)
if missing_middleware:
error_message = """Please add the following middleware (listed
after this message) to the MIDDLEWARE_CLASSES variable in your site settings.py file.
The order the middleware records may be important, please take a look at the example in
https://github.com/ASKBOT/askbot-devel/blob/master/askbot/setup_templates/settings.py\n\n%s""" \
% ', '.join(missing_middleware)
raise ImproperlyConfigured(error_message)
def run_startup_tests():
"""function that runs
all startup tests, mainly checking settings config so far
"""
#todo: refactor this when another test arrives
test_askbot_url()
test_middleware()
@transaction.commit_manually
def run():
"""runs all the startup procedures"""
run_startup_tests()
try:
badges.init_badges()
transaction.commit()
except:
transaction.rollback()
|