From 61964ded64a8184b7639bf3b9a804061013f56b9 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 14 Jan 2011 22:10:35 -0500 Subject: small refactoring - moved ViewLog class definition to the search subsystem, because it is part of the search functionality --- askbot/middleware/view_log.py | 35 ++--------------------------------- askbot/models/__init__.py | 2 -- askbot/search/indexer.py | 9 --------- askbot/search/state_manager.py | 41 +++++++++++++++++++++++++++++++++++++++++ askbot/views/readers.py | 6 ++---- 5 files changed, 45 insertions(+), 48 deletions(-) delete mode 100644 askbot/search/indexer.py diff --git a/askbot/middleware/view_log.py b/askbot/middleware/view_log.py index f6077e12..58815f89 100644 --- a/askbot/middleware/view_log.py +++ b/askbot/middleware/view_log.py @@ -1,7 +1,7 @@ """This module records the site visits by the authenticaded users and heps maintain the state of the search (for all visitors). -Included here is the ViewLogMiddleware and the helper class ViewLog. +Included here is the ViewLogMiddleware """ import logging import datetime @@ -12,44 +12,13 @@ from askbot.views.readers import questions as questions_view from askbot.views.commands import vote from askbot.views.writers import delete_comment, post_comments, retag_question from askbot.views.readers import revisions +from askbot.search.state_manager import ViewLog #todo: the list is getting bigger and bigger - maybe there is a better way to #trigger reset of sarch state? IGNORED_VIEWS = (serve, vote, delete_comment, post_comments, retag_question, revisions) -class ViewLog(object): - """The ViewLog helper obejcts store the trail of the page visits for a - given user. The trail is recorded only up to a certain depth. - - The purpose to record this info is to reset the search state - when the user walks "too far away" from the search page. - - These objects must be modified only in this middlware. - """ - def __init__(self): - self.views = [] - self.depth = 3 #todo maybe move this to const.py - - def get_previous(self, num): - """get a previous record from a certain depth""" - 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): - """insert a new record""" - 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): """ViewLogMiddleware does two things: tracks visits of pages for the diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 1bc42b89..7d8347f0 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -3,7 +3,6 @@ import re import hashlib import datetime from django.core.urlresolvers import reverse -from askbot.search.indexer import create_fulltext_indexes from django.db.models import signals as django_signals from django.template import Context from django.utils.translation import ugettext as _ @@ -2093,7 +2092,6 @@ signals.post_updated.connect( sender=Question ) signals.site_visited.connect(record_user_visit) -#post_syncdb.connect(create_fulltext_indexes) #todo: wtf??? what is x=x about? signals = signals diff --git a/askbot/search/indexer.py b/askbot/search/indexer.py deleted file mode 100644 index c7c45c59..00000000 --- a/askbot/search/indexer.py +++ /dev/null @@ -1,9 +0,0 @@ -from django.conf import settings -from django.db import connection - -def create_fulltext_indexes(): - if settings.DATABASE_ENGINE == 'mysql': - cursor = connection.cursor() - cursor.execute('ALTER TABLE question ADD FULLTEXT (title, text, tagnames)') - cursor.execute('ALTER TABLE answer ADD FULLTEXT (title, text, tagnames)') - diff --git a/askbot/search/state_manager.py b/askbot/search/state_manager.py index b50a0405..0fcb0031 100644 --- a/askbot/search/state_manager.py +++ b/askbot/search/state_manager.py @@ -172,3 +172,44 @@ class SearchState(object): def reset_scope(self): self.scope = const.DEFAULT_POST_SCOPE + +class ViewLog(object): + """The ViewLog helper obejcts store the trail of the page visits for a + given user. The trail is recorded only up to a certain depth. + + The purpose to record this info is to reset the search state + when the user walks "too far away" from the search page. + + These objects must be modified only in this middlware. + """ + def __init__(self): + self.views = [] + self.depth = 3 #todo maybe move this to const.py + + def get_previous(self, num): + """get a previous record from a certain depth""" + 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 should_reset_search_state(self): + """return True if user stepped too far from the home page + and False otherwise""" + if self.get_previous(1) != 'questions': + if self.get_previous(2) != 'questions': + return True + return False + + def set_current(self, view_name): + """insert a new record""" + 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 diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 4c462081..759c1ddc 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -86,10 +86,8 @@ def questions(request): view_log = request.session['view_log'] - if view_log.get_previous(1) != 'questions': - if view_log.get_previous(2) != 'questions': - #print 'user stepped too far, resetting search state' - search_state.reset() + if view_log.should_reset_search_state(): + search_state.reset() if request.user.is_authenticated(): search_state.set_logged_in() -- cgit v1.2.3-1-g7c22