From d7c5ea33bea6e0470e9cdb0131412e1fcd8ca6f9 Mon Sep 17 00:00:00 2001 From: Marc Stanley Date: Mon, 12 Jul 2021 16:19:12 -0500 Subject: [PATCH] completed project --- .gitignore | 2 +- manage.py | 15 +++ seeds.py | 10 ++ todo/__init__.py | 0 todo/admin.py | 3 + todo/apps.py | 5 + todo/migrations/0001_initial.py | 22 ++++ todo/migrations/__init__.py | 0 todo/models.py | 9 ++ todo/templates/todo/todo_confirm_delete.html | 5 + todo/templates/todo/todo_detail.html | 18 +++ todo/templates/todo/todo_form.html | 5 + todo/templates/todo/todo_list.html | 38 ++++++ todo/tests.py | 3 + todo/urls.py | 12 ++ todo/views.py | 47 +++++++ todoapp/__init__.py | 0 todoapp/settings.py | 121 +++++++++++++++++++ todoapp/urls.py | 7 ++ todoapp/wsgi.py | 16 +++ 20 files changed, 337 insertions(+), 1 deletion(-) create mode 100755 manage.py create mode 100644 seeds.py create mode 100644 todo/__init__.py create mode 100644 todo/admin.py create mode 100644 todo/apps.py create mode 100644 todo/migrations/0001_initial.py create mode 100644 todo/migrations/__init__.py create mode 100644 todo/models.py create mode 100644 todo/templates/todo/todo_confirm_delete.html create mode 100644 todo/templates/todo/todo_detail.html create mode 100644 todo/templates/todo/todo_form.html create mode 100644 todo/templates/todo/todo_list.html create mode 100644 todo/tests.py create mode 100644 todo/urls.py create mode 100644 todo/views.py create mode 100644 todoapp/__init__.py create mode 100644 todoapp/settings.py create mode 100644 todoapp/urls.py create mode 100644 todoapp/wsgi.py diff --git a/.gitignore b/.gitignore index 52e8ada..8e525b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -venv/ +.venv/ .vscode/ __pycache__/ *.py[cod] \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..386e3fb --- /dev/null +++ b/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == '__main__': + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todoapp.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) diff --git a/seeds.py b/seeds.py new file mode 100644 index 0000000..f1ac44e --- /dev/null +++ b/seeds.py @@ -0,0 +1,10 @@ +from todo.models import Todo + +t1 = Todo(title="Wash dogs", description="Wash them with shampoo") +t1.save() + +t2 = Todo(title="Cut grass", description="Mow, weed, edge, and blow before it rains this week") +t2.save() + +t3 = Todo(title="Sweep", description="Sweep the hardwood floors") +t3.save() diff --git a/todo/__init__.py b/todo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/admin.py b/todo/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/todo/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/todo/apps.py b/todo/apps.py new file mode 100644 index 0000000..6c85b8d --- /dev/null +++ b/todo/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TodoConfig(AppConfig): + name = 'todo' diff --git a/todo/migrations/0001_initial.py b/todo/migrations/0001_initial.py new file mode 100644 index 0000000..c4e5ffe --- /dev/null +++ b/todo/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 2.1.3 on 2021-07-12 20:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Todo', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('description', models.CharField(max_length=200)), + ], + ), + ] diff --git a/todo/migrations/__init__.py b/todo/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/models.py b/todo/models.py new file mode 100644 index 0000000..ef3e8d9 --- /dev/null +++ b/todo/models.py @@ -0,0 +1,9 @@ +from django.db import models + +# Create your models here. +class Todo(models.Model): + title = models.CharField(max_length=200) + description = models.CharField(max_length=200) + + def __str__(self) -> str: + return f"ID: {self.id} Title: {self.title}" \ No newline at end of file diff --git a/todo/templates/todo/todo_confirm_delete.html b/todo/templates/todo/todo_confirm_delete.html new file mode 100644 index 0000000..5c1afe6 --- /dev/null +++ b/todo/templates/todo/todo_confirm_delete.html @@ -0,0 +1,5 @@ +

Todo Delete

+
{% csrf_token %} + Are you sure you want to delete "{{ object.title }}" ? + +
\ No newline at end of file diff --git a/todo/templates/todo/todo_detail.html b/todo/templates/todo/todo_detail.html new file mode 100644 index 0000000..232cd0c --- /dev/null +++ b/todo/templates/todo/todo_detail.html @@ -0,0 +1,18 @@ + + + + + + + Document + + + +

