Exemple #1
0
def new_message_in_thread(request_id, msg):
    """
    Pick up the request and create a new PIAMessage in its PIAThread.
    """
    try:
        request= PIARequest.objects.get(pk=request_id)
    except Exception as e:
        print AppMessage('RequestNotFound', value=(request_id, e,)).message
        return None
    # Collect and prepare data from the message.
    data= {'request': request, 'is_response': True,
           'email_from': msg['header']['from'],
           'email_to': msg['header']['to'],
           'subject': msg['header']['subject'],
           'body': msg['content']}
    # Creating a new message in the Request's Thread.
    new_message= PIAThread(**data)
    try:
        new_message.save()
    except Exception as e:
        new_message= None
        print >> sys.stderr, '[%s] %s' % (datetime.now().isoformat(),
                                          AppMessage('MsgCreateFailed').message % e)
    if new_message:
        # Change the status of the Request to 'awaiting classification'.
        request.status= get_request_status('awaiting')
        try:
            request.save()
        except Exception as e:
            pass
        # Process attachments.
        if msg['attachments']:
            for attachment in msg['attachments']:
                filesize= attachment['filesize']
                path= attachment['filename']
                filename= path.rsplit('/')[-1]
                filetype= path.rsplit('.')[-1]
                try:
                    PIAAttachment.objects.create(message=new_message, path=path,
                        filename=filename, filetype=filetype, filesize=filesize)
                except Exception as e:
                    print AppMessage('AttachFailed', value=(
                        filename, request_id, e,)).message
    return new_message