diff options
Diffstat (limited to 'forum/views.py')
-rw-r--r-- | forum/views.py | 76 |
1 files changed, 53 insertions, 23 deletions
diff --git a/forum/views.py b/forum/views.py index 296745f9..ade6aaa7 100644 --- a/forum/views.py +++ b/forum/views.py @@ -33,7 +33,7 @@ from forum.auth import * from forum.const import * from forum.user import * from forum import auth -from django_authopenid.util import get_next_url +from utils.forms import get_next_url # used in index page INDEX_PAGE_SIZE = 20 @@ -202,7 +202,7 @@ def questions(request, tagname=None, unanswered=False): 'SELECT COUNT(1) FROM forum_markedtag, question_tags ' + 'WHERE forum_markedtag.user_id = %s ' + 'AND forum_markedtag.tag_id = question_tags.tag_id ' - + 'AND forum_markedtag.reason = "good" ' + + 'AND forum_markedtag.reason = \'good\' ' + 'AND question_tags.question_id = question.id' ), ]), @@ -220,7 +220,7 @@ def questions(request, tagname=None, unanswered=False): 'SELECT COUNT(1) FROM forum_markedtag, question_tags ' + 'WHERE forum_markedtag.user_id = %s ' + 'AND forum_markedtag.tag_id = question_tags.tag_id ' - + 'AND forum_markedtag.reason = "bad" ' + + 'AND forum_markedtag.reason = \'bad\' ' + 'AND question_tags.question_id = question.id' ) ]), @@ -436,6 +436,21 @@ def question(request, id): logging.debug('view_id=' + str(view_id)) question = get_object_or_404(Question, id=id) + try: + pattern = r'/%s%s%d/([\w-]+)' % (settings.FORUM_SCRIPT_ALIAS,_('question/'), question.id) + path_re = re.compile(pattern) + logging.debug(pattern) + logging.debug(request.path) + m = path_re.match(request.path) + if m: + slug = m.group(1) + logging.debug('have slug %s' % slug) + assert(slug == slugify(question.title)) + else: + logging.debug('no match!') + except: + return HttpResponseRedirect(question.get_absolute_url()) + if question.deleted and not can_view_deleted_post(request.user, question): raise Http404 answer_form = AnswerForm(question,request.user) @@ -1236,7 +1251,7 @@ def edit_user(request, id): from django_authopenid.views import set_new_email set_new_email(user, new_email) - user.username = sanitize_html(form.cleaned_data['username']) + #user.username = sanitize_html(form.cleaned_data['username']) user.real_name = sanitize_html(form.cleaned_data['realname']) user.website = sanitize_html(form.cleaned_data['website']) user.location = sanitize_html(form.cleaned_data['city']) @@ -1273,7 +1288,7 @@ def user_stats(request, user_id, user_view): }, select_params=[user_id], tables=['question', 'auth_user'], - where=['question.deleted = 0 AND question.author_id=%s AND question.last_activity_by_id = auth_user.id'], + where=['question.deleted=False AND question.author_id=%s AND question.last_activity_by_id = auth_user.id'], params=[user_id], order_by=['-vote_count', '-last_activity_at'] ).values('vote_count', @@ -1309,7 +1324,7 @@ def user_stats(request, user_id, user_view): 'comment_count' : 'answer.comment_count' }, tables=['question', 'answer'], - where=['answer.deleted=0 AND question.deleted=0 AND answer.author_id=%s AND answer.question_id=question.id'], + where=['answer.deleted=False AND question.deleted=False AND answer.author_id=%s AND answer.question_id=question.id'], params=[user_id], order_by=['-vote_count', '-answer_id'], select_params=[user_id] @@ -1431,7 +1446,7 @@ def user_recent(request, user_id, user_view): }, tables=['activity', 'question'], where=['activity.content_type_id = %s AND activity.object_id = ' + - 'question.id AND question.deleted=0 AND activity.user_id = %s AND activity.activity_type = %s'], + 'question.id AND question.deleted=False AND activity.user_id = %s AND activity.activity_type = %s'], params=[question_type_id, user_id, TYPE_ACTIVITY_ASK_QUESTION], order_by=['-activity.active_at'] ).values( @@ -1456,8 +1471,8 @@ def user_recent(request, user_id, user_view): }, tables=['activity', 'answer', 'question'], where=['activity.content_type_id = %s AND activity.object_id = answer.id AND ' + - 'answer.question_id=question.id AND answer.deleted=0 AND activity.user_id=%s AND '+ - 'activity.activity_type=%s AND question.deleted=0'], + 'answer.question_id=question.id AND answer.deleted=False AND activity.user_id=%s AND '+ + 'activity.activity_type=%s AND question.deleted=False'], params=[answer_type_id, user_id, TYPE_ACTIVITY_ANSWER], order_by=['-activity.active_at'] ).values( @@ -1485,7 +1500,7 @@ def user_recent(request, user_id, user_view): where=['activity.content_type_id = %s AND activity.object_id = comment.id AND '+ 'activity.user_id = comment.user_id AND comment.object_id=question.id AND '+ 'comment.content_type_id=%s AND activity.user_id = %s AND activity.activity_type=%s AND ' + - 'question.deleted=0'], + 'question.deleted=False'], params=[comment_type_id, question_type_id, user_id, TYPE_ACTIVITY_COMMENT_QUESTION], order_by=['-comment.added_at'] ).values( @@ -1515,7 +1530,7 @@ def user_recent(request, user_id, user_view): 'activity.user_id = comment.user_id AND comment.object_id=answer.id AND '+ 'comment.content_type_id=%s AND question.id = answer.question_id AND '+ 'activity.user_id = %s AND activity.activity_type=%s AND '+ - 'answer.deleted=0 AND question.deleted=0'], + 'answer.deleted=False AND question.deleted=False'], params=[comment_type_id, answer_type_id, user_id, TYPE_ACTIVITY_COMMENT_ANSWER], order_by=['-comment.added_at'] ).values( @@ -1542,7 +1557,7 @@ def user_recent(request, user_id, user_view): }, tables=['activity', 'question_revision', 'question'], where=['activity.content_type_id = %s AND activity.object_id = question_revision.id AND '+ - 'question_revision.id=question.id AND question.deleted=0 AND '+ + 'question_revision.id=question.id AND question.deleted=False AND '+ 'activity.user_id = question_revision.author_id AND activity.user_id = %s AND '+ 'activity.activity_type=%s'], params=[question_revision_type_id, user_id, TYPE_ACTIVITY_UPDATE_QUESTION], @@ -1575,7 +1590,7 @@ def user_recent(request, user_id, user_view): where=['activity.content_type_id = %s AND activity.object_id = answer_revision.id AND '+ 'activity.user_id = answer_revision.author_id AND activity.user_id = %s AND '+ 'answer_revision.answer_id=answer.id AND answer.question_id = question.id AND '+ - 'question.deleted=0 AND answer.deleted=0 AND '+ + 'question.deleted=False AND answer.deleted=False AND '+ 'activity.activity_type=%s'], params=[answer_revision_type_id, user_id, TYPE_ACTIVITY_UPDATE_ANSWER], order_by=['-activity.active_at'] @@ -1604,7 +1619,7 @@ def user_recent(request, user_id, user_view): tables=['activity', 'answer', 'question'], where=['activity.content_type_id = %s AND activity.object_id = answer.id AND '+ 'activity.user_id = question.author_id AND activity.user_id = %s AND '+ - 'answer.deleted=0 AND question.deleted=0 AND '+ + 'answer.deleted=False AND question.deleted=False AND '+ 'answer.question_id=question.id AND activity.activity_type=%s'], params=[answer_type_id, user_id, TYPE_ACTIVITY_MARK_ANSWER], order_by=['-activity.active_at'] @@ -1680,7 +1695,7 @@ def user_responses(request, user_id, user_view): }, select_params=[user_id], tables=['answer', 'question', 'auth_user'], - where=['answer.question_id = question.id AND answer.deleted=0 AND question.deleted = 0 AND '+ + where=['answer.question_id = question.id AND answer.deleted=False AND question.deleted=False AND '+ 'question.author_id = %s AND answer.author_id <> %s AND answer.author_id=auth_user.id'], params=[user_id, user_id], order_by=['-answer.id'] @@ -1711,7 +1726,7 @@ def user_responses(request, user_id, user_view): 'user_id' : 'auth_user.id' }, tables=['question', 'auth_user', 'comment'], - where=['question.deleted = 0 AND question.author_id = %s AND comment.object_id=question.id AND '+ + where=['question.deleted=False AND question.author_id = %s AND comment.object_id=question.id AND '+ 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id'], params=[user_id, question_type_id, user_id], order_by=['-comment.added_at'] @@ -1741,7 +1756,7 @@ def user_responses(request, user_id, user_view): 'user_id' : 'auth_user.id' }, tables=['answer', 'auth_user', 'comment', 'question'], - where=['answer.deleted = 0 AND answer.author_id = %s AND comment.object_id=answer.id AND '+ + where=['answer.deleted=False AND answer.author_id = %s AND comment.object_id=answer.id AND '+ 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id '+ 'AND question.id = answer.question_id'], params=[user_id, answer_type_id, user_id], @@ -1774,8 +1789,8 @@ def user_responses(request, user_id, user_view): }, select_params=[user_id], tables=['answer', 'question', 'auth_user'], - where=['answer.question_id = question.id AND answer.deleted=0 AND question.deleted = 0 AND '+ - 'answer.author_id = %s AND answer.accepted=1 AND question.author_id=auth_user.id'], + where=['answer.question_id = question.id AND answer.deleted=False AND question.deleted=False AND '+ + 'answer.author_id = %s AND answer.accepted=True AND question.author_id=auth_user.id'], params=[user_id], order_by=['-answer.id'] ).values( @@ -1922,7 +1937,7 @@ def user_favorites(request, user_id, user_view): }, select_params=[user_id], tables=['question', 'auth_user', 'favorite_question'], - where=['question.deleted = 0 AND question.last_activity_by_id = auth_user.id '+ + where=['question.deleted=True AND question.last_activity_by_id = auth_user.id '+ 'AND favorite_question.question_id = question.id AND favorite_question.user_id = %s'], params=[user_id], order_by=['-vote_count', '-question.id'] @@ -2093,7 +2108,7 @@ def badge(request, id): tables=['award', 'auth_user'], where=['badge_id=%s AND user_id=auth_user.id'], params=[id] - ).values('id').distinct() + ).distinct('id') return render_to_response('badge.html', { 'awards' : awards, @@ -2337,8 +2352,18 @@ def search(request): except KeyError: view_id = "latest" orderby = "-added_at" - - if settings.USE_SPHINX_SEARCH == True: + + if settings.USE_PG_FTS: + objects = Question.objects.filter(deleted=False).extra( + select={ + 'ranking': "ts_rank_cd(tsv, plainto_tsquery(%s), 32)", + }, + where=["tsv @@ plainto_tsquery(%s)"], + params=[keywords], + select_params=[keywords] + ).order_by('-ranking') + + elif settings.USE_SPHINX_SEARCH == True: #search index is now free of delete questions and answers #so there is not "antideleted" filtering here objects = Question.search.query(keywords) @@ -2359,6 +2384,9 @@ def search(request): if tag not in related_tags: related_tags.append(tag) + #if is_search is true in the context, prepend this string to soting tabs urls + search_uri = "?q=%s&page=%d&t=question" % ("+".join(keywords.split()), page) + return render_to_response(template_file, { "questions" : questions, "tab_id" : view_id, @@ -2368,6 +2396,8 @@ def search(request): "searchtitle" : keywords, "keywords" : keywords, "is_unanswered" : False, + "is_search": True, + "search_uri": search_uri, "context" : { 'is_paginated' : True, 'pages': objects_list.num_pages, |