Coverage for portality/events/consumers/account_created_email.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-04 15:38 +0100

1# from flask import url_for 

2from portality.util import url_for 

3 

4from portality.events.consumer import EventConsumer 

5from portality import constants 

6from portality import app_email 

7from portality import models 

8from portality.core import app 

9from portality.bll.exceptions import NoSuchPropertyException 

10 

11 

12class AccountCreatedEmail(EventConsumer): 

13 ID = "account:created:email" 

14 

15 @classmethod 

16 def consumes(cls, event): 

17 return event.id == constants.EVENT_ACCOUNT_CREATED and event.context.get("account") is not None 

18 

19 @classmethod 

20 def consume(cls, event): 

21 context = event.context 

22 acc = models.Account(**context.get("account")) 

23 if not acc.reset_token or not acc.email: 

24 raise NoSuchPropertyException("Account {x} does not have a reset_token and/or an email address".format(x=acc.id)) 

25 cls._send_account_created_email(acc) 

26 

27 @classmethod 

28 def _send_account_created_email(cls, account: models.Account): 

29 forgot_pw_url = app.config.get('BASE_URL', "https://doaj.org") + url_for('account.forgot') 

30 reset_url = forgot_pw_url 

31 

32 if account.reset_token is not None: 

33 reset_url = app.config.get('BASE_URL', "https://doaj.org") + url_for('account.reset', reset_token=account.reset_token) 

34 

35 password_create_timeout_seconds = int( 

36 app.config.get("PASSWORD_CREATE_TIMEOUT", app.config.get('PASSWORD_RESET_TIMEOUT', 86400) * 14)) 

37 password_create_timeout_days = int(password_create_timeout_seconds / (60 * 60 * 24)) 

38 

39 to = [account.email] 

40 fro = app.config.get('SYSTEM_EMAIL_FROM', 'helpdesk@doaj.org') 

41 subject = app.config.get("SERVICE_NAME", "") + " - account created, please verify your email address" 

42 

43 app_email.send_mail(to=to, 

44 fro=fro, 

45 subject=subject, 

46 template_name="email/account_created.jinja2", 

47 reset_url=reset_url, 

48 email=account.email, 

49 timeout_days=password_create_timeout_days, 

50 forgot_pw_url=forgot_pw_url 

51 )