fixup! Add forms for adding recipes and versions
Basic form for adding a version. Still needs: * Check of slug uniqueness * Choose between author user or author name * slug auto-creation * ingredients TODO: * Add recipe
This commit is contained in:
parent
6853be4ed5
commit
acbcf3f606
9
recipes/forms.py
Normal file
9
recipes/forms.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from django.forms import ModelForm, BooleanField
|
||||||
|
from .models import Recipe, Version, Ingredient
|
||||||
|
|
||||||
|
class VersionForm(ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Version
|
||||||
|
fields = ['label', 'slug', 'body']
|
||||||
|
|
||||||
|
# use_author_user = BooleanField(initial=True)
|
||||||
11
recipes/templates/add-version.html
Normal file
11
recipes/templates/add-version.html
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{% extends "base_main.html" %}
|
||||||
|
{% block title %}{{ recipe.title }}{% endblock %}
|
||||||
|
{% block main %}
|
||||||
|
<form action="" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<table>
|
||||||
|
{{ form.as_table }}
|
||||||
|
</table>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
{% block main %}
|
{% block main %}
|
||||||
<h1>{{ recipe.title }}</h1>
|
<h1>{{ recipe.title }}</h1>
|
||||||
{% if perms.recipes.add_recipe %}
|
{% if perms.recipes.add_recipe %}
|
||||||
<p><a href="#">Add version</a></p>
|
<p><a href="{% url 'add-version' recipe.slug %}">Add version</a></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for v in versions %}
|
{% for v in versions %}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
<p><a href="{{ recipe.get_absolute_url }}">Show all versions</a></p>
|
<p><a href="{{ recipe.get_absolute_url }}">Show all versions</a></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if perms.recipes.add_recipe %}
|
{% if perms.recipes.add_recipe %}
|
||||||
<p><a href="#">Add version</a></p>
|
<p><a href="{% url 'add-version' recipe.slug %}">Add version</a></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for i in ingredients %}
|
{% for i in ingredients %}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.recipes, name='recipes'),
|
path('', views.recipes, name='recipes'),
|
||||||
|
path('<slug:slug_recipe>/add-version/', views.add_version, name='add-version'),
|
||||||
path('<slug:slug_recipe>/<slug:slug_version>/', views.version, name='version'),
|
path('<slug:slug_recipe>/<slug:slug_version>/', views.version, name='version'),
|
||||||
path('<slug:slug>/', views.recipe, name='recipe'),
|
path('<slug:slug>/', views.recipe, name='recipe'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
from .models import Recipe, Version
|
from .models import Recipe, Version
|
||||||
|
from .forms import VersionForm
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
from django.contrib.auth.decorators import login_required, permission_required
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.http.response import HttpResponseRedirect
|
from django.http.response import HttpResponseRedirect
|
||||||
|
|
@ -34,3 +35,23 @@ def recipe(request, slug):
|
||||||
return version(request, slug, versions.get().slug)
|
return version(request, slug, versions.get().slug)
|
||||||
else:
|
else:
|
||||||
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
|
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@permission_required('recipes.add_recipe')
|
||||||
|
def add_version(request, slug_recipe):
|
||||||
|
recipe = Recipe.objects.get(slug=slug_recipe)
|
||||||
|
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = VersionForm(request.POST)
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
version = form.save(commit=False)
|
||||||
|
version.author_user = request.user
|
||||||
|
version.recipe = recipe
|
||||||
|
version.save()
|
||||||
|
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': version.recipe.slug, 'slug_version': version.slug}))
|
||||||
|
|
||||||
|
else:
|
||||||
|
form = VersionForm()
|
||||||
|
|
||||||
|
return render(request, 'add-version.html', {'form': form})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue