def send(mailto, subject, content): """Send an e-mail. `mailto` must be a normalized e-mail address to send this e-mail to. The system email will be designated as the sender. """ message = MIMEText(content.strip()) message["To"] = mailto message["From"] = macro.MACRO_EMAIL_ADDRESS message["Subject"] = subject # smtp.sendmail() only converts CR and LF (produced by MIMEText and our templates) to CRLF in Python 3. In Python 2, we need this: msg_crlf = re.sub(r"\r\n|[\r\n]", "\r\n", message.as_string()) smtp = SMTP(config_read_setting('host', "localhost", section='smtp')) try: smtp.sendmail( from_addr=macro.MACRO_EMAIL_ADDRESS, to_addrs=[mailto], msg=msg_crlf, ) finally: smtp.quit() define.metric('increment', 'emails')
def _active_users(seconds): usercount_url_template = config_read_setting('url_template', section='usercount') if not usercount_url_template: return try: resp = http_get(usercount_url_template % (seconds,)) except WeasylError: return if resp.status_code != 200: return return resp.json()['users']
def _decrypt_totp_secret(totp_secret): """ Decrypt a symmetrically encrypted 2FA TOTP secret. Parameters: - totp_secret: An encrypted Fernet token. Returns: The decrypted plaintext 2FA TOTP secret corresponding to the ciphertext `totp_secret`. """ key = config.config_read_setting(setting='secret_key', section='two_factor_auth') f = Fernet(key) return f.decrypt(bytes(totp_secret))
def _encrypt_totp_secret(totp_secret): """ Symmetrically encrypt a 2FA TOTP secret. Parameters: - totp_secret: A 2FA TOTP secret key to encrypt prior to storing in the DB. Returns: An encrypted Fernet token. """ key = config.config_read_setting(setting='secret_key', section='two_factor_auth') f = Fernet(key) return f.encrypt(bytes(totp_secret))
def absolutify_url(url): cdn_root = config_read_setting("cdn_root") if cdn_root and url.startswith(cdn_root): return url return urljoin(get_current_request().application_url, url)
def cdnify_url(url): cdn_root = config_read_setting("cdn_root") if not cdn_root: return url return urljoin(cdn_root, url)
import logging import os from akismet import Akismet, SpamStatus # Imported as RequestsConnectionError to not conflict with Python 3's built-in ConnectionError exception from requests.exceptions import ConnectionError as RequestsConnectionError from weasyl import config from weasyl import define as d from weasyl.error import WeasylError FILTERING_ENABLED = config.config_read_bool(setting='enabled', section='spam_filtering') AKISMET_KEY = config.config_read_setting(setting='key', value=None, section='spam_filtering') # Used to set the `is_spam` flag on requests sent to the backend _IS_ENVIRONMENT_TESTING = True if os.environ.get( "WEASYL_TESTING_ENV") else False # AKA, the link to the main page of the web application AKISMET_BLOG_URL = "http://lo.weasyl.com" if _IS_ENVIRONMENT_TESTING else "https://www.weasyl.com" if all([FILTERING_ENABLED, AKISMET_KEY]): _akismet = Akismet(AKISMET_KEY, blog=AKISMET_BLOG_URL, application_user_agent="weasyl/+({0})".format( "https://github.com/weasyl/weasyl")) else: FILTERING_ENABLED = False
for key, value in query.items(): if isinstance(value, (tuple, list, set)): for subvalue in value: if isinstance(subvalue, unicode): pairs.append((key, subvalue.encode("utf-8"))) else: pairs.append((key, subvalue)) elif isinstance(value, unicode): pairs.append((key, value.encode("utf-8"))) elif value: pairs.append((key, value)) return urllib.urlencode(pairs) _REQUESTS_PROXY = config_read_setting('requests_wrapper', section='proxy') _REQUESTS_PROXIES = {} if _REQUESTS_PROXY is None else dict.fromkeys(['http', 'https'], _REQUESTS_PROXY) def _requests_wrapper(func_name): func = getattr(requests, func_name) def wrapper(*a, **kw): request = get_current_request() try: return func(*a, proxies=_REQUESTS_PROXIES, **kw) except Exception as e: request.log_exc(level=logging.DEBUG) w = WeasylError('httpError') w.error_suffix = 'The original error was: %s' % (e,) raise w
def absolutify_url(url): cdn_root = config_read_setting("cdn_root") if cdn_root and url.startswith(cdn_root): return url return urlparse.urljoin(web.ctx.realhome, url)
def cdnify_url(url): cdn_root = config_read_setting("cdn_root") if not cdn_root: return url return urlparse.urljoin(cdn_root, url)
def absolutify_url(url): cdn_root = config_read_setting("cdn_root") if cdn_root and url.startswith(cdn_root): return url return urlparse.urljoin(get_current_request().application_url, url)