update integration file split

This commit is contained in:
vabene1111
2021-03-28 19:49:03 +02:00
parent c1287407a3
commit e4a9f56352
3 changed files with 22 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import base64 import base64
import json
from io import BytesIO from io import BytesIO
from cookbook.helper.ingredient_parser import parse, get_food, get_unit from cookbook.helper.ingredient_parser import parse, get_food, get_unit
@ -49,3 +50,6 @@ class Domestica(Integration):
def get_file_from_recipe(self, recipe): def get_file_from_recipe(self, recipe):
raise NotImplementedError('Method not implemented in storage integration') raise NotImplementedError('Method not implemented in storage integration')
def split_recipe_file(self, file):
return json.loads(file.read().decode("utf-8"))

View File

@ -42,7 +42,7 @@ class Integration:
:return: HttpResponse with a ZIP file that is directly downloaded :return: HttpResponse with a ZIP file that is directly downloaded
""" """
#TODO this is temporary, find a better solution for different export formats when doing other exporters # TODO this is temporary, find a better solution for different export formats when doing other exporters
if self.export_type != ImportExportBase.RECIPESAGE: if self.export_type != ImportExportBase.RECIPESAGE:
export_zip_stream = BytesIO() export_zip_stream = BytesIO()
export_zip_obj = ZipFile(export_zip_stream, 'w') export_zip_obj = ZipFile(export_zip_stream, 'w')
@ -115,9 +115,9 @@ class Integration:
self.handle_duplicates(recipe, import_duplicates) self.handle_duplicates(recipe, import_duplicates)
import_zip.close() import_zip.close()
elif '.json' in f['name']: elif '.json' in f['name'] or '.txt' in f['name']:
json_data = json.loads(f['file'].read().decode("utf-8")) data_list = self.split_recipe_file(f['file'])
for d in json_data: for d in data_list:
recipe = self.get_recipe_from_file(d) recipe = self.get_recipe_from_file(d)
recipe.keywords.add(self.keyword) recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n' il.msg += f'{recipe.pk} - {recipe.name} \n'
@ -164,7 +164,15 @@ class Integration:
:param file: ByteIO or any file like object, depends on provider :param file: ByteIO or any file like object, depends on provider
:return: Recipe object :return: Recipe object
""" """
raise NotImplementedError('Method not implemented in storage integration') raise NotImplementedError('Method not implemented in integration')
def split_recipe_file(self, file):
"""
Takes a file that contains multiple recipes and splits it into a list of strings of various formats (e.g. json, text, ..)
:param file: ByteIO or any file like object, depends on provider
:return: list of strings
"""
raise NotImplementedError('Method not implemented in integration')
def get_file_from_recipe(self, recipe): def get_file_from_recipe(self, recipe):
""" """
@ -175,4 +183,4 @@ class Integration:
- name - file name in export - name - file name in export
- data - string content for file to get created in export zip - data - string content for file to get created in export zip
""" """
raise NotImplementedError('Method not implemented in storage integration') raise NotImplementedError('Method not implemented in integration')

View File

@ -1,4 +1,5 @@
import base64 import base64
import json
from io import BytesIO from io import BytesIO
import requests import requests
@ -87,3 +88,6 @@ class RecipeSage(Integration):
data['recipeIngredient'].append(f'{float(i.amount)} {i.unit} {i.food}') data['recipeIngredient'].append(f'{float(i.amount)} {i.unit} {i.food}')
return data return data
def split_recipe_file(self, file):
return json.loads(file.read().decode("utf-8"))