Applies a monkeypatch which forces Django's template language to error far more loudly about invalid assumptions
Project description
- Version:
- 0.1.1
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:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.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
Built Distribution
Hashes for django-shouty-templates-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ead3e4324de2b34d798fc581c30bf92aba0422d170cfcdf0d6be48a322dd8d6 |
|
MD5 | bd8264e78400b9709a92633c058206ac |
|
BLAKE2b-256 | 18f1803a8f2e4832c9ef331821a53ffab9cc2db93c5be54560f97b402ff05a0f |
Hashes for django_shouty_templates-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1768468d9a4f4499822d0e4927c0b3052d084f199bcd7701742174820d5485b3 |
|
MD5 | 75b6b520bbf5dc04d2ed01db026022b3 |
|
BLAKE2b-256 | f8862098fa474dc13577c673d39b0b7dd03c4e5600e42e56a8dd3208376bafac |