diff --git a/Code/matthew/django/labs/01_django_redo/manage.py b/Code/matthew/django/labs/01_django_redo/manage.py new file mode 100755 index 00000000..707d435e --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unit_converter_proj.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/Code/matthew/django/labs/01_django_redo/unit_converter_app/__init__.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/admin.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/apps.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/apps.py new file mode 100644 index 00000000..7f259c7f --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UnitConverterAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'unit_converter_app' diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/migrations/__init__.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/models.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/index.html b/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/index.html new file mode 100644 index 00000000..0cd2bdcf --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/index.html @@ -0,0 +1,61 @@ + + + + + + + Unit Converter + + +

Unit Converter

+ {% comment %} + try action=" {% url APP_NAME:result %}" + action="ucp/result/" + {% endcomment %} +
+ {% csrf_token %} + + +
+ + +
+ +
+

Enter the units you are using

+ + + + + + + + + +
+ +
+

Enter the units you wish to convert to

+ + + + + + + + + +
+ + +
+ + + + diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/result.html b/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/result.html new file mode 100644 index 00000000..6d3624c2 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/templates/unit_converter_app/result.html @@ -0,0 +1,23 @@ + + + + + + + Results + + +

Results

+ +

+ You converted {{distance}}{{units}} into + {{converted_output}}{{units_to_convert}} +

+ Reset + +
+ + distance {{ distance }} units {{units}} converted units {{converted_output}} + units to convert {{units_to_convert}} + + diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/tests.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/urls.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/urls.py new file mode 100644 index 00000000..95ab0ccd --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/urls.py @@ -0,0 +1,18 @@ +from django.urls import path, include +from . import views +# . :importing the views from the views file in the same directory + +# +app_name = 'unitconverter' + +urlpatterns = [ + # 8000/ucp + path('', views.index, name='index'), + + + path('result/', views.forms, name='forms') + + # 8000/rps/result + # path('result/', views.result, name='result'), + +] diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_app/views.py b/Code/matthew/django/labs/01_django_redo/unit_converter_app/views.py new file mode 100644 index 00000000..d740b123 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_app/views.py @@ -0,0 +1,101 @@ + +from django.shortcuts import render + +# Create your views here. + + +def index(request): + + return render(request, 'unit_converter_app/index.html') + + +# request.POST is grabbing the information inside of the from submission +def forms(request): + form_post = request.POST + + #EXAMPLE: distance=form_post['distance'] + + distance = form_post['distance'] + units = form_post['user_units'] + units_to_convert = form_post['conversion_units'] + + def feet_to_meters(x): + converted_ft = float(x) * .3048 + return converted_ft + + def mile_to_meters(x): + converted_mi = float(x) * 1609.34 + return converted_mi + + def km_to_meters(x): + converted_km = float(x) * 1000 + return converted_km + + def meters_to_feet(x): + converted_ft = float(x) * 3.28084 + return converted_ft + + def meters_to_mile(x): + converted_mi = float(x) * .000621371 + return converted_mi + + def meters_to_km(x): + converted_km = float(x) * .001 + return converted_km + + if units.lower() == 'mi' and units_to_convert.lower() == 'km': + converted_output = meters_to_km(mile_to_meters(distance)) + print(f'\n {distance}mi is {converted_output}km') + + elif units.lower() == 'mi' and units_to_convert.lower() == 'm': + converted_output = mile_to_meters(distance) + print(f'\n {distance}mi is {converted_output}m') + + elif units.lower() == 'mi' and units_to_convert.lower() == 'ft': + converted_output = meters_to_feet(mile_to_meters(distance)) + print(f'\n {distance}mi is {converted_output}ft') + + elif units.lower() == 'km' and units_to_convert.lower() == 'mi': + converted_output = meters_to_mile(km_to_meters(distance)) + print(f'\n {distance}km is {converted_output}mi') + + elif units.lower() == 'km' and units_to_convert.lower() == 'm': + converted_output = km_to_meters(distance) + print(f'\n {distance}km is {converted_output}m') + + elif units.lower() == 'km' and units_to_convert.lower() == 'ft': + converted_output = meters_to_feet(km_to_meters(distance)) + print(f'\n {distance}km is {converted_output}ft') + + elif units.lower() == 'ft' and units_to_convert.lower() == 'mi': + converted_output = mile_to_meters(feet_to_meters(distance)) + print(f'\n {distance}ft is {converted_output}mi') + + elif units.lower() == 'ft' and units_to_convert.lower() == 'm': + converted_output = feet_to_meters(distance) + print(f'\n {distance}ft is {converted_output}m') + + elif units.lower() == 'ft' and units_to_convert.lower() == 'km': + converted_output = meters_to_km(feet_to_meters(distance)) + print(f'\n {distance}mi is {converted_output}ft') + + elif units.lower() == 'm' and units_to_convert.lower() == 'ft': + converted_output = meters_to_feet(distance) + print(f'\n {distance}km is {converted_output}ft') + + elif units.lower() == 'm' and units_to_convert.lower() == 'mi': + converted_output = meters_to_mile(distance) + print(f'\n {distance}ft is {converted_output}mi') + + elif units.lower() == 'm' and units_to_convert.lower() == 'km': + converted_output = meters_to_km + print(f'\n {distance}mi is {converted_output}ft') + + context = { + 'distance': distance, + 'converted_output': converted_output, + 'units': units, + 'units_to_convert': units_to_convert, + } + + return render(request, 'unit_converter_app/result.html', context) diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_proj/__init__.py b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_proj/asgi.py b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/asgi.py new file mode 100644 index 00000000..6c082a5d --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for unit_converter_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unit_converter_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_proj/settings.py b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/settings.py new file mode 100644 index 00000000..17368170 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for unit_converter_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-29itg#5d48)-=q32_h*d159m_29+t!)o#ka4px32c6=58((!8s' + +# 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', + 'unit_converter_app' +] + +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 = 'unit_converter_proj.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 = 'unit_converter_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_proj/urls.py b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/urls.py new file mode 100644 index 00000000..73b7c499 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/urls.py @@ -0,0 +1,22 @@ +"""unit_converter_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('ucp/', include('unit_converter_app.urls')) +] diff --git a/Code/matthew/django/labs/01_django_redo/unit_converter_proj/wsgi.py b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/wsgi.py new file mode 100644 index 00000000..4eaa5183 --- /dev/null +++ b/Code/matthew/django/labs/01_django_redo/unit_converter_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for unit_converter_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unit_converter_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/02_first_app/manage.py b/Code/matthew/django/labs/02_first_app/manage.py new file mode 100755 index 00000000..a7da6671 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.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/Code/matthew/django/labs/02_first_app/mysite/__init__.py b/Code/matthew/django/labs/02_first_app/mysite/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/02_first_app/mysite/asgi.py b/Code/matthew/django/labs/02_first_app/mysite/asgi.py new file mode 100644 index 00000000..674a5a5a --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/mysite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for mysite project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/02_first_app/mysite/settings.py b/Code/matthew/django/labs/02_first_app/mysite/settings.py new file mode 100644 index 00000000..624c5cc7 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/mysite/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-fefz2h=hzcahf+b-ogytoq8a7q*h)v#c&%9tiesm4eiobw=fpo' + +# 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', + # + 'polls.apps.PollsConfig', + +] + +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 = 'mysite.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 = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/02_first_app/mysite/urls.py b/Code/matthew/django/labs/02_first_app/mysite/urls.py new file mode 100644 index 00000000..9f69e0ae --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/mysite/urls.py @@ -0,0 +1,23 @@ +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + + path('index/', include('polls.urls')) +] diff --git a/Code/matthew/django/labs/02_first_app/mysite/wsgi.py b/Code/matthew/django/labs/02_first_app/mysite/wsgi.py new file mode 100644 index 00000000..48d46fe4 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/02_first_app/polls/__init__.py b/Code/matthew/django/labs/02_first_app/polls/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/02_first_app/polls/admin.py b/Code/matthew/django/labs/02_first_app/polls/admin.py new file mode 100644 index 00000000..d3ee231f --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Question +# Register your models here. + +admin.site.register(Question) diff --git a/Code/matthew/django/labs/02_first_app/polls/apps.py b/Code/matthew/django/labs/02_first_app/polls/apps.py new file mode 100644 index 00000000..5a5f94ca --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'polls' diff --git a/Code/matthew/django/labs/02_first_app/polls/migrations/0001_initial.py b/Code/matthew/django/labs/02_first_app/polls/migrations/0001_initial.py new file mode 100644 index 00000000..21b4e6d9 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.0.3 on 2022-03-29 03:09 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('question_text', models.CharField(max_length=200)), + ('pub_date', models.DateTimeField(verbose_name='date published')), + ], + ), + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')), + ], + ), + ] diff --git a/Code/matthew/django/labs/02_first_app/polls/migrations/__init__.py b/Code/matthew/django/labs/02_first_app/polls/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/02_first_app/polls/models.py b/Code/matthew/django/labs/02_first_app/polls/models.py new file mode 100644 index 00000000..05ae0a2e --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/models.py @@ -0,0 +1,25 @@ +import datetime +from django.utils import timezone +from django.db import models + +# Create your models here. + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + def __str__(self): + return self.question_text + + def was_published_recently(self): + return self.pub_date >= timezone.now() - datetime.timedelta(days=1) + + +class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __str__(self): + return self.choice_text diff --git a/Code/matthew/django/labs/02_first_app/polls/templates/polls/index.html b/Code/matthew/django/labs/02_first_app/polls/templates/polls/index.html new file mode 100644 index 00000000..0bbc0af3 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/templates/polls/index.html @@ -0,0 +1,22 @@ + + + + + + + Document + + + {% if latest_question_list %} +
+ {% else %} +

No polls are Avaliable.

+ {% endif %} + + diff --git a/Code/matthew/django/labs/02_first_app/polls/tests.py b/Code/matthew/django/labs/02_first_app/polls/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/02_first_app/polls/urls.py b/Code/matthew/django/labs/02_first_app/polls/urls.py new file mode 100644 index 00000000..9c4372fe --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/urls.py @@ -0,0 +1,28 @@ +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 . import views +from django.urls import path + +urlpatterns = [ + # ex: /polls/ + path('', views.index, name='index'), + # ex: /polls/5/ + path('/', views.detail, name='detail'), + # ex: /polls/5/results/ + path('/results/', views.results, name='results'), + # ex: /polls/5/vote/ + path('/vote/', views.vote, name='vote'), +] diff --git a/Code/matthew/django/labs/02_first_app/polls/views.py b/Code/matthew/django/labs/02_first_app/polls/views.py new file mode 100644 index 00000000..f6e453e1 --- /dev/null +++ b/Code/matthew/django/labs/02_first_app/polls/views.py @@ -0,0 +1,31 @@ + +from django.shortcuts import render +from django.template import loader +# from django.shortcuts import render +from django.http import HttpResponse +from .models import Question + +# Create your views here. + + +def index(request): + latest_question_list = Question.objects.order_by('-pub_date')[:5] + # template = loader.get_template('polls/index.html') + context = { + 'latest_question_list': latest_question_list, + } + # return HttpResponse(template.render(context, request)) + return render(request, 'polls/index.html', context) + + +def detail(request, question_id): + return HttpResponse("You're looking at question %s." % question_id) + + +def results(request, question_id): + response = "You're looking at the results of question %s." + return HttpResponse(response % question_id) + + +def vote(request, question_id): + return HttpResponse("You're voting on question %s." % question_id) diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/__init__.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/admin.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/admin.py new file mode 100644 index 00000000..64a74eba --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from . models import List + +# Register your models here. + +admin.site.register(List) \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/apps.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/apps.py new file mode 100644 index 00000000..87b0bfcb --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class GroceryAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'grocery_app' diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/migrations/0001_initial.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/migrations/0001_initial.py new file mode 100644 index 00000000..b53f41f1 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-04-04 19:37 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='List', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('new', models.CharField(max_length=50)), + ('date', models.DateField(auto_now=True)), + ('delete', models.BooleanField(default=False)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/migrations/__init__.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/models.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/models.py new file mode 100644 index 00000000..e8c21245 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/models.py @@ -0,0 +1,13 @@ +from django.db import models +from django.contrib.auth.models import User +# Create your models here. + +class List(models.Model): + new= models.CharField(max_length=50) + date= models.DateField(auto_now=True) + user= models.ForeignKey(User, on_delete=models.CASCADE) + delete= models.BooleanField(default=False) + + def __str__(self): + return f'{self.new}: {self.date}' + diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/edit.html b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/edit.html new file mode 100644 index 00000000..225e9c39 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/edit.html @@ -0,0 +1,9 @@ +{% extends "grocery_app/index.html" %} +{% load static %} + + {% block content %} +

Edit

+ {{item}} + {% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/index.html b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/index.html new file mode 100644 index 00000000..8d43da3e --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/templates/grocery_app/index.html @@ -0,0 +1,45 @@ + +{% load static %} + + + + + + + + Grocery List + + + {% block content %} + +

Home

+
+ {% csrf_token %} + {{new_item}} + +
+ + {% for item in show_items %} + + + + {% endfor %} + + + {% endblock content %} + + + + + + + +{% comment %}
+ + + +
{% endcomment %} \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/tests.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/urls.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/urls.py new file mode 100644 index 00000000..95fad3d2 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/urls.py @@ -0,0 +1,26 @@ +"""grocery_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views + +urlpatterns = [ + # home/ + path('home/', views.home, name='home'), + path('save/', views.save, name='save'), + path('delete//', views.delete, name='delete'), + path('edit//', views.edit, name='edit') +] \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/views.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/views.py new file mode 100644 index 00000000..19c74938 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_app/views.py @@ -0,0 +1,47 @@ +from django.shortcuts import get_object_or_404, render +from django import forms +from django.http import HttpResponseRedirect +from django.urls import reverse +from . models import List + +class NewItem(forms.Form): + item= forms.CharField(label='',widget=forms.TextInput( + attrs={'placeholder': 'Add new item'}), max_length=10) + +# Create your views here. +def home(request): + show_items= List.objects.filter(delete= False).order_by('date') + context= { + 'new_item': NewItem, + 'show_items': show_items + } + + return render(request, 'grocery_app/index.html', context) + +# add new item. Must be logged into a user +def save(request): + if request.method == "POST": + form= NewItem(request.POST) + if form.is_valid(): + text= form.cleaned_data['item'] + # + list=List() + list.new= text + list.user= request.user + list.save() + return HttpResponseRedirect(reverse('home')) + +def delete(request, id): + List.objects.filter(id=id).delete() + + + return HttpResponseRedirect(reverse('home')) + +def edit(request, id): + item= List.objects.filter(id=id) + + context= { + 'item': item, + } + + return render(request, 'grocery_app/edit.html', context) diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/__init__.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/asgi.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/asgi.py new file mode 100644 index 00000000..9012ee82 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for grocery_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/settings.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/settings.py new file mode 100644 index 00000000..bac176dd --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for grocery_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-&y%_hrq12*fw6=&&a4%lwl#-e*(sekapntn5_ig*m%2+q@wrj%' + +# 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', + # + 'grocery_app' +] + +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 = 'grocery_proj.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 = 'grocery_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = '/static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/urls.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/urls.py new file mode 100644 index 00000000..b2ba1e8c --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/urls.py @@ -0,0 +1,22 @@ +"""grocery_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('grocery_app.urls')) +] diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/wsgi.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/wsgi.py new file mode 100644 index 00000000..567bce68 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/grocery_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for grocery_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/03_grocery_list/grocery_list/manage.py b/Code/matthew/django/labs/03_grocery_list/grocery_list/manage.py new file mode 100755 index 00000000..f97e4e6d --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.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/Code/matthew/django/labs/03_grocery_list/grocery_list/static/css/style.css b/Code/matthew/django/labs/03_grocery_list/grocery_list/static/css/style.css new file mode 100644 index 00000000..c6172430 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/grocery_list/static/css/style.css @@ -0,0 +1,3 @@ +body { + background-color: aqua; +} \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/__init__.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/admin.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/admin.py new file mode 100644 index 00000000..879097b8 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import * +# Register your models here. + +admin.site.register(Department) +admin.site.register(GroceryItem) \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/apps.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/apps.py new file mode 100644 index 00000000..87b0bfcb --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class GroceryAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'grocery_app' diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/migrations/0001_initial.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/migrations/0001_initial.py new file mode 100644 index 00000000..f6046373 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# Generated by Django 4.0.3 on 2022-04-13 01:31 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Department', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=20)), + ], + ), + migrations.CreateModel( + name='GroceryItem', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('item', models.CharField(max_length=40)), + ('completed', models.BooleanField(default=False)), + ('department', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='item', to='grocery_app.department')), + ], + ), + ] diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/migrations/__init__.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/models.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/models.py new file mode 100644 index 00000000..89a2fc1a --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/models.py @@ -0,0 +1,17 @@ +from django.db import models + +# Create your models here. +class Department(models.Model): + name= models.CharField(max_length=20) + def __str__(self): + return f'{self.name}' + +class GroceryItem(models.Model): + item= models.CharField(max_length=40) + completed= models.BooleanField(default=False) + # create a relationship between Class Department and GroceryItem + # Each GroceryItem belongs to a "department" + # related name lets you call items from GroceryItem from Department.item + department= models.ForeignKey(Department, on_delete=models.CASCADE, related_name='item', null=True, blank=True ) + def __str__(self): + return f'{self.item} -- {self.completed}' \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/index.html b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/index.html new file mode 100644 index 00000000..65a63e6e --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/index.html @@ -0,0 +1,84 @@ + +{% load static %} + + + + + + + {% block title %} + Document + {% endblock title %} + + +{% block content %} + + +

My List

+
+ {% csrf_token %} + + + + +
+
+ {% for department in departments %} +
+ +

{{department.name}}

+ + {% for x in department.item.all %} + + {% if x.completed == True %} + + + + {% else %} + + + + {% endif %} + + {% endfor %} + +
+
+ {% endfor %} +
+ + + {% comment %} {% for department in departments %} + +

{{department}}

+
+
+ {% for item in grocery_list %} + {% if item.department == department %} +
    {{item.item}}
