Skip to content
Snippets Groups Projects
Commit 96bcf4e2 authored by geant-release-service's avatar geant-release-service
Browse files

Finished release 0.33.

parents fc604644 4f49611a
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [0.33] - 2023-08-27
- Fixed utf-8 issues with sending emails
- Fixed bug with applying migrations disabling all logging
## [0.32] - 2023-08-27 ## [0.32] - 2023-08-27
- Send an email to the user on first login - Send an email to the user on first login
- Change NREN to (N)REN on the landing page - Change NREN to (N)REN on the landing page
......
import smtplib import smtplib
import threading import threading
import logging import logging
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import Sequence, Union from typing import Sequence, Union
from sqlalchemy import select from sqlalchemy import select
from flask import current_app from flask import current_app
...@@ -22,11 +24,11 @@ Daniel and Jennifer (the Compendium admins) ...@@ -22,11 +24,11 @@ Daniel and Jennifer (the Compendium admins)
""" # noqa: E501 """ # noqa: E501
def _send_mail(smtp_server, port, sender_email, recipients, message): def _send_mail(smtp_server: str, port: int, recipients: Union[str, Sequence[str]], message: MIMEMultipart):
try: try:
with smtplib.SMTP(smtp_server, port) as server: with smtplib.SMTP(smtp_server, port) as server:
server.sendmail(from_addr=sender_email, to_addrs=recipients, msg=message) server.sendmail(from_addr=message['From'], to_addrs=recipients, msg=message.as_string())
logger.debug('Successfully sent email') logger.debug('Successfully sent email')
except Exception: except Exception:
logger.exception('Unable to send email:') logger.exception('Unable to send email:')
...@@ -53,16 +55,18 @@ def send_mail( ...@@ -53,16 +55,18 @@ def send_mail(
if not recipients: if not recipients:
recipients = admin_emails recipients = admin_emails
subject = subject.replace('\n', ' ') message = MIMEMultipart('alternative')
message = f"""Subject: {subject}\n\n{contents}""" message['Subject'] = subject
message['From'] = current_app.config['MAIL_SENDER_EMAIL']
message['To'] = ', '.join(recipients)
message.attach(MIMEText(contents, 'plain', 'utf-8'))
smtp_server = current_app.config['MAIL_SERVER'] smtp_server = current_app.config['MAIL_SERVER']
port = current_app.config['MAIL_PORT'] port = current_app.config['MAIL_PORT']
sender_email = current_app.config['MAIL_SENDER_EMAIL']
# spin off a thread since this can take some time.. # spin off a thread since this can take some time..
logger.debug('Sending email') logger.debug('Sending email')
thread = threading.Thread(target=_send_mail, args=(smtp_server, port, sender_email, recipients, message)) thread = threading.Thread(target=_send_mail, args=(smtp_server, port, recipients, message))
thread.start() thread.start()
......
...@@ -20,12 +20,16 @@ LOGGING_DEFAULT_CONFIG = { ...@@ -20,12 +20,16 @@ LOGGING_DEFAULT_CONFIG = {
}, },
}, },
'loggers': { 'loggers': {
'compendium_v2': { 'compendium_v2': {
'level': 'DEBUG', 'level': 'DEBUG',
'handlers': ['console'], 'handlers': ['console'],
'propagate': False 'propagate': False
},
'werkzeug': {
'level': 'WARNING',
'handlers': ['console'],
'propagate': False
} }
}, },
......
import logging import logging
from logging.config import fileConfig
from flask import current_app from flask import current_app
...@@ -9,10 +8,6 @@ from alembic import context ...@@ -9,10 +8,6 @@ from alembic import context
# access to the values within the .ini file in use. # access to the values within the .ini file in use.
config = context.config config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env') logger = logging.getLogger('alembic.env')
......
...@@ -2,7 +2,7 @@ from setuptools import setup, find_packages ...@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup( setup(
name='compendium-v2', name='compendium-v2',
version="0.32", version="0.33",
author='GEANT', author='GEANT',
author_email='swd@geant.org', author_email='swd@geant.org',
description='Flask and React project for displaying ' description='Flask and React project for displaying '
......
from typing import List
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText # noqa: F401
from contextlib import contextmanager from contextlib import contextmanager
from compendium_v2.db.auth_model import User from compendium_v2.db.auth_model import User
from compendium_v2.email import send_admin_signup_notification, send_user_signup_notification from compendium_v2.email import send_admin_signup_notification, send_user_signup_notification
def decode_message(msg: MIMEMultipart):
message: List[MIMEText] = msg.get_payload()
text: MIMEText = message[0]
decoded = text.get_payload(decode=True).decode('utf-8')
return decoded
@contextmanager @contextmanager
def test_user(app): def test_user(app):
with app.app_context(): with app.app_context():
...@@ -18,8 +28,10 @@ def test_user(app): ...@@ -18,8 +28,10 @@ def test_user(app):
def test_signup_email_admin(app, mocked_admin_user, mocker): def test_signup_email_admin(app, mocked_admin_user, mocker):
def _send_mail(*args, **kwargs): def _send_mail(*args, **kwargs):
_msg = args[-1]
decoded = decode_message(_msg)
message = 'testname has just signed up with the email testmail321@email.com and provider ID testsub' message = 'testname has just signed up with the email testmail321@email.com and provider ID testsub'
assert args[-1].split('\n\n')[-1] == message assert decoded == message
mocker.patch('compendium_v2.email._send_mail', _send_mail) mocker.patch('compendium_v2.email._send_mail', _send_mail)
with test_user(app) as user: with test_user(app) as user:
...@@ -29,7 +41,9 @@ def test_signup_email_admin(app, mocked_admin_user, mocker): ...@@ -29,7 +41,9 @@ def test_signup_email_admin(app, mocked_admin_user, mocker):
def test_signup_email_user(app, mocker): def test_signup_email_user(app, mocker):
def _send_mail(*args, **kwargs): def _send_mail(*args, **kwargs):
assert len(args[-1].split('\n\n', 1)[-1]) > 50 # check that there's a message _msg = args[-1]
decoded = decode_message(_msg)
assert len(decoded) > 50 # check that there's a message
mocker.patch('compendium_v2.email._send_mail', _send_mail) mocker.patch('compendium_v2.email._send_mail', _send_mail)
with test_user(app) as user: with test_user(app) as user:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment