From 2678a21fd5bf64c74a8a624710b077ecba5dbe65 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 4 Aug 2021 11:17:43 +0100 Subject: [PATCH] Add blacken-docs pre-commit hook (#666) --- .pre-commit-config.yaml | 7 ++++ HISTORY.rst | 4 +-- README.rst | 77 ++++++++++++++++++++++------------------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee12b820..75335fee 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,6 +20,13 @@ repos: hooks: - id: black language_version: python3 + +- repo: https://github.com/asottile/blacken-docs + rev: v1.10.0 + hooks: + - id: blacken-docs + additional_dependencies: + - black==21.7b0 - repo: https://github.com/pycqa/isort rev: 5.9.3 hooks: diff --git a/HISTORY.rst b/HISTORY.rst index b219b1b5..9cee2535 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -107,13 +107,13 @@ History .. code-block:: python - CORS_ORIGIN_WHITELIST = ['example.com'] + CORS_ORIGIN_WHITELIST = ["example.com"] ...to this: .. code-block:: python - CORS_ORIGIN_WHITELIST = ['https://example.com'] + CORS_ORIGIN_WHITELIST = ["https://example.com"] * Removed the ``CORS_MODEL`` setting, and associated class. It seems very few, or no users were using it, since there were no bug reports since its move to diff --git a/README.rst b/README.rst index ab4eb39c..3da3f87d 100644 --- a/README.rst +++ b/README.rst @@ -65,9 +65,9 @@ and then add it to your installed apps: .. code-block:: python INSTALLED_APPS = [ - ... - 'corsheaders', - ... + ..., + "corsheaders", + ..., ] Make sure you add the trailing comma or you might get a ``ModuleNotFoundError`` @@ -80,8 +80,8 @@ You will also need to add a middleware class to listen in on responses: MIDDLEWARE = [ ..., - 'corsheaders.middleware.CorsMiddleware', - 'django.middleware.common.CommonMiddleware', + "corsheaders.middleware.CorsMiddleware", + "django.middleware.common.CommonMiddleware", ..., ] @@ -149,7 +149,7 @@ Example: "https://example.com", "https://sub.example.com", "http://localhost:8080", - "http://127.0.0.1:9000" + "http://127.0.0.1:9000", ] Previously this setting was called ``CORS_ORIGIN_WHITELIST``, which still works @@ -203,7 +203,7 @@ Example: .. code-block:: python - CORS_URLS_REGEX = r'^/api/.*$' + CORS_URLS_REGEX = r"^/api/.*$" ``CORS_ALLOW_METHODS`` ~~~~~~~~~~~~~~~~~~~~~~ @@ -213,12 +213,12 @@ A list of HTTP verbs that are allowed for the actual request. Defaults to: .. code-block:: python CORS_ALLOW_METHODS = [ - 'DELETE', - 'GET', - 'OPTIONS', - 'PATCH', - 'POST', - 'PUT', + "DELETE", + "GET", + "OPTIONS", + "PATCH", + "POST", + "PUT", ] The default can be imported as ``corsheaders.defaults.default_methods`` so you @@ -230,7 +230,7 @@ with any future changes. For example: from corsheaders.defaults import default_methods CORS_ALLOW_METHODS = list(default_methods) + [ - 'POKE', + "POKE", ] ``CORS_ALLOW_HEADERS`` @@ -242,15 +242,15 @@ request. Defaults to: .. code-block:: python CORS_ALLOW_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", ] The default can be imported as ``corsheaders.defaults.default_headers`` so you @@ -262,7 +262,7 @@ any future changes. For example: from corsheaders.defaults import default_headers CORS_ALLOW_HEADERS = list(default_headers) + [ - 'my-custom-header', + "my-custom-header", ] ``CORS_EXPOSE_HEADERS`` @@ -312,12 +312,12 @@ For example: .. code-block:: python CORS_ALLOWED_ORIGINS = [ - 'http://read.only.com', - 'http://change.allowed.com', + "http://read.only.com", + "http://change.allowed.com", ] CSRF_TRUSTED_ORIGINS = [ - 'change.allowed.com', + "change.allowed.com", ] ``CORS_REPLACE_HTTPS_REFERER`` @@ -341,12 +341,12 @@ undo the ``Referer`` replacement: .. code-block:: python MIDDLEWARE_CLASSES = [ - ... - 'corsheaders.middleware.CorsMiddleware', - ... - 'django.middleware.csrf.CsrfViewMiddleware', - 'corsheaders.middleware.CorsPostCsrfMiddleware', - ... + ..., + "corsheaders.middleware.CorsMiddleware", + ..., + "django.middleware.csrf.CsrfViewMiddleware", + "corsheaders.middleware.CorsPostCsrfMiddleware", + ..., ] Signals @@ -370,9 +370,11 @@ For example you might define a handler like this: from myapp.models import MySite + def cors_allow_mysites(sender, request, **kwargs): return MySite.objects.filter(host=request.host).exists() + check_request_enabled.connect(cors_allow_mysites) Then connect it at app ready time using a `Django AppConfig @@ -382,7 +384,7 @@ Then connect it at app ready time using a `Django AppConfig # myapp/__init__.py - default_app_config = 'myapp.apps.MyAppConfig' + default_app_config = "myapp.apps.MyAppConfig" .. code-block:: python @@ -390,8 +392,9 @@ Then connect it at app ready time using a `Django AppConfig from django.apps import AppConfig + class MyAppConfig(AppConfig): - name = 'myapp' + name = "myapp" def ready(self): # Makes sure all signal handlers are connected @@ -412,7 +415,9 @@ unrestricted URL's. For example: # myapp/handlers.py from corsheaders.signals import check_request_enabled + def cors_allow_api_to_everyone(sender, request, **kwargs): - return request.path.startswith('/api/') + return request.path.startswith("/api/") + check_request_enabled.connect(cors_allow_api_to_everyone)