Skip to main content

Applies a monkeypatch which forces Django's template language to error far more loudly about invalid assumptions

Project description

Author:

Keryn Knight

Version:
0.1.3

Rationale

Given a template like this:

<html><head></head>
<body>
{% if chef.can_add_cakes %}
    <label class="alert alert-{{ chef.is_cake_chef|yesno:"success,danger,default" }}
{% endif %}

everything works fine, until any of the following happens:

  • chef is no longer the name of the variable.

  • can_add_cakes is refactored to be called can_add_pastries

  • is_cake_chef is renamed is_pastry_king

If those happen, the template will either silently display nothing, or will display the label incorrectly.

This app applies a monkeypatch which forces Django’s template language to error far more loudly about invalid assumptions.

Specifically:

  • chef would raise an exception if the variable were called sous_chef

  • chef.can_add_cakes would raise an exception if can_add_cakes was no longer a valid attribute/property/method of chef

  • chef.is_cake_chef would raise an exception for the same reasons.

Thus you can refactor somewhat more freely, knowing that if the template renders it’s OK. It ain’t compile time safety, but it’s better than silently swallowing errors because you forgot something!

Setup

Add shouty or shouty.Shout to your settings.INSTALLED_APPS

Optional configuration

  • settings.SHOUTY_VARIABLES may be True|False and determines if the exception is raised when trying to use a variable which doesn’t exist. Defaults to True.

  • settings.SHOUTY_URLS may be True|False and determines if an exception is raised when doing {% url 'myurl' as my_var %} and myurl doesn’t actually resolve to a view. Defaults to True.

  • settings.SHOUTY_VARIABLE_BLACKLIST is a tuple of str where each one represents a template usage to ignore. Useful for if you are trying to fix up an existing project. eg: chef.can_add_cakes may be supressed if necessary.

  • settings.SHOUTY_URL_BLACKLIST is a tuple of 2-tuple to prevent certain URLs and their output variables from shouting at you loudly. Useful for existing projects or third-party apps which are less strict. eg: {% url "myurl" as my_var %} may be suppressed with ('myurl', 'my_var') which would still let {% url "myurl as "my_other_var %} raise an exception.

Default configuration

There’s a hard-coded blacklist of variables and URLs to make sure the Django admin and django-debug-toolbar work.

Tests

Literally none. Enjoy!

The license

It’s FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2019, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-shouty-templates

0.1.3

  • Feature: Silenced errors when using debug or sql_queries in a template, as they’re conditionally set by django.template.context_processors.debug

0.1.2

  • Feature: Added 11 new tokens to the default blacklist, covering more of the default Django admin and the default exception reporter.

  • Bugfix: Changed the syntax of the AppConfig.ready method to be py2 type annotation compatible.

0.1.1

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-shouty-templates-0.1.3.tar.gz (7.3 kB view hashes)

Uploaded Source

Built Distribution

django_shouty_templates-0.1.3-py2.py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page