<s> """<STR_LIT>""" <EOL> from contextlib import contextmanager <EOL> import hashlib <EOL> from logging import getLogger <EOL> from flask import current_app <EOL> from flask . ext . mail import Message <EOL> from werkzeug . debug import tbtools <EOL> from pypi_portal . extensions import mail , redis <EOL> from pypi_portal . models . redis import EMAIL_THROTTLE <EOL> LOG = getLogger ( __name__ ) <EOL> @ contextmanager <EOL> def _override_html ( ) : <EOL> """<STR_LIT>""" <EOL> old_page_html = tbtools . PAGE_HTML <EOL> old_summary = tbtools . SUMMARY_HTML <EOL> old_frame = tbtools . FRAME_HTML <EOL> email_template = current_app . jinja_env . get_template ( '<STR_LIT>' ) <EOL> email_context = email_template . new_context ( ) <EOL> page_html = email_template . blocks [ '<STR_LIT>' ] ( email_context ) . next ( ) <EOL> summary_html = email_template . blocks [ '<STR_LIT>' ] ( email_context ) . next ( ) <EOL> frame_html = email_template . blocks [ '<STR_LIT>' ] ( email_context ) . next ( ) <EOL> tbtools . PAGE_HTML = page_html <EOL> tbtools . SUMMARY_HTML = summary_html <EOL> tbtools . FRAME_HTML = frame_html <EOL> yield <EOL> tbtools . PAGE_HTML = old_page_html <EOL> tbtools . SUMMARY_HTML = old_summary <EOL> tbtools . FRAME_HTML = old_frame <EOL> def send_exception ( subject ) : <EOL> """<STR_LIT>""" <EOL> tb = tbtools . get_current_traceback ( ) <EOL> with _override_html ( ) : <EOL> html = tb . render_full ( ) . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> html = html . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> subject = '<STR_LIT>' . format ( subject ) <EOL> md5 = hashlib . md5 ( '<STR_LIT>' . format ( subject , html ) ) . hexdigest ( ) <EOL> seconds = int ( current_app . config [ '<STR_LIT>' ] ) <EOL> lock = redis . lock ( EMAIL_THROTTLE . format ( md5 = md5 ) , timeout = seconds ) <EOL> have_lock = lock . acquire ( blocking = False ) <EOL> if not have_lock : <EOL> LOG . debug ( '<STR_LIT>' . format ( subject ) ) <EOL> return <EOL> msg = Message ( subject = subject , recipients = current_app . config [ '<STR_LIT>' ] , html = html ) <EOL> mail . send ( msg ) <EOL> def send_email ( subject , body = None , html = None , recipients = None , throttle = None ) : <EOL> """<STR_LIT>""" <EOL> recipients = recipients or current_app . config [ '<STR_LIT>' ] <EOL> if throttle is not None : <EOL> md5 = hashlib . md5 ( '<STR_LIT>' . format ( subject , body , html , recipients ) ) . hexdigest ( ) <EOL> seconds = throttle . total_seconds ( ) if hasattr ( throttle , '<STR_LIT>' ) else throttle <EOL> lock = redis . lock ( EMAIL_THROTTLE . format ( md5 = md5 ) , timeout = int ( seconds ) ) <EOL> have_lock = lock . acquire ( blocking = False ) <EOL> if not have_lock : <EOL> LOG . debug ( '<STR_LIT>' . format ( subject ) ) <EOL> return <EOL> msg = Message ( subject = subject , recipients = recipients , body = body , html = html ) <EOL> mail . send ( msg ) </s>
