mirror of
https://github.com/quantum5/django-csp-advanced.git
synced 2025-04-24 11:22:00 -04:00
Add python3 support.
This commit is contained in:
parent
854af23268
commit
591ea6f2fd
|
@ -1,7 +1,10 @@
|
|||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
- "3.5"
|
||||
- "3.6"
|
||||
- pypy
|
||||
- pypy3
|
||||
env:
|
||||
- DJANGO_VERSION=">=1.8,<1.9"
|
||||
- DJANGO_VERSION=">=1.9,<1.10"
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from itertools import chain
|
||||
|
||||
from django.utils import six
|
||||
|
||||
|
||||
class InvalidCSPError(ValueError):
|
||||
pass
|
||||
|
@ -74,7 +76,7 @@ class CSPCompiler(object):
|
|||
|
||||
def compile(self):
|
||||
pieces = []
|
||||
for name, value in self.csp.iteritems():
|
||||
for name, value in six.iteritems(self.csp):
|
||||
if name in self.CSP_LISTS:
|
||||
if value:
|
||||
pieces.append(self.compile_list(name, value))
|
||||
|
@ -126,5 +128,5 @@ class CSPCompiler(object):
|
|||
|
||||
@staticmethod
|
||||
def ensure_str(name, value):
|
||||
if not isinstance(value, basestring):
|
||||
if not isinstance(value, six.string_types):
|
||||
raise InvalidCSPError('Values for %s must be a string type, not %s', (name, type(value)))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import logging
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import MiddlewareNotUsed
|
||||
from django.utils import six
|
||||
|
||||
from csp_advanced.csp import CSPCompiler, InvalidCSPError
|
||||
from csp_advanced.utils import is_callable_csp_dict, call_csp_dict, merge_csp_dict
|
||||
|
@ -12,11 +13,11 @@ class AdvancedCSPMiddleware(object):
|
|||
def __init__(self, get_response=None):
|
||||
self.get_response = get_response
|
||||
self.enforced_csp = getattr(settings, 'ADVANCED_CSP', None) or {}
|
||||
self.enforced_csp_is_str = isinstance(self.enforced_csp, basestring)
|
||||
self.enforced_csp_is_str = isinstance(self.enforced_csp, six.string_types)
|
||||
self.enforced_csp_callable = is_callable_csp_dict(self.enforced_csp)
|
||||
self.report_csp = getattr(settings, 'ADVANCED_CSP_REPORT_ONLY', None) or {}
|
||||
self.report_csp_callable = is_callable_csp_dict(self.report_csp)
|
||||
self.report_csp_is_str = isinstance(self.enforced_csp, basestring)
|
||||
self.report_csp_is_str = isinstance(self.enforced_csp, six.string_types)
|
||||
self.report_only_csp = not self.enforced_csp
|
||||
|
||||
if not self.enforced_csp and not self.report_csp:
|
||||
|
|
|
@ -5,9 +5,9 @@ from django.http import HttpResponse
|
|||
from django.test import SimpleTestCase, RequestFactory, override_settings
|
||||
from django.utils.decorators import decorator_from_middleware_with_args
|
||||
|
||||
from csp import CSPCompiler, InvalidCSPError
|
||||
from csp_advanced.csp import CSPCompiler, InvalidCSPError
|
||||
from csp_advanced.middleware import AdvancedCSPMiddleware
|
||||
from utils import call_csp_dict, merge_csp_dict, is_callable_csp_dict
|
||||
from csp_advanced.utils import call_csp_dict, merge_csp_dict, is_callable_csp_dict
|
||||
|
||||
|
||||
class CSPCompileTest(SimpleTestCase):
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
from django.utils import six
|
||||
|
||||
|
||||
def is_callable_csp_dict(data):
|
||||
if callable(data):
|
||||
return True
|
||||
if not isinstance(data, dict):
|
||||
return False
|
||||
return any(callable(value) for value in data.itervalues())
|
||||
return any(callable(value) for value in six.itervalues(data))
|
||||
|
||||
|
||||
def call_csp_dict(data, request, response):
|
||||
|
@ -11,7 +14,7 @@ def call_csp_dict(data, request, response):
|
|||
return data(request, response)
|
||||
|
||||
result = {}
|
||||
for key, value in data.iteritems():
|
||||
for key, value in six.iteritems(data):
|
||||
if callable(value):
|
||||
result[key] = value(request, response)
|
||||
else:
|
||||
|
@ -21,7 +24,7 @@ def call_csp_dict(data, request, response):
|
|||
|
||||
def merge_csp_dict(template, override):
|
||||
result = template.copy()
|
||||
for key, value in override.iteritems():
|
||||
for key, value in six.iteritems(override):
|
||||
if key not in result:
|
||||
result[key] = value
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue