diff options
author | hrcerqueira <hrcerqueira@gmail.com> | 2010-02-22 14:34:10 +0000 |
---|---|---|
committer | hrcerqueira <hrcerqueira@gmail.com> | 2010-02-22 14:34:10 +0000 |
commit | c98f611472b65e8be5943fb6e9f134c21d9a0665 (patch) | |
tree | 107e6d3b0f4f236d794e194e7d9074bbff34aadf /forum/views | |
parent | 2ea80d9c8d6c9e6ace51c59ebfc194b59bb0480a (diff) | |
download | askbot-c98f611472b65e8be5943fb6e9f134c21d9a0665.tar.gz askbot-c98f611472b65e8be5943fb6e9f134c21d9a0665.tar.bz2 askbot-c98f611472b65e8be5943fb6e9f134c21d9a0665.zip |
"Reversed" some m2m relations with the user model.
Fixed and improved most of modules functionality.
Definitevly moved books to a separate module.
Fixed some missing imports in views.
Adapted some of the interface changes Rick did on robofaqs to osqa.
Diffstat (limited to 'forum/views')
-rw-r--r-- | forum/views/__init__.py | 1 | ||||
-rw-r--r-- | forum/views/books.py | 140 | ||||
-rw-r--r-- | forum/views/meta.py | 4 | ||||
-rw-r--r-- | forum/views/readers.py | 60 | ||||
-rw-r--r-- | forum/views/users.py | 1 |
5 files changed, 38 insertions, 168 deletions
diff --git a/forum/views/__init__.py b/forum/views/__init__.py index faea1179..291fee2a 100644 --- a/forum/views/__init__.py +++ b/forum/views/__init__.py @@ -3,4 +3,3 @@ import writers import commands import users import meta -#import books diff --git a/forum/views/books.py b/forum/views/books.py deleted file mode 100644 index bb2ada6a..00000000 --- a/forum/views/books.py +++ /dev/null @@ -1,140 +0,0 @@ -from django.contrib.auth.decorators import login_required -from django.core.urlresolvers import reverse -from django.shortcuts import render_to_response -from forum.models import BookAuthorInfo, BookAuthorRss, Book -from forum.models import Question, QuestionRevision -from django.core.urlresolvers import reverse -from django.http import HttpResponseRedirect - -def books(request): - """this view seems to redirect to a default book - maybe it should instead show some popular titles? - """ - return HttpResponseRedirect(reverse('books') + 'mysql-zhaoyang') - -def book(request, short_name, unanswered=False): - """ - 1. questions list - 2. book info - 3. author info and blog rss items - """ - """ - List of Questions, Tagged questions, and Unanswered questions. - """ - books = Book.objects.extra(where=['short_name = %s'], params=[short_name]) - match_count = len(books) - if match_count == 0: - raise Http404 - else: - # the book info - book = books[0] - # get author info - author_info = BookAuthorInfo.objects.get(book=book) - # get author rss info - author_rss = BookAuthorRss.objects.filter(book=book) - - # get pagesize from session, if failed then get default value - user_page_size = request.session.get("pagesize", QUESTIONS_PAGE_SIZE) - # set pagesize equal to logon user specified value in database - if request.user.is_authenticated() and request.user.questions_per_page > 0: - user_page_size = request.user.questions_per_page - - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 - - view_id = request.GET.get('sort', None) - view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" } - try: - orderby = view_dic[view_id] - except KeyError: - view_id = "latest" - orderby = "-added_at" - - # check if request is from tagged questions - if unanswered: - # check if request is from unanswered questions - # Article.objects.filter(publications__id__exact=1) - objects = Question.objects.filter(book__id__exact=book.id, deleted=False, answer_count=0).order_by(orderby) - else: - objects = Question.objects.filter(book__id__exact=book.id, deleted=False).order_by(orderby) - - # RISK - inner join queries - objects = objects.select_related(); - objects_list = Paginator(objects, user_page_size) - questions = objects_list.page(page) - - return render_to_response('book.html', { - "book" : book, - "author_info" : author_info, - "author_rss" : author_rss, - "questions" : questions, - "context" : { - 'is_paginated' : True, - 'pages': objects_list.num_pages, - 'page': page, - 'has_previous': questions.has_previous(), - 'has_next': questions.has_next(), - 'previous': questions.previous_page_number(), - 'next': questions.next_page_number(), - 'base_url' : request.path + '?sort=%s&' % view_id, - 'pagesize' : user_page_size - } - }, context_instance=RequestContext(request)) - -@login_required -def ask_book(request, short_name): - if request.method == "POST": - form = AskForm(request.POST) - if form.is_valid(): - added_at = datetime.datetime.now() - html = sanitize_html(markdowner.convert(form.cleaned_data['text'])) - question = Question( - title = strip_tags(form.cleaned_data['title']), - author = request.user, - added_at = added_at, - last_activity_at = added_at, - last_activity_by = request.user, - wiki = form.cleaned_data['wiki'], - tagnames = form.cleaned_data['tags'].strip(), - html = html, - summary = strip_tags(html)[:120] - ) - if question.wiki: - question.last_edited_by = question.author - question.last_edited_at = added_at - question.wikified_at = added_at - - question.save() - - # create the first revision - QuestionRevision.objects.create( - question = question, - revision = 1, - title = question.title, - author = request.user, - revised_at = added_at, - tagnames = question.tagnames, - summary = CONST['default_version'], - text = form.cleaned_data['text'] - ) - - books = Book.objects.extra(where=['short_name = %s'], params=[short_name]) - match_count = len(books) - if match_count == 1: - # the book info - book = books[0] - book.questions.add(question) - - return HttpResponseRedirect(question.get_absolute_url()) - else: - form = AskForm() - - tags = _get_tags_cache_json() - return render_to_response('ask.html', { - 'form' : form, - 'tags' : tags, - 'email_validation_faq_url': reverse('faq') + '#validate', - }, context_instance=RequestContext(request)) - diff --git a/forum/views/meta.py b/forum/views/meta.py index 164c056a..b4c7a37f 100644 --- a/forum/views/meta.py +++ b/forum/views/meta.py @@ -61,8 +61,8 @@ def badges(request):#user status/reputation system badges = Badge.objects.all().order_by('type') my_badges = [] if request.user.is_authenticated(): - my_badges = Award.objects.filter(user=request.user) - my_badges.query.group_by = ['badge_id'] + my_badges = Award.objects.filter(user=request.user).values('badge_id') + #my_badges.query.group_by = ['badge_id'] return render_to_response('badges.html', { 'badges' : badges, diff --git a/forum/views/readers.py b/forum/views/readers.py index 72378218..6b0da476 100644 --- a/forum/views/readers.py +++ b/forum/views/readers.py @@ -28,13 +28,13 @@ from forum.utils.forms import get_next_url # used in index page #refactor - move these numbers somewhere? -INDEX_PAGE_SIZE = 20 +INDEX_PAGE_SIZE = 30 INDEX_AWARD_SIZE = 15 -INDEX_TAGS_SIZE = 100 +INDEX_TAGS_SIZE = 25 # used in tags list DEFAULT_PAGE_SIZE = 60 # used in questions -QUESTIONS_PAGE_SIZE = 10 +QUESTIONS_PAGE_SIZE = 30 # used in answers ANSWERS_PAGE_SIZE = 10 @@ -87,10 +87,19 @@ def index(request):#generates front page - shows listing of questions sorted in } view_id, orderby = _get_and_remember_questions_sort_method(request, view_dic, 'latest') - page_size = request.session.get('pagesize', QUESTIONS_PAGE_SIZE) - questions = Question.objects.exclude(deleted=True).order_by(orderby)[:page_size] + pagesize = request.session.get("pagesize",QUESTIONS_PAGE_SIZE) + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + qs = Question.objects.exclude(deleted=True).order_by(orderby) + + objects_list = Paginator(qs, pagesize) + questions = objects_list.page(page) + # RISK - inner join queries - questions = questions.select_related() + #questions = questions.select_related() tags = Tag.objects.get_valid_tags(INDEX_TAGS_SIZE) awards = Award.objects.get_recent_awards() @@ -111,7 +120,17 @@ def index(request):#generates front page - shows listing of questions sorted in "tab_id" : view_id, "tags" : tags, "awards" : awards[:INDEX_AWARD_SIZE], - }, context_instance=RequestContext(request)) + "context" : { + 'is_paginated' : True, + 'pages': objects_list.num_pages, + 'page': page, + 'has_previous': questions.has_previous(), + 'has_next': questions.has_next(), + 'previous': questions.previous_page_number(), + 'next': questions.next_page_number(), + 'base_url' : request.path + '?sort=%s&' % view_id, + 'pagesize' : pagesize + }}, context_instance=RequestContext(request)) def unanswered(request):#generates listing of unanswered questions return questions(request, unanswered=True) @@ -126,7 +145,7 @@ def questions(request, tagname=None, unanswered=False):#a view generating listin # Set flag to False by default. If it is equal to True, then need to be saved. pagesize_changed = False # get pagesize from session, if failed then get default value - pagesize = request.session.get("pagesize",10) + pagesize = request.session.get("pagesize",QUESTIONS_PAGE_SIZE) try: page = int(request.GET.get('page', '1')) except ValueError: @@ -287,25 +306,16 @@ def search(request): #generates listing of questions matching a search query - i view_id = "latest" orderby = "-added_at" - 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) - #no related selection either because we're relying on full text search here - else: + def question_search(keywords, orderby): objects = Question.objects.filter(deleted=False).extra(where=['title like %s'], params=['%' + keywords + '%']).order_by(orderby) # RISK - inner join queries - objects = objects.select_related(); + return objects.select_related(); + + from forum.modules import get_handler + + question_search = get_handler('question_search', question_search) + + objects = question_search(keywords, orderby) objects_list = Paginator(objects, pagesize) questions = objects_list.page(page) diff --git a/forum/views/users.py b/forum/views/users.py index 517bb170..04ffb690 100644 --- a/forum/views/users.py +++ b/forum/views/users.py @@ -1,6 +1,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.core.paginator import Paginator, EmptyPage, InvalidPage +from django.template.defaultfilters import slugify from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse from django.shortcuts import render_to_response, get_object_or_404 |