added export support for recipe sage

This commit is contained in:
vabene1111 2021-03-28 19:29:09 +02:00
parent ba1e18410a
commit c1287407a3
5 changed files with 72 additions and 30 deletions

View File

@ -120,16 +120,16 @@ class ImportExportBase(forms.Form):
(MEALIE, 'Mealie'), (CHOWDOWN, 'Chowdown'), (SAFRON, 'Safron'), (CHEFTAP, 'ChefTap'),
(PEPPERPLATE, 'Pepperplate'), (RECIPESAGE, 'Recipe Sage'), (DOMESTICA, 'Domestica'),
))
duplicates = forms.BooleanField(help_text=_('To prevent duplicates recipes with the same name as existing ones are ignored. Check this box to import everything.'), required=False)
class ImportForm(ImportExportBase):
files = forms.FileField(required=True, widget=forms.ClearableFileInput(attrs={'multiple': True}))
duplicates = forms.BooleanField(help_text=_('To prevent duplicates recipes with the same name as existing ones are ignored. Check this box to import everything.'), required=False)
class ExportForm(ImportExportBase):
recipes = forms.ModelMultipleChoiceField(widget=MultiSelectWidget, queryset=Recipe.objects.none())
all = forms.BooleanField()
all = forms.BooleanField(required=False)
def __init__(self, *args, **kwargs):
space = kwargs.pop('space')

View File

@ -1,18 +1,16 @@
import datetime
import json
import uuid
from io import BytesIO, StringIO
from zipfile import ZipFile, BadZipFile
from django.contrib import messages
from django.core.files import File
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.http import HttpResponse
from django.utils.formats import date_format
from django.utils.translation import gettext as _
from django_scopes import scope
from cookbook.forms import ImportExportBase
from cookbook.models import Keyword, Recipe
@ -20,6 +18,7 @@ class Integration:
request = None
keyword = None
files = None
export_type = None
ignored_recipes = []
def __init__(self, request, export_type):
@ -28,6 +27,7 @@ class Integration:
:param request: request context of import session (used to link user to created objects)
"""
self.request = request
self.export_type = export_type
self.keyword = Keyword.objects.create(
name=f'Import {export_type} {date_format(datetime.datetime.now(), "DATETIME_FORMAT")}.{datetime.datetime.now().strftime("%S")}',
description=f'Imported by {request.user.get_user_name()} at {date_format(datetime.datetime.now(), "DATETIME_FORMAT")}. Type: {export_type}',
@ -41,6 +41,9 @@ class Integration:
:param recipes: list of recipe objects
: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
if self.export_type != ImportExportBase.RECIPESAGE:
export_zip_stream = BytesIO()
export_zip_obj = ZipFile(export_zip_stream, 'w')
@ -68,6 +71,14 @@ class Integration:
response = HttpResponse(export_zip_stream.getvalue(), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="export.zip"'
return response
else:
json_list = []
for r in recipes:
json_list.append(self.get_file_from_recipe(r))
response = HttpResponse(json.dumps(json_list), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="recipes.json"'
return response
def import_file_name_filter(self, zip_info_object):
"""

View File

@ -2,6 +2,7 @@ import base64
from io import BytesIO
import requests
from rest_framework.renderers import JSONRenderer
from cookbook.helper.ingredient_parser import parse, get_food, get_unit
from cookbook.integration.integration import Integration
@ -58,4 +59,31 @@ class RecipeSage(Integration):
return recipe
def get_file_from_recipe(self, recipe):
raise NotImplementedError('Method not implemented in storage integration')
data = {
'@context': 'http://schema.org',
'@type': 'Recipe',
'creditText': '',
'isBasedOn': '',
'name': recipe.name,
'description': recipe.description,
'prepTime': str(recipe.working_time),
'totalTime': str(recipe.waiting_time + recipe.working_time),
'recipeYield': str(recipe.servings),
'image': [],
'recipeCategory': [],
'comment': [],
'recipeIngredient': [],
'recipeInstructions': [],
}
for s in recipe.steps.all():
if s.type != Step.TIME:
data['recipeInstructions'].append({
'@type': 'HowToStep',
'text': s.instruction
})
for i in s.ingredients.all():
data['recipeIngredient'].append(f'{float(i.amount)} {i.unit} {i.food}')
return data

View File

@ -78,7 +78,7 @@ def export_recipe(request):
if form.is_valid():
try:
recipes = form.cleaned_data['recipes']
if form['all']:
if form.cleaned_data['all']:
recipes = Recipe.objects.filter(space=request.space, internal=True).all()
integration = get_integration(request, form.cleaned_data['type'])
return integration.do_export(recipes)

View File

@ -30,7 +30,7 @@ Overview of the capabilities of the different integrations.
| Paprika | ✔️ | ⌚ | ✔️ |
| ChefTap | ✔️ | ❌ | ❌️ |
| Pepperplate | ✔️ | ⌚ | ❌️ |
| RecipeSage | ✔️ | | ✔️ |
| RecipeSage | ✔️ | ✔️ | ✔️ |
| Domestica | ✔️ | ⌚ | ✔️ |
✔ = implemented, ❌ = not implemented and not possible/planned, ⌚ = not yet implemented
@ -46,6 +46,9 @@ easy machine readability if you want to use the data for any other purpose.
Go to Settings > Export Recipe Data and select `EXPORT AS JSON-LD (BEST)`. Then simply upload the exported file
to Tandoor.
The RecipeSage integration also allows exporting. To migrate from Tandoor to RecipeSage simply export with Recipe Sage
selected and import the json file in RecipeSage. Images are currently not supported for exporting.
## Domestica
Go to Import/Export and select `Export Recipes`. Then simply upload the exported file
to Tandoor.