First version

This commit is contained in:
Cedric Girard 2019-10-28 16:50:13 +01:00
parent 96a4b7d8ed
commit 621e0b48b0
Signed by: X-dark
GPG Key ID: E7D0E125DB9519E4
17 changed files with 337 additions and 0 deletions

0
cookbook/__init__.py Normal file
View File

126
cookbook/settings.py Normal file
View File

@ -0,0 +1,126 @@
"""
Django settings for cookbook project.
Generated by 'django-admin startproject' using Django 2.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8gsq4)bizv-z441x%g==o6@5w5k_%&(eyu5g-o23wdw8(^cm9g'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['192.168.1.42']
# Application definition
INSTALLED_APPS = [
'recipes.apps.RecipesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'markdownx',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'cookbook.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'cookbook.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cookbook',
'USER': 'cookbook',
'PASSWORD': 'cookbook',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'

23
cookbook/urls.py Normal file
View File

@ -0,0 +1,23 @@
"""cookbook URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('recipes/', include('recipes.urls')),
path('admin/', admin.site.urls),
url(r'^markdownx/', include('markdownx.urls')),
]

16
cookbook/wsgi.py Normal file
View File

@ -0,0 +1,16 @@
"""
WSGI config for cookbook project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cookbook.settings')
application = get_wsgi_application()

21
manage.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cookbook.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

0
recipes/__init__.py Normal file
View File

14
recipes/admin.py Normal file
View File

@ -0,0 +1,14 @@
from django.contrib import admin
from .models import Recipe, Ingredient, IngredientUsage
class IngredientUsageInline(admin.TabularInline):
model = IngredientUsage
extra = 1
class RecipeAdmin(admin.ModelAdmin):
inlines = (IngredientUsageInline,)
fields = [ 'name', 'protocol' ]
admin.site.register(Recipe,RecipeAdmin)
admin.site.register(Ingredient)

5
recipes/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class RecipesConfig(AppConfig):
name = 'recipes'

View File

@ -0,0 +1,43 @@
# Generated by Django 2.2.6 on 2019-10-25 12:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Ingredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
],
),
migrations.CreateModel(
name='IngredientUsage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quantity', models.CharField(max_length=64)),
('ingredient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='recipes.Ingredient')),
],
),
migrations.CreateModel(
name='Recipe',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
('ingredients', models.ManyToManyField(through='recipes.IngredientUsage', to='recipes.Ingredient')),
],
),
migrations.AddField(
model_name='ingredientusage',
name='recipe',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='recipes.Recipe'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.6 on 2019-10-28 15:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('recipes', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='recipe',
name='protocol',
field=models.TextField(default=''),
),
]

View File

20
recipes/models.py Normal file
View File

@ -0,0 +1,20 @@
from django.db import models
class Ingredient(models.Model):
name = models.CharField(max_length=128)
def __str__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=128)
ingredients = models.ManyToManyField(Ingredient, through='IngredientUsage')
protocol = models.TextField(default='')
def __str__(self):
return self.name
class IngredientUsage(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.CharField(max_length=64)

View File

@ -0,0 +1,10 @@
<h1>{{ recipe.name }}</h1>
<h2>Ingredients</h2>
<ul>
{% for ingredient in ingredient_list %}
<li>{{ ingredient.quantity }} {{ ingredient.ingredient }}</li>
{% endfor %}
</ul>
<h2>Steps</h2>
<p>{{ recipe.protocol }}</p>

View File

@ -0,0 +1,9 @@
{% if recipe_list %}
<ul>
{% for recipe in recipe_list %}
<li><a href="{% url 'recipes:detail' recipe.id %}">{{ recipe.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No recipes are available.</p>
{% endif %}

3
recipes/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

9
recipes/urls.py Normal file
View File

@ -0,0 +1,9 @@
from django.urls import path
from . import views
app_name = 'recipes'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
]

20
recipes/views.py Normal file
View File

@ -0,0 +1,20 @@
from django.views import generic
from .models import Recipe, IngredientUsage
class IndexView(generic.ListView):
template_name = 'recipes/index.html'
def get_queryset(self):
return Recipe.objects.order_by('name')
class DetailView(generic.DetailView):
model = Recipe
template_name = 'recipes/detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['ingredient_list'] = IngredientUsage.objects.filter(recipe=self.object.pk)
return context