diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-04-08 01:48:56 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-04-08 01:48:56 -0400 |
commit | 72c356b993771063e080c7cb47b8fc23a964dc1a (patch) | |
tree | 6047f195f5112f54a2a61c6b563a9e7d68012a5a /forum/middleware/view_log.py | |
parent | b94fddfb8495960569f6a6c64196a11d76b95fd4 (diff) | |
download | askbot-72c356b993771063e080c7cb47b8fc23a964dc1a.tar.gz askbot-72c356b993771063e080c7cb47b8fc23a964dc1a.tar.bz2 askbot-72c356b993771063e080c7cb47b8fc23a964dc1a.zip |
kind of merged question views, still have debugging to to
Diffstat (limited to 'forum/middleware/view_log.py')
-rw-r--r-- | forum/middleware/view_log.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/forum/middleware/view_log.py b/forum/middleware/view_log.py new file mode 100644 index 00000000..6f59568a --- /dev/null +++ b/forum/middleware/view_log.py @@ -0,0 +1,63 @@ +import logging +from django.conf import settings +from forum.views.readers import questions as questions_view +from forum.views.commands import vote as vote_view +from django.views.static import serve as django_serve_view + +class ViewLog(object): + """must be modified only in this middlware + however, can be read anywhere else + """ + def __init__(self): + self.views = [] + self.depth = 3 #todo maybe move this to const.py + def set_current(self, view_name): + thi + + def get_previous(self, num): + if num > self.depth - 1: + raise Exception("view log depth exceeded") + elif num < 0: + raise Exception("num must be positive"); + elif num <= len(self.views) - 1: + return self.views[num] + else: + return None + + def set_current(self, view_name): + self.views.insert(0, view_name) + if len(self.views) > self.depth: + self.views.pop() + + def __str__(self): + return str(self.views) + ' depth=%d' % self.depth + +class ViewLogMiddleware(object): + def process_view(self, request, view_func, view_args, view_kwargs): + if view_func == questions_view: + view_str = 'questions' + elif view_func in (django_serve_view, vote_view): + return + elif settings.DEBUG == True: + #todo: dependency! + from debug_toolbar.views import debug_media_view + if view_func == debug_media_view: + return + else: + view_str = view_func.__name__ + else: + view_str = view_func.__name__ + + if request.user.is_authenticated(): + user_name = request.user.username + else: + user_name = request.META['REMOTE_ADDR'] + logging.debug('user %s, view %s' % (request.user.username, view_str)) + + if 'view_log' in request.session: + view_log = request.session['view_log'] + else: + view_log = ViewLog() + + view_log.set_current(view_str) + request.session['view_log'] = view_log |