From d0c5ac2cb8bee88ff425ecbb3db3921c4e83ea18 Mon Sep 17 00:00:00 2001
From: hernani
Date: Tue, 1 Jun 2010 23:35:25 +0000
Subject: [PATCH] Converts all instant notifications to the new style emails.
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@354 0cfe37f9-358a-4d5e-be75-b63607b5c754
---
forum/models/action.py | 2 +
forum/settings/email.py | 4 +
.../notifications/answeraccepted.html | 44 ++++++-----
.../default/templates/notifications/base.html | 6 +-
.../templates/notifications/newanswer.html | 59 +++++++++------
.../templates/notifications/newcomment.html | 75 +++++++++++--------
.../templates/notifications/newmember.html | 40 ++++++----
.../templates/notifications/newquestion.html | 13 ++--
forum/subscriptions.py | 66 ++++------------
forum/templatetags/extra_tags.py | 6 +-
10 files changed, 170 insertions(+), 145 deletions(-)
diff --git a/forum/models/action.py b/forum/models/action.py
index 7f9a7c1..07050a8 100644
--- a/forum/models/action.py
+++ b/forum/models/action.py
@@ -181,7 +181,9 @@ def trigger_hooks_threaded(action, hooks, new):
try:
hook(action=action, new=new)
except Exception, e:
+ import traceback
logging.error("Error in %s hook: %s" % (cls.__name__, str(e)))
+ logging.error(traceback.format_exc())
class ActionProxyMetaClass(BaseMetaClass):
types = {}
diff --git a/forum/settings/email.py b/forum/settings/email.py
index d1a46c4..a4e18ed 100644
--- a/forum/settings/email.py
+++ b/forum/settings/email.py
@@ -40,4 +40,8 @@ label = _("Email subject prefix"),
help_text = _("Every email sent through your website will have the subject prefixed by this string. It's usually a good idea to have such a prefix so your users can easilly set up a filter on theyr email clients."),
required=False))
+EMAIL_CAN_SPAM = Setting(u'EMAIL_CAN_SPAM', '', EMAIL_SET, dict(
+label = _("Email Can Spam"),
+help_text = "Email Can Spam, usually the phisical address of the organization running the website. See http://en.wikipedia.org/wiki/CAN-SPAM_Act_of_2003 for more info."))
+
EMAIL_DIGEST_CONTROL = Setting('EMAIL_DIGEST_CONTROL', None)
diff --git a/forum/skins/default/templates/notifications/answeraccepted.html b/forum/skins/default/templates/notifications/answeraccepted.html
index 6c34040..367a67f 100644
--- a/forum/skins/default/templates/notifications/answeraccepted.html
+++ b/forum/skins/default/templates/notifications/answeraccepted.html
@@ -1,23 +1,33 @@
-{% extends "email_base.html" %}
-{% load i18n %}
-{% load extra_tags %}
+{% load i18n extra_tags email_tags %}
-{% block content %}
- {% var accepted_by = answer.nstate.accepted.by.username %}
- {% var answer_author = answer.author.username %}
- {% var app_url = settings.APP_URL %}
- {% var question_url = question.get_absolute_url %}
- {% var question_title = question.title %}
+{% declare %}
+ prefix = settings.EMAIL_SUBJECT_PREFIX
+ app_name = settings.APP_SHORT_NAME
+ app_url = settings.APP_URL
+ answer_author = answer.author.username
+ question = answer.question
+ question_url = question.get_absolute_url()
+ question_title = question.title
+ accepted_by = answer.nstate.accepted.by.username
+{% enddeclare %}
- {% trans "Hello" %} {% user_var username %},
+{% email %}
+ {% subject %}{% blocktrans %}{{ prefix }} New answer to {{ question_title }}{% endblocktrans %}{% endsubject %}
-
+ {% htmlcontent notifications/base.html %}
+
+ {% blocktrans %}
+ {{ accepted_by }} has just accepted {{ answer_author }}'s answer on his question
+ {{ question_title }}.
+ {% endblocktrans %}
+
+ {% endhtmlcontent %}
+
+ {% textcontent notifications/base_text.html %}
{% blocktrans %}
- Just to let you know that {{ accepted_by }} has just accepted {{ answer_author }}'s answer on his question
- {{ question_title }}:
+ {{ accepted_by }} has just accepted {{ answer_author }}'s answer on his question
+ "{{ question_title }}".
{% endblocktrans %}
-
+ {% endtextcontent %}
- {% blocktrans %}Sincerely,
- Forum Administrator{% endblocktrans %}
-{% endblock %}
+{% endemail %}
diff --git a/forum/skins/default/templates/notifications/base.html b/forum/skins/default/templates/notifications/base.html
index 6879bc2..2b0cc05 100644
--- a/forum/skins/default/templates/notifications/base.html
+++ b/forum/skins/default/templates/notifications/base.html
@@ -1,13 +1,11 @@
{% load extra_filters extra_tags i18n email_tags %}
{% declare %}
- logo_location = "http://meta.osqa.net/upfiles/osqa_logo.png"
p_style = "color:#333333;font-family:'helvetica neue', arial, Helvetica, sans-serif;line-height:18px;font-size:14px;margin-top:10px;"
a_style = "text-decoration:none;color:#3060a8;font-weight:bold;"
hr_style = "color:#ccc;border:0;height:1px;background-color:#ccc;margin-bottom:20px;"
small_style = "color:#333333;font-family:'Lucida Grande', Trebuchet, Helvetica, sans-serif;font-size:12px;"
table_style = "border:20px #e5ebf8 solid;margin:10px auto 10px auto;width:750px;text-align:left;"
- postal_address = "DZone, Inc. 140 Preston Executive Drive, Cary NC 27513, USA"
{% enddeclare %}
@@ -16,7 +14,7 @@
-
+
{% trans "Hello" %} {{ recipient.username }},
{% block content %}
@@ -26,7 +24,7 @@
here.
-{{ postal_address }}
+{{ settings.EMAIL_CAN_SPAM }}
|
diff --git a/forum/skins/default/templates/notifications/newanswer.html b/forum/skins/default/templates/notifications/newanswer.html
index 425f404..50fe504 100644
--- a/forum/skins/default/templates/notifications/newanswer.html
+++ b/forum/skins/default/templates/notifications/newanswer.html
@@ -1,27 +1,44 @@
-{% extends "email_base.html" %}
-{% load i18n %}
-{% load extra_tags %}
+{% load i18n extra_tags email_tags %}
-{% block content %}
- {% trans "Hello" %} {% user_var username %},
+{% declare %}
+ prefix = settings.EMAIL_SUBJECT_PREFIX
+ app_name = settings.APP_SHORT_NAME
+ app_url = settings.APP_URL
+ answer_author = answer.author.username
+ question = answer.question
+ question_url = question.get_absolute_url()
+ question_title = question.title
+{% enddeclare %}
-
- {% blocktrans with answer.author.username as author_name and settings.APP_SHORT_NAME as app_title and settings.APP_URL as app_url and question.get_absolute_url as question_url and question.title as question_title %}
- {{ author_name }} has just posted a new answer on {{ app_title }} to the question
- {{ question_title }}":
- {% endblocktrans %}
-
+{% email %}
+ {% subject %}{% blocktrans %}{{ prefix }} New answer to {{ question_title }}{% endblocktrans %}{% endsubject %}
+
+ {% htmlcontent notifications/base.html %}
+
+ {% blocktrans %}
+ {{ answer_author }} has just posted a new answer on {{ app_name }} to the question
+ {{ question_title }}:
+ {% endblocktrans %}
+
-
+
{{ answer.html|safe }}
-
+
+
+ {% trans "Don't forget to come over and cast your vote." %}
+ {% endhtmlcontent %}
+
+ {% textcontent notifications/base_text.html %}
+ {% blocktrans %}
+ {{ answer_author }} has just posted a new answer on {{ app_name }} to the question
+ "{{ question_title }}":
+ {% endblocktrans %}
+
+
+ {{ answer.body|safe }}
+
+ {% trans "Don't forget to come over and cast your vote." %}
+ {% endtextcontent %}
-
- {% blocktrans %}
- Don't forget to come over and cast your vote.
- {% endblocktrans %}
-
+{% endemail %}
- {% blocktrans %}Sincerely,
- Forum Administrator{% endblocktrans %}
-{% endblock %}
diff --git a/forum/skins/default/templates/notifications/newcomment.html b/forum/skins/default/templates/notifications/newcomment.html
index 30e3c65..6d59f1b 100644
--- a/forum/skins/default/templates/notifications/newcomment.html
+++ b/forum/skins/default/templates/notifications/newcomment.html
@@ -1,34 +1,47 @@
-{% extends "email_base.html" %}
-{% load i18n %}
-{% load extra_tags %}
-
-{% block content %}
- {% trans "Hello" %} {% user_var username %},
-
-
- {% blocktrans with comment.user.username as author_name %}
- {{ author_name }} has just posted a comment on
- {% endblocktrans %}
-
- {% if post.question %}
- {% blocktrans with settings.APP_URL as app_url and post.author.username as poster_name and post.author.get_profile_url as poster_url%}
- the answer posted by {{ poster_name }} to
- {% endblocktrans %}
- {% endif %}
-
- {% blocktrans with question.title as question_title and settings.APP_URL as app_url and question.get_absolute_url as question_url %}
- the question {{ question_title }}
- {% endblocktrans %}
-
-
-
+{% load i18n extra_tags email_tags %}
+
+{% declare %}
+ prefix = settings.EMAIL_SUBJECT_PREFIX
+ app_name = settings.APP_SHORT_NAME
+ app_url = settings.APP_URL
+ post = comment.parent
+ question = post.question and post.question or post
+ post_author = post.author.username
+ comment_author = comment.author
+ question_url = question.get_absolute_url()
+ question_title = question.title
+{% enddeclare %}
+
+{% email %}
+ {% subject %}{% blocktrans %}{{ prefix }} New comment on {{ question_title }}{% endblocktrans %}{% endsubject %}
+
+ {% htmlcontent notifications/base.html %}
+
+ {% blocktrans %}{{ comment_author }} has just posted a comment on {% endblocktrans %}
+ {% ifnotequal post question %}
+ {% blocktrans %}the answer posted by {{ post_author }} to {% endblocktrans %}
+ {% endifnotequal %}
+ {% blocktrans %}the question {{ question_title }}{% endblocktrans %}
+
+
+
{{ comment.comment }}
-
+
+
+ {% trans "Don't forget to come over and cast your vote." %}
+ {% endhtmlcontent %}
+
+ {% textcontent notifications/base_text.html %}
+ {% blocktrans %}{{ comment_author }} has just posted a comment on {% endblocktrans %}
+ {% ifnotequal post question %}
+ {% blocktrans %}the answer posted by {{ post_author }} to {% endblocktrans %}
+ {% endifnotequal %}
+ {% blocktrans %}the question "{{ question_title }}"{% endblocktrans %}
+
+
+ {{ comment.body }}
- {% blocktrans %}
- Don't forget to come over and cast your vote.
- {% endblocktrans %}
+ {% trans "Don't forget to come over and cast your vote." %}
+ {% endtextcontent %}
- {% blocktrans %}Sincerely,
- Forum Administrator{% endblocktrans %}
-{% endblock %}
+{% endemail %}
diff --git a/forum/skins/default/templates/notifications/newmember.html b/forum/skins/default/templates/notifications/newmember.html
index 149b9a9..528c3ac 100644
--- a/forum/skins/default/templates/notifications/newmember.html
+++ b/forum/skins/default/templates/notifications/newmember.html
@@ -1,17 +1,31 @@
-{% extends "email_base.html" %}
-{% load i18n %}
-{% load extra_tags %}
+{% load i18n extra_tags email_tags %}
-{% block content %}
- {% trans "Hello" %} {% user_var username %},
+{% declare %}
+ prefix = settings.EMAIL_SUBJECT_PREFIX
+ app_name = settings.APP_SHORT_NAME
+ app_url = settings.APP_URL
+ newmember_name = newmember.username
+ newmember_url = newmember.get_profile_url
+{% enddeclare %}
-
- {% blocktrans with newmember.username as newmember_name and settings.APP_SHORT_NAME as app_title and settings.APP_URL as app_url and newmember.get_profile_url as newmember_url %}
- {{ newmember_name }} has just joined {{ app_title }}. You can visit {{ newmember_name }}'s profile using the following link:
- {{ newmember_name }} profile
+{% email %}
+ {% subject %}{% blocktrans %}{{ newmember_name }} is a new member on {{ app_name }}{% endblocktrans %}{% endsubject %}
+
+ {% htmlcontent notifications/base.html %}
+
+ {% blocktrans %}
+ {{ newmember_name }} has just joined {{ app_name }}. You can visit {{ newmember_name }}'s profile using the following link:
+ {{ newmember_name }} profile
+ {% endblocktrans %}
+
+ {% endhtmlcontent %}
+
+ {% textcontent notifications/base_text.html %}
+ {% blocktrans %}
+ {{ newmember_name }} has just joined {{ app_name }}. You can visit {{ newmember_name }}'s profile using the following url:
+ {{ app_url }}{{ newmember_url }}
{% endblocktrans %}
-
+ {% endtextcontent %}
+
+{% endemail %}
- {% blocktrans %}Sincerely,
- Forum Administrator{% endblocktrans %}
-{% endblock %}
diff --git a/forum/skins/default/templates/notifications/newquestion.html b/forum/skins/default/templates/notifications/newquestion.html
index 6615831..d9987e6 100644
--- a/forum/skins/default/templates/notifications/newquestion.html
+++ b/forum/skins/default/templates/notifications/newquestion.html
@@ -29,14 +29,15 @@
{% endhtmlcontent %}
{% textcontent notifications/base_text.html %}
-{% blocktrans %}
-{{ question_author }} has just posted a new question on {{ app_name }}, with title
-"{{ question_title }}" and tagged {{ question_tags }}:
-{% endblocktrans %}
+ {% blocktrans %}
+ {{ question_author }} has just posted a new question on {{ app_name }}, with title
+ "{{ question_title }}" and tagged {{ question_tags }}:
+ {% endblocktrans %}
-{{ question.body|safe }}
+ {{ question.body|safe }}
-{% trans "Don't forget to come over and cast your vote." %}
+ {% trans "Don't forget to come over and cast your vote." %}
{% endtextcontent %}
+
{% endemail %}
diff --git a/forum/subscriptions.py b/forum/subscriptions.py
index c808ce9..b361823 100644
--- a/forum/subscriptions.py
+++ b/forum/subscriptions.py
@@ -15,12 +15,6 @@ def create_subscription_if_not_exists(question, user):
subscription = QuestionSubscription(question=question, user=user)
subscription.save()
-def apply_default_filters(queryset, excluded_id):
- return queryset.values('email', 'username').exclude(id=excluded_id)
-
-def create_recipients_dict(usr_list):
- return [(s['username'], s['email'], {'username': s['username']}) for s in usr_list]
-
def question_posted(action, new):
question = action.node
@@ -31,15 +25,8 @@ def question_posted(action, new):
Q(tag_selections__reason='good'))
).exclude(id=question.author.id).distinct()
- #recipients = create_recipients_dict(subscribers)
-
send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
- #send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),
- # recipients, "notifications/newquestion.html", {
- # 'question': question,
- #})
-
if question.author.subscription_settings.questions_asked:
subscription = QuestionSubscription(question=question, user=question.author)
subscription.save()
@@ -60,18 +47,13 @@ def answer_posted(action, new):
answer = action.node
question = answer.question
- subscribers = question.subscribers.values('email', 'username').filter(
+ subscribers = question.subscribers.filter(
subscription_settings__enable_notifications=True,
subscription_settings__notify_answers=True,
subscription_settings__subscribed_questions='i'
).exclude(id=answer.author.id).distinct()
- recipients = create_recipients_dict(subscribers)
- send_email(settings.EMAIL_SUBJECT_PREFIX + _("New answer to '%(question_title)s'") % dict(question_title=question.title),
- recipients, "notifications/newanswer.html", {
- 'question': question,
- 'answer': answer
- }, threaded=False)
+ send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
if answer.author.subscription_settings.questions_answered:
create_subscription_if_not_exists(question, answer.author)
@@ -81,37 +63,27 @@ AnswerAction.hook(answer_posted)
def comment_posted(action, new):
comment = action.node
- post = comment.content_object
+ post = comment.parent
if post.__class__ == Question:
question = post
else:
question = post.question
- subscribers = question.subscribers.values('email', 'username')
-
q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
- #inreply = re.search('@\w+', comment.comment)
- #if inreply is not None:
- # q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
- # username__istartswith=inreply.group(0)[1:],
- ## comments__object_id=post.id,
- # comments__content_type=ContentType.objects.get_for_model(post.__class__)
- # )
+ inreply = re.search('@\w+', comment.comment)
+ if inreply is not None:
+ q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
+ username__istartswith=inreply.group(0)[1:],
+ nodes__parent=post, nodes__node_type="comment")
- subscribers = subscribers.filter(
+ subscribers = question.subscribers.filter(
q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
).exclude(id=comment.user.id).distinct()
- recipients = create_recipients_dict(subscribers)
- send_email(settings.EMAIL_SUBJECT_PREFIX + _("New comment on %(question_title)s") % dict(question_title=question.title),
- recipients, "notifications/newcomment.html", {
- 'comment': comment,
- 'post': post,
- 'question': question,
- }, threaded=False)
+ send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
if comment.user.subscription_settings.questions_commented:
create_subscription_if_not_exists(question, comment.user)
@@ -122,34 +94,24 @@ CommentAction.hook(comment_posted)
def answer_accepted(action, new):
question = action.node.question
- subscribers = question.subscribers.values('email', 'username').filter(
+ subscribers = question.subscribers.filter(
subscription_settings__enable_notifications=True,
subscription_settings__notify_accepted=True,
subscription_settings__subscribed_questions='i'
).exclude(id=action.node.nstate.accepted.by.id).distinct()
- recipients = create_recipients_dict(subscribers)
- send_email(settings.EMAIL_SUBJECT_PREFIX + _("An answer to '%(question_title)s' was accepted") % dict(question_title=question.title),
- recipients, "notifications/answeraccepted.html", {
- 'question': question,
- 'answer': action.node
- }, threaded=False)
+ send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
AcceptAnswerAction.hook(answer_accepted)
def member_joined(action, new):
- subscribers = User.objects.values('email', 'username').filter(
+ subscribers = User.objects.filter(
subscription_settings__enable_notifications=True,
subscription_settings__member_joins='i'
).exclude(id=action.user.id).distinct()
- recipients = create_recipients_dict(subscribers)
-
- send_email(settings.EMAIL_SUBJECT_PREFIX + _("%(username)s is a new member on %(app_name)s") % dict(username=action.user.username, app_name=settings.APP_SHORT_NAME),
- recipients, "notifications/newmember.html", {
- 'newmember': action.user,
- }, threaded=False)
+ send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
UserJoinsAction.hook(member_joined)
diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py
index 9499a3f..7600bf4 100644
--- a/forum/templatetags/extra_tags.py
+++ b/forum/templatetags/extra_tags.py
@@ -427,7 +427,11 @@ class DeclareNode(template.Node):
d['os'] = os
for c in clist:
d.update(c)
- context[m.group(1).strip()] = eval(m.group(3).strip(), d)
+ try:
+ context[m.group(1).strip()] = eval(m.group(3).strip(), d)
+ except Exception, e:
+ logging.error("Error in declare tag, when evaluating: %s" % m.group(3).strip())
+ raise
return ''
@register.tag(name='declare')
--
2.39.5