From daf0f1ccd75fa81c5e7065bc39df6a6ff792b02f Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Wed, 18 Jan 2012 19:26:08 -0300 Subject: removed potentially costly calls from non-askbot urls - works only when askbot is not deployed at root of the site --- askbot/context.py | 9 +++++++++ askbot/middleware/anon_user.py | 9 +++++++++ askbot/startup_procedures.py | 18 ++++++++++++++++++ askbot/tests/page_load_tests.py | 11 ++++++----- askbot/user_messages/context_processors.py | 5 +++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/askbot/context.py b/askbot/context.py index 41298fc1..ea10a890 100644 --- a/askbot/context.py +++ b/askbot/context.py @@ -13,6 +13,15 @@ from askbot.utils import url_utils def application_settings(request): """The context processor function""" + if not request.path.startswith('/' + settings.ASKBOT_URL): + #todo: this is a really ugly hack, will only work + #when askbot is installed not at the home page. + #this will not work for the + #heavy modders of askbot, because their custom pages + #will not receive the askbot settings in the context + #to solve this properly we should probably explicitly + #add settings to the context per page + return {} my_settings = askbot_settings.as_dict() my_settings['LANGUAGE_CODE'] = getattr(request, 'LANGUAGE_CODE', settings.LANGUAGE_CODE) my_settings['ASKBOT_URL'] = settings.ASKBOT_URL diff --git a/askbot/middleware/anon_user.py b/askbot/middleware/anon_user.py index 7cd9279d..956990e4 100644 --- a/askbot/middleware/anon_user.py +++ b/askbot/middleware/anon_user.py @@ -8,6 +8,7 @@ added to the :class:`AnonymousUser` so that user could be pickled. Secondly, it sends greeting message to anonymous users. """ +from django.conf import settings as django_settings from askbot.user_messages import create_message, get_and_delete_messages from askbot.conf import settings as askbot_settings @@ -42,6 +43,10 @@ class ConnectToSessionMessagesMiddleware(object): """Enables anonymous users to receive messages the same way as authenticated users, and sets the anonymous user greeting, if it should be shown""" + if not request.path.startswith('/' + django_settings.ASKBOT_URL): + #todo: a hack, for real we need to remove this middleware + #and switch to the new-style session messages + return if request.user.is_anonymous(): #1) Attach the ability to receive messages #plug on deepcopy which may be called by django db "driver" @@ -63,6 +68,10 @@ class ConnectToSessionMessagesMiddleware(object): """Adds the ``'askbot_visitor'``key to cookie if user ever authenticates so that the anonymous user message won't be shown after the user logs out""" + if not request.path.startswith('/' + django_settings.ASKBOT_URL): + #todo: a hack, for real we need to remove this middleware + #and switch to the new-style session messages + return response if hasattr(request, 'user') and \ request.user.is_authenticated() and \ 'askbot_visitor' not in request.COOKIES : diff --git a/askbot/startup_procedures.py b/askbot/startup_procedures.py index 253e8d97..d50873d9 100644 --- a/askbot/startup_procedures.py +++ b/askbot/startup_procedures.py @@ -438,6 +438,22 @@ def test_csrf_cookie_domain(): 'setting' ) +def test_settings_for_test_runner(): + """makes sure that debug toolbar is disabled when running tests""" + errors = list() + if 'debug_toolbar' in django_settings.INSTALLED_APPS: + errors.append( + 'When testing - remove debug_toolbar from INSTALLED_APPS' + ) + if 'debug_toolbar.middleware.DebugToolbarMiddleware' in \ + django_settings.MIDDLEWARE_CLASSES: + errors.append( + 'When testing - remove debug_toolbar.middleware.DebugToolbarMiddleware ' + 'from MIDDLEWARE_CLASSES' + ) + print_errors(errors) + + def run_startup_tests(): """function that runs all startup tests, mainly checking settings config so far @@ -482,6 +498,8 @@ def run_startup_tests(): }) settings_tester.run() test_media_url() + if 'manage.py test' in ' '.join(sys.argv): + test_settings_for_test_runner() @transaction.commit_manually def run(): diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index f423230b..d5fabac8 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -474,11 +474,12 @@ class PageLoadTestCase(AskbotTestCase): class AvatarTests(AskbotTestCase): def test_avatar_for_two_word_user_works(self): - self.user = self.create_user('john doe') - response = self.client.get( - 'avatar_render_primary', - kwargs = {'user': 'john doe', 'size': 48} - ) + if 'avatar' in settings.INSTALLED_APPS: + self.user = self.create_user('john doe') + response = self.client.get( + 'avatar_render_primary', + kwargs = {'user': 'john doe', 'size': 48} + ) class QuestionPageRedirectTests(AskbotTestCase): diff --git a/askbot/user_messages/context_processors.py b/askbot/user_messages/context_processors.py index f4c5e3ac..230e967c 100644 --- a/askbot/user_messages/context_processors.py +++ b/askbot/user_messages/context_processors.py @@ -4,6 +4,7 @@ Context processor for lightweight session messages. Time-stamp: <2008-07-19 23:16:19 carljm context_processors.py> """ +from django.conf import settings as django_settings from django.utils.encoding import StrAndUnicode from askbot.user_messages import get_and_delete_messages @@ -13,6 +14,10 @@ def user_messages (request): Returns session messages for the current session. """ + if not request.path.startswith('/' + django_settings.ASKBOT_URL): + #todo: a hack, for real we need to remove this middleware + #and switch to the new-style session messages + return {} messages = request.user.get_and_delete_messages() #if request.user.is_authenticated(): #else: -- cgit v1.2.3-1-g7c22