Compare commits

...

12 commits

Author SHA1 Message Date
Benjamin aead09ef42 Add license header to source files 2023-03-19 16:18:16 +01:00
Benjamin 8df693a05d Add url patterns 2023-03-19 16:18:16 +01:00
Benjamin 9509d02508 Add views 2023-03-19 16:18:16 +01:00
Benjamin ffa1f3c0d2 Add HTML templates 2023-03-19 16:18:16 +01:00
Benjamin efc2b1f390 Add forms 2023-03-19 16:18:16 +01:00
Benjamin 69b1ee1d2d Register models to admin 2023-03-19 16:18:16 +01:00
Benjamin a528b17ff5 Add models 2023-03-19 16:18:16 +01:00
Benjamin 81baffde98 Add links to recipes add to nav list and index 2023-03-19 16:18:14 +01:00
Benjamin 39963b8161 Add recipes/ url pattern 2023-03-19 16:17:15 +01:00
Benjamin 2955010907 Start App Recipes 2023-03-19 16:17:15 +01:00
Benjamin 1c039d403e Add link to source code 2023-03-19 16:16:50 +01:00
Benjamin 98ee7de8cb Add license header to source files 2023-03-19 15:56:08 +01:00
28 changed files with 905 additions and 1 deletions

0
recipes/__init__.py Normal file
View file

25
recipes/admin.py Normal file
View file

@ -0,0 +1,25 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.contrib import admin
from .models import Recipe, Version, Ingredient
admin.site.register(Recipe)
admin.site.register(Version)
admin.site.register(Ingredient)

25
recipes/apps.py Normal file
View file

@ -0,0 +1,25 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.apps import AppConfig
class RecipesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'recipes'

56
recipes/forms.py Normal file
View file

@ -0,0 +1,56 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.forms import ModelForm, ValidationError, modelformset_factory, BooleanField
from .models import Recipe, Version, Ingredient
class RecipeForm(ModelForm):
class Meta:
model = Recipe
fields = ['title', 'slug']
class VersionForm(ModelForm):
recipe_id: int
def __init__(self, *args, **kwargs):
placeholder = None
if 'author_placeholder' in kwargs:
placeholder = kwargs.pop('author_placeholder')
super().__init__(*args, **kwargs)
if placeholder:
self.fields['author'].widget.attrs.update({'placeholder': placeholder})
class Meta:
model = Version
fields = ['label', 'slug', 'body', 'author']
def clean_slug(self):
slug = self.cleaned_data['slug']
if 'slug' in self.changed_data:
recipe = Recipe.objects.get(id=self.recipe_id)
if recipe.versions.filter(slug=slug).count() > 0: # type: ignore
raise ValidationError('A recipe version with this slug already exists.')
return slug
class IngredientForm(ModelForm):
class Meta:
model = Ingredient
fields = ['text']
IngredientFormSet = modelformset_factory(Ingredient, fields=('text',), extra=1)

View file

53
recipes/models.py Normal file
View file