+ {% endif %} +
+
+ + {% endfor %} + {% endfor %} {% endcomment %} + + + + + {% endblock content %} + \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/view.html b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/view.html new file mode 100644 index 00000000..56efbdba --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/templates/grocery_app/view.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/tests.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/urls.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/urls.py new file mode 100644 index 00000000..22e8b743 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/urls.py @@ -0,0 +1,30 @@ +"""grocery_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views +app_name= 'grocery_list' +urlpatterns = [ + # /grocery/ + path('', views.index, name='index'), + # grocery/add_item + path('add_item', views.add_item, name='add'), + # grocery/view/"dynamic item id" + path('complete_item/', views.complete_item, name='complete'), + # + path('delete/', views.delete_item, name='delete'), + +] diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_app/views.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/views.py new file mode 100644 index 00000000..26d01f8b --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_app/views.py @@ -0,0 +1,42 @@ +from django.shortcuts import render, redirect +from .models import Department,GroceryItem +from django.http import HttpResponseRedirect +from django.urls import reverse +# Create your views here. +def index(request): + # assinging all items in GroceryItem Model to grocery_list and sort by department + grocery_list= GroceryItem.objects.all().order_by('department') + # assigning all items in Department Model to departments and sort by name + departments= Department.objects.all().order_by('name') + context= { + 'grocery_list':grocery_list, + 'departments':departments + } + return render(request, 'grocery_app/index.html', context) + +def add_item(request): + # calling 'item' from input name="item" + item_text= request.POST['item'] + department_id= request.POST['department'] + new_item= GroceryItem() + new_item.item= item_text + # if department was selected via form + if department_id: + # getting the Id for the department that was selected via form + department= Department.objects.get(id=department_id) + # assigning new item to department + new_item.department= department + new_item.save() + return HttpResponseRedirect(reverse('grocery_list:index')) + +def complete_item(request, item_id): + grocery_item= GroceryItem.objects.get(id=item_id) + # reverses boolean value + grocery_item.completed = not grocery_item.completed + grocery_item.save() + return redirect(reverse('grocery_list:index')) + +def delete_item(request, item_id): + grocery_item= GroceryItem.objects.get(id=item_id) + grocery_item.delete() + return redirect(reverse('grocery_list:index')) \ No newline at end of file diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/__init__.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/asgi.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/asgi.py new file mode 100644 index 00000000..9012ee82 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for grocery_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/settings.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/settings.py new file mode 100644 index 00000000..df98762f --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for grocery_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-e4o%b05&lru)kxig7p%$_157kzgfvt(n4v!^oe%+-ahibnh^ly' + +# 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', + # + 'grocery_app', +] + +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 = 'grocery_proj.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 = 'grocery_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/urls.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/urls.py new file mode 100644 index 00000000..617482b8 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/urls.py @@ -0,0 +1,22 @@ +"""grocery_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('grocery/', include('grocery_app.urls')), +] diff --git a/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/wsgi.py b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/wsgi.py new file mode 100644 index 00000000..567bce68 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/grocery_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for grocery_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/03_grocery_list/review/manage.py b/Code/matthew/django/labs/03_grocery_list/review/manage.py new file mode 100755 index 00000000..f97e4e6d --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grocery_proj.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/Code/matthew/django/labs/03_grocery_list/review/static/css/styles.css b/Code/matthew/django/labs/03_grocery_list/review/static/css/styles.css new file mode 100644 index 00000000..83de6788 --- /dev/null +++ b/Code/matthew/django/labs/03_grocery_list/review/static/css/styles.css @@ -0,0 +1,30 @@ +body { + background-image: linear-gradient(112.5deg, rgb(214, 214, 214) 0%, rgb(214, 214, 214) 10%,rgb(195, 195, 195) 10%, rgb(195, 195, 195) 53%,rgb(176, 176, 176) 53%, rgb(176, 176, 176) 55%,rgb(157, 157, 157) 55%, rgb(157, 157, 157) 60%,rgb(137, 137, 137) 60%, rgb(137, 137, 137) 88%,rgb(118, 118, 118) 88%, rgb(118, 118, 118) 91%,rgb(99, 99, 99) 91%, rgb(99, 99, 99) 100%),linear-gradient(157.5deg, rgb(214, 214, 214) 0%, rgb(214, 214, 214) 10%,rgb(195, 195, 195) 10%, rgb(195, 195, 195) 53%,rgb(176, 176, 176) 53%, rgb(176, 176, 176) 55%,rgb(157, 157, 157) 55%, rgb(157, 157, 157) 60%,rgb(137, 137, 137) 60%, rgb(137, 137, 137) 88%,rgb(118, 118, 118) 88%, rgb(118, 118, 118) 91%,rgb(99, 99, 99) 91%, rgb(99, 99, 99) 100%),linear-gradient(135deg, rgb(214, 214, 214) 0%, rgb(214, 214, 214) 10%,rgb(195, 195, 195) 10%, rgb(195, 195, 195) 53%,rgb(176, 176, 176) 53%, rgb(176, 176, 176) 55%,rgb(157, 157, 157) 55%, rgb(157, 157, 157) 60%,rgb(137, 137, 137) 60%, rgb(137, 137, 137) 88%,rgb(118, 118, 118) 88%, rgb(118, 118, 118) 91%,rgb(99, 99, 99) 91%, rgb(99, 99, 99) 100%),linear-gradient(90deg, rgb(195, 195, 195),rgb(228, 228, 228)); background-blend-mode:overlay,overlay,overlay,normal; +} +.department { + text-decoration: underline; + margin-bottom: .5em; + margin-left: 0; + margin-right: 0; + padding: 0; +} +.list { + border: 1px solid black; + width: 15em; + text-align: center; +} +.item { + margin-top: 0; + margin-left: 0; + margin-right: 0; + padding: 0; +} +.item > a { + margin: 0; + padding: 0; + text-decoration: none; + color: black; +} +.delete { + text-decoration: none; +} \ No newline at end of file diff --git a/Code/matthew/django/labs/04_todo/manage.py b/Code/matthew/django/labs/04_todo/manage.py new file mode 100755 index 00000000..6be564d2 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.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/Code/matthew/django/labs/04_todo/static/css/styles.css b/Code/matthew/django/labs/04_todo/static/css/styles.css new file mode 100644 index 00000000..cc688347 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/static/css/styles.css @@ -0,0 +1,33 @@ +.logic{ + display: flex; + flex-direction: column; + +} +.task_list { +display: flex; +flex-direction: column; + +} +.high { + order: 1; + display: flex; + flex-direction: column; +} +.high > ul { + +} +.medium { + order: 2; +} +.medium > ul { + /* color: aquamarine; */ + /* background-color: black; */ + font-family: 'Times New Roman', Times, serif; +} +.middle { + background-color: chartreuse; + padding: .01em .1em; +} +.low { + order: 3; +} diff --git a/Code/matthew/django/labs/04_todo/todo_app/__init__.py b/Code/matthew/django/labs/04_todo/todo_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/04_todo/todo_app/admin.py b/Code/matthew/django/labs/04_todo/todo_app/admin.py new file mode 100644 index 00000000..e8e5da58 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import * + +# Register your models here. +admin.site.register(NewTask) +# admin.site.register(Priority) \ No newline at end of file diff --git a/Code/matthew/django/labs/04_todo/todo_app/apps.py b/Code/matthew/django/labs/04_todo/todo_app/apps.py new file mode 100644 index 00000000..d8f1498d --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TodoAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'todo_app' diff --git a/Code/matthew/django/labs/04_todo/todo_app/forms.py b/Code/matthew/django/labs/04_todo/todo_app/forms.py new file mode 100644 index 00000000..3fc7dfab --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/forms.py @@ -0,0 +1,13 @@ + +from django import forms +from django.forms import ModelForm +from .models import NewTask + +class NewTaskForm(forms.ModelForm): + class Meta: + model= NewTask + fields= "__all__" +class NewPriority(forms.ModelForm): + class Meta: + + fields= "__all__" diff --git a/Code/matthew/django/labs/04_todo/todo_app/migrations/0001_initial.py b/Code/matthew/django/labs/04_todo/todo_app/migrations/0001_initial.py new file mode 100644 index 00000000..d91382d4 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.0.3 on 2022-04-09 03:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='NewTask', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('task', models.CharField(max_length=25)), + ('complete', models.BooleanField(default=False)), + ('date', models.DateTimeField(auto_now=True)), + ('priority', models.IntegerField(blank=True, default=0)), + ], + ), + migrations.CreateModel( + name='Priority', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('importance', models.CharField(choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High')], default='Low', max_length=6)), + ('date', models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/Code/matthew/django/labs/04_todo/todo_app/migrations/0002_alter_newtask_priority_alter_priority_importance.py b/Code/matthew/django/labs/04_todo/todo_app/migrations/0002_alter_newtask_priority_alter_priority_importance.py new file mode 100644 index 00000000..af55d64c --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/migrations/0002_alter_newtask_priority_alter_priority_importance.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.3 on 2022-04-09 03:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo_app', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='newtask', + name='priority', + field=models.IntegerField(blank=True, choices=[('1', '1'), ('2', '2'), ('3', '3')], default=0), + ), + migrations.AlterField( + model_name='priority', + name='importance', + field=models.CharField(choices=[('1', '1'), ('2', '2'), ('3', '3')], default='Low', max_length=6), + ), + ] diff --git a/Code/matthew/django/labs/04_todo/todo_app/migrations/0003_delete_priority.py b/Code/matthew/django/labs/04_todo/todo_app/migrations/0003_delete_priority.py new file mode 100644 index 00000000..56222341 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/migrations/0003_delete_priority.py @@ -0,0 +1,16 @@ +# Generated by Django 4.0.3 on 2022-04-09 03:51 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo_app', '0002_alter_newtask_priority_alter_priority_importance'), + ] + + operations = [ + migrations.DeleteModel( + name='Priority', + ), + ] diff --git a/Code/matthew/django/labs/04_todo/todo_app/migrations/0004_alter_newtask_priority.py b/Code/matthew/django/labs/04_todo/todo_app/migrations/0004_alter_newtask_priority.py new file mode 100644 index 00000000..eeab1742 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/migrations/0004_alter_newtask_priority.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-04-10 06:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo_app', '0003_delete_priority'), + ] + + operations = [ + migrations.AlterField( + model_name='newtask', + name='priority', + field=models.IntegerField(blank=True, choices=[(1, 'Low'), (2, 'Medium'), (3, 'High')], default=1), + ), + ] diff --git a/Code/matthew/django/labs/04_todo/todo_app/migrations/__init__.py b/Code/matthew/django/labs/04_todo/todo_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/04_todo/todo_app/models.py b/Code/matthew/django/labs/04_todo/todo_app/models.py new file mode 100644 index 00000000..afed73da --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/models.py @@ -0,0 +1,46 @@ +from django.db import models +from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType +LVL= ( + (1,'Low'), + (2,'Medium'), + (3,'High'), +) +# Create your models here. +class NewTask(models.Model): + task= models.CharField(max_length=25) + complete= models.BooleanField(default=False) + date= models.DateTimeField(auto_now=True) + priority= models.IntegerField(default=1, blank=True, choices=LVL) +# trying to join priority and Newtask + # content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, default=0) + # object_id = models.PositiveIntegerField(default=False) + # content_object = GenericForeignKey('content_type', 'object_id') + + def __str__(self): + return f'{self.task}' + +# class Priority(models.Model): +# importance= models.CharField(max_length=6,choices=LVL, default='Low') +# date= models.DateTimeField(auto_now=True) +# # object_id = models.PositiveIntegerField() +# def __str__(self): +# return f'{self.importance}' + + + + + + + + +######################################################################################################## + +# class TaggedItem(models.Model): +# tag = models.SlugField() +# content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) +# object_id = models.PositiveIntegerField() +# content_object = GenericForeignKey('content_type', 'object_id') + +# def __str__(self): +# return self.tag diff --git a/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/edit.html b/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/edit.html new file mode 100644 index 00000000..2c93478b --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/edit.html @@ -0,0 +1,20 @@ + +{% extends 'todo_app/index.html' %} +{% block title %} +Edit +{% endblock title %} + +{% block content %} + +

Edit

+

ToDo list is sorted by Priority "High" all High Priority items will be highlighted

+ +
+ {% csrf_token %} + Task:{{form.task}} + Priority:{{form.priority}} + Complete:{{form.complete}} + +
+ +{% endblock content %} diff --git a/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/index.html b/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/index.html new file mode 100644 index 00000000..49578218 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/templates/todo_app/index.html @@ -0,0 +1,123 @@ + +{% load static %} + + + + + + + {% block title %} + Home + {% endblock title %} + + + + +{% block content %} + +
+ {% csrf_token %} + +

ToDo

+

Tasks will be sorted by level of Priority, High,Medium + and low

+ {{form.task}} + +
+
+
+
+ {% for task in tasks %} + {% if task.complete == False %} + {% if task.priority == 3 %} + +
+
+ Edit + Delete +
+
+
    + {{task}} +
+
+ + {% elif task.priority == 2 %} + +
+ Edit + Delete +
+
    + {{task}} +
+
+ + {% elif task.priority == 1 %} + +
+
+ Edit + Delete +
+
+
    + {{task}} +
+
+ + {% endif %} + + {% elif task.complete == True %} +
+ + Edit + Delete +
+
+
    + {{task}} +
+
+
+ {% endif %} + {% endfor %} +
+
+ + + + + {% endblock content %} + +{% comment "" %} +
+ edit + delete +
+
    + {{task}}:IMPORTANT +
+
+ {% elif task.complete == False and task.value <= 4 %} + edit + delete +
+
    + {{task}} +
+
+ {% else %} + edit + delete +
+
    + {{task}} +
