From 148ca6a2570c804a1d68013754ca96164fef4b6c Mon Sep 17 00:00:00 2001 From: Jonathon Sumner Date: Thu, 4 Jul 2019 02:36:53 -0400 Subject: [PATCH] Add csp compliance through django-csp, if installed (#34) --- README.rst | 15 ++ rangefilter/filter.py | 10 +- rangefilter/static/rangefilter/iife.js | 24 +++ .../rangefilter/date_filter_csp.html | 144 ++++++++++++++++++ 4 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 rangefilter/static/rangefilter/iife.js create mode 100644 rangefilter/templates/rangefilter/date_filter_csp.html diff --git a/README.rst b/README.rst index 88ec69a..ed8ec80 100755 --- a/README.rst +++ b/README.rst @@ -42,6 +42,21 @@ Example: ) +For Django 1.8+, if django-csp is installed, nonces will be added to style and script tags. + +Example: + +.. code:: python + + INSTALLED_APPS = ( + ... + 'rangefilter', + "csp", + ... + ) + + + Example usage ------------- diff --git a/rangefilter/filter.py b/rangefilter/filter.py index 38cdfe2..db37f28 100644 --- a/rangefilter/filter.py +++ b/rangefilter/filter.py @@ -10,6 +10,11 @@ try: except ImportError: pytz = None +try: + import csp +except ImportError: + csp = None + from collections import OrderedDict from django import forms @@ -100,7 +105,10 @@ class DateRangeFilter(admin.filters.FieldListFilter): def get_template(self): if django.VERSION[:2] <= (1, 8): return 'rangefilter/date_filter_1_8.html' - return 'rangefilter/date_filter.html' + else: + if csp: + return 'rangefilter/date_filter_csp.html' + return 'rangefilter/date_filter.html' template = property(get_template) diff --git a/rangefilter/static/rangefilter/iife.js b/rangefilter/static/rangefilter/iife.js new file mode 100644 index 0000000..15de6cf --- /dev/null +++ b/rangefilter/static/rangefilter/iife.js @@ -0,0 +1,24 @@ +(function() { + 'use strict'; + django.jQuery(".admindatefilter").each( + function(){ + var form_id = django.jQuery(this).find("form").attr('id').slice(0,-5); + var qs_name = form_id+"-query-string"; + var query_string = django.jQuery('input#'+qs_name).val(); + var form_name = form_id+"-form"; + + // Bind submit buttons + django.jQuery(this).find("input[type=select]").bind("click", + function(event){ + event.preventDefault(); + var form_data = django.jQuery('#'+form_name).serialize(); + window.location = window.location.pathname + query_string + '&' + form_data; + }); + + // Bind reset buttons + django.jQuery(this).find("input[type=reset]").bind("click", + function(){ + window.location = window.location.pathname + query_string; + }); + }); +})(); diff --git a/rangefilter/templates/rangefilter/date_filter_csp.html b/rangefilter/templates/rangefilter/date_filter_csp.html new file mode 100644 index 0000000..f5e90e8 --- /dev/null +++ b/rangefilter/templates/rangefilter/date_filter_csp.html @@ -0,0 +1,144 @@ +{% load i18n rangefilter_compat %} +

{% blocktrans with filter_title=title %}By {{ filter_title }}{% endblocktrans %}

+ + + + + + + + +
+
+ {{ spec.form.as_p }} + {% for choice in choices %} + + {% endfor %} +
+ + +
+
+
+ + +