diff options
-rw-r--r-- | askbot/mail/__init__.py | 46 | ||||
-rw-r--r-- | askbot/mail/messages.py | 48 | ||||
-rw-r--r-- | askbot/skins/default/templates/email/ask_for_signature.html | 11 |
3 files changed, 82 insertions, 23 deletions
diff --git a/askbot/mail/__init__.py b/askbot/mail/__init__.py index 2f8d7c79..299acf22 100644 --- a/askbot/mail/__init__.py +++ b/askbot/mail/__init__.py @@ -14,10 +14,9 @@ from django.template import Context from askbot import exceptions from askbot import const from askbot.conf import settings as askbot_settings +from askbot import models from askbot.utils import url_utils -from askbot.utils import html as html_utils from askbot.utils.file_utils import store_file -from askbot.skins.loaders import get_template #todo: maybe send_mail functions belong to models #or the future API def prefix_the_subject_line(subject): @@ -167,7 +166,9 @@ TAGS_INSTRUCTION_FOOTNOTE = _( the tags, use a semicolon or a comma, for example, [One tag; Other tag]</p>""" ) -def bounce_email(email, subject, reason = None, body_text = None): +def bounce_email( + email, subject, reason = None, body_text = None, reply_to = None +): """sends a bounce email at address ``email``, with the subject line ``subject``, accepts several reasons for the bounce: * ``'problem_posting'``, ``unknown_user`` and ``permission_denied`` @@ -224,10 +225,15 @@ def bounce_email(email, subject, reason = None, body_text = None): #print email #print subject #print error_message + headers = {} + if reply_to: + headers['Reply-To'] = reply_to + send_mail( recipient_list = (email,), subject_line = 'Re: ' + subject, - body_text = error_message + body_text = error_message, + headers = headers ) def extract_reply(text): @@ -316,7 +322,9 @@ def process_emailed_question( #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import User + from askbot.mail import messages + reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { @@ -332,26 +340,17 @@ def process_emailed_question( ) if user.email_isvalid == False: - raise PermissionDenied('Lacking email signature') + reply_to = models.ReplyAddress.objects.create_new( + user = user, + reply_action = 'validate_email' + ).as_email_address() + raise PermissionDenied( + messages.ask_for_signature(user, footer_code = reply_to), + reply_to = reply_to.as_email_address() + ) if user.can_post_by_email() == False: - #todo: factor this code out - template = get_template('email/insufficient_rep_to_post_by_email.html') - min_rep = askbot_settings.MIN_REP_TO_POST_BY_EMAIL - min_upvotes = 1 + \ - (min_rep/askbot_settings.REP_GAIN_FOR_RECEIVING_UPVOTE) - site_link = html_utils.site_link( - 'ask', - askbot_settings.APP_SHORT_NAME - ) - data = { - 'username': user.username, - 'site_name': askbot_settings.APP_SHORT_NAME, - 'site_link': site_link, - 'min_upvotes': min_upvotes - } - message = template.render(Context(data)) - raise PermissionDenied(message) + raise PermissionDenied(messages.insufficient_reputation(user)) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] @@ -385,7 +384,8 @@ def process_emailed_question( email_address, subject, reason = 'permission_denied', - body_text = unicode(error) + body_text = unicode(error), + reply_to = reply_to ) except ValidationError: if from_address: diff --git a/askbot/mail/messages.py b/askbot/mail/messages.py new file mode 100644 index 00000000..652a8b11 --- /dev/null +++ b/askbot/mail/messages.py @@ -0,0 +1,48 @@ +"""functions in this module return body text +of email messages for various occasions +""" +import functools +from django.template import Context +from askbot.conf import settings as askbot_settings +from askbot.skins.loaders import get_template +from askbot.utils import html as html_utils + +def message(func, template = None): + """a decorator that creates a function + which returns formatted message using the + template and data""" + @functools.wraps(func) + def wrapped(data): + template = get_template(template) + return template.render(Context(data)) + +@message('email/ask_for_signature.html') +def ask_for_signature(user, footer_code = None): + """tells that we don't have user's signature + and because of that he/she cannot make posts + the message will ask to make a simple response + """ + return { + 'username': user.username, + 'site_name': askbot_settings.APP_SHORT_NAME, + 'footer_code': footer_code + } + +@message('email/insufficient_rep_to_post_by_email.html') +def insufficient_reputation(user): + """tells user that he does not have + enough rep and suggests to ask on the web + """ + min_rep = askbot_settings.MIN_REP_TO_POST_BY_EMAIL + min_upvotes = 1 + \ + (min_rep/askbot_settings.REP_GAIN_FOR_RECEIVING_UPVOTE) + site_link = html_utils.site_link( + 'ask', + askbot_settings.APP_SHORT_NAME + ) + return { + 'username': user.username, + 'site_name': askbot_settings.APP_SHORT_NAME, + 'site_link': site_link, + 'min_upvotes': min_upvotes + } diff --git a/askbot/skins/default/templates/email/ask_for_signature.html b/askbot/skins/default/templates/email/ask_for_signature.html new file mode 100644 index 00000000..4fdec833 --- /dev/null +++ b/askbot/skins/default/templates/email/ask_for_signature.html @@ -0,0 +1,11 @@ +{% import "email/macros.html" as macros %} +<p style="{{ macros.headline() }}"> + {% trans %}{{ username }}, please reply to this message.{% endtrans %} +</p> +<p> + {% trans %}Your post could not be published, because we could not detect signature in your email.{% endtrans %}<br/> + {% trans %}Please make a simple response, without editing this message.{% endtrans %}<br/> + {% trans %}We will attempt to detect the signature in your response.{% endtrans %} +</p> +{% include "email/footer.html" %} +<p style="{{ macros.fine_print() }}">{{ footer_code }}</p> |