Closed alukach closed 1 year ago
I guess there's no way to do Draft PRs right now in this org 🤷 . Going to keep open but note that it's not ready.
It was quite a while ago, so I don't remember exactly, but I would guess it was because the change in the UI was meant to reflect the Campaign Draft related to the Deployment Draft not only in the breadcrumbs, but throughout that view (hence the addition of links within the form to the Published Campaign and Latest Draft). My understanding at the time, I think, was that the related campaign should point to the latest draft for that campaign, since we still supported multiple drafts. I expect the issues you're correcting here are the result of my misunderstanding the underlying data models, though it's odd that it's only coming up 9 months after those changes were committed.
What I'm changing
This PR fixes #513 wherein certain records weren't showing up on the DOI approval view's multiselect inputs.
The issue appears to have been caused by changes introduced in #405, where the query logic around generating records for the multi-select was altered. I can not tell why these form-logic changes were made as they do not seem to relate to the issue that #405 was addressing (breadcrumb link generation). Nonetheless, the PR altered the way in which we retrieve a queryset of drafts to be used when rendering the
<select>
element for foreign key fields on model forms. The update alters the logic to retrieve the latest update:There are a few issues with this strategy (mentioned below in the "How I Did It" section), however the major issue that created this bug is that we are returning a queryset of records with
uuid
attributes that don't necessarily match the canonical UUID of the real-world concept. We attempted to account for this by annotating the queryset with aneffective_uuid
attribute and customizing theiterator
of theChangeChoiceField
with an iterator that makes use of theeffective_uuid
attribute:https://github.com/NASA-IMPACT/admg-backend/blob/d475bfe04131c18bf0cde5d66bda0e51935e7bd6/admin_ui/fields.py#L131-L144
However, we neglected to customize the iterator on the the
ChangeMultipleChoiceField
, which is used by the DOI approval view:https://github.com/NASA-IMPACT/admg-backend/blob/d475bfe04131c18bf0cde5d66bda0e51935e7bd6/admin_ui/fields.py#L123-L128
Because of this, the
<option>
elements rendered within the multiple choice<select>
point to the UUID of the latest drafts, not the canonical UUIDs of the real-world concept. As such, the UUID set on a given DOI does not match any of the options in the select dropdown and no selected attribute is rendered.How I did it
The current strategy of retrieving the most recent drafts has a few of issues:
short_name
values. However, on the final returned queryset, we run.annotate_from_published(..., to_attr="short_name")
, meaning that we are always pulling theshort_name
attribute from the published record. If there is no published record, then the logic to get the most recent draft is superfluous because we would only have a single unpublishedaction=CREATE
draft.status=models.Change.Statuses.IN_TRASH
, but that only applies to discarding the working draft, not deleting a published record.short_name
value. This is something I'm a bit hazy on, but I believe that there was a time (possibly now?) that theupdate
attribute of anaction=UPDATE
draft only contained the diff of what was being changed on a published record. On a clone of the staging database, we can run the following to see that there are a number of drafts that do not have aupdate -> 'short_name'
value:There could have been an approach to continue to use the query as currently written in
get_queryset_for_model()
and to update ourChangeMultipleChoiceField
to descend fromModelMultipleChoiceField
rather than the currentMultipleChoiceField
. However, for the reasons described above, I felt it was better to simply back-out the changes introduced in #405. I also attempted to add more documentation, as this is a particularly dense portion of our codebase and can be challenging to understand.Outcome
This resolved the issue and improves the load time by about 65-80% (at least when running in slow DEBUG mode).
Before
http://localhost:8001/campaigns/fb60a9a9-d127-403e-acc7-9203346d48fe/doi-approval
http://localhost:8001/campaigns/cbfb7dff-400d-4639-ba2d-431b00ae8971/doi-approval
After
http://localhost:8001/campaigns/fb60a9a9-d127-403e-acc7-9203346d48fe/doi-approval
http://localhost:8001/campaigns/cbfb7dff-400d-4639-ba2d-431b00ae8971/doi-approval
How you can test it
For testing, it is recommended to have a dev database matching the staging database. To achieve this:
docker stop <WEBSERVER_PROCESS_ID>
docker exec -it <DB_PROCESS_ID> bash
dropdb -p 5432 -U user admg_webapp
createdb -p 5432 -U user admg_webapp
pg_dump -h admgtestdb.ccsbdjpqglnr.us-east-1.rds.amazonaws.com -U postgres prod_clone_20230523 | psql postgres://user:password@localhost/admg_webapp
The following URLs demonstrate the issue:
Questions
@edkeeble do you recall why you changed
fields.py
in #405? To my understanding, that logic has nothing to do with breadcrumb links (ie it is purely focused on form fields).