Skip to content

Commit

Permalink
fix(imports): add float casting for track changes
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Jul 25, 2024
1 parent d752c4b commit ea118ed
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions rdmo/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import models
from django.db.models.fields import FloatField
from django.utils.translation import gettext_lazy as _

from rest_framework.utils import model_meta
Expand Down Expand Up @@ -134,15 +135,20 @@ def track_changes_on_element(element: dict,

_initialize_track_changes_element_field(element, element_field)

lookup_model_field = None
if original_value is None and original is not None:
lookup_field = element_field if instance_field is None else instance_field
original_value = getattr(original, lookup_field, '')
lookup_model_field = original._meta.get_field(lookup_field)

if isinstance(new_value,str) and isinstance(original_value,int):
# typecasting of original value to str, for comparison '0' == 0
# specific edge-case, maybe generalize later
original_value = str(original_value)

if isinstance(lookup_model_field, FloatField):
new_value = float(new_value)

element[ImportElementFields.DIFF][element_field][ImportElementFields.CURRENT] = original_value
element[ImportElementFields.DIFF][element_field][ImportElementFields.NEW] = new_value

Expand Down Expand Up @@ -280,30 +286,30 @@ def set_foreign_field(instance, field_name, element, uploaded_uris=None, origina
def set_extra_field(instance, field_name, element,
extra_field_helper: Optional[ExtraFieldHelper] = None, original=None) -> None:

extra_value = None
extra_field_value = None
if field_name in element:
extra_value = element.get(field_name)
extra_field_value = element.get(field_name)
else:
# get the default field value from the instance
instance_value = getattr(instance, field_name)
element[field_name] = instance_value
extra_value = instance_value
extra_field_value = instance_value

if extra_field_helper is not None:
# default_value
extra_value_from_helper = extra_field_helper.get_value(instance=instance,
key=field_name)
# overwrite None or '' values by the get_value from the helper
if extra_value is None or extra_value == '':
extra_value = extra_value_from_helper
if extra_field_value is None or extra_field_value == '':
extra_field_value = extra_value_from_helper

if extra_field_helper.overwrite_in_element:
element[field_name] = extra_value
element[field_name] = extra_field_value

if extra_value is not None:
setattr(instance, field_name, extra_value)
if extra_field_value is not None:
setattr(instance, field_name, extra_field_value)
# track changes
track_changes_on_element(element, field_name, new_value=extra_value, original=original)
track_changes_on_element(element, field_name, new_value=extra_field_value, original=original)


def track_changes_m2m_instances(element, field_name,
Expand Down

0 comments on commit ea118ed

Please sign in to comment.