Exemple #1
0
def email_job_show(request,job_id):
    job = EMailJob.get_by_id(int(job_id))
    if job is None:
        raise Http404
    sub_list = EMailSubJob.all().ancestor(job)
    job_data = EMailJobData.all().ancestor(job).get()
    return render_to_response('emails/email_job_show.html', RequestContext(request, { 'sub_list': sub_list, 'job':job, 'job_data':job_data}))
Exemple #2
0
def fire_email_subjob(request,subjob_key):

    if not getConfigBool('ENABLE_MAIL_JOBS',False):
        logging.info('ENABLE_MAIL_JOBS != True, ignore') 
        return HttpResponse('disabled')
        


    sub_job = EMailSubJob.get(subjob_key)
    if sub_job is None:
        logging.info('no sub_job')
        raise Http404

    job = EMailJob.get(sub_job.parent_key())
    if job is None:
        logging.info('no job')
        raise Http404


    job_data = EMailJobData.all().ancestor(job).get()
    if job_data is None:
        logging.info('no job_data')
        raise Http404

    try:
        email = EmailMessage(job_data.data)
        email.sender = job_data.sender
        email.to = job_data.sender
        email.check_initialized()
    except:
        logging.info("can't init email! %s"%sys.exc_info()[1])
        sub_job.status = 'error'
        sub_job.status_info = "can't init email message - %s"%sys.exc_info()[1]
        sub_job.save()
        return HttpResponse('error')



    logging.info('processing mail sub job:%s'%sub_job)
    sub_job.status = 'send'
    sub_job.save()

    for e in sub_job.emails:
        logging.info('sending email to %s'%e)
        try:
            email.to = e
            email.send() 
            sub_job.emails_done.extend([e])
        except:
            logging.info("can't init email! %s"%sys.exc_info()[1])
            sub_job.emails_error.extend([e])

    sub_job.status = 'done'
    sub_job.save()
    logging.info('result:%s'%sub_job)
    
    return HttpResponse('ok')
Exemple #3
0
def email_job_start(request,job_id):
    job = EMailJob.get_by_id(int(job_id))
    if job is None:
        raise Http404
    logging.info('starting mail job:%s'%job)
    if job.status != 'prepare':
        HttpResponse('ign')
   
    taskqueue.add(url='/tasks/start_email_job/%d/'%job.key().id(), method='GET')
 
    return redirect('..')
Exemple #4
0
def email_job_start_task(request,job_id):
    job = EMailJob.get_by_id(int(job_id))
    if job is None:
        raise Http404
    logging.info('starting mail job:%s'%job)
    if job.status != 'prepare':
        HttpResponse('ign')
   
    sub_list = EMailSubJob.all().ancestor(job)
    for s in sub_list:
        logging.info('start: %s'%s)
        taskqueue.add(url='/tasks/fire_email_subjob/%s/'%s.key(), method='GET')

    logging.info('end')
    
 
    return HttpResponse('ok')
Exemple #5
0
def email_job_create(request):
    email_groups = EMailList.all()
    email_templates = EMailTemplate.all()


    if request.method == 'POST':
        form = EMailJobCreateForm(request.POST,email_groups=email_groups, email_templates=email_templates)
        if form.is_valid():
            logging.info('creating new job request')
            el = EMailList.get(form.cleaned_data['email_group_id'])
            if el is None:
                raise Http404
            et = EMailTemplate.get(form.cleaned_data['email_template_id'])
            if et is None:
                raise Http404

            job = EMailJob()
            job.name = "'%s' -> '%s'"%(et.name,el.name)
            job.save()

            job_data = EMailJobData(parent=job)             
            job_data.sender = getConfig('DEFAULT_SENDER')
            job_data.split_count = getConfigInt('MAIL_SPLIT_COUNT',10)
            job_data.emails = el.emails
            job_data.data =  et.data

            job_data.save()
            job.data_ref = job_data.key()
            job.save()

            logging.info('new job: %s'%job)

            taskqueue.add(url='/tasks/prepare_email_job/%d/'%job.key().id(), method='GET')

            return redirect('..')
    else:
        form = EMailJobCreateForm(email_groups=email_groups, email_templates=email_templates)

    return render_to_response('emails/email_job_create.html', RequestContext(request, { 'form': form}))
Exemple #6
0
def email_job_prepare(request,job_id):
    job = EMailJob.get_by_id(int(job_id))
    if job is None:
        raise Http404
    logging.info('prepare mail job:%s'%job)
    if job.status != 'new':
        HttpResponse('ign')

    job_data = EMailJobData.all().ancestor(job).get()

    for i in range(0,len(job_data.emails),job_data.split_count):
        subjob = EMailSubJob(parent = job)
        subjob.emails_offset = i
        subjob.emails = job_data.emails[i:i+job_data.split_count]
        subjob.emails_count = len(subjob.emails)
        subjob.status = 'prepare'
        subjob.save()
        logging.info('subjob:%s'%subjob)

    
    job.status = 'prepare'
    job.save()
    return HttpResponse('ok')