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

Fix crash when creating a hearing with existing file references, add option for creating new files/images by reference #490

Merged
merged 2 commits into from
May 7, 2024

Conversation

danipran
Copy link
Contributor

@danipran danipran commented Apr 26, 2024

TL;DR:

  • replace copy-or-update logic in CreateUpdateSerializers create-or-update
  • fix crash when creating a hearing with existing file references (i.e. upload files before creating a hearing and then posting that hearing with the pre-uploaded file)
  • add a write-only reference_id field for files/images when creating new hearings

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. 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

@terovirtanen
Copy link
Contributor

KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀

@danipran danipran marked this pull request as draft April 26, 2024 12:26
@danipran
Copy link
Contributor Author

Didn't go exactly as I thought. Back to the drawing board.

@terovirtanen
Copy link
Contributor

KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀

@danipran danipran changed the title KER-351: Remove copy logic from *CreateUpdateSerializers KER-351: Fix crash when creating a hearing with existing file references, add hearing copy endpoint Apr 29, 2024
@danipran danipran changed the title KER-351: Fix crash when creating a hearing with existing file references, add hearing copy endpoint Fix crash when creating a hearing with existing file references, add option for creating new files/images by reference May 2, 2024
@danipran danipran force-pushed the KER-351/remove-copy-logic branch from 5f13dd1 to 32774cf Compare May 3, 2024 07:51
@terovirtanen
Copy link
Contributor

KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀

@danipran danipran marked this pull request as ready for review May 3, 2024 08:29
@danipran danipran requested a review from a team May 3, 2024 08:30
democracy/views/section.py Outdated Show resolved Hide resolved
Copy link
Contributor

@charn charn left a 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! :shipit:

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.
@danipran danipran force-pushed the KER-351/remove-copy-logic branch from 32774cf to d360691 Compare May 6, 2024 12:33
Copy link

sonarcloud bot commented May 7, 2024

@terovirtanen
Copy link
Contributor

KERROKANTASI-API branch is deployed to platta: https://kerrokantasi-pr490.api.dev.hel.ninja 🚀🚀🚀

@danipran danipran merged commit 54f468d into master May 7, 2024
23 checks passed
@danipran danipran deleted the KER-351/remove-copy-logic branch May 7, 2024 07:24
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