diff options
author | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-11 22:29:49 +0100 |
---|---|---|
committer | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-11 22:29:49 +0100 |
commit | 8d2e82e66a578615205a8a705b30e2cd5d8e8cbe (patch) | |
tree | 01afd1fc6c10a5d8af44a389b01096e0c328f168 | |
parent | 2a0dbb6513bc68474382fd10f20ca23774ccd681 (diff) | |
download | askbot-8d2e82e66a578615205a8a705b30e2cd5d8e8cbe.tar.gz askbot-8d2e82e66a578615205a8a705b30e2cd5d8e8cbe.tar.bz2 askbot-8d2e82e66a578615205a8a705b30e2cd5d8e8cbe.zip |
Upgraded&simplified api_get_questions view
-rw-r--r-- | askbot/views/commands.py | 42 |
1 files changed, 16 insertions, 26 deletions
diff --git a/askbot/views/commands.py b/askbot/views/commands.py index 3a590b85..2f4e80f4 100644 --- a/askbot/views/commands.py +++ b/askbot/views/commands.py @@ -9,7 +9,7 @@ from django.conf import settings as django_settings from django.core import exceptions from django.core.urlresolvers import reverse from django.contrib.auth.decorators import login_required -from django.http import HttpResponse, HttpResponseRedirect, Http404 +from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseBadRequest from django.forms import ValidationError from django.shortcuts import get_object_or_404 from django.views.decorators import csrf @@ -442,31 +442,21 @@ def subscribe_for_tags(request): @decorators.get_only def api_get_questions(request): - """json api for retrieving questions - todo - see if it is possible to integrate this with the - questions view - """ - form = forms.AdvancedSearchForm(request.GET) - if form.is_valid(): - query = form.cleaned_data['query'] - questions = models.Post.objects.get_questions().get_by_text_query(query) - if should_show_sort_by_relevance(): - questions = questions.extra(order_by = ['-relevance']) - questions = questions.filter(deleted = False).distinct() - page_size = form.cleaned_data.get('page_size', 30) - questions = questions[:page_size] - - question_list = list() - for question in questions: - question_list.append({ - 'url': question.get_absolute_url(), - 'title': question.thread.title, - 'answer_count': question.thread.answer_count - }) - json_data = simplejson.dumps(question_list) - return HttpResponse(json_data, mimetype = "application/json") - else: - raise ValidationError('InvalidInput') + """json api for retrieving questions""" + query = request.GET.get('query', '').strip() + if not query: + return HttpResponseBadRequest('Invalid query') + questions = models.Post.objects.get_questions().get_by_text_query(query) + if should_show_sort_by_relevance(): + questions = questions.extra(order_by = ['-relevance']) + questions = questions.filter(deleted=False).select_related('thread').distinct()[:30] + question_list = [{ + 'url': question.get_absolute_url(), + 'title': question.thread.title, + 'answer_count': question.thread.answer_count + } for question in questions] + json_data = simplejson.dumps(question_list) + return HttpResponse(json_data, mimetype = "application/json") @csrf.csrf_exempt |