def print_pdf(request): log_id = request.GET.get("log_id", False) if not log_id: return render(request, "flights/pdf.html", log_pdf(4)) return HttpResponse("<html><body>please select a flight log to print</body></html>") # one file if log_id.find(",") == -1: # return render(request, "flights/pdf.html", log_pdf(log_id)) log = Log.objects.get(id_log=log_id) or None if not log: return HttpResponse("<html><body>please select a flight log to print</body></html>") return render_to_pdf_response("flights/pdf.html", log_pdf(log_id), format6(log.log_number)) arr = log_id.split(",") dirname = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "/../helicopters/static/media/pdf_export/" pdf_files = [] for nu in arr: print "-> getting Log id", nu, datetime.now().strftime("%H:%M:%S") log = Log.objects.get(id_log=nu) or None if not log: continue pdf_file = format6(log.log_number) pdf_files.append(pdf_file) try: # print "render_to_string ...", datetime.now().strftime('%H:%M:%S') pdf = render_to_string("flights/pdf.html", log_pdf(nu)) # print "converting HtmlToPdf ...", datetime.now().strftime('%H:%M:%S') convertHtmlToPdf(pdf, dirname + pdf_file) # print "end converting HtmlToPdf ...", datetime.now().strftime('%H:%M:%S') except Exception as e: print "---------error on pdf " print e # print "zipping ...", datetime.now().strftime('%H:%M:%S') # zip all file to one buffer = StringIO.StringIO() zf = zipfile.ZipFile(buffer, mode="w") for pdf_file in pdf_files: try: zf.write(dirname + pdf_file, pdf_file) finally: pass zf.close() buffer.seek(0) response = HttpResponse(buffer.read()) today_str = datetime.now().strftime("%m_%d_%Y") response["Content-Disposition"] = "attachment; filename=Daily_Flight_Log_" + today_str + ".zip" response["Content-Type"] = "application/x-zip" # print "end zipping ..." return response
def send_email(request, arr): render = render_to_string(constant.email_popup_page) dajax = Dajax() html = render.replace('\n', "") dajax.script(constant.append_flight_email_popup %html) emails = UserTemp.objects.filter(employee_number=request.session[constant.usernameParam]) html = '' val = '' j = 0 from flight_log.views import log_pdf from flight_log.utility import convertHtmlToPdf pdf_files = [] dirname = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +'/../helicopters/static/media/pdf_export/' for i in arr: log = Log.objects.get(id_log=i) or None if not log: continue num_formated = format6(log.log_number) if(j == 0): val = val + num_formated j = 1 else: val = val + ',' + num_formated pdf_path = dirname + num_formated pdf_files.append(num_formated) pdf = render_to_string("flights/pdf.html", log_pdf(i)) convertHtmlToPdf(pdf, pdf_path) if len(arr) > 1: from datetime import datetime today_str= datetime.now().strftime('%m_%d_%Y') i = today_str files_today = 'Daily_Flight_Log_' + today_str + '.zip' # read stream zip buffer = StringIO.StringIO() zf = zipfile.ZipFile(buffer, mode='w') for pdf_file in pdf_files: try: zf.write(dirname + pdf_file, pdf_file) finally: pass zf.close() # save file to local buffer.seek(0) f = file(dirname + files_today, "w") f.write(buffer.read()) f.close() else: i = arr[0] files_today = pdf_files[0] #print "files_today on dajaxice_register send_email", files_today html = html + "<div class=\"attack_file\"><input id=\"attack_file_1\" onclick=\"download(this)\" class=\"attack_file\" readonly=\"true\" name=\"attack_file_" + i + "\" value=\"" + files_today +"\" type=\"text\" /><div class=\"close-attack\">x</div></div>" # POST lost dot, so we remove it and add on recived html = html + '<input class="hiden_attack" name="hiden_attack" val="'+ files_today + '" type="input" />' dajax.script("var e_html = '" + html +"';\ jQuery('.attack_files').html(e_html);") for email in emails: dajax.script("jQuery('.email_from').val('" + email.email + "');") return dajax.json()