def client(): app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@localhost:3306/energy_db_test' # test database app.testing = True with app.app_context(): # db.create_all() yield app.test_client()
def send_user_created_by_owner_email(created_user, owner_full_name, subject, jwt, send_async=True): """ Şirket yöneticisi tarafından yeni kullanıcı oluşturulduğunda, kullanıcının şifresini tanımlaması için davetiye bildirimi yapar :param created_user: {User} Oluşturulan kullanıcı kaydı :param owner_full_name: {str} İşemi yapan kullanıcının tam adı :param subject: Email bildirim başlığı :return: {bool} """ with app.app_context(): link_string = app.config.get("HOST_NAME") + app.config.get("PASSWORD_RECOVERY_ENDPOINT") + jwt.decode('UTF-8') msg = Message(subject, sender=("Softnec Bilgi", "*****@*****.**"), recipients=[created_user.email]) msg.html = render_template( 'email_templates/user_created_by_owner.html', owner_full_name=owner_full_name, created_user=created_user, link=link_string ) if send_async is True: send_email_asynchronous(msg) else: try: mail.send(msg) except Exception as e: app.logger.exception("*** sendUserCreatedByOwnerEmail Send Exception :" ) app.logger.exception(e) return True
def send_email_asynchronous(message_object): """ Eşzamanlı olmayan email gönderme işlemi yapar :param message_object: {Message} Mail içeriği Message sınıfından oluşmalıdır """ with app.app_context(): try: mail.send(message_object) except Exception as e: app.logger.exception("*** send_email_asynchronous occurred an exception;") app.logger.exception(e) return True
def sendForgetPasswordEmail(receiver,title,body,subject,link): with app.app_context(): app.logger.debug("*** sendForgetPasswordEmail mail preparing as async") msg = Message(subject, sender=("Softnec Bilgi", "*****@*****.**"), recipients=receiver) msg.html = render_template('Email-ForgetPassword.html', title=title, body=body,link=link) app.logger.debug("*** sendForgetPasswordEmail Send to :" + str(receiver)) try : mail.send(msg) except Exception as e: app.logger.exception("***sendForgetPasswordEmail Send Exception :" ) app.logger.exception(e) return True
def daily_schedule_job(): print("*** daily_schedule_job starts") try: with app.app_context(): print("*** daily_schedule_job running in app context") print("*** app: {}".format(app)) # app_context altında `request` kullanılacaksa local olarak import edilmelidir # Working outside of request context. from flask import request app.logger.warning( '*** [Schedule Job Fired] daily_schedule_job job fired. Registed daily job count: {}' .format(len(daily_job_list))) if len(daily_job_list) > 0: for job in daily_job_list: if job[1] is not False and isinstance(job[1], type): try: job[1]().__getattribute__(job[0])() except AttributeError as e: app.logger.error( "*** {} has no attribute as {}\n*ERROR: {}". format(job[1], job[0], e)) except TypeError as e: app.logger.error( "*** {} is not callable\n*ERROR:{}".format( job[0], e)) except Exception as e: app.logger.error( "*** daily Schedule occurred an exception method: {}\n*ERROR:{}" .format(job[0], e)) else: if callable(job[0]): try: job[0]() except Exception as e: app.logger.error( "*** daily Schedule occurred an exception method: {}\n*ERROR: {}" .format(job[0], e)) except Exception as e: print("*** daily_schedule_job occurred an exception without start: {}". format([e, e.with_traceback])) print("*** daily_schedule_job done")
def sendTemplateEmail(receiver,title,body,subject): """ Async mail sender method :param receiver: list :param title: string :param body: ?any? :param subject: string :return: bool """ app.logger.debug("*** sendTemplateEmail fired") with app.app_context(): app.logger.debug("*** sendTemplateEmail mail preparing as async") msg = Message(subject, sender=("Softnec Bilgi", "*****@*****.**"), recipients=receiver) msg.html = render_template('Email-Template.html', title=title, body=body) app.logger.debug("*** Mail Send to :" + str(receiver)) try : mail.send(msg) except Exception as e: app.logger.exception("sendTemplateEmail Send Exception :" ) app.logger.exception(e) return True