@ -0,0 +1,53 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
class Recipe(models.Model):
title = models.CharField(max_length=100, null=False, blank=False)
slug = models.SlugField(unique=True)
def __str__(self) -> str:
return self.title
def get_absolute_url(self):
return reverse('recipe', kwargs={'slug': self.slug})
class Version(models.Model):
label = models.CharField(max_length=20, default='Original')
slug = models.SlugField(max_length=20, default='original')
body = models.TextField(null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
author = models.CharField(max_length=30, blank=True)
recipe = models.ForeignKey(Recipe, on_delete=models.PROTECT, null=False, blank=False, related_name='versions')
def __str__(self) -> str:
return self.recipe.title + ' - ' + self.label
def get_absolute_url(self):
return reverse('version', kwargs={'slug_recipe': self.recipe.slug, 'slug_version': self.slug})
class Ingredient(models.Model):
text = models.CharField(max_length=50, null=False, blank=False)
version = models.ForeignKey(Version, on_delete=models.CASCADE, null=False, blank=False, related_name='ingredients')
def __str__(self) -> str:
return self.text + ' for ' + str(self.version)

View file

@ -0,0 +1,59 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<form action="" method="post" id="add-version-form">
{%csrf_token %}
{{ recipe_form.as_div }}
<table>
{{ version_form.as_table }}
</table>
{{ ingredients_formset.management_form }}
{% for ingredient_form in ingredients_formset %}
{{ ingredient_form.as_div }}
{% endfor %}
<button id="add-ingredient" type="button">Add Ingredient</button>
<input type="submit" value="Submit">
</form>
<script>
let firstIngredientDiv = document.querySelector("input[id^=id_ingredient][id$=text]").parentElement;
let addIngredientButton = document.querySelector("#add-ingredient");
let form = document.querySelector("#add-version-form");
let totalIngredientFormsInput = document.querySelector("#id_ingredient-TOTAL_FORMS");
addIngredientButton.addEventListener('click', addIngredient);
function addIngredient(e) {
let nextIngredientIndex = document.querySelectorAll("input[id^=id_ingredient][id$=text]").length
e.preventDefault();
let newIngredientDiv = firstIngredientDiv.cloneNode(true);
let formRegex = new RegExp('ingredient-(\\d){1}-', 'g');
newIngredientDiv.innerHTML = newIngredientDiv.innerHTML.replace(formRegex, `ingredient-${nextIngredientIndex}-`);
form.insertBefore(newIngredientDiv, addIngredientButton);
totalIngredientFormsInput.setAttribute('value', `${nextIngredientIndex + 1}`);
}
</script>
{% endblock %}

View file

@ -0,0 +1,58 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<form action="" method="post" id="add-version-form">
{%csrf_token %}
<table>
{{ version_form.as_table }}
</table>
{{ ingredients_formset.management_form }}
{% for ingredient_form in ingredients_formset %}
{{ ingredient_form.as_div }}
{% endfor %}
<button id="add-ingredient" type="button">Add Ingredient</button>
<input type="submit" value="Submit">
</form>
<script>
let firstIngredientDiv = document.querySelector("input[id^=id_ingredient][id$=text]").parentElement;
let addIngredientButton = document.querySelector("#add-ingredient");
let form = document.querySelector("#add-version-form");
let totalIngredientFormsInput = document.querySelector("#id_ingredient-TOTAL_FORMS");
addIngredientButton.addEventListener('click', addIngredient);
function addIngredient(e) {
let nextIngredientIndex = document.querySelectorAll("input[id^=id_ingredient][id$=text]").length
e.preventDefault();
let newIngredientDiv = firstIngredientDiv.cloneNode(true);
let formRegex = new RegExp('ingredient-(\\d){1}-', 'g');
newIngredientDiv.innerHTML = newIngredientDiv.innerHTML.replace(formRegex, `ingredient-${nextIngredientIndex}-`);
form.insertBefore(newIngredientDiv, addIngredientButton);
totalIngredientFormsInput.setAttribute('value', `${nextIngredientIndex + 1}`);
}
</script>
{% endblock %}

View file

@ -0,0 +1,28 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<form action="" method="post">
{%csrf_token %}
{{ form.as_div }}
<input type="submit" value="Submit">
</form>
{% endblock %}

View file

@ -0,0 +1,58 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<form action="" method="post" id="edit-version-form"> <!-- TODO refactor with add-version.html-->
{%csrf_token %}
<table>
{{ version_form.as_table }}
</table>
{{ ingredients_formset.management_form }}
{% for ingredient_form in ingredients_formset %}
{{ ingredient_form.as_div }}
{% endfor %}
<button id="add-ingredient" type="button">Add Ingredient</button>
<input type="submit" value="Submit">
</form>
<script>
let firstIngredientDiv = document.querySelector("input[id^=id_ingredient][id$=text]").parentElement;
let addIngredientButton = document.querySelector("#add-ingredient");
let form = document.querySelector("#edit-version-form");
let totalIngredientFormsInput = document.querySelector("#id_ingredient-TOTAL_FORMS");
addIngredientButton.addEventListener('click', addIngredient);
function addIngredient(e) {
let nextIngredientIndex = document.querySelectorAll("input[id^=id_ingredient][id$=text]").length
e.preventDefault();
let newIngredientDiv = firstIngredientDiv.cloneNode(true);
let formRegex = new RegExp('ingredient-(\\d){1}-', 'g');
newIngredientDiv.innerHTML = newIngredientDiv.innerHTML.replace(formRegex, `ingredient-${nextIngredientIndex}-`);
form.insertBefore(newIngredientDiv, addIngredientButton);
totalIngredientFormsInput.setAttribute('value', `${nextIngredientIndex + 1}`);
}
</script>
{% endblock %}

View file

@ -0,0 +1,30 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<h1>{{ recipe.title }}</h1>
<p><a href="{% url 'edit-recipe' recipe.slug %}">Edit recipe name</a></p>
<p><a href="{% url 'add-version' recipe.slug %}">Add version</a></p>
<ul>
{% for v in versions %}
<li><a href="{{ v.get_absolute_url }}">{{ v.label }}</a></li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -0,0 +1,29 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}Recipes{% endblock %}
{% block main %}
<h1>Recipes</h1>
<p><a href="{% url 'add-recipe' %}">Add recipe</a></p>
<ul>
{% for recipe in recipes %}
<li><a href="{{ recipe.get_absolute_url }}">{{ recipe.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -0,0 +1,35 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %}
{% block title %}{{ recipe.title }}{% endblock %}
{% block main %}
<h1>{{ version.recipe.title }}{% if has_multiple_versions %} ({{ version.label }}){% endif %}</h1>
{% if has_multiple_versions %}
<p><a href="{{ recipe.get_absolute_url }}">Show all versions</a></p>
{% endif %}
<p><a href="{% url 'edit-recipe' recipe.slug %}">Edit recipe name</a></p>
<p><a href="{% url 'add-version' recipe.slug %}">Add version</a></p>
<p><a href="{% url 'edit-version' recipe.slug version.slug %}">Edit Version</a></p>
<ul>
{% for i in ingredients %}
<li>{{ i.text }}</li>
{% endfor %}
</ul>
<p>{{ version.body }}</p>
{% endblock %}

22
recipes/tests.py Normal file
View file

@ -0,0 +1,22 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.test import TestCase
# Create your tests here.

31
recipes/urls.py Normal file
View file

@ -0,0 +1,31 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.urls import path
from . import views
urlpatterns = [
path('', views.recipes, name='recipes'),
path('add-recipe/', views.add_recipe, name='add-recipe'),
path('<slug:slug>/add-version/', views.add_version, name='add-version'),
path('<slug:slug>/edit-recipe/', views.edit_recipe, name='edit-recipe'),
path('<slug:slug_recipe>/<slug:slug_version>/edit-version/', views.edit_version, name='edit-version'),
path('<slug:slug_recipe>/<slug:slug_version>/', views.version, name='version'),
path('<slug:slug>/', views.recipe, name='recipe'),
]

167
recipes/views.py Normal file
View file

@ -0,0 +1,167 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.shortcuts import render, get_object_or_404, redirect
from .models import Recipe, Version, Ingredient
from .forms import RecipeForm, VersionForm, IngredientFormSet
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.http.response import HttpResponseRedirect
from django.conf import settings
RECIPE_FORM_PREFIX = 'recipe'
VERSION_FORM_PREFIX = 'version'
INGREDIENTS_FORMSET_PREFIX = 'ingredient'
def get_name_of_user(user):
return user.first_name if user.first_name else user.username
@login_required
def recipes(request):
recipes = Recipe.objects.all()
return render(request, 'recipes.html', context={'recipes': recipes})
def version(request, slug_recipe, slug_version):
recipe = get_object_or_404(Recipe, slug=slug_recipe)
has_multiple_versions = recipe.versions.all().count() > 1 # type: ignore
version = get_object_or_404(Version, recipe=recipe, slug=slug_version)
ingredients = version.ingredients.all() # type: ignore
return render(request, 'version.html', context={'recipe': recipe, 'has_multiple_versions': has_multiple_versions, 'version': version, 'ingredients': ingredients})
@login_required
def recipe(request, slug):
recipe = get_object_or_404(Recipe, slug=slug)
versions = recipe.versions.all() # type: ignore
if (1 == versions.count()):
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': slug, 'slug_version': versions.get().slug}))
else:
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
def create_version(request, recipe: Recipe, version_form: VersionForm) -> Version:
version = version_form.save(commit=False)
version.user = request.user
version.recipe = recipe
version.save()
return version
def save_ingredients(version: Version, ingredients_formset: IngredientFormSet): # type: ignore
for ingredient in ingredients_formset.save(commit=False):
ingredient.version = version
ingredient.save()
@login_required
def add_recipe(request):
if request.method == 'POST':
recipe_form = RecipeForm(request.POST, prefix=RECIPE_FORM_PREFIX)
version_form = VersionForm(request.POST, prefix=VERSION_FORM_PREFIX, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(request.POST, queryset=Ingredient.objects.none(), prefix=INGREDIENTS_FORMSET_PREFIX)
if recipe_form.is_valid() and version_form.is_valid() and ingredients_formset.is_valid():
recipe = recipe_form.save(commit=True)
version = create_version(request, recipe, version_form)
save_ingredients(version, ingredients_formset)
return HttpResponseRedirect(reverse('recipe', kwargs={'slug': recipe.slug}))
else:
recipe_form = RecipeForm(prefix=RECIPE_FORM_PREFIX)
version_form = VersionForm(prefix=VERSION_FORM_PREFIX, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(queryset=Ingredient.objects.none(), prefix=INGREDIENTS_FORMSET_PREFIX)
return render(request, 'add-recipe.html', {'recipe_form': recipe_form, 'version_form': version_form, 'ingredients_formset': ingredients_formset})
@login_required
def edit_recipe(request, slug):
recipe = get_object_or_404(Recipe, slug=slug)
# It is assumed every recipe has at least one version
if not request.user.is_superuser:
users = set()
for version in recipe.versions.all(): # type: ignore
users.add(version.user)
if len(users) > 1 or (len(users) == 1 and next(iter(users)) != request.user):
return redirect(f"/accounts/login/?next={request.path}")
if request.method == 'POST':
form = RecipeForm(request.POST, instance=recipe, prefix=RECIPE_FORM_PREFIX)
if form.is_valid():
if form.has_changed():
form.save()
return HttpResponseRedirect(reverse('recipe', kwargs={'slug': recipe.slug}))
else:
form = RecipeForm(instance=recipe, prefix=RECIPE_FORM_PREFIX)
return render(request, 'edit-recipe.html', {'form': form})
@login_required
def add_version(request, slug):
recipe = get_object_or_404(Recipe, slug=slug)
if (recipe.versions.all().count() > 0): # type: ignore
version_initial = {'label': '', 'slug': ''}
else:
version_initial = {}
if request.method == 'POST':
version_form = VersionForm(request.POST, prefix=VERSION_FORM_PREFIX, initial=version_initial, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(request.POST, queryset=Ingredient.objects.none(), prefix=INGREDIENTS_FORMSET_PREFIX)
version_form.recipe_id = recipe.id # type: ignore
if version_form.is_valid() and ingredients_formset.is_valid():
version = create_version(request, recipe, version_form)
save_ingredients(version, ingredients_formset)
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': version.recipe.slug, 'slug_version': version.slug}))
else:
version_form = VersionForm(prefix=VERSION_FORM_PREFIX, initial=version_initial, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(queryset=Ingredient.objects.none(), prefix=INGREDIENTS_FORMSET_PREFIX)
return render(request, 'add-version.html', {'version_form': version_form, 'ingredients_formset': ingredients_formset})
@login_required
def edit_version(request, slug_recipe, slug_version):
recipe = get_object_or_404(Recipe, slug=slug_recipe)
version = get_object_or_404(Version, recipe=recipe, slug=slug_version)
if version.user != request.user and not request.user.is_superuser:
return redirect(f"/accounts/login/?next={request.path}")
if request.method == 'POST':
version_form = VersionForm(request.POST, prefix=VERSION_FORM_PREFIX, instance=version, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(request.POST, queryset=version.ingredients.all(), prefix=INGREDIENTS_FORMSET_PREFIX) # type: ignore
version_form.recipe_id = recipe.id # type: ignore
if version_form.is_valid() and ingredients_formset.is_valid():
if version_form.has_changed():
version = version_form.save()
if ingredients_formset.has_changed():
for ingredient in ingredients_formset.save(commit=False):
ingredient.version = version
ingredient.save()
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': version.recipe.slug, 'slug_version': version.slug}))
else:
version_form = VersionForm(instance=version, prefix=VERSION_FORM_PREFIX, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(queryset=version.ingredients.all(), prefix=INGREDIENTS_FORMSET_PREFIX) # type: ignore
return render(request, 'edit-version.html', {'version_form': version_form, 'ingredients_formset': ingredients_formset})

View file

@ -1,4 +1,22 @@
""" """
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
ASGI config for stadlbauer project. ASGI config for stadlbauer project.
It exposes the ASGI callable as a module-level variable named ``application``. It exposes the ASGI callable as a module-level variable named ``application``.

View file

@ -1,3 +1,22 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User from django.contrib.auth.models import User

View file

@ -1,4 +1,22 @@
""" """
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
Django settings for stadlbauer project. Django settings for stadlbauer project.
Generated by 'django-admin startproject' using Django 4.1.4. Generated by 'django-admin startproject' using Django 4.1.4.
@ -32,6 +50,7 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'recipes.apps.RecipesConfig',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',

View file

@ -1,4 +1,23 @@
"""stadlbauer URL Configuration """
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
stadlbauer URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see: The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/ https://docs.djangoproject.com/en/4.1/topics/http/urls/
@ -19,6 +38,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('recipes/', include('recipes.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('accounts/new-user/', views.new_user, name='new-user'), path('accounts/new-user/', views.new_user, name='new-user'),
path('accounts/profile/', views.profile, name='profile'), path('accounts/profile/', views.profile, name='profile'),

View file

@ -1,3 +1,22 @@
"""
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
"""
from .forms import SignupForm from .forms import SignupForm
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse

View file

@ -1,4 +1,22 @@
""" """
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
WSGI config for stadlbauer project. WSGI config for stadlbauer project.
It exposes the WSGI callable as a module-level variable named ``application``. It exposes the WSGI callable as a module-level variable named ``application``.

View file

@ -1,3 +1,21 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">

View file

@ -1,3 +1,21 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base.html" %} {% extends "base.html" %}
{% block head %} {% block head %}
@ -17,6 +35,10 @@
<li><a href="{% url 'new-user' %}">Create account</a></li> <li><a href="{% url 'new-user' %}">Create account</a></li>
{% endif %} {% endif %}
</ul> </ul>
<ul>
<li><a href="{% url 'recipes' %}">Recipes</a></li>
<li><a href="https://benjamin.stadlbauer.wien/git/Barn/django-project">Source code on Forgejo</a></li>
</ul>
</nav> </nav>
<main> <main>
{% block main %} {% block main %}

View file

@ -1,5 +1,26 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %} {% extends "base_main.html" %}
{% block title %}Barn{% endblock %} {% block title %}Barn{% endblock %}
{% block main %} {% block main %}
<h1>Barn</h1> <h1>Barn</h1>
<ul>
<li><h2><a href="{% url 'recipes' %}">Recipes</a></h2></li>
</ul>
{% endblock %} {% endblock %}

View file

@ -1,3 +1,21 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %} {% extends "base_main.html" %}
{% block main %} {% block main %}

View file

@ -1,3 +1,21 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %} {% extends "base_main.html" %}
{% block main %} {% block main %}

View file

@ -1,3 +1,21 @@
<!--
Barn Web App - A collection of web-apps for my family's personal use,
including a recipe database.
Copyright © 2023 Benjamin Stadlbauer
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
This program comes with a copy of the GNU Affero General Public License
file at the root of this project.
-->
{% extends "base_main.html" %} {% extends "base_main.html" %}
{% block title %}Profile {{ user.username }}{% endblock %} {% block title %}Profile {{ user.username }}{% endblock %}
{% block main %} {% block main %}