Add recipe and version HTMLs for viewing
This commit is contained in:
parent
e8eb37a558
commit
b898bc3ba5
|
|
@ -1,3 +1,6 @@
|
|||
from django.contrib import admin
|
||||
from .models import Recipe, Version, Ingredient
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Recipe)
|
||||
admin.site.register(Version)
|
||||
admin.site.register(Ingredient)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,34 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
|
||||
# Create your models here.
|
||||
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)
|
||||
author_user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
|
||||
author_name = 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)
|
||||
|
|
|
|||
10
recipes/templates/recipe.html
Normal file
10
recipes/templates/recipe.html
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{% extends "base_main.html" %}
|
||||
{% block title %}{{ recipe.title }}{% endblock %}
|
||||
{% block main %}
|
||||
<h1>{{ recipe.title }}</h1>
|
||||
<ul>
|
||||
{% for v in versions %}
|
||||
<li><a href="{{ v.get_absolute_url }}">{{ v.label }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
12
recipes/templates/recipes.html
Normal file
12
recipes/templates/recipes.html
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{% extends "base_main.html" %}
|
||||
{% block title %}Recipes{% endblock %}
|
||||
{% block main %}
|
||||
<h1>Recipes</h1>
|
||||
{% if perms.recipes.view_recipe %}
|
||||
<ul>
|
||||
{% for recipe in recipes %}
|
||||
<li><a href="{{ recipe.get_absolute_url }}">{{ recipe.title }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
14
recipes/templates/version.html
Normal file
14
recipes/templates/version.html
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{% extends "base_main.html" %}
|
||||
{% block title %}{{ recipe.title }}{% endblock %}
|
||||
{% block main %}
|
||||
<h1>{{ version.recipe.title }} ({{ version.label }})</h1>
|
||||
{% if has_multiple_versions %}
|
||||
<aside><a href="{{ recipe.get_absolute_url }}">Show all versions</a></aside>
|
||||
{% endif %}
|
||||
<ul>
|
||||
{% for i in ingredients %}
|
||||
<li>{{ i.text }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<p>{{ version.body }}</p>
|
||||
{% endblock %}
|
||||
8
recipes/urls.py
Normal file
8
recipes/urls.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.recipes, name='recipes'),
|
||||
path('<slug:slug_recipe>/<slug:slug_version>/', views.version, name='version'),
|
||||
path('<slug:slug>/', views.recipe, name='recipe'),
|
||||
]
|
||||
|
|
@ -1,3 +1,36 @@
|
|||
from django.shortcuts import render
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from .models import Recipe, Version
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.urls import reverse
|
||||
from django.http.response import HttpResponseRedirect
|
||||
from django.conf import settings
|
||||
|
||||
# Create your views here.
|
||||
@login_required
|
||||
@permission_required('recipes.view_recipe')
|
||||
def recipes(request):
|
||||
recipes = Recipe.objects.all()
|
||||
return render(request, 'recipes.html', context={'recipes': recipes})
|
||||
|
||||
@login_required
|
||||
@permission_required('recipes.view_recipe')
|
||||
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
|
||||
@permission_required('recipes.view_recipe')
|
||||
def recipe(request, slug):
|
||||
recipe = get_object_or_404(Recipe, slug=slug)
|
||||
versions = recipe.versions.all() # type: ignore
|
||||
|
||||
if (1 == versions.count()):
|
||||
if settings.ENFORCE_RECIPE_VERSION_URL:
|
||||
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': slug, 'slug_version': versions.get().slug}))
|
||||
else:
|
||||
return version(request, slug, versions.get().slug)
|
||||
else:
|
||||
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
|
||||
|
|
|
|||
|
|
@ -101,9 +101,15 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||
},
|
||||
]
|
||||
|
||||
# Project specific
|
||||
|
||||
LOGIN_REDIRECT_URL = '/'
|
||||
LOGOUT_REDIRECT_URL = '/'
|
||||
|
||||
# Recipes specific
|
||||
|
||||
ENFORCE_RECIPE_VERSION_URL = False
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/4.1/topics/i18n/
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ from . import views
|
|||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('recipes/', include('recipes.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
path('accounts/new-user/', views.new_user, name='new-user'),
|
||||
path('accounts/profile/', views.profile, name='profile'),
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@
|
|||
<li><a href="{% url 'new-user' %}">Create account</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
<ul>
|
||||
{% if perms.recipes %}<li><a href="{% url 'recipes' %}">Recipes</a></li>{% endif %}
|
||||
</ul>
|
||||
</nav>
|
||||
<main>
|
||||
{% block main %}
|
||||
|
|
|
|||
|
|
@ -2,4 +2,9 @@
|
|||
{% block title %}Barn{% endblock %}
|
||||
{% block main %}
|
||||
<h1>Barn</h1>
|
||||
<ul>
|
||||
{% if perms.recipes %}
|
||||
<li><h2><a href="{% url 'recipes' %}">Recipes</a></h2></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in a new issue