Todo Details

+

Title: {{ todo.title }}

+ Description: {{ todo.description }} +
+ Back + + + \ No newline at end of file diff --git a/todo/templates/todo/todo_form.html b/todo/templates/todo/todo_form.html new file mode 100644 index 0000000..4a7e7e9 --- /dev/null +++ b/todo/templates/todo/todo_form.html @@ -0,0 +1,5 @@ +

Todo Form: {{ new_or_edit }}

+
{% csrf_token %} + {{ form.as_p }} + +
\ No newline at end of file diff --git a/todo/templates/todo/todo_list.html b/todo/templates/todo/todo_list.html new file mode 100644 index 0000000..c084ba1 --- /dev/null +++ b/todo/templates/todo/todo_list.html @@ -0,0 +1,38 @@ + + + + + + + Document + + +

Todo List

+ + + + + + + + + + + + + {% for todo in all_todos %} + + + + + + + + {% endfor %} + +
TitleDescriptionViewEditDelete
{{ todo.title }}{{ todo.description }}vieweditdelete
+ +New + + + \ No newline at end of file diff --git a/todo/tests.py b/todo/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/todo/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/todo/urls.py b/todo/urls.py new file mode 100644 index 0000000..0a689f7 --- /dev/null +++ b/todo/urls.py @@ -0,0 +1,12 @@ +from django.urls import path +from . import views + +app_name = 'todo' + +urlpatterns = [ + path('', views.todo_list, name='todo_list'), + path('', views.todo_view, name='todo_view'), + path('new', views.todo_create, name='todo_new'), + path('/edit', views.todo_update, name='todo_edit'), + path('/delete', views.todo_delete, name='todo_delete'), +] \ No newline at end of file diff --git a/todo/views.py b/todo/views.py new file mode 100644 index 0000000..624ff32 --- /dev/null +++ b/todo/views.py @@ -0,0 +1,47 @@ +from django.shortcuts import render, redirect +from django.forms import ModelForm + +from todo.models import Todo + +# Create your views here. +class TodoForm(ModelForm): + class Meta: + model = Todo + fields = ['title', 'description'] + +def get_todo(todo_id): + return Todo.objects.get(id=todo_id) + + +def todo_list(request): + todos = Todo.objects.all() + data = { 'all_todos': todos} + return render(request, 'todo/todo_list.html', data) + + +def todo_view(request, todo_id): + todo = get_todo(todo_id) + data = {'todo': todo} + return render(request, 'todo/todo_detail.html', data) + +def todo_create(request): + form = TodoForm(request.POST or None) + if form.is_valid(): + form.save() + return redirect('todo:todo_list') + return render(request, 'todo/todo_form.html', {'form': form, 'new_or_edit': 'New'}) + +def todo_update(request, todo_id): + todo = get_todo(todo_id) + form = TodoForm(request.POST or None, instance=todo) + if form.is_valid(): + form.save() + return redirect('todo:todo_list') + return render(request, 'todo/todo_form.html', {'form' :form, 'new_or_edit': 'Edit'}) + +def todo_delete(request, todo_id): + todo = get_todo(todo_id) + if request.method=='POST': + todo.delete() + return redirect('todo:todo_list') + return render(request, 'todo/todo_confirm_delete.html', {'object':todo}) \ No newline at end of file diff --git a/todoapp/__init__.py b/todoapp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todoapp/settings.py b/todoapp/settings.py new file mode 100644 index 0000000..d25f5ed --- /dev/null +++ b/todoapp/settings.py @@ -0,0 +1,121 @@ +""" +Django settings for todoapp project. + +Generated by 'django-admin startproject' using Django 2.1.3. + +For more information on this file, see +https://docs.djangoproject.com/en/2.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.1/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.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'hi%ycvw$8^dw--z!@3cw2g7%yezmtf0bvl^fs*b1*!a%9f!8$$' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'todo' +] + +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 = 'todoapp.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 = 'todoapp.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'todo_db' + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.1/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.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.1/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/todoapp/urls.py b/todoapp/urls.py new file mode 100644 index 0000000..19bc5b0 --- /dev/null +++ b/todoapp/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('todo/', include('todo.urls')) +] diff --git a/todoapp/wsgi.py b/todoapp/wsgi.py new file mode 100644 index 0000000..81fb545 --- /dev/null +++ b/todoapp/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for todoapp 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.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todoapp.settings') + +application = get_wsgi_application()