Skip to content

Commit

Permalink
name uniquness checking for food import
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Feb 18, 2024
1 parent 596ec91 commit 7bc567a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cookbook/helper/open_data_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,11 @@ def import_food(self):
unit_g = Unit.objects.filter(space=self.request.space, base_unit__iexact='g').first()

existing_data = {}
for obj in Food.objects.filter(space=self.request.space, open_data_slug__isnull=False).values('pk', 'name', 'open_data_slug'):
existing_data[obj['open_data_slug']] = obj
existing_data_names = {}
for obj in Food.objects.filter(space=self.request.space).values('pk', 'name', 'open_data_slug'):
if obj.open_data_slug:
existing_data[obj['open_data_slug']] = obj
existing_data_names[obj['name']] = obj

update_list = []
create_list = []
Expand All @@ -203,9 +206,13 @@ def import_food(self):
'space': self.request.space.id,
}

if obj['open_data_slug'] in existing_data:
if obj['open_data_slug'] in existing_data or obj['name'] in existing_data_names:
if obj['open_data_slug'] in existing_data:
obj['pk'] = existing_data[obj['open_data_slug']]['pk']
elif obj['name'] in existing_data_names:
obj['pk'] = existing_data_names[obj['name']]['pk']

obj['space'] = self.request.space
obj['pk'] = existing_data[obj['open_data_slug']]['pk']
obj = Food(**obj)
update_list.append(obj)
else:
Expand Down

0 comments on commit 7bc567a

Please sign in to comment.