Ejemplo n.º 1
0
 def test_ask_for_signature(self):
     from askbot.mail.messages import AskForSignature
     user = self.create_user('user')
     message = AskForSignature({
                         'user': user,
                         'footer_code': 'nothing'
                     }).render_body()
     self.assertTrue(user.username in message)
Ejemplo n.º 2
0
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.messages import (AskForSignature, InsufficientReputation)

    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:
                email = InsufficientReputation({'user': user})
                raise PermissionDenied(email.render_body())

            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:
                footer_code = ReplyAddress.objects.create_new(
                    user=user, reply_action='validate_email').as_email_address(
                        prefix='welcome-')

                email = AskForSignature({
                    'user': user,
                    'footer_code': footer_code
                })
                raise PermissionDenied(email.render_body())

            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)