Delete {{ recipe.title }}?
This removes the recipe and its ingredients. This action cannot be undone.
{% if creating %}New recipe{% else %}Edit recipe{% endif %}
Keep ingredients structured and instructions in Markdown.
+ {% for hidden in ingredient.hidden_fields %}{{ hidden }}{% endfor %}
+
+
+ {% if ingredient.non_field_errors %}
diff --git a/main/recipes/templates/recipes/library.html b/main/recipes/templates/recipes/library.html
new file mode 100644
index 0000000..df847d1
--- /dev/null
+++ b/main/recipes/templates/recipes/library.html
@@ -0,0 +1,57 @@
+{% extends 'planner/base.html' %}{% load static %}
+{% block title %}Recipes · Tasknest{% endblock %}
+{% block page_class %}recipes-page-content{% endblock %}
+{% block content %}
+
+{{ ingredient.name }}{{ ingredient.name.errors }}
{{ ingredient.quantity }}{{ ingredient.quantity.errors }}
{{ ingredient.unit }}{{ ingredient.unit.errors }}
Ingredient details
{{ ingredient.preparation_note }}{{ ingredient.preparation_note.errors }}
{{ ingredient.display_order }}{{ ingredient.display_order.errors }}
{{ ingredient.inventory_product }}{{ ingredient.inventory_product.errors }}
{{ ingredient.nutrition_product }}{{ ingredient.nutrition_product.errors }}
{% if ingredient.DELETE %}{% endif %}{{ ingredient.non_field_errors }}
{% endif %}
+
+
+
+
+ {% if selected %}
+
+
+ {% endif %}
+
+
+ {% if selected %}{% endif %}
+
+{% endblock %}
diff --git a/main/recipes/tests.py b/main/recipes/tests.py
new file mode 100644
index 0000000..35b6e0a
--- /dev/null
+++ b/main/recipes/tests.py
@@ -0,0 +1,95 @@
+from decimal import Decimal
+from io import BytesIO
+from tempfile import TemporaryDirectory
+
+from django.contrib.auth import get_user_model
+from django.core.exceptions import ValidationError
+from django.core.files.uploadedfile import SimpleUploadedFile
+from django.test import TestCase
+from django.test.utils import override_settings
+from django.urls import reverse
+from PIL import Image
+
+from .models import Recipe, RecipeIngredient, RecipeSubrecipe
+
+
+class RecipeWebTests(TestCase):
+ def setUp(self):
+ user_model = get_user_model()
+ self.user = user_model.objects.create_user(username='cook', password='test-password')
+ self.other = user_model.objects.create_user(username='other', password='test-password')
+ self.client.force_login(self.user)
+
+ def data(self, title='Noodles', **changes):
+ data = {
+ 'title': title, 'description': 'A weeknight meal', 'category': 'Dinner',
+ 'preparation_minutes': '10', 'cooking_minutes': '15',
+ 'default_servings': '2', 'status': 'draft', 'tags_text': 'Asian, Quick',
+ 'instructions_markdown': 'Cook the noodles.', 'notes_markdown': '',
+ 'ingredients-TOTAL_FORMS': '1', 'ingredients-INITIAL_FORMS': '0',
+ 'ingredients-MIN_NUM_FORMS': '0', 'ingredients-MAX_NUM_FORMS': '1000',
+ 'ingredients-0-name': 'Noodles', 'ingredients-0-quantity': '250',
+ 'ingredients-0-unit': 'g', 'ingredients-0-display_order': '0',
+ }
+ data.update(changes)
+ return data
+
+ def test_create_search_filter_edit_favourite_and_delete(self):
+ response = self.client.post(reverse('recipes:create'), self.data())
+ self.assertEqual(response.status_code, 302)
+ recipe = Recipe.objects.get(owner=self.user)
+ self.assertEqual(recipe.tags, ['Asian', 'Quick'])
+ self.assertEqual(recipe.ingredients.get().quantity, Decimal('250'))
+ self.assertContains(self.client.get(reverse('recipes:list'), {'q': 'Noodles'}), 'Noodles')
+ self.assertContains(self.client.get(reverse('recipes:list'), {'tag': 'Asian'}), 'Noodles')
+ self.assertNotContains(self.client.get(reverse('recipes:list'), {'tag': 'Other'}), 'A weeknight meal')
+ self.client.post(reverse('recipes:favourite', args=[recipe.api_id]))
+ recipe.refresh_from_db()
+ self.assertTrue(recipe.is_favourite)
+ edit_data = self.data('Updated noodles', **{
+ 'ingredients-INITIAL_FORMS': '1', 'ingredients-0-id': str(recipe.ingredients.get().pk),
+ 'ingredients-0-quantity': '300',
+ })
+ self.assertEqual(self.client.post(reverse('recipes:edit', args=[recipe.api_id]), edit_data).status_code, 302)
+ recipe.refresh_from_db()
+ self.assertEqual(recipe.title, 'Updated noodles')
+ self.assertEqual(recipe.ingredients.get().quantity, Decimal('300'))
+ self.assertEqual(self.client.post(reverse('recipes:delete', args=[recipe.api_id])).status_code, 302)
+ self.assertFalse(Recipe.objects.filter(pk=recipe.pk).exists())
+
+ def test_owner_isolation(self):
+ recipe = Recipe.objects.create(owner=self.other, title='Private')
+ for name in ('detail', 'edit', 'delete', 'photo'):
+ self.assertEqual(self.client.get(reverse(f'recipes:{name}', args=[recipe.api_id])).status_code, 404)
+ self.assertEqual(self.client.post(reverse('recipes:favourite', args=[recipe.api_id])).status_code, 404)
+ self.assertNotContains(self.client.get(reverse('recipes:list')), 'Private')
+
+ def test_invalid_ingredient_does_not_create_recipe(self):
+ response = self.client.post(reverse('recipes:create'), self.data(**{'ingredients-0-quantity': '-1'}))
+ self.assertEqual(response.status_code, 200)
+ self.assertFalse(Recipe.objects.exists())
+
+ def test_subrecipe_ownership_and_cycles(self):
+ a = Recipe.objects.create(owner=self.user, title='A')
+ b = Recipe.objects.create(owner=self.user, title='B')
+ other = Recipe.objects.create(owner=self.other, title='Other')
+ RecipeSubrecipe.objects.create(recipe=a, component=b)
+ with self.assertRaises(ValidationError):
+ RecipeSubrecipe(recipe=b, component=a).full_clean()
+ with self.assertRaises(ValidationError):
+ RecipeSubrecipe(recipe=a, component=other).full_clean()
+ with self.assertRaises(ValidationError):
+ RecipeIngredient(recipe=a, name='Flour', quantity=Decimal('-1'), unit='g').full_clean()
+
+ def test_photo_upload_is_private(self):
+ image = BytesIO()
+ Image.new('RGB', (2, 2), '#217857').save(image, format='PNG')
+ with TemporaryDirectory() as media_root, override_settings(MEDIA_ROOT=media_root):
+ data = self.data()
+ data['photo'] = SimpleUploadedFile('noodles.png', image.getvalue(), content_type='image/png')
+ self.assertEqual(self.client.post(reverse('recipes:create'), data).status_code, 302)
+ recipe = Recipe.objects.get(owner=self.user)
+ photo_url = reverse('recipes:photo', args=[recipe.api_id])
+ self.assertEqual(self.client.get(photo_url).status_code, 200)
+ self.client.force_login(self.other)
+ self.assertEqual(self.client.get(photo_url).status_code, 404)
diff --git a/main/recipes/urls.py b/main/recipes/urls.py
new file mode 100644
index 0000000..e10b03c
--- /dev/null
+++ b/main/recipes/urls.py
@@ -0,0 +1,15 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'recipes'
+
+urlpatterns = [
+ path('', views.library, name='list'),
+ path('new/', views.create, name='create'),
+ path('
+ {% if selected.photo %}
{% else %}
+
+ No photo yet
{% endif %}
+ {% if selected.status == 'draft' %}Draft recipe{% else %}Recipe{% endif %}
{{ selected.title }}
{% if selected.category %}{{ selected.category }}{% endif %}{% for value in selected.tags %}{{ value }}{% endfor %}
{% if selected.description %}{{ selected.description }}
{% endif %}
+
+ {% if selected.notes_markdown %} {% endif %}
+
+ {% else %}
+ Ingredients
- {% for ingredient in selected.ingredients.all %}
- {{ ingredient.name }}{% if ingredient.preparation_note %}{{ ingredient.preparation_note }}{% endif %}{{ ingredient.quantity|floatformat:-3 }} {{ ingredient.unit }}{% if ingredient.is_optional %}Optional{% endif %} {% empty %}
- No ingredients added yet. {% endfor %}
Instructions
{% if selected.instructions_markdown %}{{ selected.instructions_markdown|linebreaksbr }}
{% else %}No instructions added yet.
{% endif %}Notes
{{ selected.notes_markdown|linebreaksbr }}
Make room for something delicious.
Save a recipe to see its ingredients and instructions here.
Create your first recipe