Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to audit log the changes along with old and new values. #153

Open
vivekshah1801 opened this issue May 22, 2024 · 1 comment
Open
Assignees
Labels
enhancement New feature or request

Comments

@vivekshah1801
Copy link

vivekshah1801 commented May 22, 2024

Is your feature request related to a problem? Please describe.
We use extra-settings in our services. Since the access to change the settings is with multiple teammates, we want a way so that we can effectively manage the change in values. Currently django history shows which user has changed which field. Having the ability to log the change in value will be a good enhancement.

Describe the solution you'd like
Currently django logs the change as
April 20, 2023, 12:54 p.m. username (name) Changed value_bool.
What it should be
April 20, 2023, 12:54 p.m. username (name) Changed value_bool from <old_value> to <new_value>

Describe alternatives you've considered
We can do it in post_save signal and expose a configuration via settings to enable/disable.
We can also do it by overriding construct_change_message method.

Additional context

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@vivekshah1801 vivekshah1801 added the enhancement New feature or request label May 22, 2024
@fabiocaccamo
Copy link
Owner

fabiocaccamo commented May 22, 2024

@vivekshah1801 I think that overriding construct_change_message is the best approach for achieving this.

Here a quick sketch:

from django.utils.text import get_text_list

# ...

    def construct_change_message(self, request, form, formsets, add=False):
        if add:
            return super().construct_change_message(request, form, formsets, add=add)
        
        change_message = []
        if form.changed_data:
            changes = []
            for field in form.changed_data:
                old_value = form.initial.get(field, _("<unknown>"))
                new_value = form.cleaned_data.get(field, _("<unknown>"))
                changes.append(f"{field} from {old_value} to {new_value}")
            change_message.append(f"Changed {get_text_list(changes, 'and')}.")
        return change_message

If you can test it on your side and submit a PR it would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants