From 705fa18dd6250dc139a10ed890055bf6e3e640f9 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 17 Feb 2024 08:43:41 +0100 Subject: [PATCH] fixed open data property type and category --- cookbook/helper/open_data_importer.py | 72 ++++++++++++++----- cookbook/views/api.py | 21 ++++-- .../components/OpenDataImportComponent.vue | 21 ++++-- 3 files changed, 87 insertions(+), 27 deletions(-) diff --git a/cookbook/helper/open_data_importer.py b/cookbook/helper/open_data_importer.py index 48f4495faa..833ec34e83 100644 --- a/cookbook/helper/open_data_importer.py +++ b/cookbook/helper/open_data_importer.py @@ -23,26 +23,25 @@ def import_units(self): datatype = 'unit' existing_data = {} - for u in Unit.objects.filter(space=self.request.space, open_data_slug__isnull=False).values('pk', 'name', 'open_data_slug'): - existing_data[u['open_data_slug']] = u + for obj in Unit.objects.filter(space=self.request.space, open_data_slug__isnull=False).values('pk', 'name', 'open_data_slug'): + existing_data[obj['open_data_slug']] = obj update_list = [] create_list = [] - insert_list = [] for u in list(self.data[datatype].keys()): - unit = Unit( + 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, open_data_slug=u, space=self.request.space ) - if unit.open_data_slug in existing_data: - unit.pk = existing_data[unit.open_data_slug]['pk'] - update_list.append(unit) + if obj.open_data_slug in existing_data: + obj.pk = existing_data[obj.open_data_slug]['pk'] + update_list.append(obj) else: - create_list.append(unit) + create_list.append(obj) total_count = 0 if self.update_existing and len(update_list) > 0: @@ -58,29 +57,70 @@ def import_units(self): def import_category(self): datatype = 'category' - insert_list = [] + existing_data = {} + for obj in SupermarketCategory.objects.filter(space=self.request.space, open_data_slug__isnull=False).values('pk', 'name', 'open_data_slug'): + existing_data[obj['open_data_slug']] = obj + + update_list = [] + create_list = [] + for k in list(self.data[datatype].keys()): - insert_list.append(SupermarketCategory( + obj = SupermarketCategory( name=self.data[datatype][k]['name'], open_data_slug=k, space=self.request.space - )) + ) + + if obj.open_data_slug in existing_data: + obj.pk = existing_data[obj.open_data_slug]['pk'] + update_list.append(obj) + else: + create_list.append(obj) + + total_count = 0 + if self.update_existing and len(update_list) > 0: + SupermarketCategory.objects.bulk_update(update_list, ('name', 'open_data_slug')) + total_count += len(update_list) + + if len(create_list) > 0: + SupermarketCategory.objects.bulk_create(create_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',)) + total_count += len(create_list) - return SupermarketCategory.objects.bulk_create(insert_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',)) + return total_count def import_property(self): datatype = 'property' - insert_list = [] + existing_data = {} + for obj in PropertyType.objects.filter(space=self.request.space, open_data_slug__isnull=False).values('pk', 'name', 'open_data_slug'): + existing_data[obj['open_data_slug']] = obj + + update_list = [] + create_list = [] + for k in list(self.data[datatype].keys()): - insert_list.append(PropertyType( + obj = PropertyType( name=self.data[datatype][k]['name'], unit=self.data[datatype][k]['unit'], open_data_slug=k, space=self.request.space - )) + ) + if obj.open_data_slug in existing_data: + obj.pk = existing_data[obj.open_data_slug]['pk'] + update_list.append(obj) + else: + create_list.append(obj) + + total_count = 0 + if self.update_existing and len(update_list) > 0: + PropertyType.objects.bulk_update(update_list, ('name', 'open_data_slug')) + total_count += len(update_list) - return PropertyType.objects.bulk_create(insert_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',)) + if len(create_list) > 0: + PropertyType.objects.bulk_create(create_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',)) + total_count += len(create_list) + + return total_count def import_supermarket(self): datatype = 'store' diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 1b1bd29ab7..b16d3045a6 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1605,6 +1605,7 @@ def post(self, request, *args, **kwargs): # TODO validate data print(request.data) selected_version = request.data['selected_version'] + selected_datatypes = request.data['selected_datatypes'] update_existing = str2bool(request.data['update_existing']) use_metric = str2bool(request.data['use_metric']) @@ -1614,12 +1615,20 @@ def post(self, request, *args, **kwargs): response_obj = {} data_importer = OpenDataImporter(request, data, update_existing=update_existing, use_metric=use_metric) - response_obj['unit'] = len(data_importer.import_units()) - response_obj['category'] = len(data_importer.import_category()) - response_obj['property'] = len(data_importer.import_property()) - response_obj['store'] = len(data_importer.import_supermarket()) - response_obj['food'] = len(data_importer.import_food()) - response_obj['conversion'] = len(data_importer.import_conversion()) + + if selected_datatypes['unit']['selected']: + response_obj['unit'] = data_importer.import_units() + if selected_datatypes['category']['selected']: + response_obj['category'] = data_importer.import_category() + if selected_datatypes['property']['selected']: + print('importin properties') + response_obj['property'] = data_importer.import_property() + if selected_datatypes['store']['selected']: + response_obj['store'] = len(data_importer.import_supermarket()) + if selected_datatypes['food']['selected']: + response_obj['food'] = len(data_importer.import_food()) + if selected_datatypes['conversion']['selected']: + response_obj['conversion'] = len(data_importer.import_conversion()) return Response(response_obj) diff --git a/vue/src/components/OpenDataImportComponent.vue b/vue/src/components/OpenDataImportComponent.vue index ebb3e8b2b7..a591ac4451 100644 --- a/vue/src/components/OpenDataImportComponent.vue +++ b/vue/src/components/OpenDataImportComponent.vue @@ -4,7 +4,7 @@
{{ $t('Data_Import_Info') }} - {{$t('Learn_More')}} + {{ $t('Learn_More') }}