+ {% endif %} + + {% if task.complete == False and task.value >= 5 %} + {% endcomment %} + \ No newline at end of file diff --git a/Code/matthew/django/labs/04_todo/todo_app/tests.py b/Code/matthew/django/labs/04_todo/todo_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/04_todo/todo_app/urls.py b/Code/matthew/django/labs/04_todo/todo_app/urls.py new file mode 100644 index 00000000..7f29bc7f --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/urls.py @@ -0,0 +1,26 @@ +"""todo_app URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views + +urlpatterns = [ + path('', views.home, name='home'), + path('edit//', views.edit, name='edit'), + path('delete//', views.delete, name='delete'), + + +] diff --git a/Code/matthew/django/labs/04_todo/todo_app/views.py b/Code/matthew/django/labs/04_todo/todo_app/views.py new file mode 100644 index 00000000..beb62b50 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_app/views.py @@ -0,0 +1,54 @@ +from itertools import chain +from django.urls import reverse +from django.http import HttpResponseRedirect +from django.shortcuts import redirect, render +from .models import NewTask +from .forms import NewTaskForm, NewPriority + +# using iterrools chain() to combine NewTask obj w/ Priority obj in a list +# EX ['task1','low','task2','medium','task3','high'] +# Does not combine obj.id: Can not call one obj and get the other + +# Create your views here. +def home(request): + # tasks= NewTask.objects.order_by("complete") + tasks= NewTask.objects.all() + form= NewTaskForm() + + if request.method == 'POST': + form= NewTaskForm(request.POST) + + if form.is_valid(): + form.save() + + return redirect(reverse('home')) + context= { + 'tasks': tasks, + 'form': form, + + + } + return render(request, 'todo_app/index.html', context) + +def edit(request, pk): + task=NewTask.objects.get(id=pk) + + + form= NewTaskForm(instance=task) + context= { + 'form':form, + + } + if request.method == "POST": + form= NewTaskForm(request.POST, instance=task) + if form.is_valid(): + form.save() + return redirect(reverse('home')) + return render(request, 'todo_app/edit.html', context) + +def delete(request, pk): + NewTask.objects.filter(id=pk).delete() + return redirect(reverse('home')) + + + diff --git a/Code/matthew/django/labs/04_todo/todo_proj/__init__.py b/Code/matthew/django/labs/04_todo/todo_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/04_todo/todo_proj/asgi.py b/Code/matthew/django/labs/04_todo/todo_proj/asgi.py new file mode 100644 index 00000000..5a0fd4de --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for todo_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/04_todo/todo_proj/settings.py b/Code/matthew/django/labs/04_todo/todo_proj/settings.py new file mode 100644 index 00000000..56e4426f --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_proj/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for todo_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-bk(nx6h9#_udk!*16aj2m6o+)yy@)uu9v7lvlt8ouk2ibd5bi%' + +# 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_app', +] + +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 = 'todo_proj.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 = 'todo_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/04_todo/todo_proj/urls.py b/Code/matthew/django/labs/04_todo/todo_proj/urls.py new file mode 100644 index 00000000..102e3945 --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_proj/urls.py @@ -0,0 +1,22 @@ +"""todo_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('todo_app.urls')) +] diff --git a/Code/matthew/django/labs/04_todo/todo_proj/wsgi.py b/Code/matthew/django/labs/04_todo/todo_proj/wsgi.py new file mode 100644 index 00000000..5f96a37b --- /dev/null +++ b/Code/matthew/django/labs/04_todo/todo_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for todo_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/05_blog/blog/__init__.py b/Code/matthew/django/labs/05_blog/blog/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/05_blog/blog/admin.py b/Code/matthew/django/labs/05_blog/blog/admin.py new file mode 100644 index 00000000..7b51696a --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import BlogPost + +# Register your models here. +admin.site.register(BlogPost) diff --git a/Code/matthew/django/labs/05_blog/blog/apps.py b/Code/matthew/django/labs/05_blog/blog/apps.py new file mode 100644 index 00000000..94788a5e --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'blog' diff --git a/Code/matthew/django/labs/05_blog/blog/forms.py b/Code/matthew/django/labs/05_blog/blog/forms.py new file mode 100644 index 00000000..c3f036db --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/forms.py @@ -0,0 +1,6 @@ +from django import forms +from .models import BlogPost + +class PostForm(forms.Form): + title= forms.CharField(max_length=20) + body= forms.CharField(max_length=120) \ No newline at end of file diff --git a/Code/matthew/django/labs/05_blog/blog/migrations/0001_initial.py b/Code/matthew/django/labs/05_blog/blog/migrations/0001_initial.py new file mode 100644 index 00000000..59d03452 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 4.0.3 on 2022-04-15 03:38 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='BlogPost', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=10)), + ('body', models.TextField(max_length=120)), + ('public', models.BooleanField(default=False)), + ('date_created', models.DateTimeField(auto_now_add=True)), + ('date_edited', models.DateTimeField(auto_now=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='post', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/Code/matthew/django/labs/05_blog/blog/migrations/__init__.py b/Code/matthew/django/labs/05_blog/blog/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/05_blog/blog/models.py b/Code/matthew/django/labs/05_blog/blog/models.py new file mode 100644 index 00000000..259424b5 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/models.py @@ -0,0 +1,15 @@ +from django.db import models +from django.contrib.auth.models import User + +# Create your models here. +class BlogPost(models.Model): + title= models.CharField(max_length=10) + body= models.TextField(max_length=120) + user= models.ForeignKey(User, on_delete=models.CASCADE, related_name='post') + public= models.BooleanField(default=False) + date_created= models.DateTimeField(auto_now_add=True) + date_edited= models.DateTimeField(auto_now=True) + + def __str__(self): + return f'{self.title} -- {self.body[:10]}' + diff --git a/Code/matthew/django/labs/05_blog/blog/templates/blog/home.html b/Code/matthew/django/labs/05_blog/blog/templates/blog/home.html new file mode 100644 index 00000000..006441f8 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/templates/blog/home.html @@ -0,0 +1,29 @@ + + + + + + + + Document + + + {% block content %} + +

Home

+
+ {% csrf_token %} + {{new}} + +
+
+ {% for post in posts %} +
    + {{post}} +
