From 939c7b1772d7d106f69f6d34adc2e11ef1595277 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Mon, 8 Sep 2014 13:59:47 +0700 Subject: email notification is sent when moderated post is approved --- askbot/conf/email.py | 6 +-- askbot/models/__init__.py | 33 ++++++++------ askbot/models/post.py | 50 +++++++++++----------- askbot/models/question.py | 19 ++++---- askbot/tasks.py | 49 +++++++++++---------- .../email/notify_author_about_approved_post.html | 22 ++++++---- 6 files changed, 97 insertions(+), 82 deletions(-) diff --git a/askbot/conf/email.py b/askbot/conf/email.py index 088e0590..cbdb2ba1 100644 --- a/askbot/conf/email.py +++ b/askbot/conf/email.py @@ -308,11 +308,11 @@ settings.register( livesettings.StringValue( EMAIL, 'SELF_NOTIFY_EMAILED_POST_AUTHOR_WHEN', - description = _( + description=_( 'Emailed post: when to notify author about publishing' ), - choices = const.SELF_NOTIFY_EMAILED_POST_AUTHOR_WHEN_CHOICES, - default = const.NEVER + choices=const.SELF_NOTIFY_EMAILED_POST_AUTHOR_WHEN_CHOICES, + default=const.NEVER ) ) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 3dae64b8..0d7c2fa7 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -2792,8 +2792,23 @@ def user_approve_post_revision(user, post_revision, timestamp = None): post.thread.answer_count += 1 post.thread.save() + + post.approved = True - post.save() + post.text = post_revision.text + + post_is_new = (post.revisions.count() == 1) + parse_results = post.parse_and_save(author=post_revision.author) + signals.post_updated.send( + post=post, + updated_by=post_revision.author, + newly_mentioned_users=parse_results['newly_mentioned_users'], + #suppress_email=suppress_email, + timestamp=timestamp, + created=post_is_new, + diff=parse_results['diff'], + sender=post.__class__ + ) if post_revision.post.post_type == 'question': thread = post.thread @@ -2804,8 +2819,10 @@ def user_approve_post_revision(user, post_revision, timestamp = None): #send the signal of published revision signals.post_revision_published.send( - None, revision = post_revision, was_approved = True - ) + None, + revision=post_revision, + was_approved=True + ) @auto_now_timestamp def flag_post( @@ -3374,9 +3391,7 @@ def get_reply_to_addresses(user, post): return primary_addr, secondary_addr -def notify_author_of_published_revision( - revision = None, was_approved = None, **kwargs -): +def notify_author_of_published_revision(revision=None, was_approved=False, **kwargs): """notifies author about approved post revision, assumes that we have the very first revision """ @@ -3410,12 +3425,6 @@ def record_post_update_activity( this handler will set notifications about the post """ - if post.needs_moderation(): - #do not give notifications yet - #todo: it is possible here to trigger - #moderation email alerts - return - assert(timestamp != None) assert(updated_by != None) if newly_mentioned_users is None: diff --git a/askbot/models/post.py b/askbot/models/post.py index cd45387f..d155667a 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -235,7 +235,7 @@ class PostManager(BaseQuerySetManager): parse_results = post.parse_and_save(author=author, is_private=is_private) - post.add_revision( + revision = post.add_revision( author=author, revised_at=added_at, text=text, @@ -244,16 +244,17 @@ class PostManager(BaseQuerySetManager): ip_addr=ip_addr ) - from askbot.models import signals - signals.post_updated.send( - post=post, - updated_by=author, - newly_mentioned_users=parse_results['newly_mentioned_users'], - timestamp=added_at, - created=True, - diff=parse_results['diff'], - sender=post.__class__ - ) + if revision.revision > 0: + from askbot.models import signals + signals.post_updated.send( + post=post, + updated_by=author, + newly_mentioned_users=parse_results['newly_mentioned_users'], + timestamp=added_at, + created=True, + diff=parse_results['diff'], + sender=post.__class__ + ) return post @@ -2376,24 +2377,21 @@ class PostRevision(models.Model): activity.save() activity.add_recipients(self.post.get_moderators()) - def should_notify_author_about_publishing(self, was_approved = False): + def should_notify_author_about_publishing(self, was_approved=False): """True if author should get email about making own post""" - if self.by_email: - schedule = askbot_settings.SELF_NOTIFY_EMAILED_POST_AUTHOR_WHEN - if schedule == const.NEVER: - return False - elif schedule == const.FOR_FIRST_REVISION: - return self.revision == 1 - elif schedule == const.FOR_ANY_REVISION: - return True - else: - raise ValueError() - else: - #logic not implemented yet - #the ``was_approved`` argument will be used here - #schedule = askbot_settings.SELF_NOTIFY_WEB_POST_AUTHOR_WHEN + if was_approved and self.by_email == False: return False + schedule = askbot_settings.SELF_NOTIFY_EMAILED_POST_AUTHOR_WHEN + if schedule == const.NEVER: + return False + elif schedule == const.FOR_FIRST_REVISION: + return self.revision == 1 + elif schedule == const.FOR_ANY_REVISION: + return True + else: + raise ValueError() + def __unicode__(self): return u'%s - revision %s of %s' % (self.post.post_type, self.revision, self.title) diff --git a/askbot/models/question.py b/askbot/models/question.py index 039c37f2..f851e33c 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -224,15 +224,16 @@ class ThreadManager(BaseQuerySetManager): #todo: this is handled in signal because models for posts #are too spread out - signals.post_updated.send( - post=question, - updated_by=author, - newly_mentioned_users=parse_results['newly_mentioned_users'], - timestamp=added_at, - created=True, - diff=parse_results['diff'], - sender=question.__class__ - ) + if revision.revision > 0: + signals.post_updated.send( + post=question, + updated_by=author, + newly_mentioned_users=parse_results['newly_mentioned_users'], + timestamp=added_at, + created=True, + diff=parse_results['diff'], + sender=question.__class__ + ) return thread diff --git a/askbot/tasks.py b/askbot/tasks.py index ec3e405b..5b82316c 100644 --- a/askbot/tasks.py +++ b/askbot/tasks.py @@ -75,6 +75,13 @@ def notify_author_of_published_revision_celery_task(revision): #todo: move this to ``askbot.mail`` module #for answerable email only for now, because #we don't yet have the template for the read-only notification + + data = { + 'site_name': askbot_settings.APP_SHORT_NAME, + 'post': revision.post + } + headers = None + if askbot_settings.REPLY_BY_EMAIL: #generate two reply codes (one for edit and one for addition) #to format an answerable email or not answerable email @@ -101,30 +108,26 @@ def notify_author_of_published_revision_celery_task(revision): prompt = _('To add to your post EDIT ABOVE THIS LINE') reply_separator_line = const.SIMPLE_REPLY_SEPARATOR_TEMPLATE % prompt - data = { - 'site_name': askbot_settings.APP_SHORT_NAME, - 'post': revision.post, - 'author_email_signature': revision.author.email_signature, - 'replace_content_address': replace_content_address, - 'reply_separator_line': reply_separator_line, - 'mailto_link_subject': mailto_link_subject, - 'reply_code': reply_code - } - - #load the template - activate_language(revision.post.language_code) - template = get_template('email/notify_author_about_approved_post.html') - #todo: possibly add headers to organize messages in threads + data['reply_code'] = reply_code + data['author_email_signature'] = revision.author.email_signature + data['replace_content_address'] = replace_content_address + data['reply_separator_line'] = reply_separator_line + data['mailto_link_subject'] = mailto_link_subject headers = {'Reply-To': append_content_address} - #send the message - mail.send_mail( - subject_line = _('Your post at %(site_name)s is now published') % data, - body_text = template.render(Context(data)), - recipient_list = [revision.author.email,], - related_object = revision, - activity_type = const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT, - headers = headers - ) + + #load the template + activate_language(revision.post.language_code) + template = get_template('email/notify_author_about_approved_post.html') + #todo: possibly add headers to organize messages in threads + #send the message + mail.send_mail( + subject_line = _('Your post at %(site_name)s is now published') % data, + body_text = template.render(Context(data)), + recipient_list = [revision.author.email,], + related_object = revision, + activity_type = const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT, + headers = headers + ) @task(ignore_result = True) def record_post_update_celery_task( diff --git a/askbot/templates/email/notify_author_about_approved_post.html b/askbot/templates/email/notify_author_about_approved_post.html index 6007f06e..01901cd7 100644 --- a/askbot/templates/email/notify_author_about_approved_post.html +++ b/askbot/templates/email/notify_author_about_approved_post.html @@ -10,14 +10,16 @@ * reply_code (comma-separated list of emails to respond to this message) #} -{%block content %} -{{ reply_separator_line }} -

{% trans - post_text = post.text|safe_urlquote, - subject = mailto_link_subject|safe_urlquote, - author_email_signature = author_email_signature|safe_urlquote -%}If you would like to edit by email, please -click here{% endtrans %}

+{% block content %} +{% if email_code %} + {{ reply_separator_line }} +

{% trans + post_text = post.text|safe_urlquote, + subject = mailto_link_subject|safe_urlquote, + author_email_signature = author_email_signature|safe_urlquote + %}If you would like to edit by email, please + click here{% endtrans %}

+{% endif %}

{% trans %}Below is a copy of your post:{% endtrans %}

{% if post.post_type == 'question' %}

{{ post.thread.title }}

@@ -27,5 +29,7 @@ {%block footer %} {% include "email/footer.html" %} -

{{ email_code }}

{# important #} +{% if email_code %} +

{{ email_code }}

{# important #} +{% endif %} {% endblock %} -- cgit v1.2.3-1-g7c22