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

feat(projectHistoryLogs): create project history logs when related objects change TASK-944 #5223

Merged
merged 7 commits into from
Nov 6, 2024

Conversation

rgraber
Copy link
Contributor

@rgraber rgraber commented Nov 4, 2024

📣 Summary

Create project history logs for changes to associated media files, rest services, and imported data.

📖 Description

Log changes whenever someone:

  1. adds/deletes/modifies a REST service
  2. adds/deletes a media attachment (project, not submission)
  3. dis/connects to another project
  4. modifies fields imported from another project

👀 Preview steps

  1. ℹ️ Log in as superuser and create a survey
  2. Deploy the survey
  3. Go to Settings > Media and upload a small jpg:
    tiara
  4. Go to localhost/api/v2/audit-logs/?q=log_type:project-history AND metadata__asset_uid:<asset_uid>
  5. 🟢The latest log should look like
{
    "app_label":"kpi",
    "model_name":"asset",
    "user":"http://localhost/api/v2/users/<username>/?format=json",
    "user_uid":"<your user uid>",
    "username":"<your username>",
    "action":"add-media",
    "metadata":{
        "source":"<browser><OS>",
        "asset_uid":"<asset-uid>",
        "asset-file":{
            "uid":"<uid>",
            "filename":"tiara.jpg",
            "md5_hash":"md5:e9ad5b5b0b66e3c063c4d067bfe54ca4",
            "download_url":"<some_url>"
        },
        "ip_address":"<your ip>",
        "log_subtype":"project"
    },
    "date_created":"20
  1. Verify the download url is correct by going to localhost/<download_url>
  2. Remove the picture from the project
  3. Reload the endpoint. You should see a new log with the same metadata and action: delete-media
  4. Go to Settings > Rest Services > Register a new service
  5. Add a new service named 'Test' with the endpoint 'http://www.google.com'. Keep it enabled.
  6. 🟢 Reload the endpoint. You should see a new log with action: register-service and the following in metadata.hook:
{
      "uid":"<uid>",
      "active":true,
      "endpoint":"https://www.google.com"
},
  1. Pause the hook by editing it and de-selecting 'Enabled'
  2. 🟢 Reload the endpoint. You should see a new log with action: register-service and the following in metadata.hook:
{
     "uid":"<uid>",
     "active":false,
     "endpoint":"https://www.google.com"
},
  1. Create and deploy a new survey named 'Test connect' with questions q1 and q2
  2. Go to Settings > Connect Project and enable data sharing (all questions)
  3. Go back to the original project and go to Settings > Connect Project
  4. Select a different project to import data from > 'Test connect' > Import > Import all fields
  5. 🟢Reload the endpoint. You should see a new log with action: connect-project and the following in metadata.paired_data:
{
     "source_name":"Test connect",
     "source_uid":"<test connect uid>",
     "fields":["q1", "q2"]
},
  1. Go back to Settings > Connect Project and click the Gear icon next to 'Test connect' (under Imported)
  2. Deselect q1 from the imported questions and hit Accept
  3. 🟢Reload the endpoint. You should see a new log with action: modify-imported-fields and the following in metadata.paired_data:
{
     "source_name":"Test connect",
     "source_uid":"<test connect uid>",
     "fields":["q2"]
},
  1. Disconnect the projects by clicking the trash can icon next to 'Test connect'
  2. 🟢 Reload the endpoint. You should see a new log with action: disconnect-project and the following in metadata.paired_data:
{
     "source_name":"Test connect",
     "source_uid":"<test connect uid>",
     "fields":["q2"]
},

💭 Notes

Updates the AuditLoggedViewSet code to allow fields to be put in the initial_data and/or updated_data dictionaries under a different label if desired. If logged_fields has a tuple (k,v) instead of a field, the relevant dictionary will store {'k': object.v} rather than {v: object.v}.
Updates the HookViewSet, AssetFileViewSet, and PairedDataViewsets to inherit from the AuditLoggedViewSet base class, adding the necessary fields from each to the logged_fields as well as the object id of the related asset.
Add a method in ProjectHistoryLog for dealing with requests that come from these kinds of related-object view sets, where it just takes everything in the updated_data or initial_data dictionary (except the object_id, which it uses as its own object_id) and puts it in the metadata under a given label.
Also refactors the overall create_from_request delegation method to use a dictionary of <view_name> : method rather than a whole bunch of if-elses.
Unrelated to other things, changes calls to the deprecated method assertEquals to assertEqual to get rid of warnings.

BTW @noliveleger in order to make PRs less confusing I'll keep going the way I've been doing it then do an alphabetizing PR at the very end.

Copy link

@magicznyleszek magicznyleszek removed their request for review November 4, 2024 17:05
@Akuukis
Copy link
Contributor

Akuukis commented Nov 4, 2024

Few comments according to the new PR workflow on the title:

  • use camelCase for the feature/route alike scopes ("feat(PprojectHistoryLogs): ...")
  • lower case for title ("Ccreate")
  • assign yourself

@rgraber rgraber changed the title feat(ProjectHistoryLogs): Create project history logs when related objects change feat(projectHistoryLogs): Create project history logs when related objects change Nov 4, 2024
@rgraber rgraber changed the title feat(projectHistoryLogs): Create project history logs when related objects change feat(projectHistoryLogs): create project history logs when related objects change Nov 4, 2024
@rgraber rgraber self-assigned this Nov 4, 2024
@rgraber rgraber requested a review from Guitlle November 4, 2024 17:20
@rgraber rgraber changed the title feat(projectHistoryLogs): create project history logs when related objects change feat(projectHistoryLogs): create project history logs when related objects change TASK-944 Nov 4, 2024
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.

LGTM

@rgraber rgraber merged commit 3cdd897 into main Nov 6, 2024
7 checks passed
@rgraber rgraber deleted the rsgraber/TASK-944-rest-service-changes branch November 6, 2024 15:49
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.

3 participants