Merge upstream version 2.2.1

This commit is contained in:
Aleh Rymašeŭski 2023-07-13 15:40:31 +00:00
commit c95a0c1405
8 changed files with 49 additions and 17 deletions

View file

@ -18,12 +18,12 @@ repos:
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v3.2.0
rev: v3.2.2
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.11.0
rev: 1.12.0
hooks:
- id: django-upgrade
args: [--target-version, "3.2"]

View file

@ -1,5 +1,14 @@
# Changes
## Next Release
## 2.2.1 (2022-11-28)
#### Fixes
- fix: Make log entries read-only in the admin. ([#449](https://github.com/jazzband/django-auditlog/pull/449))
- fix: Handle IPv6 addresses in `X-Forwarded-For`. ([#457](https://github.com/jazzband/django-auditlog/pull/457))
## 2.2.0 (2022-10-07)
#### Improvements

View file

@ -3,7 +3,7 @@ django-auditlog
[![Jazzband](https://jazzband.co/static/img/badge.svg)](https://jazzband.co/)
[![Build Status](https://github.com/jazzband/django-auditlog/workflows/Test/badge.svg)](https://github.com/jazzband/django-auditlog/actions)
[![Docs](https://readthedocs.org/projects/django-auditlog/badge/?version=latest)](http://django-auditlog.readthedocs.org/en/latest/?badge=latest)
[![Docs](https://readthedocs.org/projects/django-auditlog/badge/?version=latest)](https://django-auditlog.readthedocs.org/en/latest/?badge=latest)
[![codecov](https://codecov.io/gh/jazzband/django-auditlog/branch/master/graph/badge.svg)](https://codecov.io/gh/jazzband/django-auditlog)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/django-auditlog.svg)](https://pypi.python.org/pypi/django-auditlog)
[![Supported Django versions](https://img.shields.io/pypi/djversions/django-auditlog.svg)](https://pypi.python.org/pypi/django-auditlog)

View file

@ -1,7 +1,15 @@
from pkg_resources import DistributionNotFound, get_distribution
try:
__version__ = get_distribution("django-auditlog").version
except DistributionNotFound:
# package is not installed
pass
from importlib.metadata import version # New in Python 3.8
except ImportError:
from pkg_resources import ( # from setuptools, deprecated
DistributionNotFound,
get_distribution,
)
try:
__version__ = get_distribution("django-auditlog").version
except DistributionNotFound:
# package is not installed
pass
else:
__version__ = version("django-auditlog")

View file

@ -14,14 +14,21 @@ class AuditlogMiddleware:
@staticmethod
def _get_remote_addr(request):
if request.headers.get("X-Forwarded-For"):
# In case of proxy, set 'original' address
remote_addr = request.headers.get("X-Forwarded-For").split(",")[0]
# Remove port number from remote_addr
return remote_addr.split(":")[0]
else:
# In case there is no proxy, return the original address
if not request.headers.get("X-Forwarded-For"):
return request.META.get("REMOTE_ADDR")
# In case of proxy, set 'original' address
remote_addr: str = request.headers.get("X-Forwarded-For").split(",")[0]
# Remove port number from remote_addr
if "." in remote_addr and ":" in remote_addr: # IPv4 with port (`x.x.x.x:x`)
remote_addr = remote_addr.split(":")[0]
elif "[" in remote_addr: # IPv6 with port (`[:::]:x`)
remote_addr = remote_addr[1:].split("]")[0]
return remote_addr
def __call__(self, request):
remote_addr = self._get_remote_addr(request)

View file

@ -450,6 +450,11 @@ class MiddlewareTest(TestCase):
({}, "127.0.0.1"),
({"HTTP_X_FORWARDED_FOR": "127.0.0.2"}, "127.0.0.2"),
({"HTTP_X_FORWARDED_FOR": "127.0.0.3:1234"}, "127.0.0.3"),
({"HTTP_X_FORWARDED_FOR": "2606:4700:4700::1111"}, "2606:4700:4700::1111"),
(
{"HTTP_X_FORWARDED_FOR": "[2606:4700:4700::1001]:1234"},
"2606:4700:4700::1001",
),
]
for headers, expected_remote_addr in tests:
with self.subTest(headers=headers):

View file

@ -10,7 +10,7 @@ import os
import sys
from datetime import date
from pkg_resources import get_distribution
from auditlog import __version__
# -- Path setup --------------------------------------------------------------
@ -33,7 +33,7 @@ project = "django-auditlog"
author = "Jan-Jelle Kester and contributors"
copyright = f"2013-{date.today().year}, {author}"
release = get_distribution("django-auditlog").version
release = __version__
# for example take major/minor
version = ".".join(release.split(".")[:2])

View file

@ -214,6 +214,9 @@ It must be a list or tuple. Each item in this setting can be a:
},
"mask_fields": ["field5", "field6"],
"m2m_fields": ["field7", "field8"],
"serialize_data": True,
"serialize_auditlog_fields_only": False,
"serialize_kwargs": {"fields": ["foo", "bar", "biz", "baz"]},
},
"<appname>.<model3>",
)