Skip to content

Commit

Permalink
Format code with Black (#425)
Browse files Browse the repository at this point in the history
* Add black and flake8-bugbear - automatically picked up by multilint
* Add pyproject.toml black configuration to target Python 3.5
* Reconfigure isort and flake8
* Drop flake8-commas and rely on Black's trailing comma insertion only
* Run black on the code
* Add README badge
  • Loading branch information
adamchainz committed Jun 19, 2019
1 parent da2a508 commit a259cb1
Show file tree
Hide file tree
Showing 30 changed files with 501 additions and 420 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -7,6 +7,9 @@ django-cors-headers
.. image:: https://img.shields.io/pypi/v/django-cors-headers.svg
:target: https://pypi.python.org/pypi/django-cors-headers/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/python/black

A Django App that adds Cross-Origin Resource Sharing (CORS) headers to
responses. This allows in-browser requests to your Django application from
other origins.
Expand Down
2 changes: 1 addition & 1 deletion corsheaders/__init__.py
@@ -1,3 +1,3 @@
from corsheaders.checks import check_settings # noqa: F401

__version__ = '3.0.2'
__version__ = "3.0.2"
98 changes: 59 additions & 39 deletions corsheaders/checks.py
Expand Up @@ -8,7 +8,7 @@

from corsheaders.conf import conf

re_type = type(re.compile(''))
re_type = type(re.compile(""))


@checks.register
Expand All @@ -19,114 +19,134 @@ def check_settings(app_configs, **kwargs):
errors.append(
checks.Error(
"CORS_ALLOW_HEADERS should be a sequence of strings.",
id="corsheaders.E001"
id="corsheaders.E001",
)
)

if not is_sequence(conf.CORS_ALLOW_METHODS, str):
errors.append(
checks.Error(
"CORS_ALLOW_METHODS should be a sequence of strings.",
id="corsheaders.E002"
id="corsheaders.E002",
)
)

if not isinstance(conf.CORS_ALLOW_CREDENTIALS, bool):
errors.append(
checks.Error(
"CORS_ALLOW_CREDENTIALS should be a bool.",
id="corsheaders.E003"
"CORS_ALLOW_CREDENTIALS should be a bool.", id="corsheaders.E003"
)
)

if not isinstance(conf.CORS_PREFLIGHT_MAX_AGE, Integral) or conf.CORS_PREFLIGHT_MAX_AGE < 0:
if (
not isinstance(conf.CORS_PREFLIGHT_MAX_AGE, Integral)
or conf.CORS_PREFLIGHT_MAX_AGE < 0
):
errors.append(
checks.Error(
"CORS_PREFLIGHT_MAX_AGE should be an integer greater than or equal to zero.",
id="corsheaders.E004"
(
"CORS_PREFLIGHT_MAX_AGE should be an integer greater than "
+ "or equal to zero."
),
id="corsheaders.E004",
)
)

if not isinstance(conf.CORS_ORIGIN_ALLOW_ALL, bool):
errors.append(
checks.Error(
"CORS_ORIGIN_ALLOW_ALL should be a bool.",
id="corsheaders.E005"
"CORS_ORIGIN_ALLOW_ALL should be a bool.", id="corsheaders.E005"
)
)

if not is_sequence(conf.CORS_ORIGIN_WHITELIST, str):
errors.append(
checks.Error(
"CORS_ORIGIN_WHITELIST should be a sequence of strings.",
id="corsheaders.E006"
id="corsheaders.E006",
)
)
else:
for origin in conf.CORS_ORIGIN_WHITELIST:
if origin == 'null':
if origin == "null":
continue
parsed = urlparse(origin)
if parsed.scheme == '' or parsed.netloc == '':
errors.append(checks.Error(
"Origin {} in CORS_ORIGIN_WHITELIST is missing scheme or netloc".format(repr(origin)),
id="corsheaders.E013",
hint="Add a scheme (e.g. https://) or netloc (e.g. example.com)."
))
if parsed.scheme == "" or parsed.netloc == "":
errors.append(
checks.Error(
(
"Origin {} in CORS_ORIGIN_WHITELIST is missing "
+ " scheme or netloc"
).format(repr(origin)),
id="corsheaders.E013",
hint=(
"Add a scheme (e.g. https://) or netloc (e.g. "
+ "example.com)."
),
)
)
else:
# Only do this check in this case because if the scheme is not provided, netloc ends up in path
for part in ('path', 'params', 'query', 'fragment'):
if getattr(parsed, part) != '':
errors.append(checks.Error(
"Origin {} in CORS_ORIGIN_WHITELIST should not have {}".format(repr(origin), part),
id="corsheaders.E014"
))
# Only do this check in this case because if the scheme is not
# provided, netloc ends up in path
for part in ("path", "params", "query", "fragment"):
if getattr(parsed, part) != "":
errors.append(
checks.Error(
(
"Origin {} in CORS_ORIGIN_WHITELIST should "
+ "not have {}"
).format(repr(origin), part),
id="corsheaders.E014",
)
)

if not is_sequence(conf.CORS_ORIGIN_REGEX_WHITELIST, (str, re_type)):
errors.append(
checks.Error(
"CORS_ORIGIN_REGEX_WHITELIST should be a sequence of strings and/or compiled regexes.",
id="corsheaders.E007"
(
"CORS_ORIGIN_REGEX_WHITELIST should be a sequence of "
+ "strings and/or compiled regexes."
),
id="corsheaders.E007",
)
)

if not is_sequence(conf.CORS_EXPOSE_HEADERS, str):
errors.append(
checks.Error(
"CORS_EXPOSE_HEADERS should be a sequence.",
id="corsheaders.E008"
"CORS_EXPOSE_HEADERS should be a sequence.", id="corsheaders.E008"
)
)

if not isinstance(conf.CORS_URLS_REGEX, (str, re_type)):
errors.append(
checks.Error(
"CORS_URLS_REGEX should be a string or regex.",
id="corsheaders.E009"
"CORS_URLS_REGEX should be a string or regex.", id="corsheaders.E009"
)
)

if not isinstance(conf.CORS_REPLACE_HTTPS_REFERER, bool):
errors.append(
checks.Error(
"CORS_REPLACE_HTTPS_REFERER should be a bool.",
id="corsheaders.E011"
"CORS_REPLACE_HTTPS_REFERER should be a bool.", id="corsheaders.E011"
)
)

if hasattr(settings, 'CORS_MODEL'):
if hasattr(settings, "CORS_MODEL"):
errors.append(
checks.Error(
"The CORS_MODEL setting has been removed - see django-cors-headers' HISTORY.",
id="corsheaders.E012"
(
"The CORS_MODEL setting has been removed - see "
+ "django-cors-headers' HISTORY."
),
id="corsheaders.E012",
)
)

return errors


def is_sequence(thing, type_or_types):
return (
isinstance(thing, Sequence)
and all(isinstance(x, type_or_types) for x in thing)
return isinstance(thing, Sequence) and all(
isinstance(x, type_or_types) for x in thing
)
23 changes: 12 additions & 11 deletions corsheaders/conf.py
@@ -1,6 +1,7 @@
from django.conf import settings

from corsheaders.defaults import default_headers, default_methods # Kept here for backwards compatibility
# Kept here for backwards compatibility
from corsheaders.defaults import default_headers, default_methods


class Settings(object):
Expand All @@ -10,43 +11,43 @@ class Settings(object):

@property
def CORS_ALLOW_HEADERS(self):
return getattr(settings, 'CORS_ALLOW_HEADERS', default_headers)
return getattr(settings, "CORS_ALLOW_HEADERS", default_headers)

@property
def CORS_ALLOW_METHODS(self):
return getattr(settings, 'CORS_ALLOW_METHODS', default_methods)
return getattr(settings, "CORS_ALLOW_METHODS", default_methods)

@property
def CORS_ALLOW_CREDENTIALS(self):
return getattr(settings, 'CORS_ALLOW_CREDENTIALS', False)
return getattr(settings, "CORS_ALLOW_CREDENTIALS", False)

@property
def CORS_PREFLIGHT_MAX_AGE(self):
return getattr(settings, 'CORS_PREFLIGHT_MAX_AGE', 86400)
return getattr(settings, "CORS_PREFLIGHT_MAX_AGE", 86400)

@property
def CORS_ORIGIN_ALLOW_ALL(self):
return getattr(settings, 'CORS_ORIGIN_ALLOW_ALL', False)
return getattr(settings, "CORS_ORIGIN_ALLOW_ALL", False)

@property
def CORS_ORIGIN_WHITELIST(self):
return getattr(settings, 'CORS_ORIGIN_WHITELIST', ())
return getattr(settings, "CORS_ORIGIN_WHITELIST", ())

@property
def CORS_ORIGIN_REGEX_WHITELIST(self):
return getattr(settings, 'CORS_ORIGIN_REGEX_WHITELIST', ())
return getattr(settings, "CORS_ORIGIN_REGEX_WHITELIST", ())

@property
def CORS_EXPOSE_HEADERS(self):
return getattr(settings, 'CORS_EXPOSE_HEADERS', ())
return getattr(settings, "CORS_EXPOSE_HEADERS", ())

@property
def CORS_URLS_REGEX(self):
return getattr(settings, 'CORS_URLS_REGEX', r'^.*$')
return getattr(settings, "CORS_URLS_REGEX", r"^.*$")

@property
def CORS_REPLACE_HTTPS_REFERER(self):
return getattr(settings, 'CORS_REPLACE_HTTPS_REFERER', False)
return getattr(settings, "CORS_REPLACE_HTTPS_REFERER", False)


conf = Settings()
27 changes: 10 additions & 17 deletions corsheaders/defaults.py
@@ -1,20 +1,13 @@
default_headers = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
"accept",
"accept-encoding",
"authorization",
"content-type",
"dnt",
"origin",
"user-agent",
"x-csrftoken",
"x-requested-with",
)

default_methods = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
)
default_methods = ("DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT")

0 comments on commit a259cb1

Please sign in to comment.