From aba120d53928ef323048ab2d0615b6efefc1f694 Mon Sep 17 00:00:00 2001 From: Tai Wilkin-Corraggio Date: Mon, 21 Mar 2022 10:10:57 -0400 Subject: [PATCH] Fix handling of numeric custom cells in Excel Custom cells can have numeric values. When parsing the sheets, a value that appears as 130 in the workbook can appear as a float 130.0. This commit attempts to fix this issue by using an int(float(x)) nested cast to ensure that the decimal portion is removed. Note that this will convert all floats to integers when submitted as custom text. If a contributor would like to submit floats in a custom text field in the future, we will need to implement that as an enhancement. --- CHANGELOG.md | 1 + src/django/api/helpers.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f41ed96d..b8a8bace3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Hide contributor details in embed downloads [#1742](https://github.com/open-apparel-registry/open-apparel-registry/pull/1742) - Wrap long filenames in My Lists page [#1756](https://github.com/open-apparel-registry/open-apparel-registry/pull/1756) +- Fix handling of numeric custom fields [#1757](https://github.com/open-apparel-registry/open-apparel-registry/pull/1757) ### Security diff --git a/src/django/api/helpers.py b/src/django/api/helpers.py index 83e81cdec..028131e0b 100644 --- a/src/django/api/helpers.py +++ b/src/django/api/helpers.py @@ -47,12 +47,19 @@ def get_csv_values(csv_data): return values +def try_parse_int_from_float(value): + try: + return str(int(float(value))) + except ValueError: + return value + + def get_single_contributor_field_values(item, fields): data = parse_raw_data(item.raw_data) for f in fields: value = data.get(f['column_name'], None) if value is not None: - f['value'] = value + f['value'] = try_parse_int_from_float(value) return fields @@ -62,7 +69,8 @@ def get_list_contributor_field_values(item, fields): for f in fields: if f['column_name'] in list_fields: index = list_fields.index(f['column_name']) - f['value'] = data_values[index] + f['value'] = try_parse_int_from_float(data_values[index]) + return fields