importing with image files working
This commit is contained in:
@ -1,9 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import uuid
|
||||||
from io import StringIO, BytesIO
|
from io import StringIO, BytesIO
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from django.core.files import File
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
|
|
||||||
@ -38,10 +41,28 @@ class Default(Integration):
|
|||||||
response['Content-Disposition'] = 'attachment; filename="export.zip"'
|
response['Content-Disposition'] = 'attachment; filename="export.zip"'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def do_import(self, files):
|
||||||
|
for f in files:
|
||||||
|
zip = ZipFile(f.file)
|
||||||
|
for z in zip.namelist():
|
||||||
|
self.get_recipe_from_zip(ZipFile(BytesIO(zip.read(z))))
|
||||||
|
|
||||||
|
def get_recipe_from_zip(self, recipe_zip):
|
||||||
|
recipe_string = recipe_zip.read('recipe.json').decode("utf-8")
|
||||||
|
recipe = self.get_recipe(recipe_string)
|
||||||
|
for f in recipe_zip.namelist():
|
||||||
|
if '.png' in f:
|
||||||
|
recipe.image = File(BytesIO(recipe_zip.read(f)), name=f'{uuid.uuid4()}_{recipe.pk}.png')
|
||||||
|
recipe.save()
|
||||||
|
|
||||||
def get_recipe(self, string):
|
def get_recipe(self, string):
|
||||||
data = json.loads(string)
|
data = json.loads(string)
|
||||||
|
serialized_recipe = RecipeExportSerializer(data=data, context={'request': self.request})
|
||||||
|
if serialized_recipe.is_valid():
|
||||||
|
recipe = serialized_recipe.save()
|
||||||
|
return recipe
|
||||||
|
|
||||||
return RecipeExportSerializer(data=data, context={'request': self.request})
|
return None
|
||||||
|
|
||||||
def get_export(self, recipe):
|
def get_export(self, recipe):
|
||||||
export = RecipeExportSerializer(recipe).data
|
export = RecipeExportSerializer(recipe).data
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
import os
|
|
||||||
import tempfile
|
|
||||||
|
|
||||||
|
|
||||||
class Integration:
|
class Integration:
|
||||||
request = None
|
request = None
|
||||||
|
|
||||||
@ -11,7 +7,7 @@ class Integration:
|
|||||||
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')
|
||||||
|
|
||||||
def do_import(self):
|
def do_import(self, files):
|
||||||
raise Exception('Method not implemented in storage integration')
|
raise Exception('Method not implemented in storage integration')
|
||||||
|
|
||||||
def get_recipe(self, string):
|
def get_recipe(self, string):
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
<h2>{% trans 'Import' %}</h2>
|
<h2>{% trans 'Import' %}</h2>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col col-md-12">
|
<div class="col col-md-12">
|
||||||
<form action="." method="post">
|
<form action="." method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form|crispy }}
|
{{ form|crispy }}
|
||||||
<button class="btn btn-success" type="submit"><i class="fas fa-file-import"></i> {% trans 'Import' %}
|
<button class="btn btn-success" type="submit"><i class="fas fa-file-import"></i> {% trans 'Import' %}
|
||||||
|
@ -491,7 +491,10 @@ def test(request):
|
|||||||
return HttpResponseRedirect(reverse('index'))
|
return HttpResponseRedirect(reverse('index'))
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = NewImportForm(request.POST)
|
form = NewImportForm(request.POST, request.FILES)
|
||||||
|
if form.is_valid():
|
||||||
|
integration = Default(request)
|
||||||
|
return integration.do_import(request.FILES.getlist('files'))
|
||||||
else:
|
else:
|
||||||
form = NewImportForm()
|
form = NewImportForm()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user