From 89644df19bb0050fe65dd0ae41c423ebc2697cea Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 8 Mar 2013 20:36:47 -0300 Subject: minor edit option for question and answer to suppress email alerts --- askbot/doc/source/changelog.rst | 2 +- askbot/forms.py | 14 +++- askbot/models/__init__.py | 146 ++++++++++++++++++++---------------- askbot/models/post.py | 32 ++++---- askbot/tasks.py | 12 +-- askbot/templates/answer_edit.html | 3 + askbot/templates/question_edit.html | 3 + askbot/views/writers.py | 24 ++++-- 8 files changed, 145 insertions(+), 91 deletions(-) diff --git a/askbot/doc/source/changelog.rst b/askbot/doc/source/changelog.rst index 888ad286..669c48d2 100644 --- a/askbot/doc/source/changelog.rst +++ b/askbot/doc/source/changelog.rst @@ -8,7 +8,7 @@ Development version Italian, Japanese (requires package textsearch_ja), Norwegian, Portugese, Romanian, Russian, Spanish, Swedish, Turkish. * repost answer as a comment under the previous (older) answer - +* minor edit option for question and answer, to suppress email alerts 0.7.48 (Jan 28, 2013) --------------------- diff --git a/askbot/forms.py b/askbot/forms.py index d6ee2bb1..5e9a7850 100644 --- a/askbot/forms.py +++ b/askbot/forms.py @@ -211,6 +211,13 @@ class LanguageField(forms.ChoiceField): super(LanguageField, self).__init__(*args, **kwargs) +class SuppressEmailField(forms.BooleanField): + def __init__(self): + super(SuppressEmailField, self).__init__() + self.required = False + self.label = _("minor edit (don't send alerts)") + + class DomainNameField(forms.CharField): """Field for Internet Domain Names todo: maybe there is a standard field for this? @@ -227,7 +234,7 @@ class DomainNameField(forms.CharField): class TitleField(forms.CharField): - """Fild receiving question title""" + """Field receiving question title""" def __init__(self, *args, **kwargs): super(TitleField, self).__init__(*args, **kwargs) self.required = kwargs.get('required', True) @@ -1193,6 +1200,7 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): label=_('reveal identity'), required=False, ) + suppress_email = SuppressEmailField() #todo: this is odd that this form takes question as an argument def __init__(self, *args, **kwargs): @@ -1310,6 +1318,7 @@ class EditQuestionForm(PostAsSomeoneForm, PostPrivatelyForm): class EditAnswerForm(PostAsSomeoneForm, PostPrivatelyForm): summary = SummaryField() wiki = WikiField() + suppress_email = SuppressEmailField() def __init__(self, answer, revision, *args, **kwargs): self.answer = answer @@ -1330,6 +1339,9 @@ class EditAnswerForm(PostAsSomeoneForm, PostPrivatelyForm): else: return False +class EditCommentForm(forms.Form): + comment_id = forms.IntegerField() + suppress_email = SuppressEmailField() class EditTagWikiForm(forms.Form): text = forms.CharField(required=False) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index c4f4454c..1c08cb0c 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -514,6 +514,8 @@ def _assert_user_can( if assertion fails, method raises exception.PermissionDenied with appropriate text as a payload """ + if general_error_message is None: + general_error_message = _('Sorry, this operation is not allowed') if blocked_error_message and user.is_blocked(): error_message = blocked_error_message elif post and owner_can and user == post.get_owner(): @@ -1678,9 +1680,10 @@ def user_post_question( def user_edit_comment( self, comment_post=None, - body_text = None, - timestamp = None, - by_email = False + body_text=None, + timestamp=None, + by_email=False, + suppress_email=False ): """apply edit to a comment, the method does not change the comments timestamp and no signals are sent @@ -1689,20 +1692,22 @@ def user_edit_comment( """ self.assert_can_edit_comment(comment_post) comment_post.apply_edit( - text = body_text, - edited_at = timestamp, - edited_by = self, - by_email = by_email + text=body_text, + edited_at=timestamp, + edited_by=self, + by_email=by_email, + suppress_email=suppress_email ) comment_post.thread.invalidate_cached_data() def user_edit_post(self, - post = None, - body_text = None, - revision_comment = None, - timestamp = None, - by_email = False, - is_private = False + post=None, + body_text=None, + revision_comment=None, + timestamp=None, + by_email=False, + is_private=False, + suppress_email=False, ): """a simple method that edits post body todo: unify it in the style of just a generic post @@ -1711,36 +1716,39 @@ def user_edit_post(self, """ if post.post_type == 'comment': self.edit_comment( - comment_post = post, - body_text = body_text, - by_email = by_email + comment_post=post, + body_text=body_text, + by_email=by_email, + suppress_email=suppress_email ) elif post.post_type == 'answer': self.edit_answer( - answer = post, - body_text = body_text, - timestamp = timestamp, - revision_comment = revision_comment, - by_email = by_email + answer=post, + body_text=body_text, + timestamp=timestamp, + revision_comment=revision_comment, + by_email=by_email, + suppress_email=suppress_email ) elif post.post_type == 'question': self.edit_question( - question = post, - body_text = body_text, - timestamp = timestamp, - revision_comment = revision_comment, - by_email = by_email, - is_private = is_private + question=post, + body_text=body_text, + timestamp=timestamp, + revision_comment=revision_comment, + by_email=by_email, + is_private=is_private, + suppress_email=suppress_email, ) elif post.post_type == 'tag_wiki': post.apply_edit( - edited_at = timestamp, - edited_by = self, - text = body_text, + edited_at=timestamp, + edited_by=self, + text=body_text, #todo: summary name clash in question and question revision - comment = revision_comment, - wiki = True, - by_email = False + comment=revision_comment, + wiki=True, + by_email=False ) else: raise NotImplementedError() @@ -1748,17 +1756,18 @@ def user_edit_post(self, @auto_now_timestamp def user_edit_question( self, - question = None, - title = None, - body_text = None, - revision_comment = None, - tags = None, - wiki = False, - edit_anonymously = False, - is_private = False, - timestamp = None, - force = False,#if True - bypass the assert - by_email = False + question=None, + title=None, + body_text=None, + revision_comment=None, + tags=None, + wiki=False, + edit_anonymously=False, + is_private=False, + timestamp=None, + force=False,#if True - bypass the assert + by_email=False, + suppress_email=False ): if force == False: self.assert_can_edit_question(question) @@ -1774,7 +1783,8 @@ def user_edit_question( wiki = wiki, edit_anonymously = edit_anonymously, is_private = is_private, - by_email = by_email + by_email = by_email, + suppress_email=suppress_email ) question.thread.invalidate_cached_data() @@ -1789,14 +1799,15 @@ def user_edit_question( @auto_now_timestamp def user_edit_answer( self, - answer = None, - body_text = None, - revision_comment = None, - wiki = False, - is_private = False, - timestamp = None, - force = False,#if True - bypass the assert - by_email = False + answer=None, + body_text=None, + revision_comment=None, + wiki=False, + is_private=False, + timestamp=None, + force=False,#if True - bypass the assert + by_email=False, + suppress_email=False, ): if force == False: self.assert_can_edit_answer(answer) @@ -1808,7 +1819,8 @@ def user_edit_answer( comment=revision_comment, wiki=wiki, is_private=is_private, - by_email=by_email + by_email=by_email, + suppress_email=suppress_email ) answer.thread.invalidate_cached_data() @@ -3178,11 +3190,12 @@ def calculate_gravatar_hash(instance, **kwargs): def record_post_update_activity( post, - newly_mentioned_users = None, - updated_by = None, - timestamp = None, - created = False, - diff = None, + newly_mentioned_users=None, + updated_by=None, + suppress_email=False, + timestamp=None, + created=False, + diff=None, **kwargs ): """called upon signal askbot.models.signals.post_updated @@ -3204,13 +3217,14 @@ def record_post_update_activity( from askbot import tasks tasks.record_post_update_celery_task.delay( - post_id = post.id, - post_content_type_id = ContentType.objects.get_for_model(post).id, - newly_mentioned_user_id_list = [u.id for u in newly_mentioned_users], - updated_by_id = updated_by.id, - timestamp = timestamp, - created = created, - diff = diff, + post_id=post.id, + post_content_type_id=ContentType.objects.get_for_model(post).id, + newly_mentioned_user_id_list=[u.id for u in newly_mentioned_users], + updated_by_id=updated_by.id, + suppress_email=suppress_email, + timestamp=timestamp, + created=created, + diff=diff, ) diff --git a/askbot/models/post.py b/askbot/models/post.py index 322a3759..65ea535d 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -651,6 +651,7 @@ class Post(models.Model): updated_by=None, notify_sets=None, activity_type=None, + suppress_email=False, timestamp=None, diff=None ): @@ -694,7 +695,7 @@ class Post(models.Model): user.update_response_counts() #shortcircuit if the email alerts are disabled - if askbot_settings.ENABLE_EMAIL_ALERTS == False: + if suppress_email == True or askbot_settings.ENABLE_EMAIL_ALERTS == False: return #todo: fix this temporary spam protection plug if askbot_settings.MIN_REP_TO_TRIGGER_EMAIL: @@ -1681,7 +1682,8 @@ class Post(models.Model): wiki=False, edit_anonymously=False, is_private=False, - by_email=False + by_email=False, + suppress_email=False ): if text is None: text = self.get_latest_revision().text @@ -1716,6 +1718,7 @@ class Post(models.Model): post=self, updated_by=edited_by, newly_mentioned_users=parse_results['newly_mentioned_users'], + suppress_email=suppress_email, timestamp=edited_at, created=False, diff=parse_results['diff'], @@ -1725,13 +1728,14 @@ class Post(models.Model): def _answer__apply_edit( self, - edited_at = None, - edited_by = None, - text = None, - comment = None, - wiki = False, - is_private = False, - by_email = False + edited_at=None, + edited_by=None, + text=None, + comment=None, + wiki=False, + is_private=False, + by_email=False, + suppress_email=False, ): ##it is important to do this before __apply_edit b/c of signals!!! @@ -1748,7 +1752,8 @@ class Post(models.Model): comment=comment, wiki=wiki, by_email=by_email, - is_private=is_private + is_private=is_private, + suppress_email=suppress_email ) if edited_at is None: @@ -1757,8 +1762,8 @@ class Post(models.Model): def _question__apply_edit(self, edited_at=None, edited_by=None, title=None,\ text=None, comment=None, tags=None, wiki=False,\ - edit_anonymously = False, is_private = False, - by_email = False + edit_anonymously=False, is_private=False,\ + by_email=False, suppress_email=False ): #todo: the thread editing should happen outside of this @@ -1799,7 +1804,8 @@ class Post(models.Model): wiki=wiki, edit_anonymously=edit_anonymously, is_private=is_private, - by_email=by_email + by_email=by_email, + suppress_email=suppress_email ) self.thread.set_last_activity(last_activity_at=edited_at, last_activity_by=edited_by) diff --git a/askbot/tasks.py b/askbot/tasks.py index 5df24a4c..92cd9a1d 100644 --- a/askbot/tasks.py +++ b/askbot/tasks.py @@ -98,11 +98,12 @@ def notify_author_of_published_revision_celery_task(revision): def record_post_update_celery_task( post_id, post_content_type_id, - newly_mentioned_user_id_list = None, - updated_by_id = None, - timestamp = None, - created = False, - diff = None, + newly_mentioned_user_id_list=None, + updated_by_id=None, + suppress_email=False, + timestamp=None, + created=False, + diff=None, ): #reconstitute objects from the database updated_by = User.objects.get(id=updated_by_id) @@ -124,6 +125,7 @@ def record_post_update_celery_task( updated_by=updated_by, notify_sets=notify_sets, activity_type=activity_type, + suppress_email=suppress_email, timestamp=timestamp, diff=diff ) diff --git a/askbot/templates/answer_edit.html b/askbot/templates/answer_edit.html index 887714cb..f80715ec 100644 --- a/askbot/templates/answer_edit.html +++ b/askbot/templates/answer_edit.html @@ -29,6 +29,9 @@ {% if settings.WIKI_ON and answer.wiki == False %} {{ macros.checkbox_in_div(form.wiki) }} {% endif %} + {% if settings.ENABLE_EMAIL_ALERTS %} + {{ macros.checkbox_in_div(form.suppress_email) }} + {% endif %} {% if settings.GROUPS_ENABLED and request.user.is_authenticated() and request.user.can_make_group_private_posts() diff --git a/askbot/templates/question_edit.html b/askbot/templates/question_edit.html index d8053e45..7cf1c143 100644 --- a/askbot/templates/question_edit.html +++ b/askbot/templates/question_edit.html @@ -35,6 +35,9 @@ {% if settings.WIKI_ON and question.wiki == False %} {{ macros.checkbox_in_div(form.wiki) }} {% endif %} + {% if settings.ENABLE_EMAIL_ALERTS %} + {{ macros.checkbox_in_div(form.suppress_email) }} + {% endif %} {% if form.can_stay_anonymous() %} {{ macros.checkbox_in_div(form.reveal_identity) }} {% endif %} diff --git a/askbot/views/writers.py b/askbot/views/writers.py index b9e637ad..ee10c1ab 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -424,6 +424,7 @@ def edit_question(request, id): 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'] user = form.get_post_user(request.user) @@ -435,7 +436,8 @@ def edit_question(request, id): tags = form.cleaned_data['tags'], wiki = is_wiki, edit_anonymously = is_anon_edit, - is_private = post_privately + is_private = post_privately, + suppress_email=suppress_email ) return HttpResponseRedirect(question.get_absolute_url()) else: @@ -510,13 +512,15 @@ def edit_answer(request, id): if form.is_valid(): if form.has_changed(): user = form.get_post_user(request.user) + suppress_email = form.cleaned_data['suppress_email'] + is_private = form.cleaned_data.get('post_privately', False) user.edit_answer( answer=answer, body_text=form.cleaned_data['text'], revision_comment=form.cleaned_data['summary'], wiki=form.cleaned_data.get('wiki', answer.wiki), - is_private=form.cleaned_data.get('post_privately', False) - #todo: add wiki field to form + is_private=is_private, + suppress_email=suppress_email ) return HttpResponseRedirect(answer.get_absolute_url()) else: @@ -679,10 +683,20 @@ def edit_comment(request): if request.user.is_anonymous(): raise exceptions.PermissionDenied(_('Sorry, anonymous users cannot edit comments')) - comment_id = int(request.POST['comment_id']) + form = forms.EditCommentForm(request.POST) + if form.is_valid() == False: + return HttpResponseBadRequest() + + comment_id = form.cleaned_data['comment_id'] + suppress_email = form.cleaned_data['suppress_email'] + comment_post = models.Post.objects.get(post_type='comment', id=comment_id) - request.user.edit_comment(comment_post=comment_post, body_text = request.POST['comment']) + request.user.edit_comment( + comment_post=comment_post, + body_text = request.POST['comment'], + suppress_email=suppress_email + ) is_deletable = template_filters.can_delete_comment(comment_post.author, comment_post) is_editable = template_filters.can_edit_comment(comment_post.author, comment_post) -- cgit v1.2.3-1-g7c22