improve importer
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
from django.db.models import Q
|
||||
|
||||
from cookbook.models import Unit, SupermarketCategory, FoodProperty, FoodPropertyType, Supermarket, SupermarketCategoryRelation, Food, Automation
|
||||
from cookbook.models import Unit, SupermarketCategory, FoodProperty, FoodPropertyType, Supermarket, SupermarketCategoryRelation, Food, Automation, UnitConversion
|
||||
|
||||
|
||||
class OpenDataImporter:
|
||||
@ -172,6 +172,25 @@ class OpenDataImporter:
|
||||
created_by=self.request.user,
|
||||
))
|
||||
|
||||
FoodProperty.objects.bulk_create(food_property_list)
|
||||
FoodProperty.objects.bulk_create(food_property_list, ignore_conflicts=True, unique_fields=('space', 'food', 'property_type'))
|
||||
Automation.objects.bulk_create(alias_list)
|
||||
return len(insert_list)
|
||||
|
||||
def import_conversion(self):
|
||||
datatype = 'conversion'
|
||||
|
||||
insert_list = []
|
||||
for k in list(self.data[datatype].keys()):
|
||||
insert_list.append(UnitConversion(
|
||||
base_amount=self.data[datatype][k]['base_amount'],
|
||||
base_unit_id=self.slug_id_cache['unit'][self.data[datatype][k]['base_unit']],
|
||||
converted_amount=self.data[datatype][k]['converted_amount'],
|
||||
converted_unit_id=self.slug_id_cache['unit'][self.data[datatype][k]['converted_unit']],
|
||||
food_id=self.slug_id_cache['food'][self.data[datatype][k]['food']],
|
||||
open_data_slug=k,
|
||||
space=self.request.space,
|
||||
created_by=self.request.user,
|
||||
))
|
||||
|
||||
UnitConversion.objects.bulk_create(insert_list, ignore_conflicts=True, unique_fields=('space', 'base_unit', 'converted_unit', 'food', 'open_data_slug'))
|
||||
return len(insert_list)
|
||||
|
@ -0,0 +1,17 @@
|
||||
# Generated by Django 4.1.7 on 2023-05-04 05:57
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cookbook', '0192_remove_foodpropertytype_fdc_id_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddConstraint(
|
||||
model_name='unitconversion',
|
||||
constraint=models.UniqueConstraint(fields=('space', 'base_unit', 'converted_unit', 'food'), name='f_unique_conversion_per_space'),
|
||||
),
|
||||
]
|
@ -678,6 +678,11 @@ class UnitConversion(ExportModelOperationsMixin('unit_conversion'), models.Model
|
||||
def __str__(self):
|
||||
return f'{self.base_amount} {self.base_unit} -> {self.converted_amount} {self.converted_unit} {self.food}'
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['space', 'base_unit', 'converted_unit', 'food'], name='f_unique_conversion_per_space')
|
||||
]
|
||||
|
||||
|
||||
class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, PermissionModelMixin):
|
||||
# delete method on Food and Unit checks if they are part of a Recipe, if it is raises a ProtectedError instead of cascading the delete
|
||||
|
@ -1444,6 +1444,7 @@ class ImportOpenData(APIView):
|
||||
data_importer.import_property()
|
||||
data_importer.import_supermarket()
|
||||
data_importer.import_food() # TODO pass metric parameter
|
||||
data_importer.import_conversion()
|
||||
|
||||
return Response({
|
||||
'test': ''
|
||||
|
Reference in New Issue
Block a user