mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
further improvised import/export feature
This commit is contained in:
@@ -189,6 +189,17 @@ class StepSerializer(WritableNestedModelSerializer):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# used for the import export. temporary workaround until that module is finally fixed
|
||||||
|
class StepExportSerializer(WritableNestedModelSerializer):
|
||||||
|
ingredients = IngredientSerializer(many=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Step
|
||||||
|
fields = (
|
||||||
|
'id', 'name', 'type', 'instruction', 'ingredients', 'time', 'order', 'show_as_header'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NutritionInformationSerializer(serializers.ModelSerializer):
|
class NutritionInformationSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = NutritionInformation
|
model = NutritionInformation
|
||||||
@@ -227,6 +238,11 @@ class RecipeSerializer(WritableNestedModelSerializer):
|
|||||||
return super().create(validated_data)
|
return super().create(validated_data)
|
||||||
|
|
||||||
|
|
||||||
|
# used for the import export. temporary workaround until that module is finally fixed
|
||||||
|
class RecipeExportSerializer(RecipeSerializer):
|
||||||
|
steps = StepExportSerializer(many=True)
|
||||||
|
|
||||||
|
|
||||||
class RecipeImageSerializer(WritableNestedModelSerializer):
|
class RecipeImageSerializer(WritableNestedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Recipe
|
model = Recipe
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from rest_framework.renderers import JSONRenderer
|
|||||||
from cookbook.forms import ExportForm, ImportForm
|
from cookbook.forms import ExportForm, ImportForm
|
||||||
from cookbook.helper.permission_helper import group_required
|
from cookbook.helper.permission_helper import group_required
|
||||||
from cookbook.models import Recipe
|
from cookbook.models import Recipe
|
||||||
from cookbook.serializer import RecipeSerializer
|
from cookbook.serializer import RecipeSerializer, RecipeExportSerializer
|
||||||
|
|
||||||
|
|
||||||
@group_required('user')
|
@group_required('user')
|
||||||
@@ -24,10 +24,10 @@ def import_recipe(request):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
try:
|
try:
|
||||||
data = json.loads(
|
data = json.loads(
|
||||||
re.sub(r'"id":([0-9])+,', '', form.cleaned_data['recipe'])
|
re.sub(r'"id":([0-9]+),', '', re.sub(r',(\s)*"recipe":([0-9]+)', '', form.cleaned_data['recipe']))
|
||||||
)
|
)
|
||||||
|
|
||||||
sr = RecipeSerializer(data=data, context={'request': request})
|
sr = RecipeExportSerializer(data=data, context={'request': request})
|
||||||
if sr.is_valid():
|
if sr.is_valid():
|
||||||
sr.validated_data['created_by'] = request.user
|
sr.validated_data['created_by'] = request.user
|
||||||
recipe = sr.save()
|
recipe = sr.save()
|
||||||
@@ -63,7 +63,8 @@ def import_recipe(request):
|
|||||||
messages.add_message(
|
messages.add_message(
|
||||||
request, messages.WARNING, sr.errors
|
request, messages.WARNING, sr.errors
|
||||||
)
|
)
|
||||||
except JSONDecodeError:
|
except JSONDecodeError as e:
|
||||||
|
print(e)
|
||||||
messages.add_message(
|
messages.add_message(
|
||||||
request,
|
request,
|
||||||
messages.ERROR,
|
messages.ERROR,
|
||||||
@@ -84,7 +85,7 @@ def export_recipe(request):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
recipe = form.cleaned_data['recipe']
|
recipe = form.cleaned_data['recipe']
|
||||||
if recipe.internal:
|
if recipe.internal:
|
||||||
export = RecipeSerializer(recipe).data
|
export = RecipeExportSerializer(recipe).data
|
||||||
|
|
||||||
if recipe.image and form.cleaned_data['image']:
|
if recipe.image and form.cleaned_data['image']:
|
||||||
with open(recipe.image.path, 'rb') as img_f:
|
with open(recipe.image.path, 'rb') as img_f:
|
||||||
|
|||||||
Reference in New Issue
Block a user