diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-08-15 04:47:04 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2013-08-15 04:47:04 -0400 |
commit | a9b85525c4af71154b47408425e7d78acab574e1 (patch) | |
tree | d21ea7fa301ccbd0b0eb80a09cd05a81718cb618 | |
parent | c6a8d0bfaf96c4275de553baafa883fabd0f7728 (diff) | |
download | askbot-a9b85525c4af71154b47408425e7d78acab574e1.tar.gz askbot-a9b85525c4af71154b47408425e7d78acab574e1.tar.bz2 askbot-a9b85525c4af71154b47408425e7d78acab574e1.zip |
allowed change of default answer sorting method
-rw-r--r-- | askbot/conf/forum_data_rules.py | 10 | ||||
-rw-r--r-- | askbot/const/__init__.py | 8 | ||||
-rw-r--r-- | askbot/forms.py | 6 | ||||
-rw-r--r-- | askbot/models/question.py | 31 | ||||
-rw-r--r-- | askbot/views/readers.py | 4 |
5 files changed, 41 insertions, 18 deletions
diff --git a/askbot/conf/forum_data_rules.py b/askbot/conf/forum_data_rules.py index f32f34bf..caa93563 100644 --- a/askbot/conf/forum_data_rules.py +++ b/askbot/conf/forum_data_rules.py @@ -225,6 +225,16 @@ settings.register( ) settings.register( + livesettings.StringValue( + FORUM_DATA_RULES, + 'DEFAULT_ANSWER_SORT_METHOD', + default=const.DEFAULT_ANSWER_SORT_METHOD, + choices=const.ANSWER_SORT_METHODS, + description=_('How to sort answers by default') + ) +) + +settings.register( livesettings.BooleanValue( FORUM_DATA_RULES, 'TAGS_ARE_REQUIRED', diff --git a/askbot/const/__init__.py b/askbot/const/__init__.py index 262a01c4..31a631d9 100644 --- a/askbot/const/__init__.py +++ b/askbot/const/__init__.py @@ -110,9 +110,13 @@ REPLY_WITH_COMMENT_TEMPLATE = _( ) REPLY_SEPARATOR_REGEX = re.compile(r'==== .* -=-==', re.MULTILINE|re.DOTALL) -ANSWER_SORT_METHODS = (#no translations needed here - 'latest', 'oldest', 'votes' +ANSWER_SORT_METHODS = ( + ('latest' , _('latest first')), + ('oldest', _('oldest first')), + ('votes', _('most voted first')), ) +DEFAULT_ANSWER_SORT_METHOD = 'votes' + #todo: add assertion here that all sort methods are unique #because they are keys to the hash used in implementations #of Q.run_advanced_search diff --git a/askbot/forms.py b/askbot/forms.py index aec40d97..ce27ebc8 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -557,14 +557,16 @@ class ShowQuestionForm(forms.Form): in_data = self.get_pruned_data() out_data = dict() + default_answer_sort = askbot_settings.DEFAULT_ANSWER_SORT_METHOD if ('answer' in in_data) ^ ('comment' in in_data): out_data['show_page'] = None - out_data['answer_sort_method'] = 'votes' + out_data['answer_sort_method'] = default_answer_sort out_data['show_comment'] = in_data.get('comment', None) out_data['show_answer'] = in_data.get('answer', None) else: out_data['show_page'] = in_data.get('page', 1) - out_data['answer_sort_method'] = in_data.get('sort', 'votes') + answer_sort_method = in_data.get('sort', default_answer_sort) + out_data['answer_sort_method'] = answer_sort_method out_data['show_comment'] = None out_data['show_answer'] = None self.cleaned_data = out_data diff --git a/askbot/models/question.py b/askbot/models/question.py index 8637ea23..87b01f3c 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -915,7 +915,8 @@ class Thread(models.Model): changes in the post data - on votes, adding, deleting, editing content""" #we can call delete_many() here if using Django > 1.2 - for sort_method in const.ANSWER_SORT_METHODS: + sort_methods = map(lambda v: v[0], const.ANSWER_SORT_METHODS) + for sort_method in sort_methods: cache.cache.delete(self.get_post_data_cache_key(sort_method)) def invalidate_cached_data(self, lazy=False): @@ -925,9 +926,11 @@ class Thread(models.Model): else: self.update_summary_html() - def get_cached_post_data(self, user = None, sort_method = 'votes'): + def get_cached_post_data(self, user = None, sort_method = None): """returns cached post data, as calculated by the method get_post_data()""" + sort_method = sort_method or askbot_settings.DEFAULT_ANSWER_SORT_METHOD + if askbot_settings.GROUPS_ENABLED: #temporary plug: bypass cache where groups are enabled return self.get_post_data(sort_method=sort_method, user=user) @@ -938,7 +941,7 @@ class Thread(models.Model): cache.cache.set(key, post_data, const.LONG_TIME) return post_data - def get_post_data(self, sort_method='votes', user=None): + def get_post_data(self, sort_method=None, user=None): """returns question, answers as list and a list of post ids for the given thread, and the list of published post ids (four values) @@ -946,6 +949,8 @@ class Thread(models.Model): all (both posts and the comments sorted in the correct order) """ + sort_method = sort_method or askbot_settings.DEFAULT_ANSWER_SORT_METHOD + thread_posts = self.posts.all() if askbot_settings.GROUPS_ENABLED: if user is None or user.is_anonymous(): @@ -961,12 +966,18 @@ class Thread(models.Model): 'oldest':'added_at', 'votes':'-points' } - if sort_method in order_by_method: - order_by = order_by_method[sort_method] + + default_answer_sort_method = askbot_settings.DEFAULT_ANSWER_SORT_METHOD + default_order_by_method = order_by_method[default_answer_sort_method] + order_by = order_by_method.get(sort_method, default_order_by_method) + #we add secondary sort method for the answers to make + #discussion more coherent + if order_by != default_order_by_method: + order_by = (order_by, default_order_by_method) else: - order_by = order_by_method['latest'] + order_by = (order_by,) - thread_posts = thread_posts.order_by(order_by) + thread_posts = thread_posts.order_by(*order_by) #1) collect question, answer and comment posts and list of post id's answers = list() post_map = dict() @@ -1026,11 +1037,7 @@ class Thread(models.Model): ).filter( deleted=False ).order_by( - { - 'latest':'-added_at', - 'oldest':'added_at', - 'votes':'-points' - }[sort_method] + *order_by ).values_list('id', flat=True) published_answer_ids = reversed(published_answer_ids) diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 158c84ac..02bd8ce0 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -464,8 +464,8 @@ def question(request, id):#refactor - long subroutine. display question body, an #load answers and post id's->athor_id mapping #posts are pre-stuffed with the correctly ordered comments updated_question_post, answers, post_to_author, published_answer_ids = thread.get_cached_post_data( - sort_method = answer_sort_method, - user = request.user + sort_method=answer_sort_method, + user=request.user ) question_post.set_cached_comments( updated_question_post.get_cached_comments() |