def process_emailed_question(from_address, subject, body_text, stored_files, tags=None, group_id=None): """posts question received by email or bounces the message""" # a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: # todo: delete uploaded files when posting by email fails!!! data = {"sender": from_address, "subject": subject, "body_text": body_text} form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data["email"] user = User.objects.get(email__iexact=email_address) if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data["body_text"] stripped_body_text = user.strip_email_signature(body_text) signature_not_detected = stripped_body_text == body_text and user.email_signature # ask for signature response if user's email has not been # validated yet or if email signature could not be found if user.email_isvalid is False or signature_not_detected: reply_to = ReplyAddress.objects.create_new(user=user, reply_action="validate_email").as_email_address() message = messages.ask_for_signature(user, footer_code=reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data["tagnames"] title = form.cleaned_data["title"] # defect - here we might get "too many tags" issue if tags: tagnames += " " + " ".join(tags) user.post_question( title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id, ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason="unknown_user") except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason="problem_posting") except PermissionDenied, error: bounce_email(email_address, subject, reason="permission_denied", body_text=unicode(error), reply_to=reply_to)
def process_emailed_question(from_address, subject, body_text, stored_files, tags=None, group_id=None): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get(email__iexact=email_address) if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data['body_text'] stripped_body_text = user.strip_email_signature(body_text) signature_not_detected = (stripped_body_text == body_text and user.email_signature) #ask for signature response if user's email has not been #validated yet or if email signature could not be found if user.email_isvalid is False or signature_not_detected: reply_to = ReplyAddress.objects.create_new( user=user, reply_action='validate_email').as_email_address() message = messages.ask_for_signature(user, footer_code=reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) user.post_question(title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason='unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason='problem_posting') except PermissionDenied, error: bounce_email(email_address, subject, reason='permission_denied', body_text=unicode(error), reply_to=reply_to)
def process_emailed_question( from_address, subject, body_text, stored_files, tags=None, group_id=None ): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } user = User.objects.get(email__iexact=from_address) form = AskByEmailForm(data, user=user) if form.is_valid(): email_address = form.cleaned_data['email'] if user.can_post_by_email() is False: raise PermissionDenied(messages.insufficient_reputation(user)) body_text = form.cleaned_data['body_text'] stripped_body_text = user.strip_email_signature(body_text) #note that signature '' means it is unset and 'empty signature' is a sentinel #because there is no other way to indicate unset signature without adding #another field to the user model signature_changed = ( stripped_body_text == body_text and user.email_signature != 'empty signature' ) need_new_signature = ( user.email_isvalid is False or user.email_signature == '' or signature_changed ) #ask for signature response if user's email has not been #validated yet or if email signature could not be found if need_new_signature: reply_to = ReplyAddress.objects.create_new( user = user, reply_action = 'validate_email' ).as_email_address(prefix='welcome-') message = messages.ask_for_signature(user, footer_code = reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] #defect - here we might get "too many tags" issue if tags: tagnames += ', ' + ', '.join(tags) user.post_question( title=title, tags=tagnames.strip(), body_text=stripped_body_text, by_email=True, email_address=from_address, group_id=group_id ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error), reply_to = reply_to )
def test_ask_for_signature(self): from askbot.mail import messages user = self.create_user("user") message = messages.ask_for_signature(user, footer_code="nothing") self.assertTrue(user.username in message)
def test_ask_for_signature(self): from askbot.mail import messages user = self.create_user('user') message = messages.ask_for_signature(user, footer_code = 'nothing') self.assertTrue(user.username in message)
def process_emailed_question( from_address, subject, body_text, stored_files, tags = None ): """posts question received by email or bounces the message""" #a bunch of imports here, to avoid potential circular import issues from askbot.forms import AskByEmailForm from askbot.models import ReplyAddress, User from askbot.mail import messages reply_to = None try: #todo: delete uploaded files when posting by email fails!!! data = { 'sender': from_address, 'subject': subject, 'body_text': body_text } form = AskByEmailForm(data) if form.is_valid(): email_address = form.cleaned_data['email'] user = User.objects.get( email__iexact = email_address ) if user.can_post_by_email() == False: raise PermissionDenied(messages.insufficient_reputation(user)) if user.email_isvalid == False: reply_to = ReplyAddress.objects.create_new( user = user, reply_action = 'validate_email' ).as_email_address() message = messages.ask_for_signature(user, footer_code = reply_to) raise PermissionDenied(message) tagnames = form.cleaned_data['tagnames'] title = form.cleaned_data['title'] body_text = form.cleaned_data['body_text'] #defect - here we might get "too many tags" issue if tags: tagnames += ' ' + ' '.join(tags) stripped_body_text = user.strip_email_signature(body_text) if stripped_body_text == body_text and user.email_signature: #todo: send an email asking to update the signature raise ValueError('email signature changed') user.post_question( title = title, tags = tagnames.strip(), body_text = stripped_body_text, by_email = True, email_address = from_address ) else: raise ValidationError() except User.DoesNotExist: bounce_email(email_address, subject, reason = 'unknown_user') except User.MultipleObjectsReturned: bounce_email(email_address, subject, reason = 'problem_posting') except PermissionDenied, error: bounce_email( email_address, subject, reason = 'permission_denied', body_text = unicode(error), reply_to = reply_to )