+ View + + {% endfor %} + {% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/labs/05_blog/blog/templates/blog/view.html b/Code/matthew/django/labs/05_blog/blog/templates/blog/view.html new file mode 100644 index 00000000..3c607ed9 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/templates/blog/view.html @@ -0,0 +1,5 @@ +{% extends 'blog/home.html' %} + +{% block content %} +{{post}} +{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/labs/05_blog/blog/tests.py b/Code/matthew/django/labs/05_blog/blog/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/labs/05_blog/blog/urls.py b/Code/matthew/django/labs/05_blog/blog/urls.py new file mode 100644 index 00000000..b70ab643 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('home/', views.home, name="home"), + path('new/', views.new_post, name="new_post"), + path('view/', views.view_post, name="view_post"), +] diff --git a/Code/matthew/django/labs/05_blog/blog/views.py b/Code/matthew/django/labs/05_blog/blog/views.py new file mode 100644 index 00000000..4b3985c1 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog/views.py @@ -0,0 +1,34 @@ +from django.urls import reverse +from django.shortcuts import redirect, render +from .models import BlogPost +from .forms import PostForm + + +# Create your views here. + +def home(request): + new= PostForm() + posts=BlogPost.objects.all() + context= { + 'posts':posts, + 'new':new + } + return render(request, 'blog/home.html', context) + +def new_post(request): + if request.POST: + form= PostForm(request.POST) + if form.is_valid(): + post= BlogPost() + post.user= request.user + post.title = form.cleaned_data['title'] + post.body = form.cleaned_data['body'] + post.save() + return redirect(reverse('home')) + +def view_post(request, id): + post= BlogPost.objects.get(id=id) + context= { + 'post':post + } + return render(request, 'blog/view.html', context ) \ No newline at end of file diff --git a/Code/matthew/django/labs/05_blog/blog_proj/__init__.py b/Code/matthew/django/labs/05_blog/blog_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/labs/05_blog/blog_proj/asgi.py b/Code/matthew/django/labs/05_blog/blog_proj/asgi.py new file mode 100644 index 00000000..740a6d46 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for blog_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/labs/05_blog/blog_proj/settings.py b/Code/matthew/django/labs/05_blog/blog_proj/settings.py new file mode 100644 index 00000000..3f2f7da7 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog_proj/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for blog_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-(btb^uyr1g9qx=v509q@_3e#pwjvd4h38n8f399ly5xbx&_luo' + +# 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', + # + 'blog', +] + +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 = 'blog_proj.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 = 'blog_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/labs/05_blog/blog_proj/urls.py b/Code/matthew/django/labs/05_blog/blog_proj/urls.py new file mode 100644 index 00000000..4e3b939a --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog_proj/urls.py @@ -0,0 +1,22 @@ +"""blog_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('blog.urls')), +] diff --git a/Code/matthew/django/labs/05_blog/blog_proj/wsgi.py b/Code/matthew/django/labs/05_blog/blog_proj/wsgi.py new file mode 100644 index 00000000..86ed6ae8 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/blog_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for blog_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/labs/05_blog/manage.py b/Code/matthew/django/labs/05_blog/manage.py new file mode 100755 index 00000000..aac5dde4 --- /dev/null +++ b/Code/matthew/django/labs/05_blog/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_proj.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/Code/matthew/django/lectures/01_app_setup/hello_app/__init__.py b/Code/matthew/django/lectures/01_app_setup/hello_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/admin.py b/Code/matthew/django/lectures/01_app_setup/hello_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/apps.py b/Code/matthew/django/lectures/01_app_setup/hello_app/apps.py new file mode 100644 index 00000000..37e3c0e4 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class HelloAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'hello_app' diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/migrations/__init__.py b/Code/matthew/django/lectures/01_app_setup/hello_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/models.py b/Code/matthew/django/lectures/01_app_setup/hello_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/static/style.css b/Code/matthew/django/lectures/01_app_setup/hello_app/static/style.css new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/templates/hello_app/index.html b/Code/matthew/django/lectures/01_app_setup/hello_app/templates/hello_app/index.html new file mode 100644 index 00000000..c4e9cc18 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/templates/hello_app/index.html @@ -0,0 +1,14 @@ + + + + + + + Document + + + +

Hello {{name}}

+ + + diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/tests.py b/Code/matthew/django/lectures/01_app_setup/hello_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/urls.py b/Code/matthew/django/lectures/01_app_setup/hello_app/urls.py new file mode 100644 index 00000000..bf828431 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/urls.py @@ -0,0 +1,22 @@ +from unicodedata import name +from django.urls import URLPattern, path +# . is short cut for calling the views file. Because it is in the same folder +from . import views + +urlpatterns = [ + # 8000/hello + path('', views.hello, name='hello'), + + # 8000/hello/bruce + path('bruce', views.bruce, name='bruce'), + + # 8000/hello/battman + path('batman', views.batman, name='batman'), + + # 8000/hello/stringname + path('', views.say_hello, name='say_hello'), + # calls the f string on views.py. + # creates a dynamic URL ex: hello/matthew + # prints 'hello matthew' on page + +] diff --git a/Code/matthew/django/lectures/01_app_setup/hello_app/views.py b/Code/matthew/django/lectures/01_app_setup/hello_app/views.py new file mode 100644 index 00000000..8022d200 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/hello_app/views.py @@ -0,0 +1,25 @@ +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render +# import http response from django +from django.http import HttpResponse + +# Create your views here. + + +def hello(request): + return HttpResponse('

Hello World

') + + +def bruce(request): + return HttpResponse('Hello Bruce') + + +def batman(request): + return HttpResponse('We know your batman') + + +def say_hello(request, name): + # return HttpResponse(f'hello {name}') + + # calling index.html + return render(request, 'hello_app/index.html', {'name': name}) diff --git a/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/__init__.py b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/asgi.py b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/asgi.py new file mode 100644 index 00000000..8d6f1c66 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for kiwi_first_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kiwi_first_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/settings.py b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/settings.py new file mode 100644 index 00000000..f1d6519f --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for kiwi_first_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-0^t^mepds2pdc@4bu+%3uionofuim#yxm5(+np)yrph4o6k2**' + +# 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', + + # add your project apps + 'hello_app', +] + +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 = 'kiwi_first_proj.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 = 'kiwi_first_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/urls.py b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/urls.py new file mode 100644 index 00000000..d3f4d4ba --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/urls.py @@ -0,0 +1,25 @@ +"""kiwi_first_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 xml.etree.ElementInclude import include +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('hello/', include('hello_app.urls')), + + # path('goodbye/', include('goodbye_app.urls')), +] diff --git a/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/wsgi.py b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/wsgi.py new file mode 100644 index 00000000..97eac6ce --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/kiwi_first_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for kiwi_first_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kiwi_first_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/01_app_setup/manage.py b/Code/matthew/django/lectures/01_app_setup/manage.py new file mode 100755 index 00000000..e77fbc01 --- /dev/null +++ b/Code/matthew/django/lectures/01_app_setup/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kiwi_first_proj.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/Code/matthew/django/lectures/04_/christmas/__init__.py b/Code/matthew/django/lectures/04_/christmas/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/04_/christmas/asgi.py b/Code/matthew/django/lectures/04_/christmas/asgi.py new file mode 100644 index 00000000..cff24ad2 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for christmas project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'christmas.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/04_/christmas/settings.py b/Code/matthew/django/lectures/04_/christmas/settings.py new file mode 100644 index 00000000..fde1fa54 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for christmas project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-y)w9o$74i$qmtv_o*%mt*xp^z=o4sr-6o^pd@e=u0y!2vx2wn*' + +# 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', + # + 'christmas_app' +] + +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 = 'christmas.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 = 'christmas.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/04_/christmas/urls.py b/Code/matthew/django/lectures/04_/christmas/urls.py new file mode 100644 index 00000000..fd6a5494 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas/urls.py @@ -0,0 +1,22 @@ +"""christmas URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('christmas_app.urls')) +] diff --git a/Code/matthew/django/lectures/04_/christmas/wsgi.py b/Code/matthew/django/lectures/04_/christmas/wsgi.py new file mode 100644 index 00000000..890d8a62 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for christmas 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'christmas.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/04_/christmas_app/__init__.py b/Code/matthew/django/lectures/04_/christmas_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/04_/christmas_app/admin.py b/Code/matthew/django/lectures/04_/christmas_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/lectures/04_/christmas_app/apps.py b/Code/matthew/django/lectures/04_/christmas_app/apps.py new file mode 100644 index 00000000..290b40c9 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChristmasAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'christmas_app' diff --git a/Code/matthew/django/lectures/04_/christmas_app/migrations/__init__.py b/Code/matthew/django/lectures/04_/christmas_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/04_/christmas_app/models.py b/Code/matthew/django/lectures/04_/christmas_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/lectures/04_/christmas_app/templates/christmas_app/index.html b/Code/matthew/django/lectures/04_/christmas_app/templates/christmas_app/index.html new file mode 100644 index 00000000..34b00c5f --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/templates/christmas_app/index.html @@ -0,0 +1,21 @@ + + + + + + + Document + + +

Christmas

+ + {% if xmas %} +

YES

+ + {% else %} + +

NO

+ + {% endif %} + + diff --git a/Code/matthew/django/lectures/04_/christmas_app/tests.py b/Code/matthew/django/lectures/04_/christmas_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/04_/christmas_app/urls.py b/Code/matthew/django/lectures/04_/christmas_app/urls.py new file mode 100644 index 00000000..e300e82c --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/urls.py @@ -0,0 +1,24 @@ +"""christmas URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path, include +from . import views + +app_name = 'christmas_app' + +urlpatterns = [ + path('home/', views.home, name='home') +] diff --git a/Code/matthew/django/lectures/04_/christmas_app/views.py b/Code/matthew/django/lectures/04_/christmas_app/views.py new file mode 100644 index 00000000..c84aca4b --- /dev/null +++ b/Code/matthew/django/lectures/04_/christmas_app/views.py @@ -0,0 +1,18 @@ +from multiprocessing import context +from django.shortcuts import render +import datetime as dt + +# Create your views here. + + +def home(request): + now = dt.datetime.now() + + print(now) + + context = { + 'now': now, + 'xmas': now.month == 12 and now.day == 25 + } + + return render(request, 'christmas_app/index.html', context) diff --git a/Code/matthew/django/lectures/04_/manage.py b/Code/matthew/django/lectures/04_/manage.py new file mode 100755 index 00000000..50361c5c --- /dev/null +++ b/Code/matthew/django/lectures/04_/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'christmas.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/Code/matthew/django/lectures/05_chirpy/chirpy_app/__init__.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/admin.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/admin.py new file mode 100644 index 00000000..23976cb8 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from . models import Cheep +# Register your models here. + +admin.site.register(Cheep) diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/apps.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/apps.py new file mode 100644 index 00000000..b0149d00 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChirpyAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'chirpy_app' diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/0001_initial.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/0001_initial.py new file mode 100644 index 00000000..9d40d805 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 4.0.3 on 2022-03-30 02:33 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Cheep', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('chirp', models.CharField(max_length=125)), + ('date_published', models.DateTimeField(auto_now=True)), + ('deleted', models.BooleanField(default=False)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/__init__.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/models.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/models.py new file mode 100644 index 00000000..860c4d0a --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/models.py @@ -0,0 +1,19 @@ +from django.contrib.auth.models import User +from django.db import models + + +# For importing ~User objects~ (username, password, email, first_name, last_name) + + +# Create your models here. + +class Cheep(models.Model): + chirp = models.CharField(max_length=125) + # auto_now gives timestamp based off settings time_zone + date_published = models.DateTimeField(auto_now=True) + # on_delete=CASCADE - deletes all user data and associated information + user = models.ForeignKey(User, on_delete=models.CASCADE) + deleted = models.BooleanField(default=False) + + def __str__(self): + return f'{self.user}: {self.date_published} -- {self.chirp}' diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/templates/chirpy_app/index.html b/Code/matthew/django/lectures/05_chirpy/chirpy_app/templates/chirpy_app/index.html new file mode 100644 index 00000000..956e2669 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/templates/chirpy_app/index.html @@ -0,0 +1,26 @@ + + + + + + + Chirp + + +

Welcome to Class Kiwi Chirpy

+
+ {% csrf_token %} {{form}} + + +
+ {% for cheep in cheeps %} +
    + {% comment %} Front end deletion {% endcomment %} + {% if cheep.deleted == False %} {{cheep}} {% endif %} +
+ + {% endfor %} + + + + diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/tests.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/urls.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/urls.py new file mode 100644 index 00000000..1e1714e2 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/urls.py @@ -0,0 +1,23 @@ + + +"""chirpy_app URL Configuration + +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.urls import path +from . import views + + +urlpatterns = [ + path('', views.index, name='index'), + path('save/', views.save_cheep, name='save') +] diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_app/views.py b/Code/matthew/django/lectures/05_chirpy/chirpy_app/views.py new file mode 100644 index 00000000..3153b0ad --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_app/views.py @@ -0,0 +1,39 @@ + +from django.shortcuts import render +from django import forms +from django.http import HttpResponseRedirect +from django.urls import reverse +from .models import Cheep +# Create your views here. + + +class NewCheepForm(forms.Form): + text1 = forms.CharField(label='Cheep your thoughts here', widget=forms.TextInput( + attrs={'placeholder': 'Cheep away'}), max_length=120) + + +def index(request): + cheeps = Cheep.objects.all().order_by('-date_published') + # Backend deletion + # cheeps = Cheep.objects.filter(deleted=False).order_by('-date_published') + + return render(request, 'chirpy_app/index.html', { + 'form': NewCheepForm(), + 'cheeps': cheeps + }) + + +def save_cheep(request): + if request.method == 'POST': + form = NewCheepForm(request.POST) + if form.is_valid(): + text = form.cleaned_data['text1'] + user = request.user + # Linking Cheep() in models.py and automatically updating chirp and user + + cheep = Cheep() # Ex: cheep.date_published + cheep.chirp = text + cheep.user = user + cheep.save() + + return HttpResponseRedirect(reverse('index')) diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_proj/__init__.py b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_proj/asgi.py b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/asgi.py new file mode 100644 index 00000000..a8c08843 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for chirpy_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chirpy_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_proj/settings.py b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/settings.py new file mode 100644 index 00000000..294fec4c --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for chirpy_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-(1^puj&==bj1hrk)lxjlj#76p6)&ny!#jt#v^iu_2ws4fi8n0i' + +# 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', + # + 'chirpy_app' +] + +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 = 'chirpy_proj.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 = 'chirpy_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_proj/urls.py b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/urls.py new file mode 100644 index 00000000..38a7d057 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/urls.py @@ -0,0 +1,22 @@ +"""chirpy_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('chirp/', include('chirpy_app.urls')) +] diff --git a/Code/matthew/django/lectures/05_chirpy/chirpy_proj/wsgi.py b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/wsgi.py new file mode 100644 index 00000000..a7d60919 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/chirpy_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for chirpy_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chirpy_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/05_chirpy/manage.py b/Code/matthew/django/lectures/05_chirpy/manage.py new file mode 100755 index 00000000..327acf62 --- /dev/null +++ b/Code/matthew/django/lectures/05_chirpy/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chirpy_proj.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/Code/matthew/django/lectures/06_template_inheritance/manage.py b/Code/matthew/django/lectures/06_template_inheritance/manage.py new file mode 100755 index 00000000..d416ec1b --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'routing_proj.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/Code/matthew/django/lectures/06_template_inheritance/routing_app/__init__.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/admin.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/apps.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/apps.py new file mode 100644 index 00000000..9fabc5e3 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class RoutingAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'routing_app' diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/migrations/__init__.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/models.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/animation.html b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/animation.html new file mode 100644 index 00000000..a478a228 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/animation.html @@ -0,0 +1,10 @@ + +{% extends 'routing_app/base.html' %} +{% block title %} + Animation +{% endblock title %} + +{% block content %} +

This is my Animation page

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum impedit

+{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/base.html b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/base.html new file mode 100644 index 00000000..a22956e0 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/base.html @@ -0,0 +1,27 @@ + +{% load static %} + + + + + + + + + + + {% block title %} + {% endblock title %} + + + + {% block content %} + + {% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/bio.html b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/bio.html new file mode 100644 index 00000000..ff638abf --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/bio.html @@ -0,0 +1,10 @@ + +{% extends 'routing_app/base.html' %} +{% block title %} + Bio +{% endblock title %} + +{% block content %} +

This is my Bio page

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum impedit

+{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/blog.html b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/blog.html new file mode 100644 index 00000000..cc6f79ce --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/blog.html @@ -0,0 +1,10 @@ + +{% extends 'routing_app/base.html' %} +{% block title %} + Blog +{% endblock title %} + +{% block content %} +

This is my Blog page

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum impedit

+{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/company.html b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/company.html new file mode 100644 index 00000000..874e510d --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/templates/routing_app/company.html @@ -0,0 +1,10 @@ + +{% extends 'routing_app/base.html' %} +{% block title %} + Company +{% endblock title %} + +{% block content %} +

This is my Company page

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum impedit

+{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/tests.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/urls.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/urls.py new file mode 100644 index 00000000..47cd1778 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/urls.py @@ -0,0 +1,21 @@ +"""routing_App URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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') + +""" + +from django.urls import path +from . import views + +urlpatterns = [ + path('bio/', views.bio, name='bio'), + path('animation/', views.animation, name='animation'), + path('blog/', views.blog, name='blog'), + path('', views.base, name='base'), + path('company/', views.company, name='company') +] \ No newline at end of file diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_app/views.py b/Code/matthew/django/lectures/06_template_inheritance/routing_app/views.py new file mode 100644 index 00000000..b2ae621a --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_app/views.py @@ -0,0 +1,19 @@ +from django.shortcuts import render + +# Create your views here. + +def bio(request): + + return render(request, 'routing_app/bio.html') + +def animation(request): + return render(request, 'routing_app/animation.html') + +def base(request): + return render(request, 'routing_app/base.html') + +def blog(request): + return render(request, 'routing_app/blog.html') + +def company(request): + return render(request, 'routing_app/company.html') diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_proj/__init__.py b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_proj/asgi.py b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/asgi.py new file mode 100644 index 00000000..28181650 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for routing_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'routing_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_proj/settings.py b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/settings.py new file mode 100644 index 00000000..48278bce --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for routing_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-=u+f$a_(o9luw8n9eak&ra7_z*(h&r&p)h!_&rb7cd)@jk_%mc' + +# 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', + # + 'routing_app' +] + +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 = 'routing_proj.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 = 'routing_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = '/static/' +# STATIC_URL = '/static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_proj/urls.py b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/urls.py new file mode 100644 index 00000000..68e2994f --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/urls.py @@ -0,0 +1,22 @@ +"""routing_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('routing/', include('routing_app.urls')) +] diff --git a/Code/matthew/django/lectures/06_template_inheritance/routing_proj/wsgi.py b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/wsgi.py new file mode 100644 index 00000000..ca63dd49 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/routing_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for routing_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'routing_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/06_template_inheritance/static/css/styles.css b/Code/matthew/django/lectures/06_template_inheritance/static/css/styles.css new file mode 100644 index 00000000..eefc52f1 --- /dev/null +++ b/Code/matthew/django/lectures/06_template_inheritance/static/css/styles.css @@ -0,0 +1,13 @@ +body { + background-color: aqua; + +} + +.alert{ + color: orange; +} + +nav { + display: flex; + justify-content: flex-end; +} \ No newline at end of file diff --git a/Code/matthew/django/lectures/07_/manage.py b/Code/matthew/django/lectures/07_/manage.py new file mode 100755 index 00000000..4b0773d9 --- /dev/null +++ b/Code/matthew/django/lectures/07_/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sessions_proj.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/Code/matthew/django/lectures/07_/sessions_app/__init__.py b/Code/matthew/django/lectures/07_/sessions_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/07_/sessions_app/admin.py b/Code/matthew/django/lectures/07_/sessions_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/lectures/07_/sessions_app/apps.py b/Code/matthew/django/lectures/07_/sessions_app/apps.py new file mode 100644 index 00000000..49afd7fb --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SessionsAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'sessions_app' diff --git a/Code/matthew/django/lectures/07_/sessions_app/migrations/__init__.py b/Code/matthew/django/lectures/07_/sessions_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/07_/sessions_app/models.py b/Code/matthew/django/lectures/07_/sessions_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/add.html b/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/add.html new file mode 100644 index 00000000..00ad8863 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/add.html @@ -0,0 +1,23 @@ + + + + + + + Add Task + + +

Add Task

+ +
+ {% csrf_token %} + {{form}} + + +
+ + + + View Task + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/index.html b/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/index.html new file mode 100644 index 00000000..9c282693 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/templates/sessions_app/index.html @@ -0,0 +1,21 @@ + + + + + + + Sessions + + +

Home page

+ {% for task in tasks %} +
  • + {{task}} +
  • + {%empty%} +
  • No Tasks
  • + {% endfor %} +
    + Add Task + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/07_/sessions_app/tests.py b/Code/matthew/django/lectures/07_/sessions_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/07_/sessions_app/urls.py b/Code/matthew/django/lectures/07_/sessions_app/urls.py new file mode 100644 index 00000000..8043ad49 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/urls.py @@ -0,0 +1,24 @@ +"""sessions_app URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views + +app_name= 'tasks' +urlpatterns = [ + path('', views.index, name='index'), + path('add/', views.add, name='add') +] diff --git a/Code/matthew/django/lectures/07_/sessions_app/views.py b/Code/matthew/django/lectures/07_/sessions_app/views.py new file mode 100644 index 00000000..ec4b44de --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_app/views.py @@ -0,0 +1,38 @@ +from django.urls import reverse +from django.http import HttpResponseRedirect +from django.shortcuts import render +from django import forms + +class NewTaskForm(forms.Form): + task= forms.CharField(label='new task') + +tasks= [] + +# Create your views here. + +def index(request): + # .session tracks user by cookies + # session is a request object + # method is a request object + if 'tasks' not in request.session: + request.session['tasks'] = [] + return render(request, 'sessions_app/index.html', { + 'tasks': tasks + }) + +def add(request): + if request.method == 'POST': + form = NewTaskForm(request.POST) + if form.is_valid(): + task= form.cleaned_data['task'] + tasks.append(task) + # request.session['tasks'] +=[task] + + return HttpResponseRedirect(reverse('tasks:index')) + else: + return render(request, 'sessions_app/add.html', { + 'form': form + }) + return render(request, 'sessions_app/add.html', { + 'form': NewTaskForm() + }) diff --git a/Code/matthew/django/lectures/07_/sessions_proj/__init__.py b/Code/matthew/django/lectures/07_/sessions_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/07_/sessions_proj/asgi.py b/Code/matthew/django/lectures/07_/sessions_proj/asgi.py new file mode 100644 index 00000000..4495b5f2 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for sessions_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sessions_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/07_/sessions_proj/settings.py b/Code/matthew/django/lectures/07_/sessions_proj/settings.py new file mode 100644 index 00000000..902854fb --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_proj/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for sessions_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-3!yn6t7husk-f*@4-al^k!i6)5za%_s&*q)7n-#tmb8o=pf-my' + +# 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', + # + 'sessions_app' +] + +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 = 'sessions_proj.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 = 'sessions_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/07_/sessions_proj/urls.py b/Code/matthew/django/lectures/07_/sessions_proj/urls.py new file mode 100644 index 00000000..f2a77727 --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_proj/urls.py @@ -0,0 +1,22 @@ +"""sessions_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('sessions/', include('sessions_app.urls')) +] diff --git a/Code/matthew/django/lectures/07_/sessions_proj/wsgi.py b/Code/matthew/django/lectures/07_/sessions_proj/wsgi.py new file mode 100644 index 00000000..77ec292c --- /dev/null +++ b/Code/matthew/django/lectures/07_/sessions_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for sessions_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sessions_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/08_model_forms/manage.py b/Code/matthew/django/lectures/08_model_forms/manage.py new file mode 100755 index 00000000..6be564d2 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.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/Code/matthew/django/lectures/08_model_forms/static/css/styles.css b/Code/matthew/django/lectures/08_model_forms/static/css/styles.css new file mode 100644 index 00000000..62910870 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/static/css/styles.css @@ -0,0 +1,30 @@ +body { + background-color: aqua; +} +.all_tasks { + width: 400px; + +} +.user_controls { + /* display: flex; + flex-direction: column; */ + text-align: end; +} +.tasks { + display: flex; + flex-direction: column; + + border: 1px solid black; + margin: 5px; +} +.item { + text-decoration: underline; +} +.done { + display: flex; + flex-direction: column; + border: 1px solid black; + margin: 5px; + +} + diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/__init__.py b/Code/matthew/django/lectures/08_model_forms/todo_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/admin.py b/Code/matthew/django/lectures/08_model_forms/todo_app/admin.py new file mode 100644 index 00000000..ff7ce428 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/admin.py @@ -0,0 +1,7 @@ +from django.contrib import admin +# * imports all from models +from . models import * + +# Register your models here. + +admin.site.register(Task) diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/apps.py b/Code/matthew/django/lectures/08_model_forms/todo_app/apps.py new file mode 100644 index 00000000..d8f1498d --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TodoAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'todo_app' diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/forms.py b/Code/matthew/django/lectures/08_model_forms/todo_app/forms.py new file mode 100644 index 00000000..28013a93 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/forms.py @@ -0,0 +1,10 @@ +from django import forms +from django.forms import ModelForm +from . models import * + +# naming convention: Task() is model so this is TaskForm +class TaskForm(forms.ModelForm): + class Meta: + model= Task + # + fields= '__all__' \ No newline at end of file diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/migrations/0001_initial.py b/Code/matthew/django/lectures/08_model_forms/todo_app/migrations/0001_initial.py new file mode 100644 index 00000000..64f646eb --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.3 on 2022-04-05 02:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Task', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('complete', models.BooleanField(default=False)), + ('created', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/migrations/__init__.py b/Code/matthew/django/lectures/08_model_forms/todo_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/models.py b/Code/matthew/django/lectures/08_model_forms/todo_app/models.py new file mode 100644 index 00000000..8ef87a10 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/models.py @@ -0,0 +1,12 @@ +from django.db import models + +# Create your models here. + +class Task(models.Model): + title= models.CharField(max_length=200) + complete= models.BooleanField(default=False) + created= models.DateTimeField(auto_now_add=True) + + def __str__(self): + return self.title + diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/delete.html b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/delete.html new file mode 100644 index 00000000..f4d7f7df --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/delete.html @@ -0,0 +1,12 @@ +{% extends 'todo_app/index.html' %} + + {% block content %} + +

    Do you want to delete "{{item}}"

    +
    + {% csrf_token %} + +
    +
    +Cancel + {% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/index.html b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/index.html new file mode 100644 index 00000000..d90b5fe7 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/index.html @@ -0,0 +1,58 @@ + +{% load static %} + + + + + + + Home + + + +
    + {% block content %} + +

    Todo List

    + +
    +
    + {% csrf_token %} + + {{form.title}} + +
    +
    +
    +
    + + +
    +

    ToDo

    + {% for task in tasks %} +
    + Edit + Delete +
    + +
    + {{task}} +
    +
    + {% endfor %} +
    + + +
    +

    Completed

    + {% for d_task in done_tasks %} + {{d_task}} + {% endfor %} +
    +
    + + {% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/update.html b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/update.html new file mode 100644 index 00000000..593d97ab --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/templates/todo_app/update.html @@ -0,0 +1,11 @@ +{% extends "todo_app/index.html" %} + + {% block content %} + +
    + {% csrf_token %} + {{form}} + +
    + + {% endblock content %} diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/tests.py b/Code/matthew/django/lectures/08_model_forms/todo_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/urls.py b/Code/matthew/django/lectures/08_model_forms/todo_app/urls.py new file mode 100644 index 00000000..354bfe57 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/urls.py @@ -0,0 +1,24 @@ +"""todo_app URL Configuration + + +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.urls import path +from . import views + +urlpatterns = [ + # + path('', views.index, name='index'), + path('update//', views.updateTask, name='update'), + path('delete//', views.deleteTask, name='delete'), + + +] \ No newline at end of file diff --git a/Code/matthew/django/lectures/08_model_forms/todo_app/views.py b/Code/matthew/django/lectures/08_model_forms/todo_app/views.py new file mode 100644 index 00000000..766914cc --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_app/views.py @@ -0,0 +1,55 @@ +from django.forms import BooleanField +from django.shortcuts import redirect, render +# from . models import Task +from . models import * +from todo_app.forms import TaskForm + +# Create your views here. + +def index(request): + # tasks= Task.objects.all() + tasks= Task.objects.filter(complete=False) + done_tasks= Task.objects.filter(complete=True) + form= TaskForm() + + if request.method == 'POST': + form= TaskForm(request.POST) + if form.is_valid(): + # .save() saves information to database + form.save() + return redirect('/') + + context= { + 'tasks': tasks, + 'done_tasks': done_tasks, + 'form': form + } + return render(request, 'todo_app/index.html', context) + +def updateTask(request, pk): + # targets specific id + task= Task.objects.get(id=pk) + # edit specifc id + form= TaskForm(instance=task) + context= { + 'form': form + } + if request.method == "POST": + # specifically targeting the id=pk for the indavidual task + form= TaskForm(request.POST, instance=task) + if form.is_valid(): + form.save() + return redirect('/') + + return render(request, 'todo_app/update.html', context) + +def deleteTask(request, pk): + item= Task.objects.get(id=pk) + if request.method == "POST": + item.delete() + return redirect('/') + context= { + 'item': item, + } + + return render(request, 'todo_app/delete.html', context) \ No newline at end of file diff --git a/Code/matthew/django/lectures/08_model_forms/todo_proj/__init__.py b/Code/matthew/django/lectures/08_model_forms/todo_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/08_model_forms/todo_proj/asgi.py b/Code/matthew/django/lectures/08_model_forms/todo_proj/asgi.py new file mode 100644 index 00000000..5a0fd4de --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for todo_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/08_model_forms/todo_proj/settings.py b/Code/matthew/django/lectures/08_model_forms/todo_proj/settings.py new file mode 100644 index 00000000..05ec1c02 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_proj/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for todo_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-*6yqfu9-4yc0dzh^tnr0^53%@)+fh=r(lx*$+tcr&&=539w*98' + +# 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_app' +] + +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 = 'todo_proj.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 = 'todo_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/08_model_forms/todo_proj/urls.py b/Code/matthew/django/lectures/08_model_forms/todo_proj/urls.py new file mode 100644 index 00000000..102e3945 --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_proj/urls.py @@ -0,0 +1,22 @@ +"""todo_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('todo_app.urls')) +] diff --git a/Code/matthew/django/lectures/08_model_forms/todo_proj/wsgi.py b/Code/matthew/django/lectures/08_model_forms/todo_proj/wsgi.py new file mode 100644 index 00000000..5f96a37b --- /dev/null +++ b/Code/matthew/django/lectures/08_model_forms/todo_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for todo_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/09_twitter_clone/manage.py b/Code/matthew/django/lectures/09_twitter_clone/manage.py new file mode 100755 index 00000000..3026f7f3 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'twiter_proj.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/Code/matthew/django/lectures/09_twitter_clone/messages_app/__init__.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/admin.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/admin.py new file mode 100644 index 00000000..1a1b657b --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from . models import * + +# Register your models here. +admin.site.register(Tweet) +admin.site.register(Reply) \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/apps.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/apps.py new file mode 100644 index 00000000..e44dcdbc --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MessagesAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'messages_app' diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/forms.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/forms.py new file mode 100644 index 00000000..358f03e2 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/forms.py @@ -0,0 +1,4 @@ +from django import forms + +class NewTweetForm(forms.Form): + text= forms.CharField(label='Tweet',max_length=120) \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0001_initial.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0001_initial.py new file mode 100644 index 00000000..0a8cf815 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0001_initial.py @@ -0,0 +1,28 @@ +# Generated by Django 4.0.3 on 2022-04-07 03:57 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Tweet', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('text', models.CharField(max_length=120)), + ('likes', models.IntegerField(default=0)), + ('dislikes', models.IntegerField(default=1)), + ('pub_date', models.DateTimeField(auto_now=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tweets', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0002_alter_tweet_pub_date.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0002_alter_tweet_pub_date.py new file mode 100644 index 00000000..4b4c0926 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0002_alter_tweet_pub_date.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.3 on 2022-04-12 02:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('messages_app', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='tweet', + name='pub_date', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0003_reply.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0003_reply.py new file mode 100644 index 00000000..10cf5f80 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/0003_reply.py @@ -0,0 +1,22 @@ +# Generated by Django 4.0.3 on 2022-04-12 02:49 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('messages_app', '0002_alter_tweet_pub_date'), + ] + + operations = [ + migrations.CreateModel( + name='Reply', + fields=[ + ('tweet_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='messages_app.tweet')), + ('tweet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='messages_app.tweet')), + ], + bases=('messages_app.tweet',), + ), + ] diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/__init__.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/models.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/models.py new file mode 100644 index 00000000..af3ce3ef --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/models.py @@ -0,0 +1,21 @@ +from django.db import models +from django.contrib.auth.models import User +# Create your models here. + +class Tweet(models.Model): + text= models.CharField(max_length=120) + # related_name: ? + user= models.ForeignKey(User, on_delete=models.CASCADE, related_name='tweets') + likes= models.IntegerField(default=0) + dislikes= models.IntegerField(default=1) + # auto_now=True updates everytime the field is updated or modified + # auto_now_add=True updates when created + pub_date= models.DateTimeField(auto_now_add=True) + + def __str__(self): + return f'{self.user}: {self.text[:15]} {self.pub_date.year}/{self.pub_date.month}/{self.pub_date.day} ' + +# inheriting tweet characteristics/Properties +class Reply(Tweet): + # create relationship between Reply and Tweet + tweet= models.ForeignKey(Tweet,on_delete=models.CASCADE, related_name='replies') \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/detail.html b/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/detail.html new file mode 100644 index 00000000..4fc5bb24 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/detail.html @@ -0,0 +1,17 @@ +{% extends 'messages_app/index.html' %} +{% block content %} + +

    {{tweet.text}}

    + + +{% if not request.user.is_anonymous %} +👍Likes: {{tweet.likes}} +👎Dislikes: {{tweet.dislikes}} +{% endif %} + +{% if request.user == tweet.user %} +Delete +{% endif %} +
    +

    {{tweet.pub_date}}

    +{% endblock content %} \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/index.html b/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/index.html new file mode 100644 index 00000000..154c69d8 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/templates/messages_app/index.html @@ -0,0 +1,87 @@ + +{% load static %} + + + + + + + + + {% block title %} + Home + {% endblock title %} + + + +
    + + {% block content %} + + {% if request.user.is_anonymous %} + {% else %} +
    + {% csrf_token %} + {{form}} + +
    + {% endif %} + + {% for tweet in tweets %} + +
    +
    {{tweet.user}}
    +

    {{tweet.text}}

    + + {% if request.user == tweet.user %} + 👍Likes: {{tweet.likes}} + 👎Dislikes: {{tweet.dislikes}} +

    {{tweet.pub_date}}

    + delete + edit + {% endif %} +
    +
    +
    + {% endfor %} + + + {% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/tests.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/urls.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/urls.py new file mode 100644 index 00000000..847abc5c --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/urls.py @@ -0,0 +1,27 @@ +"""messages_app URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views +app_name= 'messages' +urlpatterns = [ + path('', views.index, name='index'), + path('new/', views.new_tweet, name='new_tweet'), + path('delete/', views.del_tweet, name='del_tweet'), + path('like/', views.like, name='like'), + path('dislike/', views.dislike, name='dislike'), + path('detail/', views.detail, name='detail'), +] diff --git a/Code/matthew/django/lectures/09_twitter_clone/messages_app/views.py b/Code/matthew/django/lectures/09_twitter_clone/messages_app/views.py new file mode 100644 index 00000000..07422193 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/messages_app/views.py @@ -0,0 +1,75 @@ +from django.shortcuts import redirect, render +from django.http import HttpResponseRedirect +from django.urls import reverse +# importing forms +from messages_app.forms import NewTweetForm +# importing models +from .models import Tweet +from django.contrib.auth.decorators import login_required +from django.shortcuts import get_list_or_404 + +# Create your views here. +def index(request): + tweets= Tweet.objects.all().order_by('-pub_date')[:20] + form= NewTweetForm() + context= { + 'tweets': tweets, + 'form': form + } + return render(request, 'messages_app/index.html', context) + +@login_required +def new_tweet(request): + if request.method == "POST": + form= NewTweetForm(request.POST) + if form.is_valid(): + tweet= Tweet() + tweet.user= request.user + tweet.text= form.cleaned_data['text'] + tweet.save() + return HttpResponseRedirect(reverse('messages:index')) + +@login_required +def like(request, tweet_id): + if Tweet.objects.filter(id=tweet_id): + tweet= Tweet.objects.get(id=tweet_id) + tweet.likes += 1 + tweet.save() + return redirect(reverse('messages:index')) + +@login_required +def dislike(request, tweet_id): + if Tweet.objects.filter(id=tweet_id): + tweet= Tweet.objects.get(id=tweet_id) + tweet.dislikes += 1 + tweet.save() + return redirect(reverse('messages:index')) + + +@login_required +def del_tweet(request, tweet_id): +# helps reduce user error +# wont load a page that doesnt exits ex: delete/30000 + if Tweet.objects.filter(id=tweet_id, user=request.user).exists(): + tweet= Tweet.objects.get(id=tweet_id, user=request.user) + tweet.delete() + return redirect(reverse('messages:index')) + +# @login_required +def detail(request, tweet_id): + tweet= Tweet.objects.get(id=tweet_id) + return render(request, 'messages_app/detail.html',{ + 'tweet': tweet + }) + + + +# other delete options w/ no filtering + +# @login_required +# def del_tweet(request, tweet_id): +# # tweet= Tweet.objects.get(id=tweet_id, user=request.user) +# tweet= get_list_or_404(Tweet, id=tweet_id, user=request.user) +# tweet.delete() +# return redirect(reverse('messages:index')) + diff --git a/Code/matthew/django/lectures/09_twitter_clone/static/css/styles.css b/Code/matthew/django/lectures/09_twitter_clone/static/css/styles.css new file mode 100644 index 00000000..c35be2c3 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/static/css/styles.css @@ -0,0 +1,16 @@ +nav > div > h1 > a { + text-decoration: none; +} +body { + background-color: rgb(154, 178, 194); +} + +.tweet { + border: 1px solid black; + display: flex; + flex-direction: column; + width: 25em; +} +.tweet > a { + text-decoration: none; +} diff --git a/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/__init__.py b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/asgi.py b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/asgi.py new file mode 100644 index 00000000..9b68bb06 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for twiter_proj project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'twiter_proj.settings') + +application = get_asgi_application() diff --git a/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/settings.py b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/settings.py new file mode 100644 index 00000000..efd07999 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for twiter_proj project. + +Generated by 'django-admin startproject' using Django 4.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-9k#i#wc+kw&y16@w^oh))t@e34d57m$e!e_$@grapc%(dsb&&+' + +# 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', + # + 'users_app', + 'messages_app' +] + +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 = 'twiter_proj.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 = 'twiter_proj.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/Los_Angeles' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' +STATICFILES_DIRS= [str(BASE_DIR.joinpath('static'))] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/urls.py b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/urls.py new file mode 100644 index 00000000..b86dea31 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/urls.py @@ -0,0 +1,23 @@ +"""twiter_proj URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('users/', include('users_app.urls')), + path('', include('messages_app.urls')) +] diff --git a/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/wsgi.py b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/wsgi.py new file mode 100644 index 00000000..456a2a07 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/twiter_proj/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for twiter_proj 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/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'twiter_proj.settings') + +application = get_wsgi_application() diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/__init__.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/admin.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/apps.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/apps.py new file mode 100644 index 00000000..7200dd57 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users_app' diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/forms.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/forms.py new file mode 100644 index 00000000..88308881 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/forms.py @@ -0,0 +1,13 @@ +from django import forms + +class NewSignUpForm(forms.Form): + username= forms.CharField(label='Username', max_length=20) + # widget=forms.PasswordInput hides characters + password= forms.CharField(widget=forms.PasswordInput, label='Password', max_length=10) + first_name= forms.CharField(label='First Name', max_length=20) + last_name= forms.CharField(label='Last Name', max_length=20) + email= forms.EmailField(label='Email') + +class NewLoginForm(forms.Form): + username= forms.CharField(label='Username', max_length=20, ) + password= forms.CharField(widget=forms.PasswordInput, label='Password', max_length=10) \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/migrations/__init__.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/models.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/login.html b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/login.html new file mode 100644 index 00000000..2c231d48 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/login.html @@ -0,0 +1,14 @@ + +{% extends 'messages_app/index.html' %} +{% block content %} + + +

    Login

    +
    + {% csrf_token %} + {{form}} + +
    + +{% endblock content %} + \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/profile.html b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/profile.html new file mode 100644 index 00000000..663ee038 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/profile.html @@ -0,0 +1,26 @@ + +{% extends 'messages_app/index.html' %} + +{% block content %} +

    welcome {{request.user}}

    + +{% if not request.user.is_anonymous %} +{% for tweet in tweets %} +
    +

    {{tweet.text}}

    +{% if request.user == tweet.user %} +

    {{tweet.pub_date}}

    +{% endif %} + 👍Likes: {{tweet.likes}} + 👎Dislikes: {{tweet.dislikes}} +
    + edit + delete +
    + {% endfor %} + {% endif %} + + +{% endblock content %} + + \ No newline at end of file diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/signup.html b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/signup.html new file mode 100644 index 00000000..53958704 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/templates/users_app/signup.html @@ -0,0 +1,12 @@ + +{% extends 'messages_app/index.html' %} +{% block content %} + +

    Sign Up

    +
    + {% csrf_token %} + {{form}} + +
    + +{% endblock content %} diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/tests.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/urls.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/urls.py new file mode 100644 index 00000000..df0530d6 --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/urls.py @@ -0,0 +1,33 @@ + +"""twiter_app URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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.urls import path +from . import views +app_name= 'user' +urlpatterns = [ + # users/signup/ + path('signup/', views.signup, name='signup'), + # users/login/ + path('login/', views.user_login, name='login'), + # users/profile/ + path('profile/', views.profile, name='profile'), + # users/logout/ + path('logout/', views.user_logout, name='logout'), + + +] diff --git a/Code/matthew/django/lectures/09_twitter_clone/users_app/views.py b/Code/matthew/django/lectures/09_twitter_clone/users_app/views.py new file mode 100644 index 00000000..8aed703c --- /dev/null +++ b/Code/matthew/django/lectures/09_twitter_clone/users_app/views.py @@ -0,0 +1,71 @@ + + +from django.http import HttpResponseRedirect +from django.shortcuts import render +from .forms import * +from django.contrib.auth.models import User +from django.urls import reverse +from django.contrib.auth import authenticate, login, logout +# import @login_required +from django.contrib.auth.decorators import login_required +# import Tweet model from messages app +from messages_app.models import Tweet + +# Create your views here. +def signup(request): + + if request.method == "GET": + form= NewSignUpForm() + return render(request, 'users_app/signup.html', { + 'form':form + }) + elif request.method == "POST": + form= NewSignUpForm(request.POST) + if form.is_valid(): + # create a new user + user= User.objects.create_user( + username= form.cleaned_data['username'], + first_name= form.cleaned_data['first_name'], + last_name= form.cleaned_data['last_name'], + email= form.cleaned_data['email'], + password= form.cleaned_data['password'] + ) + user.save() + return HttpResponseRedirect(reverse('user:login')) + +def user_login(request): + if request.method == "GET": + return render(request, 'users_app/login.html', { + 'form': NewLoginForm() + }) + elif request.method == "POST": + form= NewLoginForm(request.POST) + if form.is_valid(): + password= form.cleaned_data['password'] + user= authenticate(request, username=form.cleaned_data['username'], password=password) + # authenticate returns either authenticated or none + if user is not None: + login(request, user) + return HttpResponseRedirect(reverse('user:profile')) + + # if the user has not been authenticated + else: + form.add_error('username', 'Invalid Credentials') + return render(request, 'users_app/login.html', { + 'form': form + }) + +def profile(request): + # filtering through all Tweet objects for ones the user posted + + tweets= Tweet.objects.all().filter(user=request.user) + + context= { + 'tweets': tweets + } + return render(request, 'users_app/profile.html', context) + +def user_logout(request): + logout(request) + return HttpResponseRedirect(reverse('user:login')) + diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/__init__.py b/Code/matthew/django/lectures/10_phone_book/contact_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/admin.py b/Code/matthew/django/lectures/10_phone_book/contact_app/admin.py new file mode 100644 index 00000000..c5e4edc2 --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Contact + +# Register your models here. +admin.site.register(Contact) \ No newline at end of file diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/apps.py b/Code/matthew/django/lectures/10_phone_book/contact_app/apps.py new file mode 100644 index 00000000..6bdff151 --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ContactAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'contact_app' diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/migrations/0001_initial.py b/Code/matthew/django/lectures/10_phone_book/contact_app/migrations/0001_initial.py new file mode 100644 index 00000000..b9aac097 --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.0.3 on 2022-04-14 01:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Contact', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=15)), + ('last_name', models.CharField(blank=True, max_length=15, null=True)), + ('phone_number', models.CharField(blank=True, max_length=15, null=True)), + ('email', models.EmailField(blank=True, max_length=254, null=True)), + ('address', models.CharField(blank=True, max_length=100, null=True)), + ('fav', models.BooleanField(default=False)), + ], + ), + ] diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/migrations/__init__.py b/Code/matthew/django/lectures/10_phone_book/contact_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/models.py b/Code/matthew/django/lectures/10_phone_book/contact_app/models.py new file mode 100644 index 00000000..a7a5fe8e --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/models.py @@ -0,0 +1,32 @@ +import email +from django.db import models + +# Create your models here. +class Contact(models.Model): + first_name= models.CharField(max_length=15) + # null=True and blank=True allows the field to be blank + last_name= models.CharField(max_length=15, null=True, blank=True) + phone_number= models.CharField(max_length=15, null=True, blank=True) + email= models.EmailField(null=True, blank=True) + address= models.CharField(max_length=100, null=True, blank=True) + fav= models.BooleanField(default=False) + + # def __str__(self): + # return f'{self.first_name} {self.last_name}' + + def full_name(self): + # if self.last_name has a value + if self.last_name: + return f'{self.first_name} {self.last_name}' + else: + return f'{self.first_name}' + + def favorite(self): + # if fav = True + if self.fav: + return 'a favorite' + else: + return "not fav" + + def __str__(self): + return f'{self.favorite()} -- {self.full_name()}' diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/all_contacts.html b/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/all_contacts.html new file mode 100644 index 00000000..edf0627e --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/all_contacts.html @@ -0,0 +1,40 @@ + +{% extends "contact_app/index.html" %} + +{% block title %} +All Contacts +{% endblock title %} + +{% block content %} + + +

    All Contacts

    + +
    + {% csrf_token %} + + + + + + + + +
    +
    + + {% for contact in contacts %} + +
    + {{contact.full_name}} is {{contact.favorite}} and contact.fav= {{contact.fav}} +
    + +
    + {% endfor %} + + +{% endblock content %} + \ No newline at end of file diff --git a/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/index.html b/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/index.html new file mode 100644 index 00000000..7dd05fb5 --- /dev/null +++ b/Code/matthew/django/lectures/10_phone_book/contact_app/templates/contact_app/index.html @@ -0,0 +1,41 @@ + +{% load static %} + + + + + + + + + + + {% block title %} + Contacts + + {% endblock title %} + +