Skip to content

Add Global Auditing Disable Feature#43

Merged
snopoke merged 18 commits intomainfrom
sk/skip-audit
Nov 13, 2025
Merged

Add Global Auditing Disable Feature#43
snopoke merged 18 commits intomainfrom
sk/skip-audit

Conversation

@snopoke
Copy link
Contributor

@snopoke snopoke commented Nov 7, 2025

Note

This code was originally written by Claude AI

Summary

Adds the ability to globally disable auditing in django-field-audit, providing both application-wide control via Django settings and fine-grained runtime control via context managers. This feature is particularly useful for unit tests, data migrations, bulk imports, and maintenance operations where audit overhead is not needed.

Implementation

Core Infrastructure

  • Context Variable: Uses Python's contextvars for thread-safe, async-safe state management
  • Check Function: is_audit_enabled() checks context variable first, then falls back to Django setting
  • Context Managers: disable_audit() and enable_audit() for runtime control

API

New public API exports:

from field_audit import disable_audit, enable_audit

New Django setting:

FIELD_AUDIT_ENABLED = False  # Default: True

Usage Examples

Global Disable via Setting

# settings.py
FIELD_AUDIT_ENABLED = False

Runtime Disable

from field_audit import disable_audit

# Temporarily disable auditing
with disable_audit():
    obj.save()  # No audit event created
    MyModel.objects.bulk_create(objects)  # No audit events

Override Global Setting

from field_audit import enable_audit

# When FIELD_AUDIT_ENABLED=False, temporarily enable
with enable_audit():
    obj.save()  # Audit event IS created

Use Cases

Unit Tests - Improve performance by skipping audit overhead:

def test_without_audit(self):
    with disable_audit():
        obj = MyModel.objects.create(field="test")

Data Migrations - Skip auditing during bulk operations:

def migrate_data():
    with disable_audit():
        MyModel.objects.filter(old=True).update(new=True)

Backwards Compatibility

The new features added in this PR are opt-in and do not break backward compatibility.

@snopoke snopoke requested a review from SmittieC November 7, 2025 10:18
Copy link
Contributor

@millerdev millerdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero performance impact when enabled: Check overhead <1μs

Is this always true? Is this AI-generated PR description content? Seems like the entire Backwards Compatibility section could be removed or replaced with a single line: "The new features added in this PR are opt-in and do not break backward compatibility."


This PR seems unnecessarily verbose—and I didn't even read the whole plan doc, which feels wrong because it's slated to be kept in the repo. Wading through AI slop is tiring. The time savings gained by using AI seem to have translated to extra review time. That's frustrating.

- Add audit_enabled context variable
- Add is_audit_enabled() check function
- Add disable_audit() and enable_audit() context managers
- Update __all__ exports
- Add check to _decorate_db_write for save/delete
- Add check to _m2m_changed_handler for M2M changes
- Add check to audit_field_changes
- Add check to create_audit_event
- Add check to bulk_create
- Add check to delete
- Add check to update
- Test setting-based disable
- Test context manager disable/enable
- Test M2M field auditing
- Test QuerySet operations
- Test thread safety
- Test backwards compatibility
- Test migration scenarios
- Update global disable tests to use correct model names
- Fix test isolation issues
- Simplify thread safety test to avoid Django transaction complexity
- All 200 tests now pass
- Add FIELD_AUDIT_ENABLED to settings table
- Add Disabling Auditing section with examples
- Document disable_audit() and enable_audit() context managers
- Include use cases for tests, migrations, and imports
@snopoke
Copy link
Contributor Author

snopoke commented Nov 10, 2025

@millerdev apologies for the rebase, I wasn't thinking.

Copy link
Contributor

@SmittieC SmittieC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me

Copy link
Contributor

@millerdev millerdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@millerdev apologies for the rebase, I wasn't thinking.

Looks like the plan doc was somehow completely removed in the rebase, and surprisingly I don't even see it removed in the before/after rebase compare view (although it's in the previous branch history). I guess that's fine.

Maybe I missed it, but I think there is no attribution to AI code generation anywhere in this PR anymore. How do you think about that? Is it reasonable to not mention when AI is used to write code?

@snopoke
Copy link
Contributor Author

snopoke commented Nov 10, 2025

@millerdev apologies for the rebase, I wasn't thinking.

Looks like the plan doc was somehow completely removed in the rebase, and surprisingly I don't even see it removed in the before/after rebase compare view (although it's in the previous branch history). I guess that's fine.

Maybe I missed it, but I think there is no attribution to AI code generation anywhere in this PR anymore. How do you think about that? Is it reasonable to not mention when AI is used to write code?

I thought the original commits had co-authorship - but maybe not since I rebased.

I've added a note at the top of the PR description.

@snopoke snopoke merged commit a1b6b50 into main Nov 13, 2025
10 checks passed
@snopoke snopoke deleted the sk/skip-audit branch November 13, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants