def recover(): """recover""" if request.method == 'GET': return render_template('recover_password.html') username = request.form['username'] registered_user = User.query.filter_by(username=username, ).first() if registered_user is None: flash(gettext('Invalid username'), 'danger') return redirect(url_for('recover', lang=g.current_lang)) fromaddr = '%s <%s>' % (APP.config['GHC_SITE_TITLE'], APP.config['GHC_ADMIN_EMAIL']) toaddr = registered_user.email template_vars = { 'config': APP.config, 'password': registered_user.password } msg = render_template2('recover_password_email.txt', template_vars) send_email(APP.config['GHC_SMTP'], fromaddr, toaddr, msg) flash(gettext('Password sent via email'), 'success') if 'next' in request.args: return redirect(request.args.get('next')) return redirect(url_for('home', lang=g.current_lang))
def recover(): """recover""" if request.method == 'GET': return render_template('recover_password.html') username = request.form['username'] registered_user = User.query.filter_by(username=username,).first() if registered_user is None: flash(gettext('Invalid username'), 'danger') return redirect(url_for('recover', lang=g.current_lang)) fromaddr = '%s <%s>' % (APP.config['GHC_SITE_TITLE'], APP.config['GHC_ADMIN_EMAIL']) toaddr = registered_user.email template_vars = { 'config': APP.config, 'password': registered_user.password } msg = render_template2('recover_password_email.txt', template_vars) send_email(APP.config['GHC_SMTP'], fromaddr, toaddr, msg) flash(gettext('Password sent via email'), 'success') if 'next' in request.args: return redirect(request.args.get('next')) return redirect(url_for('home', lang=g.current_lang))
def notify(config, resource, run, last_run_success): """execute a notification""" status_changed = False this_run_success = run.success if last_run_success and not this_run_success: result = gettext('Failing') elif not last_run_success and this_run_success: result = gettext('Fixed') elif not last_run_success and not this_run_success: result = gettext('Still Failing') elif last_run_success and this_run_success: result = gettext('Passing') if result != gettext('Passing'): status_changed = True if not status_changed: return template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msg = render_template2('notification_email.txt', template_vars) fromaddr = '%s <%s>' % (config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL']) toaddrs = config['GHC_ADMIN_EMAIL'] server = smtplib.SMTP('%s:%s' % (config['GHC_SMTP']['server'], config['GHC_SMTP']['port'])) if config['GHC_SMTP']['tls']: server.starttls() server.login(config['GHC_SMTP']['username'], config['GHC_SMTP']['password']) server.sendmail(fromaddr, toaddrs, msg) server.quit() return True
def notify(config, resource, run, last_run_success): """execute a notification""" status_changed = False this_run_success = run.success if last_run_success and not this_run_success: result = 'Failing' elif not last_run_success and this_run_success: result = 'Fixed' elif not last_run_success and not this_run_success: result = 'Still Failing' elif last_run_success and this_run_success: result = 'Passing' if result != 'Passing': status_changed = True if not status_changed: return template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msg = render_template2('notification_email.txt', template_vars) fromaddr = '%s <%s>' % (config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL']) toaddrs = config['GHC_ADMIN_EMAIL'] server = smtplib.SMTP( '%s:%s' % (config['GHC_SMTP']['server'], config['GHC_SMTP']['port'])) if config['GHC_SMTP']['tls']: server.starttls() server.login(config['GHC_SMTP']['username'], config['GHC_SMTP']['password']) server.sendmail(fromaddr, toaddrs, msg) server.quit() return True
def do_email(config, resource, run, status_changed, result): # List of global email addresses to notify, may be list or # comma-separated str "To" needs comma-separated list, # while sendmail() requires list... if isinstance(config['GHC_NOTIFICATIONS_EMAIL'], types.StringTypes): config['GHC_NOTIFICATIONS_EMAIL'] = \ config['GHC_NOTIFICATIONS_EMAIL'].split(',') # this should always be a list global_notifications = config['GHC_NOTIFICATIONS_EMAIL'] or [] if not isinstance(global_notifications, (list, tuple, set,)): raise TypeError("Cannot use {} as list of emails".format( type(global_notifications))) notifications_email = global_notifications +\ resource.get_recipients('email') if not notifications_email: LOGGER.warning("No emails for notification set for resource %s", resource.identifier) return template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msgbody = render_template2('notification_email.txt', template_vars) msg = MIMEText(msgbody, 'plain', 'utf-8') msg['From'] = email.utils.formataddr((config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL'])) msg['To'] = ','.join(notifications_email) msg['Subject'] = '[%s] %s: %s' % (config['GHC_SITE_TITLE'], result, resource.title) if not config.get('GHC_SMTP') or not\ (any([config['GHC_SMTP'][k] for k in ('port', 'server', 'username', 'password',)])): LOGGER.warning("No SMTP configuration. Not sending to %s", notifications_email) print(msg.as_string()) return server = smtplib.SMTP(config['GHC_SMTP']['server'], config['GHC_SMTP']['port']) if config['DEBUG']: server.set_debuglevel(True) try: if config['GHC_SMTP']['tls']: server.starttls() except Exception, err: LOGGER.exception("Cannot connect to smtp: %s[:%s]: %s", config['GHC_SMTP']['server'], config['GHC_SMTP']['port'], err, exc_info=err) return
def notify(config, resource, run, last_run_success): """execute a notification""" status_changed = False this_run_success = run.success if last_run_success and not this_run_success: result = gettext('Failing') elif not last_run_success and this_run_success: result = gettext('Fixed') elif not last_run_success and not this_run_success: result = gettext('Still Failing') elif last_run_success and this_run_success: result = gettext('Passing') if result != gettext('Passing'): status_changed = True # Check if still 'Still Failing' result should be notified if result == gettext('Still Failing') \ and not config['GHC_NOTIFICATIONS_VERBOSITY']: # Receive just 'Failing' and 'Fixed' notifications status_changed = False if not status_changed: return print('Notifying: status changed: result=%s' % result) template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msgbody = render_template2('notification_email.txt', template_vars) msg = MIMEText(msgbody) msg['From'] = email.utils.formataddr((config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL'])) msg['To'] = ','.join(config['GHC_NOTIFICATIONS_EMAIL']) msg['Subject'] = '[%s] %s: %s' % (config['GHC_SITE_TITLE'], result, resource.title) server = smtplib.SMTP(config['GHC_SMTP']['server'], config['GHC_SMTP']['port']) if config['DEBUG']: server.set_debuglevel(True) if config['GHC_SMTP']['tls']: server.starttls() server.login(config['GHC_SMTP']['username'], config['GHC_SMTP']['password']) try: server.sendmail(config['GHC_ADMIN_EMAIL'], config['GHC_NOTIFICATIONS_EMAIL'], msg.as_string()) finally: server.quit() return True
def do_email(config, resource, run, status_changed, result): # List of global email addresses to notify, may be list or # comma-separated str "To" needs comma-separated list, # while sendmail() requires list... if isinstance(config['GHC_NOTIFICATIONS_EMAIL'], str): config['GHC_NOTIFICATIONS_EMAIL'] = \ config['GHC_NOTIFICATIONS_EMAIL'].split(',') # this should always be a list global_notifications = config['GHC_NOTIFICATIONS_EMAIL'] or [] if not isinstance(global_notifications, ( list, tuple, set, )): raise TypeError("Cannot use {} as list of emails".format( type(global_notifications))) notifications_email = global_notifications +\ resource.get_recipients('email') if not notifications_email: LOGGER.warning("No emails for notification set for resource %s", resource.identifier) return template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msgbody = render_template2('notification_email.txt', template_vars) msg = MIMEText(msgbody, 'plain', 'utf-8') msg['From'] = email.utils.formataddr( (config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL'])) msg['To'] = ','.join(notifications_email) msg['Subject'] = '[%s] %s: %s' % (config['GHC_SITE_TITLE'], result, resource.title) if not config.get('GHC_SMTP') or not\ (any([config['GHC_SMTP'][k] for k in ('port', 'server', 'username', 'password',)])): LOGGER.warning("No SMTP configuration. Not sending to %s", notifications_email) print(msg.as_string()) return server = smtplib.SMTP(config['GHC_SMTP']['server'], config['GHC_SMTP']['port']) if config['DEBUG']: server.set_debuglevel(True) try: if config['GHC_SMTP']['tls']: server.starttls() except Exception as err: LOGGER.exception("Cannot connect to smtp: %s[:%s]: %s", config['GHC_SMTP']['server'], config['GHC_SMTP']['port'], err, exc_info=err) return try: server.login(config['GHC_SMTP']['username'], config['GHC_SMTP']['password']) except Exception as err: LOGGER.exception("Cannot log in to smtp: %s", err, exc_info=err) try: server.sendmail(config['GHC_ADMIN_EMAIL'], notifications_email, msg.as_string()) except Exception as err: LOGGER.exception(str(err), exc_info=err) finally: server.quit()
def notify(config, resource, run, last_run_success): """execute a notification""" status_changed = False this_run_success = run.success if last_run_success and not this_run_success: result = gettext('Failing') elif not last_run_success and this_run_success: result = gettext('Fixed') elif not last_run_success and not this_run_success: result = gettext('Still Failing') elif last_run_success and this_run_success: result = gettext('Passing') if result != gettext('Passing'): status_changed = True # Check if still 'Still Failing' result should be notified if result == gettext('Still Failing') \ and not config['GHC_NOTIFICATIONS_VERBOSITY']: # Receive just 'Failing' and 'Fixed' notifications status_changed = False if not status_changed: return print('Notifying: status changed: result=%s' % result) template_vars = { 'result': result, 'config': config, 'resource': resource, 'run': run } msgbody = render_template2('notification_email.txt', template_vars) msg = MIMEText(msgbody) msg['From'] = email.utils.formataddr( (config['GHC_SITE_TITLE'], config['GHC_ADMIN_EMAIL'])) # List of email addresses to notify, may be list or comma-separated str # "To" needs comma-separated list, while sendmail() requires list... notifications_email = config['GHC_NOTIFICATIONS_EMAIL'] if type(notifications_email) is list: notifications_email = ','.join(config['GHC_NOTIFICATIONS_EMAIL']) if type(config['GHC_NOTIFICATIONS_EMAIL']) is str: config['GHC_NOTIFICATIONS_EMAIL'] = \ config['GHC_NOTIFICATIONS_EMAIL'].split(',') msg['To'] = notifications_email msg['Subject'] = '[%s] %s: %s' % (config['GHC_SITE_TITLE'], result, resource.title) server = smtplib.SMTP(config['GHC_SMTP']['server'], config['GHC_SMTP']['port']) if config['DEBUG']: server.set_debuglevel(True) if config['GHC_SMTP']['tls']: server.starttls() server.login(config['GHC_SMTP']['username'], config['GHC_SMTP']['password']) try: server.sendmail(config['GHC_ADMIN_EMAIL'], config['GHC_NOTIFICATIONS_EMAIL'], msg.as_string()) except Exception as err: LOGGER.exception(str(err)) finally: server.quit() return True