def render_to_pdf(template_url, data, studio): ##generate pdf with data #import pdb;pdb.set_trace(); try: template = get_template(template_url) context = Context(data) html = template.render(context) filename = data['todayslist']['data'][0]['studio_name'] + str( datetime.today().date()) result = open(filename + '.pdf', 'wb') #result = StringIO.StringIO() pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result) if not pdf.err: ###get and save the pdf result = open(filename + '.pdf', 'r') pdf = File(result) rep = MerchantDailyReportStatus(studio_id=studio, report=pdf) rep.save() studio_dt = StudioProfile.objects.values('studio').get(id=studio) studio_email = Studio.objects.values('email').get( id=studio_dt['studio']) try: #send email subject = (responses.MAIL_SUBJECTS['DAILY_REPORT_EMAIL']) % ( today) generic_utils.sendEmail(studio_email['email'], subject, message) except Exception, e: print repr(e) ##save pdf to table ##location should be inside studio except Exception, pdfrenderr: print(pdfrenderr)
def create(self,request,*args,**kwards): try: data = self.request.DATA email = data['email'] mobile_no = data['mobile_no'] studio_name = data['name'] area = data['area'] password = data['password'] #studio_group = data['studio_group'] logger_studios.info("Studio registration data "+str(data)) existing_email = StudioAddRequest.objects.filter(email = email) if not existing_email: studio_req = StudioAddRequest(email = email, area = area, \ mobile_no = mobile_no,studio_name = studio_name) studio_req.save() studio = Studio(email = email, password = password) studio.save(); studio_details = {'email':email,'studio_name':studio_name,'studio_pin':studio.id, \ 'password':password} logger_studios.info("Studios details "+str(studio_details)) message = get_template('emails/studio_req_register.html').render(Context(studio_details)) studio_mail = email subject = responses.MAIL_SUBJECTS['STUDIO_REQ_REGISTER'] try: generic_utils.sendEmail(studio_mail,subject,message) except Exception,e: transaction.rollback() logger_error.error(traceback.format_exc()) return Response(status = status.HTTP_500_INTERNAL_SERVER_ERROR) else:
def send_thanks_mail(): #import pdb;pdb.set_trace(); try: yesterday = datetime.today().date()-timedelta(days = 1) status_code = responses.BOOKING_CODES['BOOKED'] bookings = BookingDetails.objects.filter(appointment_date = yesterday, \ booking_status = 'BOOKED', status_code = status_code, is_valid = True) ##log code starting status_code = responses.BOOKING_CODES['USED'] for every_book in bookings: try: #code = every_book.booking_code #studio_name = StudioProfile.objects.values('name').get(id = every_book.studio.id) #user = User.objects.values('first_name','email','id').get(id = every_book.user.id) date = yesterday #get email template and render all variables review_link = None review_key = ReviewLink.objects.values('link_code').filter(booking_id = every_book.id, is_reviewed = 0) if review_key: review_link = settings.HOST_NAME+'/booking/review_from_email/?review_key='+str(review_key[0]['link_code'])+ \ '&&booking_id='+str(every_book.id) user_details = {'first_name':every_book.user.first_name,'studio_name':every_book.studio.name, \ 'date':every_book.appointment_date,'review_link':review_link} logger_booking.info("Thanks mail user details - "+str(user_details)) message = get_template('emails/thanks_email.html').render(Context(user_details)) to_user = every_book.user.email subject = responses.MAIL_SUBJECTS['THANKS_EMAIL']%(every_book.studio.name) has_sent = ThanksMail.objects.filter(booking_id = every_book.id, status = True) if not has_sent: status = generic_utils.sendEmail(to_user, subject, message) updated = BookingDetails.objects.filter(id = every_book.id).update(is_valid = False, \ booking_status = 'USED', status_code = status_code, \ service_updated = "thanks mail") if updated: thanks_mail = ThanksMail(booking_id = every_book.id, email = to_user, status = status,user_id = every_book.user.id, service_updated = "thanks mail", \ ) thanks_mail.save() except Exception,smserr: logger_error.error(traceback.format_exc()) status = False thanks_mail = ThanksMail(booking_id = every_book.id, email = to_user, \ status = status,user_id = every_book.user.id ,service_updated = "thanks mail", \ ) thanks_mail.save() except Exception,errorz: logger_error.error(traceback.format_exc())
def render_to_pdf(template_url,data,studio): ##generate pdf with data #import pdb;pdb.set_trace(); try: global till_time,today,mail_exception response = 0 template = get_template(template_url) data['todayslist']['date'] = today data['todayslist']['time'] = till_time context = Context(data) html = template.render(context) filename = data['todayslist']['studio_name'] + str(today)+"__bookings" result = open(filename+'.pdf', 'wb') #result = StringIO.StringIO() logger_booking.info("File name "+str(filename)) logger_booking.info("File name "+str(data)) try: pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result) except: logger_error.error(traceback.format_exc()) pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result) result.close() if not pdf.err: ###get and save the pdf file_ = open(filename+'.pdf', 'r') pdf = File(file_) rep = DailyInvoiceConfirmation(studio_id = studio, booking_pdf = pdf, \ service_updated = "studio report sender") rep.save() studio_dt = StudioProfile.objects.values('studio').get(id = studio) studio_email = Studio.objects.values('email').get(id = studio_dt['studio']) try: #send email message = "Please find the invoice data for %s"%(today) subject = (responses.MAIL_SUBJECTS['DAILY_INVOICE_MAIL'])%(today) generic_utils.sendEmail(studio_email['email'],subject,message,filename+'.pdf') generic_utils.sendEmail('*****@*****.**',subject,message,filename+'.pdf') #generic_utils.sendEmail(studio_email['email'],subject,message) rep = DailyInvoiceConfirmation.objects.filter(studio_id = studio, report_for_date = \ today).update(mail_sent = 1, updated_date_time = datetime.now()) response = 1 logger_booking.info('studio details %s '%(data)) except Exception,e: response = 0 err = {} err['studio'] = studio err['Exception'] = e mail_exception.append(err) logger_error.error(traceback.format_exc()) logger_error.error('studio details %s '%(data)) ##save pdf to table ##location should be inside studio file_.close(); os.remove(filename+'.pdf') except Exception,pdfrenderr: transaction.rollback() err = {} err['studio'] = studio err['Exception'] = pdfrenderr mail_exception.append(err) logger_error.error(traceback.format_exc()) return response
logger_booking.info('studio details %s '%(data)) except Exception,pdfgenrateerr: #sent = sent - 1 logger_error.error(traceback.format_exc()) err = {} err['studio'] = data['studio_id'] err['Exception'] = pdfgenrateerr mail_exception.append(err) logger_error.error('failed for %s'%(data)) logger_booking.info("Daily invoice script starts running "+datetime.strftime(datetime.now(), \ '%y-%m-%d %H:%M')) generate_pdf() if uniq_studios != sent: message = "Daily invoice Failed;<br/>Total unique studios - %s; <br/> Total processes - %s ;<br/> Total sent -%s ;<br/> \ Failed for - %s;<br/><br/><br/>Mail Exceptions:<br/>%s<br/></br/>Parse Exceptions:<br/>%s"%(str(uniq_studios),str(buks), str(sent), str(uniq_studios-sent), \ mail_exception, parse_exception) generic_utils.sendEmail(settings.ADMINS[1][1], 'Failures(emailing),Daily invoice script run failed',message,cc = 1) else: message = "Daily invoice Success.Have sent for %s out of %s"%(str(sent), str(uniq_studios)) generic_utils.sendEmail(settings.ADMINS[1][1], 'Success.Daily invoice script run sucessfull',message,cc = 1 ) logger_booking.info("Daily invoice script stops running "+datetime.strftime(datetime.now(), \ '%y-%m-%d %H:%M'))
daily_reminder = DailyReminder(booking_id = every_book.id, mobile_no = mobile_no, \ status = status,user_id = every_book.user.id,service_updated = "daily reminder", message = sms_template, \ ) daily_reminder.save() else: daily_reminder = DailyReminder(booking_id = every_book.id, mobile_no = mobile_no, \ status = status,user_id = every_book.user.id, service_updated = "daily reminder", message = sms_template) daily_reminder.save() ###log code end stats except Exception,errorz: logger_error.error(traceback.format_exc()) else: global buks buks = len(bookings) logger_booking.info("Total reminder SMS sent - "+str(buks)) ###log code end stats logger_booking.info("Booking reminder start time- "+ str(datetime.strftime(datetime.now(),'%y-%m-%d %H:%M'))) get_Bookings_for_day() message = "Sent %s reminder SMS"%(str(buks)) generic_utils.sendEmail(settings.ADMINS[1][1], 'Booking reminder script run successful',message,cc = 1) logger_booking.info("Booking reminder end time- "+ str(datetime.strftime(datetime.now(),'%y-%m-%d %H:%M')))
) thanks_mail.save() except Exception,smserr: logger_error.error(traceback.format_exc()) status = False thanks_mail = ThanksMail(booking_id = every_book.id, email = to_user, \ status = status,user_id = every_book.user.id ,service_updated = "thanks mail", \ ) thanks_mail.save() except Exception,errorz: logger_error.error(traceback.format_exc()) else: buks = len(bookings) logger_booking.info("Total thanks mail sent - "+str(buks)) message = "Sent %s mails"%(str(buks)) status = generic_utils.sendEmail(settings.ADMINS[1][1], 'Thanks mail run successful',message,cc=1) ###log code end stats logger_booking.info("Thanks mail start time- "+ str(datetime.strftime(datetime.now(),'%y-%m-%d %H:%M'))) send_thanks_mail() logger_booking.info("Thanks mail end time- "+ str(datetime.strftime(datetime.now(),'%y-%m-%d %H:%M'))) """ try: BookingDetails.objects.filter(appointment_date = yesterday, booking_status = 'BOOKED', \ status_code = 'B001', is_valid = True).update(booking_status = 'EXPIRED', \ status_code = status_code, is_valid = False)