From 8e02237fd44c6ba5a2501f484ef8ef6968a83edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Oca=C3=B1a=20G=C3=A1lvez?= Date: Thu, 2 Jun 2022 11:38:24 +0200 Subject: [PATCH] Rely on Django DB routing when storing log entries (#5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sergio Ocaña Gálvez --- .github/workflows/release.yml | 6 +----- .github/workflows/test.yml | 13 +++++++------ auditlog/models.py | 8 +------- auditlog_tests/tests.py | 17 ++++++++++++++++- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 55a6b6b3..1a14ae4a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,7 +24,6 @@ jobs: id: pip-cache run: | echo "::set-output name=dir::$(pip cache dir)" - - name: Cache uses: actions/cache@v2 with: @@ -32,22 +31,19 @@ jobs: key: release-${{ hashFiles('**/setup.py') }} restore-keys: | release- - - name: Install dependencies run: | python -m pip install -U pip python -m pip install -U setuptools twine wheel - - name: Build package run: | python setup.py --version python setup.py sdist --format=gztar bdist_wheel twine check dist/* - - name: Upload packages to Jazzband if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') uses: pypa/gh-action-pypi-publish@master with: user: jazzband password: ${{ secrets.JAZZBAND_RELEASE_KEY }} - repository_url: https://jazzband.co/projects/django-auditlog/upload + repository_url: https://jazzband.co/projects/django-auditlog/upload \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e745005..3190bc6b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,11 +9,11 @@ jobs: fail-fast: false max-parallel: 5 matrix: - python-version: ['3.6', '3.7', '3.8', '3.9'] + python-version: ['3.7', '3.8', '3.9', '3.10'] services: postgres: - image: postgres:10 + image: postgres:12 env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres @@ -25,7 +25,6 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 - steps: - uses: actions/checkout@v2 @@ -38,7 +37,6 @@ jobs: id: pip-cache run: | echo "::set-output name=dir::$(pip cache dir)" - - name: Cache uses: actions/cache@v2 with: @@ -47,12 +45,10 @@ jobs: -${{ matrix.python-version }}-v1-${{ hashFiles('**/setup.py') }} restore-keys: | -${{ matrix.python-version }}-v1- - - name: Install dependencies run: | python -m pip install --upgrade pip python -m pip install --upgrade tox tox-gh-actions - - name: Tox tests run: | tox -v @@ -62,3 +58,8 @@ jobs: TEST_DB_PASS: postgres TEST_DB_NAME: postgres TEST_DB_PORT: ${{ job.services.postgres.ports[5432] }} + + - name: Upload coverage + uses: codecov/codecov-action@v1 + with: + name: Python ${{ matrix.python-version }} \ No newline at end of file diff --git a/auditlog/models.py b/auditlog/models.py index 69da510c..45b941d2 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -67,13 +67,7 @@ def log_create(self, instance, **kwargs): content_type=kwargs.get("content_type"), object_pk=kwargs.get("object_pk", ""), ).delete() - # save LogEntry to same database instance is using - db = instance._state.db - return ( - self.create(**kwargs) - if db is None or db == "" - else self.using(db).create(**kwargs) - ) + return self.create(**kwargs) return None def get_for_object(self, instance): diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 9048383b..d75afbe7 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -12,6 +12,7 @@ from django.test import RequestFactory, TestCase from django.utils import dateformat, formats, timezone +from auditlog.diff import model_instance_diff from auditlog.middleware import AuditlogMiddleware from auditlog.models import LogEntry from auditlog.registry import auditlog @@ -33,11 +34,25 @@ UUIDPrimaryKeyModel, ) - class SimpleModelTest(TestCase): def setUp(self): self.obj = SimpleModel.objects.create(text="I am not difficult.") + def test_log_create_updates_on_original_database(self): + """Log creation should use object's default database""" + obj = self.obj + obj._state.db = "readonly" + changes = model_instance_diff(None, obj) + log_entry = LogEntry.objects.log_create( + obj, action=LogEntry.Action.CREATE, changes=json.dumps(changes) + ) + + self.assertEqual( + log_entry._state.db, + "default", + msg="Object should have used the default database", + ) + def test_create(self): """Creation is logged correctly.""" # Get the object to work with