Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added emails status across email calls taking place in views #216

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions app/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ def register():
subject='Confirm Your Account',
template='account/email/confirm',
user=user,
mailTriggerStatus=current_app.config["SEND_MAIL_STATUS"],
confirm_link=confirm_link)
flash('A confirmation link has been sent to {}.'.format(user.email),
if current_app.config["SEND_MAIL_STATUS"]:
flash('A confirmation link has been sent to {}.'.format(user.email),
'warning')
else:
flash('You are running in dev or test mode. Your account needs'
' to be confirmed via the command line interface or through the CLI', 'warning')
return redirect(url_for('main.index'))
return render_template_with_nav_info('account/register.html', form=form)

Expand Down Expand Up @@ -106,9 +111,15 @@ def reset_password_request():
template='account/email/reset_password',
user=user,
reset_link=reset_link,
mailTriggerStatus=current_app.config["SEND_MAIL_STATUS"],
next=request.args.get('next'))
flash('A password reset link has been sent to {}.'.format(
form.email.data), 'warning')
if current_app.config["SEND_MAIL_STATUS"]:
flash('A password reset link has been sent to {}.'.format(
form.email.data), 'warning')
else:
flash('You are running in dev or test mode. No emails can be sent'
'for this function. Use the admin account'
' (check source code for passwords)', 'warning')
return redirect(url_for('account.login'))
return render_template_with_nav_info('account/reset_password.html', form=form)

Expand Down Expand Up @@ -170,9 +181,15 @@ def change_email_request():
# current_user is a LocalProxy, we want the underlying user
# object
user=current_user._get_current_object(),
mailTriggerStatus=current_app.config["SEND_MAIL_STATUS"],
change_email_link=change_email_link)
flash('A confirmation link has been sent to {}.'.format(new_email),
if current_app.config["SEND_MAIL_STATUS"]:
flash('A confirmation link has been sent to {}.'.format(new_email),
'warning')
else:
flash('You are running in dev or test mode. Your account needs'
' to be confirmed via the command line interface or through the CLI',
'warning')
return redirect(url_for('main.index'))
else:
flash('Invalid email or password.', 'form-error')
Expand Down Expand Up @@ -203,8 +220,15 @@ def confirm_request():
template='account/email/confirm',
# current_user is a LocalProxy, we want the underlying user object
user=current_user._get_current_object(),
mailTriggerStatus=current_app.config["SEND_MAIL_STATUS"],
confirm_link=confirm_link)
flash('A new confirmation link has been sent to {}.'.format(current_user.email), 'warning')
if current_app.config["SEND_MAIL_STATUS"]:
flash('A new confirmation link has been sent to {}.'.format(
current_user.email), 'warning')
else:
flash('You are running in dev or test mode.'
' Your account needs to be confirmed via the command line'
' interface or through the CLI', 'warning')
return redirect(url_for('main.index'))


Expand Down Expand Up @@ -266,7 +290,13 @@ def join_from_invite(user_id, token):
subject='You Are Invited To Join',
template='account/email/invite',
user=new_user,
mailTriggerStatus=current_app.config["SEND_MAIL_STATUS"],
invite_link=invite_link)
if not current_app.config["SEND_MAIL_STATUS"]:
flash('You are running this application without mail server.'
' This functionnality can\'t work: no email was sent. '
'Check the CLI or use SQL commands to confirm the account.',
'warning')
return redirect(url_for('main.index'))


Expand Down
25 changes: 15 additions & 10 deletions app/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ def _async(app, msg):
logger.warning(str(e))


def send_email_async(app, recipient, subject, template, bcc=None, **kwargs):
if not isinstance(recipient, list):
recipient = [recipient]
msg = Message(
app.config['EMAIL_SUBJECT_PREFIX'] + ' ' + subject,
sender=app.config['EMAIL_SENDER'],
recipients=recipient, bcc=bcc)
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
Thread(target=_async, args=(app, msg)).start()
def send_email_async(app, recipient, subject, template, bcc=None, mailTriggerStatus=False, **kwargs):
if (mailTriggerStatus):
if not isinstance(recipient, list):
recipient = [recipient]
msg = Message(
app.config['EMAIL_SUBJECT_PREFIX'] + ' ' + subject,
sender=app.config['EMAIL_SENDER'],
recipients=recipient, bcc=bcc)
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
Thread(target=_async, args=(app, msg)).start()
else:
logger.info('Warning, you are using Pyrrha without mail confirmation.'
' If you are running it locally or for development purposes, do not worry'
' about this message. If you are using an online version, well, worry about it.')
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Config:
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER')
SEND_MAIL_STATUS = os.environ.get('SEND_MAIL_STATUS', True)

# Admin account
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'admin'
Expand Down Expand Up @@ -61,6 +62,7 @@ class DevelopmentConfig(Config):
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'admin'
ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL') or '[email protected]'
EMAIL_SUBJECT_PREFIX = '[{}]'.format(Config.APP_NAME)
SEND_MAIL_STATUS = os.environ.get('SEND_MAIL_STATUS', False)
EMAIL_SENDER = '{app_name} Admin <{email}>'.format(app_name=Config.APP_NAME, email=MAIL_USERNAME)

LEMMATIZERS = [
Expand All @@ -86,6 +88,7 @@ class TestConfig(Config):
MAIL_USERNAME = os.environ.get('MAIL_USERNAME') or '[email protected]'
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') or 'c3c7cc3c785815a8728a6266745a70db-b6183ad4-c78d7487'
MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER')
SEND_MAIL_STATUS = os.environ.get('SEND_MAIL_STATUS', False)

ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') or 'admin'
ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL') or '[email protected]'
Expand Down