From 621e0b48b0435c665a82043015f7dfc98c101f1e Mon Sep 17 00:00:00 2001 From: Cedric Girard Date: Mon, 28 Oct 2019 16:50:13 +0100 Subject: [PATCH] First version --- cookbook/__init__.py | 0 cookbook/settings.py | 126 +++++++++++++++++++++ cookbook/urls.py | 23 ++++ cookbook/wsgi.py | 16 +++ manage.py | 21 ++++ recipes/__init__.py | 0 recipes/admin.py | 14 +++ recipes/apps.py | 5 + recipes/migrations/0001_initial.py | 43 +++++++ recipes/migrations/0002_recipe_protocol.py | 18 +++ recipes/migrations/__init__.py | 0 recipes/models.py | 20 ++++ recipes/templates/recipes/detail.html | 10 ++ recipes/templates/recipes/index.html | 9 ++ recipes/tests.py | 3 + recipes/urls.py | 9 ++ recipes/views.py | 20 ++++ 17 files changed, 337 insertions(+) create mode 100644 cookbook/__init__.py create mode 100644 cookbook/settings.py create mode 100644 cookbook/urls.py create mode 100644 cookbook/wsgi.py create mode 100755 manage.py create mode 100644 recipes/__init__.py create mode 100644 recipes/admin.py create mode 100644 recipes/apps.py create mode 100644 recipes/migrations/0001_initial.py create mode 100644 recipes/migrations/0002_recipe_protocol.py create mode 100644 recipes/migrations/__init__.py create mode 100644 recipes/models.py create mode 100644 recipes/templates/recipes/detail.html create mode 100644 recipes/templates/recipes/index.html create mode 100644 recipes/tests.py create mode 100644 recipes/urls.py create mode 100644 recipes/views.py diff --git a/cookbook/__init__.py b/cookbook/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cookbook/settings.py b/cookbook/settings.py new file mode 100644 index 0000000..5b1ac94 --- /dev/null +++ b/cookbook/settings.py @@ -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/' diff --git a/cookbook/urls.py b/cookbook/urls.py new file mode 100644 index 0000000..6086e43 --- /dev/null +++ b/cookbook/urls.py @@ -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')), +] diff --git a/cookbook/wsgi.py b/cookbook/wsgi.py new file mode 100644 index 0000000..5592589 --- /dev/null +++ b/cookbook/wsgi.py @@ -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() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..1a013e2 --- /dev/null +++ b/manage.py @@ -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() diff --git a/recipes/__init__.py b/recipes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recipes/admin.py b/recipes/admin.py new file mode 100644 index 0000000..d603913 --- /dev/null +++ b/recipes/admin.py @@ -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) diff --git a/recipes/apps.py b/recipes/apps.py new file mode 100644 index 0000000..076e356 --- /dev/null +++ b/recipes/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class RecipesConfig(AppConfig): + name = 'recipes' diff --git a/recipes/migrations/0001_initial.py b/recipes/migrations/0001_initial.py new file mode 100644 index 0000000..99371ee --- /dev/null +++ b/recipes/migrations/0001_initial.py @@ -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'), + ), + ] diff --git a/recipes/migrations/0002_recipe_protocol.py b/recipes/migrations/0002_recipe_protocol.py new file mode 100644 index 0000000..bb0e0e8 --- /dev/null +++ b/recipes/migrations/0002_recipe_protocol.py @@ -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=''), + ), + ] diff --git a/recipes/migrations/__init__.py b/recipes/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recipes/models.py b/recipes/models.py new file mode 100644 index 0000000..e53264c --- /dev/null +++ b/recipes/models.py @@ -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) diff --git a/recipes/templates/recipes/detail.html b/recipes/templates/recipes/detail.html new file mode 100644 index 0000000..907c9e2 --- /dev/null +++ b/recipes/templates/recipes/detail.html @@ -0,0 +1,10 @@ +

{{ recipe.name }}

+

Ingredients

+ + +

Steps

+

{{ recipe.protocol }}

diff --git a/recipes/templates/recipes/index.html b/recipes/templates/recipes/index.html new file mode 100644 index 0000000..51f131b --- /dev/null +++ b/recipes/templates/recipes/index.html @@ -0,0 +1,9 @@ +{% if recipe_list %} + +{% else %} +

No recipes are available.

+{% endif %} diff --git a/recipes/tests.py b/recipes/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/recipes/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/recipes/urls.py b/recipes/urls.py new file mode 100644 index 0000000..2a82984 --- /dev/null +++ b/recipes/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from . import views + +app_name = 'recipes' +urlpatterns = [ + path('', views.IndexView.as_view(), name='index'), + path('/', views.DetailView.as_view(), name='detail'), +] diff --git a/recipes/views.py b/recipes/views.py new file mode 100644 index 0000000..7a7d52b --- /dev/null +++ b/recipes/views.py @@ -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