Example #1
0
File: tasks.py Project: CCLab/sezam
def check_overdue():
    """
    Check in the db for overdue requests.
    Executed daily at midnight.
    Returns status of completion.
    """
    total_overdue_requests= 0

    # Process overdue requests (those remain unanswered for OVERDUE_DAYS).
    when= datetime.utcnow().replace(tzinfo=utc) - timedelta(days=OVERDUE_DAYS)
    in_progress= 'in_progress'
    for pia_request in PIARequest.objects.filter(created__lt=when,
                                                 status=in_progress):
        if PIAThread.objects.filter(request=pia_request,
                                    is_response=True).count() == 0:
            total_overdue_requests += 1
            
            # Send a reminder to the Authority.
            send_reminder(pia_request)
            # Send an overdue report to user.
            send_report(pia_request,
                        status='overdue',
                        template='emails/report_overdue_to_user.txt')

            # Set 'overdue' status to the request:
            # this doesn't depend on sending messages - even if there are errors
            # the request should be marked as `overdue`.
            pia_request.status= get_request_status('overdue')
            try:
                pia_request.save()
            except Exception as e:
                pass

    # Process long_overdue requests (those that remain unanswered for twice OVERDUE_DAYS).
    when= datetime.utcnow().replace(tzinfo=utc) - timedelta(days=OVERDUE_DAYS * 2)
    in_progress= ['in_progress', 'overdue']
    for pia_request in PIARequest.objects.filter(created__lt=when,
                                                 status__in=in_progress):
        if PIAThread.objects.filter(request=pia_request,
                                    is_response=True).count() == 0:
            total_overdue_requests += 1

            # Send a reminder to the Authority.
            send_reminder(pia_request,
                          email_template='emails/reminder_long_overdue.txt')
            # Send an overdue report to user.
            send_report(pia_request,
                        status='long_overdue',
                        template='emails/report_long_overdue_to_user.txt')
            # Set 'long_overdue' status to the request.
            pia_request.status= 'long_overdue'
            try:
                pia_request.save()
            except Exception as e:
                pass

    return AppMessage('CheckOverdueComplete').message % total_overdue_requests
Example #2
0
File: tasks.py Project: CCLab/sezam
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