From 8f7f9c537865875390c574363b4e1826d10b987c Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Thu, 21 Aug 2014 11:43:09 +0400 Subject: fixed revealing of author identity upon editing a post --- askbot/management/commands/rename_tags_id.py | 2 +- askbot/migrations/0172_sync_thread_deleted.py | 2 +- askbot/models/post.py | 17 ++- askbot/models/question.py | 7 ++ askbot/templates/macros.html | 123 ++++++++------------- askbot/templates/question/answer_author_info.html | 9 +- .../templates/question/question_author_info.html | 9 +- askbot/templates/revisions.html | 7 +- askbot/views/readers.py | 1 + 9 files changed, 77 insertions(+), 100 deletions(-) diff --git a/askbot/management/commands/rename_tags_id.py b/askbot/management/commands/rename_tags_id.py index 7926d28d..2f6e666f 100644 --- a/askbot/management/commands/rename_tags_id.py +++ b/askbot/management/commands/rename_tags_id.py @@ -106,7 +106,7 @@ rename_tags, but using tag id's to_tags = get_tags_by_ids(to_tag_ids) #all tags must belong to the same language - lang_codes = {tag.language_code for tag in (from_tags + to_tags)} + lang_codes = set(tag.language_code for tag in (from_tags + to_tags)) if len(lang_codes) != 1: langs = ', '.join(lang_codes) raise CommandError('all tags must belong to the same language, have: %s' % langs) diff --git a/askbot/migrations/0172_sync_thread_deleted.py b/askbot/migrations/0172_sync_thread_deleted.py index 961df840..222d2d77 100644 --- a/askbot/migrations/0172_sync_thread_deleted.py +++ b/askbot/migrations/0172_sync_thread_deleted.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from south.utils import datetime_utils as datetime +#from south.utils import datetime_utils as datetime from south.db import db from south.v2 import DataMigration from django.db import models diff --git a/askbot/models/post.py b/askbot/models/post.py index 7721b37b..fac0daf5 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -1489,9 +1489,22 @@ class Post(models.Model): ) return result + def cache_latest_revision(self, rev): + setattr(self, '_last_rev_cache', rev) def get_latest_revision(self): - return self.revisions.order_by('-revision')[0] + if hasattr(self, '_last_rev_cache'): + return self._last_rev_cache + rev = self.revisions.order_by('-revision')[0] + self.cache_latest_revision(rev) + return rev + + def get_earliest_revision(self): + if hasattr(self, '_first_rev_cache'): + return self._first_rev_cache + rev = self.revisions.order_by('revision')[0] + setattr(self, '_first_rev_cache', rev) + return rev def get_latest_revision_number(self): try: @@ -2223,6 +2236,8 @@ class PostRevisionManager(models.Manager): if needs_moderation: revision.place_on_moderation_queue() + revision.post.cache_latest_revision(revision) + return revision class PostRevision(models.Model): diff --git a/askbot/models/question.py b/askbot/models/question.py index 2ce7d124..05085959 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -1209,6 +1209,13 @@ class Thread(models.Model): post_to_author = dict() question_post = None for post in thread_posts: + + #precache some revision data + first_rev = post.get_earliest_revision() + last_rev = post.get_latest_revision() + first_rev.post = post + last_rev.post = post + #pass through only deleted question posts if post.deleted and post.post_type != 'question': continue diff --git a/askbot/templates/macros.html b/askbot/templates/macros.html index 942d150f..19aab815 100644 --- a/askbot/templates/macros.html +++ b/askbot/templates/macros.html @@ -52,42 +52,21 @@ {%- endmacro -%} -{%- macro post_contributor_avatar_and_credentials(post, user, karma_mode = None, badges_mode = None) -%} - {% if post.is_anonymous %} +{%- macro post_contributor_avatar_and_credentials(revision) -%} + {% if revision.is_anonymous %} {% trans %}anonymous user{% endtrans %} -

{{ user.get_anonymous_name() }}

+

{{ revision.author.get_anonymous_name() }}

