Skip to content

Commit

Permalink
improved open data importer and fdc API
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Feb 18, 2024
1 parent 03ccc8e commit f57d2ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cookbook/helper/open_data_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def import_units(self):
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
)
Expand Down Expand Up @@ -102,6 +102,7 @@ def import_property(self):
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
)
Expand All @@ -113,7 +114,7 @@ def import_property(self):

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:
Expand Down
6 changes: 3 additions & 3 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def fdc(self, request, pk):
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,
))
Expand Down Expand Up @@ -669,9 +669,9 @@ def get_queryset(self):
order_field = 'id'

ordering = f"{'' if order_direction == 'asc' else '-'}{order_field}"

self.queryset = self.queryset.filter(Q(created_by=self.request.user) | Q(shared=self.request.user)).filter(
space=self.request.space).distinct().order_by(ordering)
space=self.request.space).distinct().order_by(ordering)
return super().get_queryset()


Expand Down

0 comments on commit f57d2ca

Please sign in to comment.