From 85ffe7978b53564313781d14e5af69dfdc98cb27 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Wed, 27 Aug 2014 23:12:16 +0700 Subject: fixed anonymity display on questons --- askbot/forms.py | 91 +++++--------------------- askbot/models/post.py | 12 +++- askbot/models/question.py | 36 ++++++++-- askbot/models/user.py | 2 +- askbot/templates/question_edit.html | 2 +- askbot/templates/widgets/question_summary.html | 7 +- askbot/tests/form_tests.py | 6 +- askbot/views/writers.py | 33 +++++----- 8 files changed, 78 insertions(+), 111 deletions(-) diff --git a/askbot/forms.py b/askbot/forms.py index b9171007..180323ba 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -1241,10 +1241,6 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): tags = TagNamesField() summary = SummaryField() wiki = WikiField() - reveal_identity = forms.BooleanField( - label=_('reveal identity'), - required=False, - ) suppress_email = SuppressEmailField() #todo: this is odd that this form takes question as an argument @@ -1262,8 +1258,11 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): self.fields['tags'].initial = revision.tagnames self.fields['wiki'].initial = self.question.wiki #hide the reveal identity field - if not self.can_stay_anonymous(): - self.hide_field('reveal_identity') + if self.can_edit_anonymously(): + self.fields['reveal_identity'] = forms.BooleanField( + label=_('remove anonymity'), + required=False, + ) if getattr(django_settings, 'ASKBOT_MULTILINGUAL', False): self.fields['language'] = LanguageField() @@ -1271,9 +1270,17 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): if should_use_recaptcha(self.user): self.fields['recaptcha'] = AskbotRecaptchaField() + + def clean(self): + edit_anonymously = not self.cleaned_data.get('reveal_identity', True) + self.cleaned_data['edit_anonymously'] = edit_anonymously + return self.cleaned_data + + def has_changed(self): if super(EditQuestionForm, self).has_changed(): return True + if askbot_settings.GROUPS_ENABLED: was_private = self.question.is_private() if was_private != self.cleaned_data['post_privately']: @@ -1286,7 +1293,8 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): else: return False - def can_stay_anonymous(self): + + def can_edit_anonymously(self): """determines if the user cat keep editing the question anonymously""" return (askbot_settings.ALLOW_ASK_ANONYMOUSLY @@ -1294,75 +1302,6 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): and self.user.is_owner_of(self.question) ) - def clean_reveal_identity(self): - """cleans the reveal_identity field - which determines whether previous anonymous - edits must be rewritten as not anonymous - this does not necessarily mean that the edit will be anonymous - - only does real work when question is anonymous - based on the following truth table: - - is_anon can owner checked cleaned data - - * * * False (ignore choice in checkbox) - + + + + True - + + + - False - + + - + Raise(Not owner) - + + - - False - + - + + True (setting "can" changed, say yes) - + - + - False, warn (but prev edits stay anon) - + - - + Raise(Not owner) - + - - - False - """ - value = self.cleaned_data['reveal_identity'] - if self.question.is_anonymous: - if value is True: - if self.user.is_owner_of(self.question): - #regardless of the ALLOW_ASK_ANONYMOUSLY - return True - else: - self.show_field('reveal_identity') - del self.cleaned_data['reveal_identity'] - raise forms.ValidationError( - _( - 'Sorry, only owner of the anonymous ' - 'question can reveal his or her ' - 'identity, please uncheck the ' - 'box' - ) - ) - else: - can_ask_anon = askbot_settings.ALLOW_ASK_ANONYMOUSLY - is_owner = self.user.is_owner_of(self.question) - if can_ask_anon is False and is_owner: - self.show_field('reveal_identity') - raise forms.ValidationError( - _( - 'Sorry, apparently rules have just changed - ' - 'it is no longer possible to ask anonymously. ' - 'Please either check the "reveal identity" box ' - 'or reload this page and try editing the question ' - 'again.' - ) - ) - return False - else: - #takes care of 8 possibilities - first row of the table - return False - - def clean(self): - """Purpose of this function is to determine whether - it is ok to apply edit anonymously in the synthetic - field edit_anonymously. It relies on correct cleaning - if the "reveal_identity" field - """ - super(EditQuestionForm, self).clean() - reveal_identity = self.cleaned_data.get('reveal_identity', False) - stay_anonymous = False - if reveal_identity is False and self.can_stay_anonymous(): - stay_anonymous = True - self.cleaned_data['stay_anonymous'] = stay_anonymous - return self.cleaned_data class EditAnswerForm(PostAsSomeoneForm, PostPrivatelyForm): summary = SummaryField() diff --git a/askbot/models/post.py b/askbot/models/post.py index fac0daf5..a50f3725 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -1835,7 +1835,10 @@ class Post(models.Model): self.last_edited_by = edited_by #self.html is denormalized in save() self.text = text - self.is_anonymous = edit_anonymously + if edit_anonymously: + self.is_anonymous = edit_anonymously + #else: + #pass - we remove anonymity via separate function call #wiki is an eternal trap whence there is no exit if self.wiki == False and wiki == True: @@ -1857,6 +1860,7 @@ class Post(models.Model): comment=comment, by_email=by_email, ip_addr=ip_addr, + is_anonymous=edit_anonymously ) parse_results = self.parse_and_save(author=edited_by, is_private=is_private) @@ -1997,7 +2001,8 @@ class Post(models.Model): text=None, comment=None, by_email=False, - ip_addr=None + ip_addr=None, + is_anonymous=False ): #todo: this may be identical to Question.add_revision if None in (author, revised_at, text): @@ -2009,7 +2014,8 @@ class Post(models.Model): text=text, summary=comment, by_email=by_email, - ip_addr=ip_addr + ip_addr=ip_addr, + is_anonymous=is_anonymous ) def _question__add_revision( diff --git a/askbot/models/question.py b/askbot/models/question.py index 05085959..a068224d 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -731,11 +731,32 @@ class Thread(models.Model): return answers[0].id return None - def get_latest_post(self): - """returns latest non-deleted post""" - if askbot_settings.GROUPS_ENABLED: - raise NotImplementedError() - return self.posts.filter(deleted=False).order_by('-added_at')[0] + def get_latest_revision(self, user=None): + #todo: add denormalized field to Thread model + from askbot.models import Post, PostRevision + posts_filter = { + 'thread': self, + 'post_type__in': ('question', 'answer'), + 'deleted': False + } + + if user and askbot_settings.GROUPS_ENABLED: + #get post with groups shared with having at least + #one of the user groups + #of those posts return the latest revision + posts_filter['groups__in'] = user.get_groups() + + posts = Post.objects.filter(**posts_filter) + post_ids = list(posts.values_list('id', flat=True)) + + revs = PostRevision.objects.filter( + post__id__in=post_ids, + revision__gt=0 + ) + try: + return revs.order_by('-id')[0] + except IndexError: + return None def get_sharing_info(self, visitor=None): """returns a dictionary with abbreviated thread sharing info: @@ -1036,6 +1057,9 @@ class Thread(models.Model): def invalidate_cached_thread_content_fragment(self): cache.cache.delete(self.SUMMARY_CACHE_KEY_TPL % (self.id, get_language())) + def get_summary_cache_key(self, lang, group_id=0): + return self.SUMMARY_CACHE_KEY_TPL % (self.id, lang, group_id) + def get_post_data_cache_key(self, sort_method = None): return 'thread-data-%s-%s' % (self.id, sort_method) @@ -1692,7 +1716,7 @@ class Thread(models.Model): return last_updated_at, last_updated_by - def get_summary_html(self, search_state=None, visitor = None): + def get_summary_html(self, search_state=None, visitor=None): html = self.get_cached_summary_html(visitor) if not html: html = self.update_summary_html(visitor) diff --git a/askbot/models/user.py b/askbot/models/user.py index 5501f30f..e965cb05 100644 --- a/askbot/models/user.py +++ b/askbot/models/user.py @@ -449,7 +449,7 @@ class GroupQuerySet(models.query.QuerySet): user=user ).exclude(id=global_group.id) else: - return self.filter(user = user) + return self.filter(user=user) def get_by_name(self, group_name = None): from askbot.models.tag import clean_group_name#todo - delete this diff --git a/askbot/templates/question_edit.html b/askbot/templates/question_edit.html index f6de6bdf..d7dbb5f2 100644 --- a/askbot/templates/question_edit.html +++ b/askbot/templates/question_edit.html @@ -46,7 +46,7 @@ {% if settings.ENABLE_EMAIL_ALERTS %} {{ macros.checkbox_in_div(form.suppress_email) }} {% endif %} - {% if form.can_stay_anonymous() %} + {% if form.can_edit_anonymously() %} {{ macros.checkbox_in_div(form.reveal_identity) }} {% endif %} {% if settings.GROUPS_ENABLED and diff --git a/askbot/templates/widgets/question_summary.html b/askbot/templates/widgets/question_summary.html index 62c97a53..352ad4b2 100644 --- a/askbot/templates/widgets/question_summary.html +++ b/askbot/templates/widgets/question_summary.html @@ -7,7 +7,7 @@ {% else -%} some-views {%- endif -%}"> - {{thread.view_count|humanize_counter}} + {{ thread.view_count|humanize_counter }}
{% trans cnt=thread.view_count %}view{% pluralize %}views{% endtrans %}
@@ -41,10 +41,11 @@ {% trans cnt=question.score %}vote{% pluralize %}votes{% endtrans %} -
+
{{ timeago(thread.last_activity_at) }} - {% if question.is_anonymous %}{# todo: here we need to look at the anonymity of the last edit instead #} + {% set latest_revision = thread.get_latest_revision(user=visitor) %} + {% if latest_revision and latest_revision.is_anonymous %} {{ thread.last_activity_by.get_anonymous_name() }} {% else %} {{thread.last_activity_by.username|escape}} {{ user_country_flag(thread.last_activity_by) }} diff --git a/askbot/tests/form_tests.py b/askbot/tests/form_tests.py index 9a82b42a..9db7e627 100644 --- a/askbot/tests/form_tests.py +++ b/askbot/tests/form_tests.py @@ -159,11 +159,7 @@ class EditQuestionAnonymouslyFormTests(AskbotTestCase): (0, 1, 1, 0, False), (0, 1, 1, 1, False),#all up to this point are False (1, 0, 0, 0, False), - (1, 0, 0, 1, 'error'),#not owner - (1, 0, 1, 0, 'error'),#rules changed either reload page or check box - (1, 0, 1, 1, True),#rules changed - say yes here (1, 1, 0, 0, False), - (1, 1, 0, 1, 'error'), (1, 1, 1, 0, False), (1, 1, 1, 1, True), ) @@ -213,7 +209,7 @@ class EditQuestionAnonymouslyFormTests(AskbotTestCase): self.setup_data(*(entry[:4])) if self.form.is_valid(): - result = self.form.cleaned_data['reveal_identity'] + result = self.form.cleaned_data.get('reveal_identity', False) else: result = 'error' diff --git a/askbot/views/writers.py b/askbot/views/writers.py index 55c11ee9..42477d07 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -274,16 +274,16 @@ def ask(request):#view used to ask a new question else: request.session.flush() - session_key = request.session.session_key + session_key=request.session.session_key models.AnonymousQuestion.objects.create( - session_key = session_key, - title = title, - tagnames = tagnames, - wiki = wiki, - is_anonymous = ask_anonymously, - text = text, - added_at = timestamp, - ip_addr = request.META.get('REMOTE_ADDR'), + session_key=session_key, + title=title, + tagnames=tagnames, + wiki=wiki, + is_anonymous=ask_anonymously, + text=text, + added_at=timestamp, + ip_addr=request.META.get('REMOTE_ADDR'), ) return HttpResponseRedirect(url_utils.get_login_url()) @@ -439,10 +439,11 @@ def edit_question(request, id): revision_form = forms.RevisionForm(question, revision) if form.is_valid(): if form.has_changed(): - if form.cleaned_data['reveal_identity']: + + if form.can_edit_anonymously() and form.cleaned_data['reveal_identity']: question.thread.remove_author_anonymity() + question.is_anonymous = False - is_anon_edit = form.cleaned_data['stay_anonymous'] is_wiki = form.cleaned_data.get('wiki', question.wiki) post_privately = form.cleaned_data['post_privately'] suppress_email = form.cleaned_data['suppress_email'] @@ -453,11 +454,11 @@ def edit_question(request, id): question=question, title=form.cleaned_data['title'], body_text=form.cleaned_data['text'], - revision_comment = form.cleaned_data['summary'], - tags = form.cleaned_data['tags'], - wiki = is_wiki, - edit_anonymously = is_anon_edit, - is_private = post_privately, + revision_comment=form.cleaned_data['summary'], + tags=form.cleaned_data['tags'], + wiki=is_wiki, + edit_anonymously=form.cleaned_data['edit_anonymously'], + is_private=post_privately, suppress_email=suppress_email, ip_addr=request.META.get('REMOTE_ADDR') ) -- cgit v1.2.3-1-g7c22