-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fix crash when creating a hearing with existing file references, add option for creating new files/images by reference #490
Conversation
KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀 |
Didn't go exactly as I thought. Back to the drawing board. |
KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀 |
5f13dd1
to
32774cf
Compare
KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't spot anything obvious, LGTM!
Some of the serializers worked with a update-or-copy logic instead of update-or-create (as their name would imply), which was added in PR #363 (6c97234). This caused an error whenever you're creating a new hearing and have already uploaded a file to a section (via the form's file upload functionality). Added a test to confirm that this scenario functions correctly now. This removes that copy (or rather, save as copy) logic and moves the burden to the consumer of the API, i.e. the consumer needs to remove the IDs from the data they want to copy. However, files and images still need a reference to the instance they're copied from, so add a new field "reference_id" for this. This allows the user to create new files or images based on existing instances. In its current state, it's meant for "save as new" type of situations. This means that if the pk is set for an image/file, then it will ignore the reference_id, i.e. you cannot update an existing instance using reference_id.
32774cf
to
d360691
Compare
Quality Gate passedIssues Measures |
KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀 |
TL;DR:
reference_id
field for files/images when creating new hearingsSome of the serializers worked with a update-or-copy logic instead of update-or-create (as their name would imply), which was added in PR #363 (6c97234).
This caused an error whenever you're creating a new hearing and have already uploaded a file to a section (via the form's file upload functionality). Added a test to confirm that this scenario functions correctly now. The crash is fixed (or should be fixed) in the PR's commit 32774cf, but leaves the rather strange serializer logic in place.
E.g. if you want to create a new hearing using an existing hearing as a base, you'll have to remove the IDs from the hearing and the sections, but need to have the IDs for files and images.
Since this works on an update-or-copy basis, it means that you cannot use an existing instance for a file or an image on creation; if you do, it gets copied. The only other way to use existing instances is to first create the hearing without the files/images and then update the hearing.
This PR replaces that copy (or rather, save as copy) logic with create logic (so create-or-update rather than copy-or-update) and moves the burden to the consumer of the API, i.e. the consumer needs to remove the IDs from the data they want to copy.
However, files and images still need a reference to the instance they're copied from, so add a new field "reference_id" for this. Not the perfect solution, but this allows the user to create new files or images based on existing instances while retaining some kind of sanity with the API.
In its current state, it's meant for "save as new" type of situations. This means that if the pk is set for an image/file, then it will ignore the reference_id, i.e. you cannot update an existing instance using reference_id.
Also fixes a crash when trying to get a URL for a file the user is trying to copy by adding a check for whether the file has a PK or not. E.g. try to copy a file -> get reference file from database -> set pk to
None
-> validation checks for URL -> URL requires a PK -> 💥Also note that while the previous code referred to copying in comments and in its associated commit/PR, it's actually saving as a copy/saving as new rather than just copying. This is a very important distinction, since it means that you have the option to edit the hearing before creating a copy of it instead of just creating a clone of another hearing.
Refs KER-351
Related front-end PR: City-of-Helsinki/kerrokantasi-ui#1025