improved open data importer and fdc API

This commit is contained in:
vabene1111 2024-02-18 08:50:50 +01:00
parent 03ccc8e044
commit f57d2ca832
2 changed files with 6 additions and 5 deletions

View File

@ -33,7 +33,7 @@ class OpenDataImporter:
obj = Unit(
name=self.data[datatype][u]['name'],
plural_name=self.data[datatype][u]['plural_name'],
base_unit=self.data[datatype][u]['base_unit'] if self.data[datatype][u]['base_unit'] != '' else None,
base_unit=self.data[datatype][u]['base_unit'].lower() if self.data[datatype][u]['base_unit'] != '' else None,
open_data_slug=u,
space=self.request.space
)
@ -102,6 +102,7 @@ class OpenDataImporter:
obj = PropertyType(
name=self.data[datatype][k]['name'],
unit=self.data[datatype][k]['unit'],
fdc_id=self.data[datatype][k]['fdc_id'],
open_data_slug=k,
space=self.request.space
)
@ -113,7 +114,7 @@ class OpenDataImporter:
total_count = 0
if self.update_existing and len(update_list) > 0:
PropertyType.objects.bulk_update(update_list, ('name', 'open_data_slug'))
PropertyType.objects.bulk_update(update_list, ('name', 'fdc_id', 'unit', 'open_data_slug'))
total_count += len(update_list)
if len(create_list) > 0:

View File

@ -629,7 +629,7 @@ class FoodViewSet(viewsets.ModelViewSet, TreeMixin):
if fn['nutrient']['id'] == pt.fdc_id:
food_property_list.append(Property(
property_type_id=pt.id,
property_amount=round(fn['amount'], 2),
property_amount=max(0, round(fn['amount'], 2)), # sometimes FDC might return negative values which make no sense, set to 0
import_food_id=food.id,
space=self.request.space,
))