From 430d42d193ecdde6ebcd642133e392dca60a3d41 Mon Sep 17 00:00:00 2001 From: Vasil Vangelovski Date: Tue, 17 Jan 2012 01:23:14 +0100 Subject: Posting reply by email - saving post --- askbot/conf/email.py | 12 ++++++++ askbot/models/__init__.py | 14 +++++++-- askbot/models/reply_by_email.py | 20 ++++++++++++- .../instant_notification_reply_by_email.html | 7 +++++ askbot/tests/reply_by_email_tests.py | 35 +++++++++++++++++++++- 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 askbot/skins/default/templates/instant_notification_reply_by_email.html diff --git a/askbot/conf/email.py b/askbot/conf/email.py index 13c079ef..ebfab0d7 100644 --- a/askbot/conf/email.py +++ b/askbot/conf/email.py @@ -281,6 +281,18 @@ settings.register( ) ) +settings.register( + livesettings.StringValue( + EMAIL, + 'REPLY_BY_EMAIL_HOSTNAME', + default = "", + description=_('Reply by email hostname'), + #TODO give a better explanation depending on lamson startup procedure + + ) +) + + settings.register( livesettings.IntegerValue( diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 3d90dfae..656fa6f3 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -2278,6 +2278,8 @@ def format_instant_notification_email( update_data = { 'update_author_name': from_user.username, 'receiving_user_name': to_user.username, + 'receiving_user_karma': to_user.reputation, + 'reply_by_email_karma_threshold': askbot_settings.MIN_REP_TO_POST_BY_EMAIL, 'content_preview': content_preview,#post.get_snippet() 'update_type': update_type, 'post_url': site_url + post.get_absolute_url(), @@ -2315,7 +2317,8 @@ def send_instant_notifications_about_activity_in_post( origin_post = post.get_origin_post() for user in recipients: - + if askbot_settings.REPLY_BY_EMAIL: + template = get_template('instant_notification_reply_by_email.html') subject_line, body_text = format_instant_notification_email( to_user = user, from_user = update_activity.user, @@ -2325,13 +2328,20 @@ def send_instant_notifications_about_activity_in_post( ) #todo: this could be packaged as an "action" - a bundle #of executive function with the activity log recording + #TODO check user reputation + headers = mail.thread_headers(post, origin_post, update_activity.activity_type) + if askbot_settings.REPLY_BY_EMAIL: + reply_address = "noreply" + if user.reputation >= askbot_settings.MIN_REP_TO_POST_BY_EMAIL: + reply_address = ReplyAddress.objects.create_new(post, user).address + headers.update({'Reply-To': "%s@%s"%(reply_address, askbot_settings.REPLY_BY_EMAIL_HOSTNAME)}) mail.send_mail( subject_line = subject_line, body_text = body_text, recipient_list = [user.email], related_object = origin_post, activity_type = const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT, - headers = mail.thread_headers(post, origin_post, update_activity.activity_type) + headers = headers ) diff --git a/askbot/models/reply_by_email.py b/askbot/models/reply_by_email.py index 74c7b666..124fce4b 100644 --- a/askbot/models/reply_by_email.py +++ b/askbot/models/reply_by_email.py @@ -1,3 +1,4 @@ +from datetime import datetime import random import string @@ -6,6 +7,7 @@ from django.contrib.auth.models import User from askbot.models.post import Post from askbot.models.base import BaseQuerySetManager +from askbot.conf import settings as askbot_settings class ReplyAddressManager(BaseQuerySetManager): @@ -29,6 +31,22 @@ class ReplyAddress(models.Model): objects = ReplyAddressManager() + class Meta: app_label = 'askbot' - db_table = 'askbot_replyaddress' \ No newline at end of file + db_table = 'askbot_replyaddress' + + def create_reply(self, content): + result = None + if self.post.post_type == 'answer' or self.post.post_type == 'comment': + result = self.user.post_comment(self.post, content) + elif self.post.post_type == 'question': + wordcount = len(content.rsplit()) + if wordcount > askbot_settings.MIN_WORDS_FOR_ANSWER_BY_EMAIL: + result = self.user.post_answer(self.post, content) + else: + result = self.user.post_comment(self.post, content) + self.used_at = datetime.now() + self.save() + return result + diff --git a/askbot/skins/default/templates/instant_notification_reply_by_email.html b/askbot/skins/default/templates/instant_notification_reply_by_email.html new file mode 100644 index 00000000..84506143 --- /dev/null +++ b/askbot/skins/default/templates/instant_notification_reply_by_email.html @@ -0,0 +1,7 @@ +{% trans %} +{# Don't change the following line in the template. #} +======= Reply above this line. ====-=-= +You can post an answer or a comment by replying to this email. To reply to this email +you need {{reply_by_email_karma_threshold}} karma, you have {{receiving_user_karma}} karma. +{% endtrans %} +{% include 'instant_notification.html' %} \ No newline at end of file diff --git a/askbot/tests/reply_by_email_tests.py b/askbot/tests/reply_by_email_tests.py index c40d425d..c4d70c0b 100644 --- a/askbot/tests/reply_by_email_tests.py +++ b/askbot/tests/reply_by_email_tests.py @@ -8,7 +8,13 @@ class ReplyAddressModelTests(AskbotTestCase): def setUp(self): self.u1 = self.create_user(username='user1') + self.u1.set_status('a') + self.u1.moderate_user_reputation(self.u1, reputation_change = 100, comment= "no comment") self.u2 = self.create_user(username='user2') + self.u1.moderate_user_reputation(self.u2, reputation_change = 100, comment= "no comment") + self.u3 = self.create_user(username='user3') + self.u1.moderate_user_reputation(self.u3, reputation_change = 100, comment= "no comment") + self.question = self.post_question( user = self.u1, follow = True, @@ -17,10 +23,37 @@ class ReplyAddressModelTests(AskbotTestCase): user = self.u2, question = self.question ) + + self.comment = self.post_comment(user = self.u2, parent_post = self.answer) - def test_creation(self): + def test_address_creation(self): self.assertEquals(ReplyAddress.objects.all().count(), 0) result = ReplyAddress.objects.create_new( self.answer, self.u1) self.assertTrue(len(result.address) >= 12 and len(result.address) <= 25) self.assertEquals(ReplyAddress.objects.all().count(), 1) + def test_create_answer_reply(self): + result = ReplyAddress.objects.create_new( self.answer, self.u1) + post = result.create_reply("A test post") + self.assertEquals(post.post_type, "comment") + self.assertEquals(post.text, "A test post") + + + def test_create_comment_reply(self): + result = ReplyAddress.objects.create_new( self.comment, self.u1) + post = result.create_reply("A test reply") + self.assertEquals(post.post_type, "comment") + self.assertEquals(post.text, "A test reply") + + + def test_create_question_comment_reply(self): + result = ReplyAddress.objects.create_new( self.question, self.u3) + post = result.create_reply("A test post") + self.assertEquals(post.post_type, "comment") + self.assertEquals(post.text, "A test post") + + def test_create_question_answer_reply(self): + result = ReplyAddress.objects.create_new( self.question, self.u3) + post = result.create_reply("A test post "* 10) + self.assertEquals(post.post_type, "answer") + self.assertEquals(post.text, "A test post "* 10) \ No newline at end of file -- cgit v1.2.3-1-g7c22