import tag recipes and show results after import
This commit is contained in:
parent
6c52b7bbd9
commit
f312631676
@ -7,7 +7,8 @@ from zipfile import ZipFile
|
|||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
|
from django.urls import reverse
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
|
|
||||||
from cookbook.integration.integration import Integration
|
from cookbook.integration.integration import Integration
|
||||||
@ -30,7 +31,10 @@ class Default(Integration):
|
|||||||
recipe_zip_obj.writestr('recipe.json', recipe_json_stream.getvalue())
|
recipe_zip_obj.writestr('recipe.json', recipe_json_stream.getvalue())
|
||||||
recipe_json_stream.close()
|
recipe_json_stream.close()
|
||||||
|
|
||||||
recipe_zip_obj.write(r.image.path, basename(r.image.path))
|
try:
|
||||||
|
recipe_zip_obj.write(r.image.path, 'image.png')
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
recipe_zip_obj.close()
|
recipe_zip_obj.close()
|
||||||
export_zip_obj.writestr(str(r.pk) + '.zip', recipe_zip_stream.getvalue())
|
export_zip_obj.writestr(str(r.pk) + '.zip', recipe_zip_stream.getvalue())
|
||||||
@ -47,13 +51,15 @@ class Default(Integration):
|
|||||||
for z in zip.namelist():
|
for z in zip.namelist():
|
||||||
self.get_recipe_from_zip(ZipFile(BytesIO(zip.read(z))))
|
self.get_recipe_from_zip(ZipFile(BytesIO(zip.read(z))))
|
||||||
|
|
||||||
|
return HttpResponseRedirect(reverse('view_search') + '?keywords=' + str(self.keyword.pk))
|
||||||
|
|
||||||
def get_recipe_from_zip(self, recipe_zip):
|
def get_recipe_from_zip(self, recipe_zip):
|
||||||
recipe_string = recipe_zip.read('recipe.json').decode("utf-8")
|
recipe_string = recipe_zip.read('recipe.json').decode("utf-8")
|
||||||
recipe = self.get_recipe(recipe_string)
|
recipe = self.get_recipe(recipe_string)
|
||||||
for f in recipe_zip.namelist():
|
if 'image.png' in recipe_zip.namelist():
|
||||||
if '.png' in f:
|
recipe.image = File(BytesIO(recipe_zip.read('image.png')), name=f'{uuid.uuid4()}_{recipe.pk}.png')
|
||||||
recipe.image = File(BytesIO(recipe_zip.read(f)), name=f'{uuid.uuid4()}_{recipe.pk}.png')
|
recipe.save()
|
||||||
recipe.save()
|
recipe.keywords.add(self.keyword)
|
||||||
|
|
||||||
def get_recipe(self, string):
|
def get_recipe(self, string):
|
||||||
data = json.loads(string)
|
data = json.loads(string)
|
||||||
|
@ -1,8 +1,19 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
from cookbook.models import Keyword
|
||||||
|
|
||||||
|
|
||||||
class Integration:
|
class Integration:
|
||||||
request = None
|
request = None
|
||||||
|
keyword = None
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
self.request = request
|
self.request = request
|
||||||
|
self.keyword = Keyword.objects.create(
|
||||||
|
name=f'Import {datetime.datetime.now()}',
|
||||||
|
description=f'Imported by {request.user.get_user_name()} on {datetime.datetime.now()}',
|
||||||
|
icon='📥'
|
||||||
|
)
|
||||||
|
|
||||||
def do_export(self, recipes):
|
def do_export(self, recipes):
|
||||||
raise Exception('Method not implemented in storage integration')
|
raise Exception('Method not implemented in storage integration')
|
||||||
|
Loading…
Reference in New Issue
Block a user