From 43eb10e488b25c30abedf40da7cd3fd55f104a56 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 25 Apr 2020 22:05:55 +0200 Subject: [PATCH] added basic exporting capability --- cookbook/forms.py | 7 +++ cookbook/models.py | 2 +- cookbook/templates/export.html | 70 +++++++++++++++++++++++++++ cookbook/templates/import.html | 10 ++++ cookbook/templates/shopping_list.html | 2 +- cookbook/urls.py | 5 +- cookbook/views/import_export.py | 44 +++++++++++++++++ 7 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 cookbook/templates/export.html create mode 100644 cookbook/templates/import.html create mode 100644 cookbook/views/import_export.py diff --git a/cookbook/forms.py b/cookbook/forms.py index d2ccf60ea..69ac558b0 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -100,6 +100,13 @@ class ShoppingForm(forms.Form): ) +class ExportForm(forms.Form): + recipe = forms.ModelChoiceField( + queryset=Recipe.objects.filter(internal=True).all(), + widget=SelectWidget + ) + + class UnitMergeForm(forms.Form): prefix = 'unit' diff --git a/cookbook/models.py b/cookbook/models.py index 53a5cf7c1..3befd9b35 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -154,8 +154,8 @@ class Ingredient(models.Model): class RecipeIngredient(models.Model): - ingredient = models.ForeignKey(Ingredient, on_delete=models.PROTECT) recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) + ingredient = models.ForeignKey(Ingredient, on_delete=models.PROTECT) unit = models.ForeignKey(Unit, on_delete=models.PROTECT) amount = models.DecimalField(default=0, decimal_places=2, max_digits=16) note = models.CharField(max_length=64, null=True, blank=True) diff --git a/cookbook/templates/export.html b/cookbook/templates/export.html new file mode 100644 index 000000000..cac74ae31 --- /dev/null +++ b/cookbook/templates/export.html @@ -0,0 +1,70 @@ +{% extends "base.html" %} +{% load i18n %} +{% load crispy_forms_filters %} +{% load static %} + +{% block title %}{% trans 'Export Recipes' %}{% endblock %} + +{% block extra_head %} + {{ form.media }} +{% endblock %} + +{% block content %} + +
+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+ + {% if export %} + +
+
+
+ + +
+
+ +
+
+
+ +
+
+ + + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/cookbook/templates/import.html b/cookbook/templates/import.html new file mode 100644 index 000000000..8d78ca2d0 --- /dev/null +++ b/cookbook/templates/import.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% load i18n %} +{% load crispy_forms_tags %} +{% load static %} + +{% block title %}{% trans 'Import Recipes' %}{% endblock %} + +{% block content %} + +{% endblock %} \ No newline at end of file diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index 1abfeb329..4d43dd4f6 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -34,7 +34,7 @@
diff --git a/cookbook/urls.py b/cookbook/urls.py index 4ab72b02a..ae08750e4 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -3,7 +3,7 @@ from pydoc import locate from django.urls import path from .views import * -from cookbook.views import api +from cookbook.views import api, import_export from cookbook.helper import dal urlpatterns = [ @@ -14,6 +14,9 @@ urlpatterns = [ path('shopping/', views.shopping_list, name='view_shopping'), path('settings/', views.settings, name='view_settings'), + path('import/', import_export.import_recipe, name='view_import'), + path('export/', import_export.export_recipe, name='view_export'), + path('view/recipe/', views.recipe_view, name='view_recipe'), path('new/recipe_import//', new.create_new_external_recipe, name='new_recipe_import'), diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py new file mode 100644 index 000000000..def140216 --- /dev/null +++ b/cookbook/views/import_export.py @@ -0,0 +1,44 @@ +import json + +from django.core import serializers +from django.shortcuts import render + +from cookbook.forms import ExportForm +from cookbook.models import Recipe, Ingredient, RecipeIngredient, Unit + + +def import_recipe(request): + return render(request, 'import.html', {}) + + +def export_recipe(request): + # TODO filter internal only + context = {} + + if request.method == "POST": + form = ExportForm(request.POST) + if form.is_valid(): + recipe = form.cleaned_data['recipe'] + + export = { + 'recipe': {'name': recipe.name, 'instructions': recipe.instructions, 'working_time': recipe.working_time, 'waiting_time': recipe.working_time}, + 'units': [], + 'ingredients': [], + 'recipe_ingredients': [] + } + + for ri in RecipeIngredient.objects.filter(recipe=recipe).all(): + 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: + form = ExportForm() + + context['form'] = form + + return render(request, 'export.html', context)