mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-05 06:08:46 -05:00
basic import export working
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
from dal import autocomplete
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.forms import widgets
|
from django.forms import widgets
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
@@ -107,6 +106,10 @@ class ExportForm(forms.Form):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ImportForm(forms.Form):
|
||||||
|
recipe = forms.CharField(widget=forms.Textarea)
|
||||||
|
|
||||||
|
|
||||||
class UnitMergeForm(forms.Form):
|
class UnitMergeForm(forms.Form):
|
||||||
prefix = 'unit'
|
prefix = 'unit'
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,14 @@
|
|||||||
{% block title %}{% trans 'Import Recipes' %}{% endblock %}
|
{% block title %}{% trans 'Import Recipes' %}{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="col col-md-12">
|
||||||
|
<form action="." method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form|crispy }}
|
||||||
|
<button class="btn btn-success" type="submit"><i class="fas fa-file-import"></i> {% trans 'Import' %}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@@ -1,41 +1,74 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from django.core import serializers
|
from django.contrib import messages
|
||||||
|
from django.db import IntegrityError
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.urls import reverse_lazy
|
||||||
from cookbook.forms import ExportForm
|
from django.utils.translation import gettext as _
|
||||||
from cookbook.models import Recipe, Ingredient, RecipeIngredient, Unit
|
from cookbook.forms import ExportForm, ImportForm
|
||||||
|
from cookbook.models import RecipeIngredient, Recipe, Unit, Ingredient
|
||||||
|
|
||||||
|
|
||||||
def import_recipe(request):
|
def import_recipe(request):
|
||||||
return render(request, 'import.html', {})
|
if request.method == "POST":
|
||||||
|
form = ImportForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
data = json.loads(form.cleaned_data['recipe'])
|
||||||
|
|
||||||
|
recipe = Recipe.objects.create(name=data['recipe']['name'], instructions=data['recipe']['instructions'],
|
||||||
|
working_time=data['recipe']['working_time'], waiting_time=data['recipe']['waiting_time'],
|
||||||
|
created_by=request.user, internal=True)
|
||||||
|
|
||||||
|
for u in data['units']:
|
||||||
|
try:
|
||||||
|
Unit.objects.create(name=u['name'], description=u['description']).save()
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for i in data['ingredients']:
|
||||||
|
try:
|
||||||
|
Ingredient.objects.create(name=i['name']).save()
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
for ri in data['recipe_ingredients']:
|
||||||
|
RecipeIngredient.objects.create(recipe=recipe, ingredient=Ingredient.objects.get(name=ri['ingredient']),
|
||||||
|
unit=Unit.objects.get(name=ri['unit']), amount=ri['amount'], note=ri['note'])
|
||||||
|
|
||||||
|
messages.add_message(request, messages.SUCCESS, _('Recipe imported successfully!'))
|
||||||
|
return HttpResponseRedirect(reverse_lazy('view_recipe', args=[recipe.pk]))
|
||||||
|
else:
|
||||||
|
form = ImportForm()
|
||||||
|
|
||||||
|
return render(request, 'import.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
def export_recipe(request):
|
def export_recipe(request):
|
||||||
# TODO filter internal only
|
|
||||||
context = {}
|
context = {}
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = ExportForm(request.POST)
|
form = ExportForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
recipe = form.cleaned_data['recipe']
|
recipe = form.cleaned_data['recipe']
|
||||||
|
if recipe.internal:
|
||||||
|
export = {
|
||||||
|
'recipe': {'name': recipe.name, 'instructions': recipe.instructions, 'working_time': recipe.working_time, 'waiting_time': recipe.working_time},
|
||||||
|
'units': [],
|
||||||
|
'ingredients': [],
|
||||||
|
'recipe_ingredients': []
|
||||||
|
}
|
||||||
|
|
||||||
export = {
|
for ri in RecipeIngredient.objects.filter(recipe=recipe).all():
|
||||||
'recipe': {'name': recipe.name, 'instructions': recipe.instructions, 'working_time': recipe.working_time, 'waiting_time': recipe.working_time},
|
if ri.unit not in export['units']:
|
||||||
'units': [],
|
export['units'].append({'name': ri.unit.name, 'description': ri.unit.description})
|
||||||
'ingredients': [],
|
if ri.ingredient not in export['ingredients']:
|
||||||
'recipe_ingredients': []
|
export['ingredients'].append({'name': ri.ingredient.name})
|
||||||
}
|
|
||||||
|
|
||||||
for ri in RecipeIngredient.objects.filter(recipe=recipe).all():
|
export['recipe_ingredients'].append({'ingredient': ri.ingredient.name, 'unit': ri.unit.name, 'amount': float(ri.amount), 'note': ri.note})
|
||||||
if ri.unit not in export['units']:
|
|
||||||
export['units'].append({'name': ri.unit.name, 'description': ri.unit.description})
|
|
||||||
if ri.ingredient not in export['ingredients']:
|
|
||||||
export['ingredients'].append({'name': ri.ingredient.name})
|
|
||||||
|
|
||||||
export['recipe_ingredients'].append({'ingredient': ri.ingredient.name, 'unit': ri.unit.name, 'amount': float(ri.amount), 'note': ri.note})
|
context['export'] = json.dumps(export, indent=4)
|
||||||
|
else:
|
||||||
context['export'] = json.dumps(export, indent=4)
|
form.add_error('recipe', _('External recipes cannot be exported, please share the file directly or select an internal recipe.'))
|
||||||
else:
|
else:
|
||||||
form = ExportForm()
|
form = ExportForm()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user