diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-02-26 22:49:39 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-02-26 22:49:39 -0300 |
commit | 73dc5b26ef0ebb33af6c422e40f17882a8c8d9e2 (patch) | |
tree | cb93f9e72f889f29540b0f408a8e6941a0e0e6b9 | |
parent | d93adae8d5237b873729e5af245c3eb3ccc8d8d8 (diff) | |
download | askbot-73dc5b26ef0ebb33af6c422e40f17882a8c8d9e2.tar.gz askbot-73dc5b26ef0ebb33af6c422e40f17882a8c8d9e2.tar.bz2 askbot-73dc5b26ef0ebb33af6c422e40f17882a8c8d9e2.zip |
shaved a couple of queries from the question view
-rw-r--r-- | askbot/models/post.py | 6 | ||||
-rw-r--r-- | askbot/models/question.py | 6 | ||||
-rw-r--r-- | askbot/tests/page_load_tests.py | 2 | ||||
-rw-r--r-- | askbot/views/readers.py | 39 |
4 files changed, 31 insertions, 22 deletions
diff --git a/askbot/models/post.py b/askbot/models/post.py index 1543a438..dead32bc 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -466,13 +466,15 @@ class Post(models.Model): } elif self.is_question(): url = urlresolvers.reverse('question', args=[self.id]) - if no_slug is False: + if thread: + url += django_urlquote(slugify(thread.title)) + elif no_slug is False: url += django_urlquote(self.slug) return url elif self.is_comment(): origin_post = self.get_origin_post() return '%(url)s?comment=%(id)d#comment-%(id)d' % \ - {'url': origin_post.get_absolute_url(), 'id':self.id} + {'url': origin_post.get_absolute_url(thread=thread), 'id':self.id} raise NotImplementedError diff --git a/askbot/models/question.py b/askbot/models/question.py index 6daa3057..2bb4d6a0 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -7,6 +7,7 @@ from django.db import models from django.contrib.auth.models import User from django.utils.translation import ugettext as _ from django.core import cache # import cache, not from cache import cache, to be able to monkey-patch cache.cache in test cases +from django.core.urlresolvers import reverse import askbot import askbot.conf @@ -17,6 +18,7 @@ from askbot.models import signals from askbot import const from askbot.utils.lists import LazyList from askbot.utils import mysql +from askbot.utils.slug import slugify from askbot.skins.loaders import get_template #jinja2 template loading enviroment from askbot.search.state_manager import DummySearchState @@ -337,7 +339,9 @@ class Thread(models.Model): return self._question_cache def get_absolute_url(self): - return self._question_post().get_absolute_url() + return self._question_post().get_absolute_url(thread = self) + #question_id = self._question_post().id + #return reverse('question', args = [question_id]) + slugify(self.title) def update_favorite_count(self): self.favourite_count = FavoriteQuestion.objects.filter(thread=self).count() diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index 10bded11..558ee617 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -546,7 +546,7 @@ class QuestionPageRedirectTests(AskbotTestCase): self.c.old_comment_id = 301 self.c.save() - def test_bare_question(self): + def test_show_bare_question(self): resp = self.client.get(self.q.get_absolute_url()) self.assertEqual(200, resp.status_code) self.assertEqual(self.q, resp.context['question']) diff --git a/askbot/views/readers.py b/askbot/views/readers.py index a6f65e28..c14d8008 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -315,11 +315,24 @@ def question(request, id):#refactor - long subroutine. display question body, an show_page = form.cleaned_data['show_page'] answer_sort_method = form.cleaned_data['answer_sort_method'] + #load question and maybe refuse showing deleted question + #if the question does not exist - try mapping to old questions + #and and if it is not found again - then give up + try: + question_post = models.Post.objects.filter( + post_type = 'question', + id = id + ).select_related('thread')[0] + except IndexError: # Handle URL mapping - from old Q/A/C/ URLs to the new one - if not models.Post.objects.get_questions().filter(id=id).exists() and models.Post.objects.get_questions().filter(old_question_id=id).exists(): - old_question = models.Post.objects.get_questions().get(old_question_id=id) + try: + question_post = models.Post.objects.filter( + post_type='question', + old_question_id = id + ).select_related('thread')[0] + except IndexError: + raise Http404 - # If we are supposed to show a specific answer or comment, then just redirect to the new URL... if show_answer: try: old_answer = models.Post.objects.get_answers().get(old_answer_id=show_answer) @@ -334,13 +347,11 @@ def question(request, id):#refactor - long subroutine. display question body, an except models.Post.DoesNotExist: pass - # ...otherwise just patch question.id, to make URLs like this one work: /question/123#345 - # This is because URL fragment (hash) (i.e. #345) is not passed to the server so we can't know which - # answer user expects to see. If we made a redirect to the new question.id then that hash would be lost. - # And if we just hack the question.id (and in question.html template /or its subtemplate/ we create anchors for both old and new id-s) - # then everything should work as expected. - id = old_question.id - + try: + question_post.assert_is_visible_to(request.user) + except exceptions.QuestionHidden, error: + request.user.message_set.create(message = unicode(error)) + return HttpResponseRedirect(reverse('index')) #resolve comment and answer permalinks #they go first because in theory both can be moved to another question @@ -397,14 +408,6 @@ def question(request, id):#refactor - long subroutine. display question body, an request.user.message_set.create(message = unicode(error)) return HttpResponseRedirect(reverse('question', kwargs = {'id': id})) - #load question and maybe refuse showing deleted question - try: - question_post = get_object_or_404(models.Post, post_type='question', id=id) - question_post.assert_is_visible_to(request.user) - except exceptions.QuestionHidden, error: - request.user.message_set.create(message = unicode(error)) - return HttpResponseRedirect(reverse('index')) - thread = question_post.thread #redirect if slug in the url is wrong |