Select Git revision
test_main.cpp
-
Ondřej Mička authoredOndřej Mička authored
util.py 1.66 KiB
# Různé
import datetime
import email.message
import email.headerregistry
import re
from sqlalchemy.orm import joinedload
import subprocess
from typing import Any, Optional
import textwrap
import mo.db as db
import config
current_log_user: Optional[db.User] = None
def log(type: db.LogType, what: int, details: Any):
entry = db.Log(
changed_by=current_log_user.user_id if current_log_user else None,
type=type,
id=what,
details=details,
)
db.get_session().add(entry)
def send_password_reset_email(user: db.User, link: str):
if not hasattr(config, 'MAIL_FROM'):
raise RuntimeError('V configu chybí pole MAIL_FROM.')
msg = email.message.EmailMessage()
msg['From'] = email.headerregistry.Address(
display_name='Odevzdávací Systém MO', addr_spec=config.MAIL_FROM
)
msg['To'] = [
email.headerregistry.Address(
display_name='{} {}'.format(user.first_name, user.last_name),
addr_spec=user.email,
)
]
msg['Subject'] = 'OSMO – obnova hesla'
msg['Date'] = datetime.datetime.now()
body = textwrap.dedent('''
Pro obnovení hesla pro váš účet v Odevzávacím Systému MO klikněte sem: {}
Váš OSMO
'''.format(link))
msg.set_content(body, cte='quoted-printable')
sm = subprocess.Popen(
[
'/usr/sbin/sendmail',
'-oi',
'-f',
config.MAIL_FROM,
user.email,
],
stdin=subprocess.PIPE,
)
sm.communicate(msg.as_bytes())
if sm.returncode != 0:
raise RuntimeError('Sendmail failed with return code {}'.format(sm.returncode))