def email_result(self): key_name = self.request.get('key') obj = Eval.get_by_key_name(key_name) if not obj: return self.get(errors=['Invalid key: {!r}'.format(key_name)]) if obj.sent_results: return self.get(errors=['Results already emailed.']) safe_ta_name = obj.ta.replace(' ', '_') ta_result_name = '{}-{}.txt'.format(obj.course, safe_ta_name) ta_result = Eval.generate_summary([obj]) course_list = [x for x in Eval.all().filter('course =', obj.course)] course_result = Eval.generate_summary(course_list, True) all_result = Eval.generate_summary([x for x in Eval.all()], True) settings = Settings.get_by_key_name('settings') email_to = '{} <{}>'.format(obj.ta, obj.ta_email) email_cc = '{} <{}>'.format(obj.instructor, obj.instructor_email) body = const.RESULT_EMAIL_TEMPLATE.format(obj.ta) attachments = [(ta_result_name, ta_result), ('{}.txt'.format(obj.course), course_result), ('all.txt', all_result)] try: mail.send_mail(sender=settings.admin_email, to=email_to, cc=email_cc, subject=const.RESULT_EMAIL_SUBJECT, body=body, attachments=attachments) obj.sent_results = True obj.put() self.get(['Sent result to {} and {}'.format(obj.ta, obj.instructor)]) except Exception, e: self.get(errors=[str(e)])
def get(self): outfile = StringIO.StringIO() tgz = tarfile.open(fileobj=outfile, mode='w:gz') for ev in Eval.all(): filename = 'taevals/{}-{}.txt'.format(ev.course, ev.ta.replace(' ', '_')) results = Eval.generate_summary([ev]).encode('utf-8') tarinfo = tarfile.TarInfo(filename) tarinfo.size = len(results) tarinfo.mtime = time.time() tgz.addfile(tarinfo, StringIO.StringIO(results)) tgz.close() cd = 'attachement; filename="taevals.tar.gz"' self.response.headers['Content-Type'] = 'application/x-compressed' self.response.headers['Content-Disposition'] = cd self.response.out.write(outfile.getvalue())
def get(self, course=None, ta=None): evals = [] key_name = None if ta != None: ta = urllib.unquote(ta) course = urllib.unquote(course) if course == None: return self.redirect('/admin') key_name = '{}-{}'.format(ta, course) obj = Eval.get_by_key_name(key_name) if obj == None: return self.redirect('/admin') evals.append(obj) title = '{}-{}'.format(course, ta) elif course != None: course = urllib.unquote(course) evals.extend([x for x in Eval.all().filter('course =', course)]) title = course else: evals.extend([x for x in Eval.all()]) title = 'all' if not evals: return self.redirect('/admin') results = Eval.generate_summary(evals, len(evals) > 1) if self.request.get('dl') == '1': filename = title.replace(' ', '_') cd = 'attachement; filename="{}.txt"'.format(filename) self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = cd self.response.out.write(results) else: form_token, cookie = helpers.generate_validation_token() self.response.headers.add_header('Set-Cookie', cookie) dl_url = '{}?dl=1'.format(self.request.url) email_url = '{}?email=1'.format(self.request.url) values = {'results': cgi.escape(results), 'title': title, 'dl_url': dl_url, 'email_url': email_url, 'form_token': form_token, 'key': key_name} template = jinja_environment.get_template('results.html') self.response.out.write(template.render(values))