{% else %} - {{ user_card(user, karma_mode=karma_mode, badges_mode=badges_mode) }} + {{ user_card(revision.author, karma_mode=karma_mode, badges_mode=badges_mode) }} {% endif %} {% if settings.GROUPS_ENABLED %} - {{ user_primary_group(user) }} + {{ user_primary_group(revision.author) }} {% endif %} {%- endmacro -%} -{%- macro post_last_updater_and_creator_info( - post, min_rep_to_edit_wiki, karma_mode = None, badges_mode = None - ) -%} - {{ post_contributor_info( - post, "original_author", post.wiki, min_rep_to_edit_wiki, - karma_mode = karma_mode, badges_mode = badges_mode - ) - }} - {{ post_contributor_info( - post, "last_updater", post.wiki, min_rep_to_edit_wiki, - karma_mode = karma_mode, badges_mode = badges_mode - ) - }} -{%- endmacro -%} - -{%- macro post_contributor_info( - post, contributor_type, is_wiki, wiki_min_rep, - karma_mode = None, badges_mode = None - ) -%} -{# there is a whole bunch of trickery here, probably indicative of -poor design of the data or methods on data objects #} -{% if contributor_type=="original_author" %} +{%- macro post_last_updater_and_creator_info(post) -%} + {% if post.wiki %}
- {% if is_wiki %}

{%- if post.post_type == 'question' -%} {{ settings.WORDS_ASKED|escape }} @@ -103,61 +82,55 @@ poor design of the data or methods on data objects #} alt="{% trans %}this post is marked as community wiki{% endtrans %}" style="float:left" /> + {% set wiki_min_rep = settings.MIN_REP_TO_EDIT_WIKI %}

{% trans %}This post is a wiki. Anyone with karma >{{wiki_min_rep}} is welcome to improve it.{% endtrans %}

+
{% else %} -

- {# todo: access to class names needs to be removed here #} - {% if post.post_type == 'question' %} - {{ settings.WORDS_ASKED|escape }} - {% elif post.post_type == 'answer' %} - {{ settings.WORDS_ANSWERED|escape }} - {% else %} - {% trans %}posted{% endtrans %} - {% endif %} - {% if post.__class__.__name__ == 'PostRevision' %} - {{ timeago(post.revised_at) }} - {% else %} - {{ timeago(post.added_at) }} - {% endif %} -

- {{ post_contributor_avatar_and_credentials( - post, post.author, karma_mode = karma_mode, badges_mode = badges_mode - ) }} + {% set first_rev = post.get_earliest_revision() %} + {% set last_rev = post.get_latest_revision() %} + {{ post_contributor_info(first_rev, "original_author") }} + {% if first_rev != last_rev %} + {{ post_contributor_info(last_rev, "last_updater") }} + {% endif %} {% endif %} - -{% elif contributor_type=="last_updater" %} - {% if post.post_type in ('question', 'answer') %} - {% set last_edited_at = post.last_edited_at %} - {% set original_author = post.author %} - {% set update_author = post.last_edited_by %} - {% elif post.__class__.__name__ == 'PostRevision' %} - {% set last_edited_at = post.revised_at %} - {% set original_author = None %}{# fake value to force display widget in the revision views #} - {% set update_author = post.author %} +{%- endmacro -%} + +{%- macro post_contributor_info(revision, contributor_type) -%} +
+{% if contributor_type=="original_author" %} + {% if revision.post.post_type == 'question' %} + {% set action_label = settings.WORDS_ASKED|escape %} + {% elif revision.post.post_type == 'answer' %} + {% set action_label = settings.WORDS_ANSWERED|escape %} + {% else %} + {% set action_label = gettext('posted') %} {% endif %} - {% if last_edited_at %} -
-

- + {% trans %}updated{% endtrans %} {{ timeago(last_edited_at) }} -

- {% if original_author != update_author or is_wiki %} - {{ - post_contributor_avatar_and_credentials( - post, update_author, - karma_mode = karma_mode, badges_mode = badges_mode - ) - }} + href="{% url answer_revisions revision.post.id %}" {% endif %} -
+ >{{ action_label }} {{ timeago(revision.revised_at) }} +

+ {% set show_user_info = (contributor_type == 'original_author' or + (contributor_type == 'last_updater' and + original_author != revision.author )) + %} + {% if show_user_info %} + {{ post_contributor_avatar_and_credentials(revision) }} {% endif %} -{% endif %} +
{%- endmacro -%} {%- macro if_else(condition, if_true, if_false) -%} diff --git a/askbot/templates/question/answer_author_info.html b/askbot/templates/question/answer_author_info.html index 1c729b51..54b36b28 100644 --- a/askbot/templates/question/answer_author_info.html +++ b/askbot/templates/question/answer_author_info.html @@ -1,8 +1 @@ -{{ - macros.post_last_updater_and_creator_info( - answer, - settings.MIN_REP_TO_EDIT_WIKI, - karma_mode = settings.KARMA_MODE, - badges_mode = settings.BADGES_MODE - ) -}} +{{ macros.post_last_updater_and_creator_info(answer) }} diff --git a/askbot/templates/question/question_author_info.html b/askbot/templates/question/question_author_info.html index c25b7d84..368344a5 100644 --- a/askbot/templates/question/question_author_info.html +++ b/askbot/templates/question/question_author_info.html @@ -1,8 +1 @@ -{{ - macros.post_last_updater_and_creator_info( - question, - settings.MIN_REP_TO_EDIT_WIKI, - karma_mode = settings.KARMA_MODE, - badges_mode = settings.BADGES_MODE - ) -}} +{{ macros.post_last_updater_and_creator_info(question) }} diff --git a/askbot/templates/revisions.html b/askbot/templates/revisions.html index 1617ff25..24038b20 100644 --- a/askbot/templates/revisions.html +++ b/askbot/templates/revisions.html @@ -49,12 +49,7 @@ {% else %} {% set contributor_type = "last_updater" %} {% endif %} - {{ macros.post_contributor_info( - revision, - contributor_type, - False, - 0, - ) + {{ macros.post_contributor_info(revision, contributor_type) }} diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 79db9a12..2e3c8249 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -673,6 +673,7 @@ def question(request, id):#refactor - long subroutine. display question body, an extra = context.get_extra('ASKBOT_QUESTION_PAGE_EXTRA_CONTEXT', request, data) data.update(extra) + #print 'generated in ', datetime.datetime.now() - before return render(request, 'question.html', data) def revisions(request, id, post_type = None): -- cgit v1.2.3-1-g7c22