Exemplo n.º 1
0
 def handle(self, *args, **options):
     subject = options['subject']
     to = set(options['to'])
     all_ = options['all']
     template = options['template']
     if all_:
         to |= {email['fromaddr'] for email in Email.objects.all().values()}
     t = loader.get_template('mails/{}.html'.format(template))
     context = Context(locals())
     message = t.render(context)
     sender = _mail_sender()
     print "sending mails to %d address:" % len(to)
     print '-'*40 
     for i, toaddr in enumerate(to):
         print "%d.  %s" % (i, toaddr)
     print '-'*40 
     sender.send_mail(subject, message, DEFAULT_SENDER, set(to))
Exemplo n.º 2
0
 def handle(self, *args, **options):
     preview = options['preview']
     preview_flag = 0
     subject = options['subject']
     t = loader.get_template('mails/notifyscore.html')
     sender = _mail_sender()
     assignments = Assignment.objects.order_by('sequence').values()
     assignments_len = len(assignments)
     for student in Student.objects.all():
         submission_count = student.submissions.count()
         if submission_count==0: continue
         submissions = student.submissions.all()
         results = [0] * assignments_len
         email = ''
         for submission in submissions:
             seq = submission.assignment.sequence
             if seq >0 and seq <= assignments_len:
                 results[seq-1] = float(submission.score)
                 if (not email) and submission.mails.count():
                     mail = submission.mails.all()[0]
                     email = mail.fromaddr
         if not email:
             print "email not found for student: %s" % student
             continue
         context = Context({'student':student,'results':results, 'results_sum':sum(results)})
         message = t.render(context)
         if preview:
             if preview_flag == 0:
                 sender.send_mail(subject, None, DEFAULT_SENDER, [DEFAULT_SENDER], html_message=message)
                 preview_flag = random.randint(20, 40)
                 print "sent preview email to %s" % DEFAULT_SENDER
             else:
                 preview_flag -= 1
                 print "sent to %s test passed" % email
         else:
             print "sending mail to %s" % email
             sender.send_mail(subject, None, DEFAULT_SENDER, [email], html_message=message)
Exemplo n.º 3
0
def report(request, seq):
    assignment = get_object_or_404(Assignment, sequence=seq)
    cs = []
    all_sum = 0
    sub_sum = 0
    for c in collections:
        students = Student.objects.filter(collection=c).order_by('student_num').values()
        s_count = len(students)
        sub_count = 0
        zeros = []
        for student in students:
            submissions = Submission.objects.filter(
                    student=student['id'],
                    assignment=assignment).values()
            if submissions:
                sub_count += 1
            else:
                zeros.append(student)
        all_sum += s_count
        sub_sum += sub_count
        cs.append((zeros, s_count, sub_count, s_count-sub_count))

    zero_sum = all_sum - sub_sum
    now = timezone.now()
    to = None

    if request.POST and request.POST.has_key('to'):
        from mailer.process_mail import _mail_sender
        import re
        to = request.POST['to']
        to = set(re.split(r'[\s,;]+', to))
        to.add(DEFAULT_SENDER)
        to.discard('')
        t = loader.get_template('mails/report.html')
        receivers = to | set(DEFAULT_RECEIVERS)
        context = Context({
            'title':assignment.title,
            'collections':cs,
            'now':now,
            'all_sum':all_sum,
            'sub_sum':sub_sum,
            'zero_sum':zero_sum,
            'to':to,
            'receivers':receivers,
            })
        message = t.render(context)
        sender = _mail_sender()
        subject = "数据结构 %s 提交情况报告".decode('utf8')% assignment.title
        print "sending mails to %d address:" % len(to)
        print '-'*40 
        for i, toaddr in enumerate(to):
            print "%d.  %s" % (i, toaddr)
        print '-'*40 
        sender.send_mail(subject, None, DEFAULT_SENDER, set(to), html_message=message)

    return render_to_response('report.html',
            {   'title':assignment.title,
                'collections':cs,
                'now':now,
                'all_sum':all_sum,
                'sub_sum':sub_sum,
                'zero_sum':zero_sum,
                'to':to,
                'receivers':DEFAULT_RECEIVERS,
                }, context_instance=RequestContext(request))