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 import element sorting when saving (for related items) #1078

Merged
merged 18 commits into from
Aug 15, 2024

Conversation

MyPyDavid
Copy link
Member

@MyPyDavid MyPyDavid commented Jul 24, 2024

Description

Fixes for the Import Feature

  • revert order function back to original (pre dev-2.2.0) append_element
  • that fixes the order of m2m parts in uploaded elements
  • needed to cast the order through field items to int
  • removes keys from the uploaded element that are not relevant for the import (like legacy keys)
  • type casting of certain fields (extra_fields) is done by the instance.full_clean() and these instance values are written back into the element[field]
    • for example, to properly see the differences, the uploaded value needs to be case to float for FloatField otherwise a str will be compare to float

Motivation and Context

Made an overview of the models and the relations with the CLI tool graph_models

/manage.py graph_models conditions options domain questions tasks views -g --relation-fields-only=True --no-inheritance -x=created,updated,editors,sites,groups -l=dot -o rdmo_content_relations.svg

rdmo_content_relations

How has this been tested?

Screenshots (if appropriate)

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactoring (no functional changes, no api changes)

Checklist

  • I have read the contributor guide.
  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@MyPyDavid MyPyDavid added this to the RDMO 2.2.0 milestone Jul 24, 2024
@MyPyDavid MyPyDavid self-assigned this Jul 24, 2024
@MyPyDavid MyPyDavid marked this pull request as ready for review July 25, 2024 12:43
rdmo/core/imports.py Outdated Show resolved Hide resolved
@MyPyDavid
Copy link
Member Author

MyPyDavid commented Aug 2, 2024

I broke the sorting with append_element with a typo before in the import feature branch.
Am now reverting it back to original sorting, but get a RecursionError for the legacy tests..

typo, it should be value instead of element_value in:

if isinstance(element_value, dict):

rdmo/core/xml.py Show resolved Hide resolved
rdmo/management/imports.py Show resolved Hide resolved
rdmo/core/xml.py Outdated
@@ -257,6 +257,21 @@ def validate_pre_conversion_for_missing_key_in_legacy_elements(elements, version
raise ValueError(f"Missing legacy elements, elements containing 'key' were expected for this XML with version {version} and elements {models_in_elements}.") # noqa: E501


def update_related_elements(elements: Dict, related_uri: str, related_model: str, related_key: str, field_name: str):
Copy link
Member

Choose a reason for hiding this comment

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

What ist this method used for?

Copy link
Member Author

@MyPyDavid MyPyDavid Aug 7, 2024

Choose a reason for hiding this comment

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

I think there was something broken/missing with the current legacy xml parsing because the related elements are not written back into the ascending elements (following the current element Models).
For example, for the sections I added:

del element['catalog']  # sections do not have catalog anymore

This is correct right? They don't have catalog anymore..
But then the corresponding catalog needs to know about this section so I've added this function to "update" the catalog in that case.
The same then holds for pages, questionsets, questions etc.. right?

Still it's for the legacy xml's.. I could also revert if needed...

Copy link
Member Author

@MyPyDavid MyPyDavid Aug 8, 2024

Choose a reason for hiding this comment

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

  • rename DEFAULT_XML_VERSION to LEGACY_XML_VERSION

rdmo/core/import_helpers.py Show resolved Hide resolved
package-lock.json Outdated Show resolved Hide resolved
try:
# Instantiate the DRF field and use it to validate and convert the value
drf_field = drf_field_class()
element[field_name] = drf_field.to_internal_value(element_field_value)
Copy link
Member Author

Choose a reason for hiding this comment

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

Ive added some deserialization via the DRF Fields in here, it happens only for the so-called 'extra fields' of a rdmo model.

Copy link
Member

Choose a reason for hiding this comment

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

nice.

Copy link
Member

Choose a reason for hiding this comment

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

I am arguing about creating to many functions all the time, but this thing could be a relatively generic function validate_with_serializer_field(instance, field_name) or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes that's true, I can refactor it to that, as the last change of this PR

Copy link
Member

Choose a reason for hiding this comment

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

... as the last change of this PR

🙄

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, Ive refactored it into a separate function, it could be moved to core it it's needed by other apps/functions..

try:
# Instantiate the DRF field and use it to validate and convert the value
drf_field = drf_field_class()
element[field_name] = drf_field.to_internal_value(element_field_value)
Copy link
Member

Choose a reason for hiding this comment

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

nice.

try:
# Instantiate the DRF field and use it to validate and convert the value
drf_field = drf_field_class()
element[field_name] = drf_field.to_internal_value(element_field_value)
Copy link
Member

Choose a reason for hiding this comment

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

I am arguing about creating to many functions all the time, but this thing could be a relatively generic function validate_with_serializer_field(instance, field_name) or something.

@MyPyDavid
Copy link
Member Author

thanks for the review!

@MyPyDavid MyPyDavid merged commit 2f71c09 into dev-2.2.0 Aug 15, 2024
18 checks passed
@MyPyDavid MyPyDavid deleted the fix_import_element_sorting branch August 22, 2024 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants