-
Notifications
You must be signed in to change notification settings - Fork 16
/
account_passwordreset_email.py
42 lines (35 loc) · 1.77 KB
/
account_passwordreset_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# ~~ AccountPasswordResetEmail:Consumer ~~
from portality.util import url_for
from portality.events.consumer import EventConsumer
from portality import constants
from portality import app_email
from portality import models
from portality.core import app
from portality.bll.exceptions import NoSuchPropertyException
class AccountPasswordResetEmail(EventConsumer):
ID = "account:password_reset:email"
@classmethod
def should_consume(cls, event):
return event.id == constants.EVENT_ACCOUNT_PASSWORD_RESET and event.context.get("account") is not None
@classmethod
def consume(cls, event):
acc = models.Account(**event.context.get("account"))
if not acc.reset_token or not acc.email:
raise NoSuchPropertyException("Account {x} does not have a reset_token and/or an email address".format(x=acc.id))
cls._send_password_reset_email(acc)
@classmethod
def _send_password_reset_email(cls, account: models.Account):
# ~~-> AccountPasswordReset:Email~~
reset_url = app.config.get('BASE_URL', "https://doaj.org") + url_for('account.reset', reset_token=account.reset_token)
to = [account.email]
fro = app.config.get('SYSTEM_EMAIL_FROM', app.config['ADMIN_EMAIL'])
subject = app.config.get("SERVICE_NAME", "") + " - password reset"
# ~~-> Email:Library ~~
app_email.send_mail(to=to,
fro=fro,
subject=subject,
template_name="email/account_password_reset.jinja2",
email=account.email,
reset_url=reset_url,
forgot_pw_url=app.config.get('BASE_URL', "https://doaj.org") + url_for('account.forgot')
)