Example #1
0
def send_template(recipient, subject, template_name, context=None, theme=None, **kwargs):
    """
    Renders a template and sends an email in the same way as :func:`send`.
    templates should be stored in ``/templates/email/<template>.html`` and ``/templates/email/<template>.txt``.
    If the txt template is not specified, then the html one will be used for the "plaintext" body.

    For example::

        mail.send_template(
            recipient='*****@*****.**',
            subject='A Test Email',
            template_name='test',
            context={
                'name': 'George'
            }
        )

    Would render the template ``/templates/email/test.html`` and email the rendered html.
    """
    context = context if context else {}

    name = ('email/' + template_name + '.html', template)
    body = template.render_template(name, context, theme=theme)

    try:
        name = ('email/' + template_name + '.txt', template)
        text_body = template.render_template(name, context, theme=theme)
    except:
        logging.warning("No txt template found for email template %s, html will be used for plaintext as well." % template_name)
        text_body = None

    res = send(recipient, subject, body, text_body=text_body, **kwargs)

    return res, body, text_body
Example #2
0
def send_template(recipient,
                  subject,
                  template_name,
                  context=None,
                  theme=None,
                  **kwargs):
    """
    Renders a template and sends an email in the same way as :func:`send`.
    templates should be stored in ``/templates/email/<template>.html``.

    For example:

        mail.send(
            recipient='*****@*****.**',
            subject='A Test Email',
            template_name='test',
            context={
                'name': 'George'
            })

    Would render the template ``/templates/email/test.html``.
    """
    name = ('email/' + template_name + '.html', template)
    context = context if context else {}
    body = template.render_template(name, context, theme=theme)
    res = send(recipient, subject, body, **kwargs)
    return res, body
Example #3
0
def send_template(recipient,
                  subject,
                  template_name,
                  context=None,
                  theme=None,
                  **kwargs):
    """
    Renders a template and sends an email in the same way as :func:`send`.
    templates should be stored in ``/templates/email/<template>.html`` and ``/templates/email/<template>.txt``.
    If the txt template is not specified, then the html one will be used for the "plaintext" body.

    For example::

        mail.send_template(
            recipient='*****@*****.**',
            subject='A Test Email',
            template_name='test',
            context={
                'name': 'George'
            }
        )

    Would render the template ``/templates/email/test.html`` and email the rendered html.
    """
    context = context if context else {}

    name = ('email/' + template_name + '.html', template)
    body = template.render_template(name, context, theme=theme)

    try:
        name = ('email/' + template_name + '.txt', template)
        text_body = template.render_template(name, context, theme=theme)
    except:
        logging.warning(
            "No txt template found for email template %s, html will be used for plaintext as well."
            % template_name)
        text_body = None

    res = send(recipient, subject, body, text_body=text_body, **kwargs)

    return res, body, text_body
    def inner(request, response, exception):
        logging.exception(exception)

        response.set_status(code)

        if 'application/json' in request.headers.get('Accept', []) or request.headers.get('Content-Type') == 'application/json':
            response.text = unicode(json.dumps({
                'error': str(exception),
                'code': code
            }, encoding='utf-8', ensure_ascii=False))

        else:
            response.content_type = 'text/html'
            response.text = render_template(template, {'request': request, 'exception': exception})
def handle_403(request, response, exception):
    logging.exception(exception)
    response.set_status(403)

    if 'application/json' in request.headers.get('Accept') or request.headers.get('Content-Type') == 'application/json':
        response.text = unicode(json.dumps({
            'error': str(exception), 
            'code': 403
        }, encoding='utf-8', ensure_ascii=False))

    else:
        template = ('errors/403.html', 'errors/500.html')
        login_url, login_name = create_login_for(request)
        context = { 'request': request, 'exception': exception, 'login_url': login_url, 'login_name': login_name }
        response.content_type = 'text/html'
        response.text = render_template(template, context)
Example #6
0
def send_template(recipient, subject, template_name, context=None, theme=None, **kwargs):
    """
    Renders a template and sends an email in the same way as :func:`send`.
    templates should be stored in ``/templates/email/<template>.html``.

    For example:

        mail.send(
            recipient='*****@*****.**',
            subject='A Test Email',
            template_name='test',
            context={
                'name': 'George'
            })

    Would render the template ``/templates/email/test.html``.
    """
    name = ('email/' + template_name + '.html', template)
    context = context if context else {}
    body = template.render_template(name, context, theme=theme)
    res = send(recipient, subject, body, **kwargs)
    return res, body
Example #7
0
    def inner(request, response, exception):
        logging.exception(exception)

        response.set_status(code)

        if 'application/json' in request.headers.get(
                'Accept') or request.headers.get(
                    'Content-Type') == 'application/json':
            response.text = unicode(
                json.dumps({
                    'error': str(exception),
                    'code': code
                },
                           encoding='utf-8',
                           ensure_ascii=False))

        else:
            response.content_type = 'text/html'
            response.text = render_template(template, {
                'request': request,
                'exception': exception
            })
Example #8
0
 def inner(request, response, exception):
     logging.exception(exception)
     response.content_type = 'text/html'
     response.text = render_template(template, {'request': request, 'exception': exception})
     response.set_status(code)