Skip to content
Browse files

Refactor mail send

  • Loading branch information...
1 parent 2687ada commit 209c1c6898dd384077d2cc8b5c81d191c6a93501 @klen committed
Showing with 16 additions and 10 deletions.
  1. +2 −2 Makefile
  2. +3 −3 base/config/core.py
  3. +1 −1 base/config/production.py
  4. +3 −3 base/core/__init__.py
  5. +0 −1 base/core/templates/core/500.html
  6. +7 −0 base/core/tests.py
View
4 Makefile
@@ -50,8 +50,8 @@ test: .env/ manage.py clean
# target: clean - Clean repo
.PHONY: clean
clean:
- find $(CURDIR) -name "*.pyc" -delete
- find $(CURDIR) -name "*.orig" -delete
+ rm -f *.py[co] *.orig
+ rm -f */*.py[co] */*.orig
# target: babel - Recompile language files
View
6 base/config/core.py
@@ -22,6 +22,6 @@
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
-MAIL_USERNAME = 'username@gmail.com'
-MAIL_PASSWORD = 'password_here'
-DEFAULT_MAIL_SENDER = 'Admin <%s>' % MAIL_USERNAME
+MAIL_USERNAME = None
+MAIL_PASSWORD = None
+DEFAULT_MAIL_SENDER = MAIL_USERNAME and ('Admin <%s>' % MAIL_USERNAME) or None
View
2 base/config/production.py
@@ -5,7 +5,7 @@
SECRET_KEY = 'SecretKeyForSessionSigning'
-ADMINS = frozenset([MAIL_USERNAME])
+ADMINS = MAIL_USERNAME and [MAIL_USERNAME] or None
# flask.ext.collect
# -----------------
View
6 base/core/__init__.py
@@ -1,5 +1,5 @@
" base.core "
-from flask import request
+from flask import request, current_app
from flask_mail import Message
from logging import Handler, ERROR
@@ -22,7 +22,7 @@ def loader_meta(app=None):
from flask import render_template
app.errorhandler(404)(lambda e: (render_template('core/404.html'), 404))
- if not app.debug:
+ if not app.debug and app.config.get('ADMINS'):
mailhandler = FlaskMailHandler(ERROR)
app.logger.addHandler(mailhandler)
@@ -33,7 +33,7 @@ class FlaskMailHandler(Handler):
def emit(self, record):
sbj = "APP ERROR: %s%s" % (request.host_url.rstrip('/'), request.path)
- msg = Message(sbj, body=self.format(record), recipients=mail.app.config.get('ADMINS', []))
+ msg = Message(sbj, body=self.format(record), recipients=current_app.config.get('ADMINS', []))
mail.send(msg)
View
1 base/core/templates/core/500.html
@@ -4,4 +4,3 @@
<p>{{ _('The server encountered an unexpected error.') }}</p>
<p>{{ _('Please try again later.') }}</p>
{% endblock %}
-
View
7 base/core/tests.py
@@ -88,7 +88,14 @@ def test_after_change(self):
self.assertEqual(Alembic.before_new.call_count, 1)
def test_mail_handler(self):
+ from . import FlaskMailHandler
from ..ext import mail
+
+ mail.username = 'test@test.com'
+ mail.password = 'test'
+ self.app.config['ADMINS'] = ['test@test.com']
+ self.app.config['DEFAULT_MAIL_SENDER'] = 'test@test.com'
+ self.app.logger.addHandler(FlaskMailHandler(40))
with mail.record_messages() as outbox:
self.app.logger.error('Attention!')
self.assertTrue(outbox)

0 comments on commit 209c1c6

Please sign in to comment.
Something went wrong with that request. Please try again.