Skip to content

Commit

Permalink
Merge branch 'main' into django-4-2-or-better
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Feb 22, 2024
2 parents 0e37b45 + 7d77a34 commit a580a48
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 51 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.js

This file was deleted.

13 changes: 13 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,13 @@
# Keep GitHub Actions up to date with GitHub's Dependabot...
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
version: 2
updates:
- package-ecosystem: github-actions
directory: /
groups:
github-actions:
patterns:
- "*" # Group all Actions updates into a single larger pull request
schedule:
interval: weekly
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
37 changes: 19 additions & 18 deletions .github/workflows/test.yml
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
Expand All @@ -44,7 +44,7 @@ jobs:
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand All @@ -67,9 +67,9 @@ jobs:
DB_PORT: 3306

- name: Upload coverage data
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-data
name: coverage-data-${{ matrix.python-version }}-${{ matrix.os }}-${{ matrix.arch }}-mysql
path: ".coverage.*"

postgres:
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
Expand All @@ -119,7 +119,7 @@ jobs:
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand All @@ -145,9 +145,9 @@ jobs:
DB_PORT: 5432

- name: Upload coverage data
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-data
name: coverage-data-${{ matrix.python-version }}-${{ matrix.os }}-${{ matrix.arch }}-${{ matrix.database }}
path: ".coverage.*"

sqlite:
Expand All @@ -162,7 +162,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
Expand All @@ -173,7 +173,7 @@ jobs:
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand All @@ -193,9 +193,9 @@ jobs:
DB_NAME: ":memory:"

- name: Upload coverage data
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-data
name: coverage-data-${{ matrix.python-version }}-${{ matrix.os }}-${{ matrix.arch }}-sqlite
path: ".coverage.*"

coverage:
Expand All @@ -204,17 +204,18 @@ jobs:
needs: [sqlite, mysql, postgres]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
# Use latest, so it understands all syntax.
python-version: "3.11"

- run: python -m pip install --upgrade coverage[toml]

- name: Download coverage data.
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: coverage-data
pattern: coverage-data-*
merge-multiple: true

- name: Combine coverage & check percentage
run: |
Expand All @@ -223,7 +224,7 @@ jobs:
python -m coverage report
- name: Upload HTML report if check failed.
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: html-report
path: htmlcov
Expand All @@ -238,7 +239,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand All @@ -248,7 +249,7 @@ jobs:
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand Down
10 changes: 7 additions & 3 deletions .pre-commit-config.yaml
Expand Up @@ -14,7 +14,7 @@ repos:
hooks:
- id: doc8
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.15.0
rev: 1.16.0
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]
Expand All @@ -32,15 +32,19 @@ repos:
args:
- --trailing-comma=es5
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.56.0
rev: v9.0.0-beta.0
hooks:
- id: eslint
additional_dependencies:
- "eslint@v9.0.0-beta.0"
- "@eslint/js@v9.0.0-beta.0"
- "globals"
files: \.js?$
types: [file]
args:
- --fix
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.2.1'
rev: 'v0.2.2'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
18 changes: 17 additions & 1 deletion debug_toolbar/middleware.py
Expand Up @@ -3,6 +3,7 @@
"""

import re
import socket
from functools import lru_cache

from django.conf import settings
Expand All @@ -19,7 +20,22 @@ def show_toolbar(request):
"""
Default function to determine whether to show the toolbar on a given page.
"""
return settings.DEBUG and request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS
internal_ips = settings.INTERNAL_IPS.copy()

try:
# This is a hack for docker installations. It attempts to look
# up the IP address of the docker host.
# This is not guaranteed to work.
docker_ip = (
# Convert the last segment of the IP address to be .1
".".join(socket.gethostbyname("host.docker.internal").rsplit(".")[:-1])
+ ".1"
)
internal_ips.append(docker_ip)
except socket.gaierror:
# It's fine if the lookup errored since they may not be using docker
pass
return settings.DEBUG and request.META.get("REMOTE_ADDR") in internal_ips


@lru_cache(maxsize=None)
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Expand Up @@ -5,6 +5,8 @@ Pending
-------

* Raised the minimum Django version to 4.2.
* Automatically support Docker rather than having the developer write a
workaround for ``INTERNAL_IPS``.

4.3.0 (2024-02-01)
------------------
Expand Down
10 changes: 4 additions & 6 deletions docs/installation.rst
Expand Up @@ -145,12 +145,10 @@ option.

.. warning::

If using Docker the following will set your ``INTERNAL_IPS`` correctly in Debug mode::

if DEBUG:
import socket # only if you haven't already imported this
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "10.0.2.2"]
If using Docker, the toolbar will attempt to look up your host name
automatically and treat it as an allowable internal IP. If you're not
able to get the toolbar to work with your docker installation, review
the code in ``debug_toolbar.middleware.show_toolbar``.

Troubleshooting
---------------
Expand Down
28 changes: 28 additions & 0 deletions eslint.config.js
@@ -0,0 +1,28 @@
const js = require("@eslint/js");
const globals = require("globals");

module.exports = [
js.configs.recommended,
{
files: ["**/*.js"],
languageOptions:{
ecmaVersion: "latest",
sourceType: "module",
globals: {
...globals.browser,
...globals.node
}
}
},
{
rules: {
"curly": ["error", "all"],
"dot-notation": "error",
"eqeqeq": "error",
"no-eval": "error",
"no-var": "error",
"prefer-const": "error",
"semi": "error"
}
}
];
9 changes: 9 additions & 0 deletions tests/test_integration.py
Expand Up @@ -2,6 +2,7 @@
import re
import time
import unittest
from unittest.mock import patch

import html5lib
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
Expand Down Expand Up @@ -66,6 +67,14 @@ def test_show_toolbar_INTERNAL_IPS(self):
with self.settings(INTERNAL_IPS=[]):
self.assertFalse(show_toolbar(self.request))

@patch("socket.gethostbyname", return_value="127.0.0.255")
def test_show_toolbar_docker(self, mocked_gethostbyname):
with self.settings(INTERNAL_IPS=[]):
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
# is shifted to be 1.
self.assertTrue(show_toolbar(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")

def test_should_render_panels_RENDER_PANELS(self):
"""
The toolbar should force rendering panels on each request
Expand Down

0 comments on commit a580a48

Please sign in to comment.