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

Log project name and settings changes #5193

Merged
merged 18 commits into from
Oct 29, 2024

Conversation

rgraber
Copy link
Contributor

@rgraber rgraber commented Oct 24, 2024

Checklist

  1. If you've added code that should be tested, add tests
  2. If you've changed APIs, update (or create!) the documentation
  3. Ensure the tests pass
  4. Run ./python-format.sh to make sure that your code lints and that you've followed our coding style
  5. Write a title and, if necessary, a description of your work suitable for publishing in our release notes
  6. Mention any related issues in this repository (as #ISSUE) and in other repositories (as kobotoolbox/other#ISSUE)
  7. Open an issue in the docs if there are UI/UX changes
  8. Create a testing plan for the reviewer and add it to the Testing section

Description

Create project history logs for changes to project name and settings.

Notes

Refactor the asset model to be more explicit about which settings fields are standardized for saving (makes testing easier)

  1. Add settings and latest_version.uid to the fields tracked on the AssetViewSet requests
  2. Add a method to ProjectHistoryLog that handles an update request by determining which fields were changed and delegating to an appropriate handler.
  3. Add handlers to deal with changes to name and settings
    a. For settings, if the setting is a list, include values added and values removed, otherwise, just store the old and new values

Testing

  1. Create a new project
  2. Go to Settings > General
  3. Update the name and save
  4. Go to /api/v2/audit-logs/?q=metadata__asset_uid=<project uid> AND log_type='project-history'&format=json
  5. You should see a project history log that looks like:
        {
            "app_label": "kpi",
            "model_name": "asset",
            "user": "http://kf.kobo.local:8080/api/v2/users/{you}/",
            "user_uid": "{your uid}",
            "username": "{your username}",
            "action": "update-name",
            "metadata": {
                "source": "{browser} ({OS})",
                "asset_uid": "{new project uid}",
                "ip_address": "172.18.0.1",
                "log_subtype": "project",
                "latest_version_uid": "{version uid}",
                "name": {
                      "old": <old_name>,
                      "new": <new_name>,
                }
            },
            "date_created": "{whenever}",
            "log_type": "project-history"
        }

  1. Update the description to 'new description' and countries to Albania and Algeria and save
  2. Reload the endpoint. You should see a new project history log with:
    a. action = 'update-settings'
    b. metadata.settings =
    { 'description': { 'old': '', 'new': 'new description'}, 'country': {'added': [{'label':'Albania', 'value':'ALB'}, {'label': 'Algeria', 'value': 'ALG'}, 'removed': []}, 'country_codes': {'added': ['ALB','ALG'], 'removed': []} }
  3. Update the name (don't save yet)
  4. Update the sector to 'Environment' and save
  5. Reload the endpoint. You should see 2 new project history logs, one for the name change and one for the settings change
  6. Go to edit the form
  7. Change the name of the project from the form editing screen
  8. Reload the endpoint and make sure there's a new log for the name change
  9. To test deleting/resetting the usual settings: run curl -X PATCH -H 'Authorization: Token <your token>' -H 'Content-Type: application/json' -H 'Accept: application/json' http://kf.kobo.local/api/v2/assets/<asset uid> --data '{"settings": {}}'
  10. Reload the endpoint. You should see a new project history log with metadata.settings =
    {
         'country': {
               'added': [],
               'removed': [{'label':'Albania', 'value':'ALB'}, {'label':'Algeria', 'value': 'ALG'}], 
         }
         'country_codes': {
                'added': [],
                'removed': ['ALB', 'ALG']
         }
         'description': {
                 'old': 'new description',
                 'new': '',
          }
         'sector': {
                 'old': {'label': 'environment', 'value':'environment'},
                 'new': {}
         }
    }
    
  11. To test new settings: run curl -X PATCH -H 'Authorization: Token <your token>' -H 'Content-Type: application/json' -H 'Accept: application/json' http://kf.kobo.local/api/v2/assets/<asset uid> --data '{"settings": {"new_setting": "new_value"}}'
  12. Reload the endpoint. You should see a new project history log with metadata.settings =
    {
          'new_setting': {
                'old': null,
                'new': 'new_value',
           }
    }
    
  13. To test deleting an arbitrary setting: run curl -X PATCH -H 'Authorization: Token <your token>' -H 'Content-Type: application/json' -H 'Accept: application/json' http://kf.kobo.local/api/v2/assets/<asset uid> --data '{"settings": {}}'
  14. Reload the endpoint. You should see a new project history log with metadata.settings =
    {
          'new_setting': {
                'old': 'new_value',
                'new': null,
           }
    }
    

Copy link

@rgraber rgraber marked this pull request as ready for review October 24, 2024 18:05
@rgraber rgraber requested a review from Guitlle October 24, 2024 18:08
Base automatically changed from rsgraber/TASK-944-log-deployment-changes-better to main October 25, 2024 17:20
Copy link
Contributor

@Guitlle Guitlle left a comment

Choose a reason for hiding this comment

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

A couple of change suggestions

kobo/apps/audit_log/base_views.py Outdated Show resolved Hide resolved
kobo/apps/audit_log/tests/test_project_history_logs.py Outdated Show resolved Hide resolved
@rgraber rgraber merged commit a60a3b8 into main Oct 29, 2024
7 checks passed
@rgraber rgraber deleted the rsgraber/TASK-944-record-name-and-settings-changes branch November 1, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants