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

Avoid depending on app context when templating failure reports #4231

Merged
merged 5 commits into from
Oct 15, 2019
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
7 changes: 3 additions & 4 deletions redash/tasks/failure_report.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import datetime
import re
from collections import Counter
from flask import render_template
from redash.tasks.general import send_mail
from redash.worker import celery
from redash import redis_connection, settings, models
from redash.utils import json_dumps, json_loads, base_url
from redash.utils import json_dumps, json_loads, base_url, render_template


def key(user_id):
Expand Down Expand Up @@ -50,9 +49,9 @@ def send_failure_report(user_id):
'base_url': base_url(user.org)
}

html = render_template('emails/failures.html', **context)
text = render_template('emails/failures.txt', **context)
subject = "Redash failed to execute {} of your scheduled queries".format(len(unique_errors.keys()))
html, text = [render_template('emails/failures.{}'.format(f), context) for f in ['html', 'txt']]

send_mail.delay([user.email], subject, html, text)

redis_connection.delete(key(user_id))
Expand Down
9 changes: 9 additions & 0 deletions redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pystache
import pytz
import simplejson
from flask import current_app
from funcy import select_values
from redash import settings
from sqlalchemy.orm.query import Query
Expand Down Expand Up @@ -205,3 +206,11 @@ def wrapper(K):
return K

return wrapper


def render_template(path, context):
""" Render a template with context, without loading the entire app context.
Using Flask's `render_template` function requires the entire app context to load, which in turn triggers any
function decorated with the `context_processor` decorator, which is not explicitly required for rendering purposes.
"""
current_app.jinja_env.get_template(path).render(**context)
2 changes: 1 addition & 1 deletion tests/tasks/test_failure_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def notify(self, message="Oh no, I failed!", query=None, **kwargs):
def send_email(self, user, render_template):
send_failure_report(user.id)

_, context = render_template.call_args
_, context = render_template.call_args[0]
return context['failures']

def test_schedules_email_if_failure_count_is_beneath_limit(self):
Expand Down