From d6e30bef98ac85a8606284aba4adaf17de811b63 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Tue, 29 May 2012 03:15:43 -0400 Subject: hopefully it is possible to edit by email --- askbot/mail/lamson_handlers.py | 25 ++++++++++++++--------- askbot/models/__init__.py | 24 +++++++++++----------- askbot/models/reply_by_email.py | 39 +++++++++++++++++++++++++++--------- askbot/tests/reply_by_email_tests.py | 8 ++++---- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/askbot/mail/lamson_handlers.py b/askbot/mail/lamson_handlers.py index e98e3e99..d803dcf2 100644 --- a/askbot/mail/lamson_handlers.py +++ b/askbot/mail/lamson_handlers.py @@ -259,16 +259,18 @@ def VALIDATE_EMAIL( def PROCESS( parts = None, reply_address_object = None, + subject_line = None, **kwargs ): """handler to process the emailed message and make a post to askbot based on the contents of the email, including the text body and the file attachments""" - #split email into bits + #1) get actual email content + # todo: factor this out into the process_reply decorator reply_code = reply_address_object.address body_text, stored_files, signature = mail.process_parts(parts, reply_code) - #if have signature update signature + #2) process body text and email signature user = reply_address_object.user if signature:#if there, then it was stripped if signature != user.email_signature: @@ -281,14 +283,17 @@ def PROCESS( raise ValueError('email signature changed or unknown') body_text = stripped_body_text - #validate email address + #3) validate email address and save user user.email_isvalid = True user.save()#todo: actually, saving is not necessary, if nothing changed - #todo: elaborate - here in some cases we want to add an edit - #and in other cases - replace the text entirely - if reply_address_object.was_used: - action = reply_address_object.edit_post - else: - action = reply_address_object.create_reply - action(body_text, stored_files) + #4) actually make an edit in the forum + robj = reply_address_object + add_post_actions = ('post_comment', 'post_answer', 'auto_answer_or_comment') + if robj.reply_action in ('replace_content', 'append_content'): + robj.edit_post(body_text, title = subject_line) + elif robj.reply_action in add_post_actions: + if robj.was_used: + robj.edit_post(body_text, reply_action = 'append_content') + else: + robj.create_reply(body_text) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 701238e1..2526c4b7 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -1503,18 +1503,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, - timestamp = None, - force = False,#if True - bypass the assert - by_email = False - ): + self, + question = None, + title = None, + body_text = None, + revision_comment = None, + tags = None, + wiki = False, + edit_anonymously = False, + timestamp = None, + force = False,#if True - bypass the assert + by_email = False + ): if force == False: self.assert_can_edit_question(question) diff --git a/askbot/models/reply_by_email.py b/askbot/models/reply_by_email.py index 4981765f..bcd82645 100644 --- a/askbot/models/reply_by_email.py +++ b/askbot/models/reply_by_email.py @@ -36,6 +36,8 @@ class ReplyAddressManager(BaseQuerySetManager): REPLY_ACTION_CHOICES = ( ('post_answer', _('Post an answer')), ('post_comment', _('Post a comment')), + ('replace_content', _('Edit post')), + ('append_content', _('Append to post')), ('auto_answer_or_comment', _('Answer or comment, depending on the size of post')), ('validate_email', _('Validate email and record signature')), ) @@ -83,22 +85,41 @@ class ReplyAddress(models.Model): askbot_settings.REPLY_BY_EMAIL_HOSTNAME ) - def edit_post(self, body_text, stored_files): + def edit_post( + self, body_text, title = None, reply_action = None + ): """edits the created post upon repeated response to the same address""" - assert self.was_used == True - self.user.edit_post( - post = self.response_post, - body_text = stored_files, - revision_comment = _('edited by email'), - by_email = True - ) + reply_action = reply_action or self.reply_action + + if reply_action == 'append_content': + body_text = self.post.text + '\n\n' + body_text + revision_comment = _('added content by email') + else: + revision_comment = _('edited by email') + + if self.post.post_type == 'question': + self.user.edit_question( + question = self.post, + body_text = body_text, + title = title, + revision_comment = revision_comment, + by_email = True + ) + else: + self.user.edit_post( + post = self.response_post, + body_text = body_text, + revision_comment = revision_comment, + by_email = True + ) self.response_post.thread.invalidate_cached_data() - def create_reply(self, body_text, stored_files): + def create_reply(self, body_text): """creates a reply to the post which was emailed to the user """ + assert(self.was_used == False) result = None if self.post.post_type == 'answer': result = self.user.post_comment( diff --git a/askbot/tests/reply_by_email_tests.py b/askbot/tests/reply_by_email_tests.py index 177deebd..698662fc 100644 --- a/askbot/tests/reply_by_email_tests.py +++ b/askbot/tests/reply_by_email_tests.py @@ -101,7 +101,7 @@ class ReplyAddressModelTests(AskbotTestCase): post = self.answer, user = self.u1 ) - post = result.create_reply(TEST_CONTENT, []) + post = result.create_reply(TEST_CONTENT) self.assertEquals(post.post_type, "comment") self.assertEquals(post.text, TEST_CONTENT) self.assertEquals(self.answer.comments.count(), 2) @@ -111,7 +111,7 @@ class ReplyAddressModelTests(AskbotTestCase): post = self.comment, user = self.u1 ) - post = result.create_reply(TEST_CONTENT, []) + post = result.create_reply(TEST_CONTENT) self.assertEquals(post.post_type, "comment") self.assertEquals(post.text, TEST_CONTENT) self.assertEquals(self.answer.comments.count(), 2) @@ -122,7 +122,7 @@ class ReplyAddressModelTests(AskbotTestCase): post = self.question, user = self.u3 ) - post = result.create_reply(TEST_CONTENT, []) + post = result.create_reply(TEST_CONTENT) self.assertEquals(post.post_type, "comment") self.assertEquals(post.text, TEST_CONTENT) @@ -131,7 +131,7 @@ class ReplyAddressModelTests(AskbotTestCase): post = self.question, user = self.u3 ) - post = result.create_reply(TEST_LONG_CONTENT, []) + post = result.create_reply(TEST_LONG_CONTENT) self.assertEquals(post.post_type, "answer") self.assertEquals(post.text, TEST_LONG_CONTENT) -- cgit v1.2.3-1-g7c22