summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-02-27 22:51:36 -0300
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-02-27 22:51:36 -0300
commit96c4d81d6095b5b7c8fd4d38250583070eaf3e16 (patch)
tree541d7be191b837376259b9c7de5f1dd31b0fa852
parent3cf65e8aa33271e06d6e9d9f564f5595dcbd6fc4 (diff)
downloadaskbot-96c4d81d6095b5b7c8fd4d38250583070eaf3e16.tar.gz
askbot-96c4d81d6095b5b7c8fd4d38250583070eaf3e16.tar.bz2
askbot-96c4d81d6095b5b7c8fd4d38250583070eaf3e16.zip
refactored the templates for the question page to make them more easily cacheable, but no gain on the queries yet
-rw-r--r--askbot/skins/common/templates/question/answer_controls.html25
-rw-r--r--askbot/skins/common/templates/question/answer_vote_buttons.html2
-rw-r--r--askbot/skins/common/templates/question/question_vote_buttons.html2
-rw-r--r--askbot/skins/default/templates/base.html1
-rw-r--r--askbot/skins/default/templates/macros.html26
-rw-r--r--askbot/skins/default/templates/meta/bottom_scripts.html10
-rw-r--r--askbot/skins/default/templates/meta/html_head_javascript.html12
-rw-r--r--askbot/skins/default/templates/question.html70
-rw-r--r--askbot/skins/default/templates/question/answer_card.html8
-rw-r--r--askbot/skins/default/templates/question/question_card.html19
-rw-r--r--askbot/views/readers.py17
11 files changed, 126 insertions, 66 deletions
diff --git a/askbot/skins/common/templates/question/answer_controls.html b/askbot/skins/common/templates/question/answer_controls.html
index fd12c856..2e8cd4f9 100644
--- a/askbot/skins/common/templates/question/answer_controls.html
+++ b/askbot/skins/common/templates/question/answer_controls.html
@@ -8,13 +8,7 @@
{% trans %}link{% endtrans %}
</a>
</span>
-{% if request.user.is_authenticated() and
- (
- request.user == answer.author or
- request.user.is_administrator_or_moderator()
- )
-%}
-<span class="action-link delete-post">
+<span id='delete-answer-{{answer.id}}' class="action-link delete-post">
<a class="question-delete"
>{% if answer.deleted %}{% trans %}undelete{% endtrans %}{% else %}{% trans %}delete{% endtrans %}{% endif %}</a>
</span>
@@ -52,7 +46,20 @@
<a class="question-flag">{% trans %}flag offensive{% endtrans %}</a>
</span>
{% endif %}
-<span class="action-link">
+<span id='edit-answer-{{answer.id}}' class="action-link">
<a class="question-edit" href="{% url edit_answer answer.id %}">{% trans %}edit{% endtrans %}</a>
</span>
-{% endif %}
+<script type="text/javascript">
+ (function(){
+ var del_link = document.getElementById(
+ 'delete-answer-' + '{{answer.id}}'
+ );
+ var edit_link = document.getElementById(
+ 'edit-answer-' + '{{answer.id}}'
+ );
+ if (askbot['data']['userIsAuthenticated'] === false){
+ del_link.parentNode.removeChild(del_link);
+ edit_link.parentNode.removeChild(edit_link);
+ }
+ })();
+</script>
diff --git a/askbot/skins/common/templates/question/answer_vote_buttons.html b/askbot/skins/common/templates/question/answer_vote_buttons.html
index 9097fec2..6ac2e07d 100644
--- a/askbot/skins/common/templates/question/answer_vote_buttons.html
+++ b/askbot/skins/common/templates/question/answer_vote_buttons.html
@@ -1,4 +1,4 @@
-{{ macros.post_vote_buttons(post = answer, visitor_vote = user_answer_votes[answer.id]) }}
+{{ macros.post_vote_buttons(post = answer) }}
<img id="answer-img-accept-{{ answer.id }}" class="answer-img-accept"
{% if answer.accepted() %}
src="{{'/images/vote-accepted-on.png'|media}}"
diff --git a/askbot/skins/common/templates/question/question_vote_buttons.html b/askbot/skins/common/templates/question/question_vote_buttons.html
index 8466beb9..6b8774cc 100644
--- a/askbot/skins/common/templates/question/question_vote_buttons.html
+++ b/askbot/skins/common/templates/question/question_vote_buttons.html
@@ -1 +1 @@
-{{ macros.post_vote_buttons(post = question, visitor_vote = user_question_vote) }}
+{{ macros.post_vote_buttons(post = question) }}
diff --git a/askbot/skins/default/templates/base.html b/askbot/skins/default/templates/base.html
index bd19f707..bc0a8d6c 100644
--- a/askbot/skins/default/templates/base.html
+++ b/askbot/skins/default/templates/base.html
@@ -9,6 +9,7 @@
{% include "meta/html_head_stylesheets.html" %}
{% block forestyle %}{% endblock %}
{% include "meta/html_head_javascript.html" %}
+ {% block forejs %}{% endblock %}
{% if settings.USE_CUSTOM_HTML_HEAD %}
{{ settings.CUSTOM_HTML_HEAD }}
{% endif %}
diff --git a/askbot/skins/default/templates/macros.html b/askbot/skins/default/templates/macros.html
index 93eae41b..e1945aba 100644
--- a/askbot/skins/default/templates/macros.html
+++ b/askbot/skins/default/templates/macros.html
@@ -23,16 +23,10 @@
</div>
{%- endmacro -%}
-{%- macro post_vote_buttons(post = None, visitor_vote = None) -%}
-<div
- id="{{post.post_type}}-img-upvote-{{ post.id }}"
- class="{{post.post_type}}-img-upvote post-vote{% if visitor_vote == 1 %} on{% endif %}"
- {% if post.post_type == 'question' %}
- title="{% trans %}i like this question (click again to cancel){% endtrans %}"
- {% else %}
- title="{% trans %}i like this answer (click again to cancel){% endtrans %}"
- {% endif %}
-/></div>
+{%- macro post_vote_buttons(post = None) -%}
+<div id="{{post.post_type}}-img-upvote-{{ post.id }}"
+ class="{{post.post_type}}-img-upvote post-vote">
+</div>
<div
id="{{post.post_type}}-vote-number-{{ post.id }}"
class="vote-number"
@@ -40,13 +34,11 @@
>{{ post.score }}</div>
<div
id="{{post.post_type}}-img-downvote-{{ post.id }}"
- class="{{post.post_type}}-img-downvote post-vote{% if visitor_vote == -1 %} on{% endif %}"
- {% if post.post_type == 'question' %}
- title="{% trans %}i dont like this question (click again to cancel){% endtrans %}"
- {% else %}
- title="{% trans %}i dont like this answer (click again to cancel){% endtrans %}"
- {% endif %}
-/></div>
+ class="{{post.post_type}}-img-downvote post-vote">
+</div>
+<script type="text/javascript">
+ askbot['functions']['renderPostVoteButtons']('{{post.post_type}}', '{{post.id}}');
+</script>
{%- endmacro -%}
{%- macro post_contributor_avatar_and_credentials(post, user) -%}
diff --git a/askbot/skins/default/templates/meta/bottom_scripts.html b/askbot/skins/default/templates/meta/bottom_scripts.html
index dd5cb202..244cec21 100644
--- a/askbot/skins/default/templates/meta/bottom_scripts.html
+++ b/askbot/skins/default/templates/meta/bottom_scripts.html
@@ -12,16 +12,6 @@
var scriptUrl = '/{{settings.ASKBOT_URL}}'
var askbotSkin = '{{settings.ASKBOT_DEFAULT_SKIN}}';
var enableMathJax = {% if settings.ENABLE_MATHJAX %}true{% else %}false{% endif %};
- {% if request.user.is_authenticated() %}
- askbot['data']['userIsAuthenticated'] = true;
- askbot['data']['userId'] = {{request.user.id}};
- askbot['data']['userIsAdminOrMod'] = {% if
- request.user.is_administrator()
- or request.user.is_moderator()
- %}true{% else %}false{% endif %};
- {% else %}
- askbot['data']['userIsAuthenticated'] = false;
- {% endif %}
askbot['urls']['mark_read_message'] = '{% url "read_message" %}';
askbot['urls']['get_tags_by_wildcard'] = '{% url "get_tags_by_wildcard" %}';
askbot['urls']['get_tag_list'] = '{% url "get_tag_list" %}';
diff --git a/askbot/skins/default/templates/meta/html_head_javascript.html b/askbot/skins/default/templates/meta/html_head_javascript.html
index 2d453215..58f6430d 100644
--- a/askbot/skins/default/templates/meta/html_head_javascript.html
+++ b/askbot/skins/default/templates/meta/html_head_javascript.html
@@ -2,10 +2,18 @@
<script type="text/javascript">
var askbot = {};
askbot['data'] = {};
+ {% if request.user.is_authenticated() %}
+ askbot['data']['userIsAuthenticated'] = true;
+ askbot['data']['userId'] = {{request.user.id}};
+ askbot['data']['userIsAdminOrMod'] = {% if
+ request.user.is_administrator()
+ or request.user.is_moderator()
+ %}true{% else %}false{% endif %};
+ {% else %}
+ askbot['data']['userIsAuthenticated'] = false;
+ {% endif %}
askbot['urls'] = {};
askbot['settings'] = {};
askbot['messages'] = {};
</script>
-{% block forejs %}
-{% endblock %}
{# avoid adding javascript here so that pages load faster #}
diff --git a/askbot/skins/default/templates/question.html b/askbot/skins/default/templates/question.html
index b2462faf..29301e30 100644
--- a/askbot/skins/default/templates/question.html
+++ b/askbot/skins/default/templates/question.html
@@ -9,15 +9,75 @@
<link rel="canonical" href="{{settings.APP_URL|strip_path}}{{question.get_absolute_url()}}" />
<link rel="stylesheet" type="text/css" href="{{'/js/wmd/wmd.css'|media}}" />
{% endblock %}
+{% block forejs %}
+ <script type="text/javascript">
+ (function(){
+ var data = askbot['data'];
+ if (data['userIsAuthenticated']){
+ var votes = {};
+ votes['{{question.id}}'] = {{user_question_vote}};
+ {% for answer_id in user_answer_votes %}
+ votes['{{answer_id}}'] = {{user_answer_votes[answer_id]}};
+ {% endfor %}
+ data['user_votes'] = votes;
+ }
+
+ function render_vote_buttons(post_type, post_id){
+ var upvote_btn = document.getElementById(
+ post_type + '-img-upvote-' + post_id
+ );
+ var downvote_btn = document.getElementById(
+ post_type + '-img-downvote-' + post_id
+ );
+ if (data['userIsAuthenticated']){
+ if (post_id in data['user_votes']){
+ var vote = data['user_votes'][post_id];
+ if (vote == -1){
+ var btn = downvote_btn;
+ } else if (vote == 1){
+ var btn = upvote_btn;
+ } else {
+ return;
+ }
+ btn.className = btn.className + ' on';
+ }
+ }
+ }
+ askbot['functions'] = askbot['functions'] || {};
+ askbot['functions']['renderPostVoteButtons'] = render_vote_buttons;
+ })();
+ </script>
+{% endblock %}
{% block content %}
- {# ==== BEGIN: question/content.html ==== #}
- {% include "question/content.html" %}
- {# ==== END: question/content.html ==== #}
+ {% if is_cacheable %}
+ {% cache 1000 "thread" thread.id %}
+ {% include "question/content.html" %}
+ {% endcache %}
+ {% else %}
+ {% include "question/content.html" %}
+ {% endif %}
{% endblock %}
{% block sidebar %}
- {%include "question/sidebar.html" %}
+ {% include "question/sidebar.html" %}
{% endblock %}
{% block endjs %}
- {%include "question/javascript.html" %}
+ {% include "question/javascript.html" %}
+ {#
+ <script type="text/javascript">
+ var messages = askbot['messages'];
+ messages['upvote_question'] = gettext(
+ 'I like this question (click again to cancel)'
+ );
+ messages['upvote_answer'] = gettext(
+ 'I like this answer (click again to cancel)'
+ );
+ messages['downvote_question'] = gettext(
+ "I don't like this question (click again to cancel)"
+ );
+ messages['downvote_answer'] = gettext(
+ "I don't like this answer (click again to cancel)"
+ );
+ </script>
+ #}
{% endblock %}
diff --git a/askbot/skins/default/templates/question/answer_card.html b/askbot/skins/default/templates/question/answer_card.html
index d71131a8..7161c186 100644
--- a/askbot/skins/default/templates/question/answer_card.html
+++ b/askbot/skins/default/templates/question/answer_card.html
@@ -7,29 +7,21 @@
id="post-id-{{ answer.id }}"
class="{{ macros.answer_classes(answer, question) }}">
<div class="vote-buttons">
- {# ==== START: question/answer_vote_buttons.html ==== #}
{% include "question/answer_vote_buttons.html" %}
- {# ==== END: question/answer_vote_buttons.html ==== #}
</div>
<div class="answer-table">
<div class="item-right">
<div class="answer-body">
<div class="post-update-info-container">
- {# ==== START: question/answer_author_info.html ==== #}
{% include "question/answer_author_info.html" %}
- {# ==== END: question/answer_author_info.html ==== #}
</div>
{{ answer.html }}
</div>
<div class="answer-controls post-controls">
- {# ==== START: question/answer_controls.html ==== #}
{% include "question/answer_controls.html" %}
- {# ==== END: question/answer_controls.html ==== #}
</div>
- {# ==== START: question/answer_comments.html ==== #}
{% include "question/answer_comments.html" %}
- {# ==== END: question/answer_comments.html ==== #}
</div>
</div>
<div class="clean"></div>
diff --git a/askbot/skins/default/templates/question/question_card.html b/askbot/skins/default/templates/question/question_card.html
index 7077a8d1..08f7ccee 100644
--- a/askbot/skins/default/templates/question/question_card.html
+++ b/askbot/skins/default/templates/question/question_card.html
@@ -1,34 +1,31 @@
<div class="vote-buttons">
- {# ==== BEGIN: question/question_vote_buttons.html ==== #}
{% include "question/question_vote_buttons.html" %}
- {# ==== END: question/question_vote_buttons.html ==== #}
- {# ==== BEGIN: question/share_buttons.html ==== #}
{% include "question/share_buttons.html" %}
- {# ==== END: question/share_buttons.html ==== #}
</div>
<div id="post-id-{{question.id}}" class="question-content{% if question.deleted %} deleted{% endif %}">
<h1><a href="{{ question.get_absolute_url() }}">{{ thread.get_title(question)|escape }}</a></h1>
{% include "question/question_tags.html" %}
- {# ==== END: question/question_tags.html" #}
<div id="question-table">
<div class="question-body">
<div class="post-update-info-container">
- {# ==== START: "question/question_author_info.html" #}
{% include "question/question_author_info.html" %}
- {# ==== END: "question/question_author_info.html" #}
</div>
{{ question.html }}
</div>
<div id="question-controls" class="post-controls">
- {# ==== START: include "question/question_controls.html" #}
{% include "question/question_controls.html" %}
- {# ==== END: include "question/question_controls.html" #}
</div>
- {# ==== START: question/question_comments.html ==== #}
+ <script type="text/javascript">
+ (function(){
+ if (askbot['data']['userIsAuthenticated'] === false){
+ var ctrl = document.getElementById('question-controls')
+ ctrl.parentNode.removeChild(ctrl);
+ }
+ })();
+ </script>
{% include "question/question_comments.html" %}
- {# ==== END: question/question_comments.html ==== #}
</div>
</div>
diff --git a/askbot/views/readers.py b/askbot/views/readers.py
index 8dd49a0b..c2818ac8 100644
--- a/askbot/views/readers.py
+++ b/askbot/views/readers.py
@@ -434,6 +434,7 @@ def question(request, id):#refactor - long subroutine. display question body, an
answers.remove(thread.accepted_answer)
answers.insert(0, thread.accepted_answer)
+ #maybe remove the personalized per-visitor caching?
Post.objects.precache_comments(for_posts=[question_post] + answers, visitor=request.user)
user_answer_votes = {}
@@ -442,6 +443,7 @@ def question(request, id):#refactor - long subroutine. display question body, an
for vote in votes:
user_answer_votes[vote.voted_post.id] = int(vote)
+ #not necessary any more?
filtered_answers = [answer for answer in answers if ((not answer.deleted) or (answer.deleted and answer.author_id == request.user.id))]
#resolve page number and comment number for permalinks
@@ -499,16 +501,27 @@ def question(request, id):#refactor - long subroutine. display question body, an
}
paginator_context = functions.setup_paginator(paginator_data)
+ #todo: maybe consolidate all activity in the thread
+ #for the user into just one query?
favorited = thread.has_favorite_by_user(request.user)
user_question_vote = 0
if request.user.is_authenticated():
+ #todo: narrow scope of the "select related" call here?
+ #todo: maybe consolidate this query with the answer vote query?
votes = question_post.votes.select_related().filter(user=request.user)
try:
user_question_vote = int(votes[0])
except IndexError:
user_question_vote = 0
+ is_cacheable = True
+ if show_page != 1:
+ is_cacheable = False
+ elif show_comment_position > askbot_settings.MAX_COMMENTS_TO_SHOW:
+ is_cacheable = False
+
data = {
+ 'is_cacheable': is_cacheable,
'page_class': 'question-page',
'active_tab': 'questions',
'question' : question_post,
@@ -518,10 +531,10 @@ def question(request, id):#refactor - long subroutine. display question body, an
'answer' : AnswerForm(question_post, request.user),
'answers' : page_objects.object_list,
'user_answer_votes': user_answer_votes,
- 'tags' : thread.tags.all(),
+ 'tags' : thread.tags.all(),#todo: do we need actual tags here?
'tab_id' : answer_sort_method,
'favorited' : favorited,
- 'similar_threads' : thread.get_similar_threads(),
+ 'similar_threads' : thread.get_similar_threads(),#todo: cache this?
'language_code': translation.get_language(),
'paginator_context' : paginator_context,
'show_post': show_post,