From 114d79349448895ab6d91e1f1edbb6cc68424977 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 17:40:28 +0000 Subject: [PATCH 01/38] (MVP) : Setup the virtual environment, started the django app, done initial migrations, added todos --- Pipfile | 12 ++++ Pipfile.lock | 36 +++++++++++ djorg/__init__.py | 0 djorg/settings.py | 120 +++++++++++++++++++++++++++++++++++ djorg/urls.py | 21 ++++++ djorg/wsgi.py | 16 +++++ manage.py | 15 +++++ notes/__init__.py | 0 notes/admin.py | 3 + notes/apps.py | 5 ++ notes/migrations/__init__.py | 0 notes/models.py | 4 ++ notes/tests.py | 3 + notes/views.py | 3 + 14 files changed, 238 insertions(+) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 djorg/__init__.py create mode 100644 djorg/settings.py create mode 100644 djorg/urls.py create mode 100644 djorg/wsgi.py create mode 100755 manage.py create mode 100644 notes/__init__.py create mode 100644 notes/admin.py create mode 100644 notes/apps.py create mode 100644 notes/migrations/__init__.py create mode 100644 notes/models.py create mode 100644 notes/tests.py create mode 100644 notes/views.py diff --git a/Pipfile b/Pipfile new file mode 100644 index 000000000..967df8311 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +django = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 000000000..76307f4d0 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,36 @@ +{ + "_meta": { + "hash": { + "sha256": "627ef89f247ecee27e9ef0dabe116108d09c47abf171c900a8817befa64f9dd2" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "django": { + "hashes": [ + "sha256:068d51054083d06ceb32ce02b7203f1854256047a0d58682677dd4f81bceabd7", + "sha256:55409a056b27e6d1246f19ede41c6c610e4cab549c005b62cbeefabc6433356b" + ], + "index": "pypi", + "version": "==2.1.4" + }, + "pytz": { + "hashes": [ + "sha256:31cb35c89bd7d333cd32c5f278fca91b523b0834369e757f4c5641ea252236ca", + "sha256:8e0f8568c118d3077b46be7d654cc8167fa916092e28320cde048e54bfc9f1e6" + ], + "version": "==2018.7" + } + }, + "develop": {} +} diff --git a/djorg/__init__.py b/djorg/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/djorg/settings.py b/djorg/settings.py new file mode 100644 index 000000000..83565f8f5 --- /dev/null +++ b/djorg/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for djorg project. + +Generated by 'django-admin startproject' using Django 2.1.4. + +For more information on this file, see +https://docs.djangoproject.com/en/2.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.1/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'eaxzkm_)_tlmuurxj(=kztr3c2a2i0iwgde+_i-0^r#=^z*-lx' + +# 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', +] + +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 = 'djorg.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 = 'djorg.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.1/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/djorg/urls.py b/djorg/urls.py new file mode 100644 index 000000000..f4f667215 --- /dev/null +++ b/djorg/urls.py @@ -0,0 +1,21 @@ +"""djorg URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.1/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 + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/djorg/wsgi.py b/djorg/wsgi.py new file mode 100644 index 000000000..7bb827bb0 --- /dev/null +++ b/djorg/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for djorg project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djorg.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 000000000..ae1e94bc6 --- /dev/null +++ b/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == '__main__': + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djorg.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) diff --git a/notes/__init__.py b/notes/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/notes/admin.py b/notes/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/notes/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/notes/apps.py b/notes/apps.py new file mode 100644 index 000000000..b6155aca3 --- /dev/null +++ b/notes/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class NotesConfig(AppConfig): + name = 'notes' diff --git a/notes/migrations/__init__.py b/notes/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/notes/models.py b/notes/models.py new file mode 100644 index 000000000..b7d2b5662 --- /dev/null +++ b/notes/models.py @@ -0,0 +1,4 @@ +from django.db import models + +# TODO: Note class + diff --git a/notes/tests.py b/notes/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/notes/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notes/views.py b/notes/views.py new file mode 100644 index 000000000..91ea44a21 --- /dev/null +++ b/notes/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. From 18ba21d285d6a402d27ed89fb802ba3d7646f0b5 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 17:44:27 +0000 Subject: [PATCH 02/38] (MVP DAY 1) : wrote the initial notes class in the notes model file --- notes/models.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/notes/models.py b/notes/models.py index b7d2b5662..0a0a452b6 100644 --- a/notes/models.py +++ b/notes/models.py @@ -1,4 +1,10 @@ from django.db import models +from uuid import uuid4 # TODO: Note class +class Note(models.Model): + title = models.CharField(max_length=200) + content = models.TextField(blank=True) + pub_date = models.DateTimeField('date_published') + id = models.UUIDField(primary_key=True, default=uuid4, editable=False) \ No newline at end of file From f250ccaa215d0144b9ea15df410ef5c0e51dcc04 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 17:47:10 +0000 Subject: [PATCH 03/38] (MVP DAY1) : added the notes app config to the project config file --- djorg/settings.py | 1 + notes/models.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/djorg/settings.py b/djorg/settings.py index 83565f8f5..0c5b69479 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -31,6 +31,7 @@ # Application definition INSTALLED_APPS = [ + 'notes.apps.NotesConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/notes/models.py b/notes/models.py index 0a0a452b6..b5c4a3c56 100644 --- a/notes/models.py +++ b/notes/models.py @@ -4,7 +4,7 @@ # TODO: Note class class Note(models.Model): + id = models.UUIDField(primary_key=True, default=uuid4, editable=False) title = models.CharField(max_length=200) content = models.TextField(blank=True) - pub_date = models.DateTimeField('date_published') - id = models.UUIDField(primary_key=True, default=uuid4, editable=False) \ No newline at end of file + pub_date = models.DateTimeField('date_published') \ No newline at end of file From acc64dd69a71aec910c430b1460b7227cacd2398 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 17:49:00 +0000 Subject: [PATCH 04/38] (MVP DAY1) : migrated the notes table / model --- notes/migrations/0001_initial.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 notes/migrations/0001_initial.py diff --git a/notes/migrations/0001_initial.py b/notes/migrations/0001_initial.py new file mode 100644 index 000000000..26cc2a818 --- /dev/null +++ b/notes/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 2.1.4 on 2018-12-03 17:47 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('title', models.CharField(max_length=200)), + ('content', models.TextField(blank=True)), + ('pub_date', models.DateTimeField(verbose_name='date_published')), + ], + ), + ] From 6ea3524a3a550f964a3e7d036908d947df87870b Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 18:16:45 +0000 Subject: [PATCH 05/38] (MVP DAY 1) : added a str method to my notes class for a bit of debugging --- notes/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/notes/models.py b/notes/models.py index b5c4a3c56..993956d11 100644 --- a/notes/models.py +++ b/notes/models.py @@ -7,4 +7,7 @@ class Note(models.Model): id = models.UUIDField(primary_key=True, default=uuid4, editable=False) title = models.CharField(max_length=200) content = models.TextField(blank=True) - pub_date = models.DateTimeField('date_published') \ No newline at end of file + pub_date = models.DateTimeField('date_published') + + def __str__(self): + return F"Title: {self.title}, Content: {self.content}" \ No newline at end of file From 72ae9f14238c06ebffbe226186e7c0fd0a2296f0 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 18:26:11 +0000 Subject: [PATCH 06/38] (MVP DAY 1) : installed decouple, moved the key and debug variables to the env file after regenerating the secret key and set the changes in the config file --- Pipfile | 1 + Pipfile.lock | 9 ++++++++- djorg/settings.py | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Pipfile b/Pipfile index 967df8311..86e1565e3 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,7 @@ verify_ssl = true [packages] django = "*" +python-decouple = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index 76307f4d0..666b71ab8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "627ef89f247ecee27e9ef0dabe116108d09c47abf171c900a8817befa64f9dd2" + "sha256": "0bb3b331760d823d01166b55e53cd9c02ded57f108f27a6e4dd739e3ed291a1f" }, "pipfile-spec": 6, "requires": { @@ -24,6 +24,13 @@ "index": "pypi", "version": "==2.1.4" }, + "python-decouple": { + "hashes": [ + "sha256:1317df14b43efee4337a4aa02914bf004f010cd56d6c4bd894e6474ec8c4fe2d" + ], + "index": "pypi", + "version": "==3.1" + }, "pytz": { "hashes": [ "sha256:31cb35c89bd7d333cd32c5f278fca91b523b0834369e757f4c5641ea252236ca", diff --git a/djorg/settings.py b/djorg/settings.py index 0c5b69479..726b576eb 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -11,6 +11,7 @@ """ import os +from decouple import config # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -20,10 +21,10 @@ # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'eaxzkm_)_tlmuurxj(=kztr3c2a2i0iwgde+_i-0^r#=^z*-lx' +SECRET_KEY = config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = config('DEBUG', cast=bool) ALLOWED_HOSTS = [] From 59b5c8d736a8ef5bc0ee3a8e3c491c7b5d919e3c Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 18:28:53 +0000 Subject: [PATCH 07/38] (MVP DAY 1) : completed day 1 --- djorg/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djorg/settings.py b/djorg/settings.py index 726b576eb..91d90789d 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -9,7 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ - +# day 1 completeed MVP import os from decouple import config From 01462be2cacdd12aa143c00e4fc50f340ca6aae7 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 18:36:40 +0000 Subject: [PATCH 08/38] (MVP DAY 2) added created_at , last_modified fileds to the Note model. then made migrations and mograted --- notes/admin.py | 4 +++- notes/migrations/0002_auto_20181203_1835.py | 25 +++++++++++++++++++++ notes/models.py | 2 ++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 notes/migrations/0002_auto_20181203_1835.py diff --git a/notes/admin.py b/notes/admin.py index 8c38f3f3d..c9145fb1c 100644 --- a/notes/admin.py +++ b/notes/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Note -# Register your models here. +# Register Note Model +admin.site.register(Note) \ No newline at end of file diff --git a/notes/migrations/0002_auto_20181203_1835.py b/notes/migrations/0002_auto_20181203_1835.py new file mode 100644 index 000000000..c12e7f8b8 --- /dev/null +++ b/notes/migrations/0002_auto_20181203_1835.py @@ -0,0 +1,25 @@ +# Generated by Django 2.1.4 on 2018-12-03 18:35 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('notes', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='created_at', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AddField( + model_name='note', + name='last_modified', + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/notes/models.py b/notes/models.py index 993956d11..0ebf3e136 100644 --- a/notes/models.py +++ b/notes/models.py @@ -8,6 +8,8 @@ class Note(models.Model): title = models.CharField(max_length=200) content = models.TextField(blank=True) pub_date = models.DateTimeField('date_published') + created_at = models.DateTimeField(auto_now_add=True) + last_modified = models.DateTimeField(auto_now=True) def __str__(self): return F"Title: {self.title}, Content: {self.content}" \ No newline at end of file From 76022f71247c6a85780d8970ec599322f66794c3 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 18:39:34 +0000 Subject: [PATCH 09/38] (MVP DAY 2) : added the PersonalNote subclass to the models file --- notes/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/notes/models.py b/notes/models.py index 0ebf3e136..58369bce8 100644 --- a/notes/models.py +++ b/notes/models.py @@ -1,5 +1,6 @@ from django.db import models from uuid import uuid4 +from django.contrib.auth.models import User # TODO: Note class @@ -12,4 +13,7 @@ class Note(models.Model): last_modified = models.DateTimeField(auto_now=True) def __str__(self): - return F"Title: {self.title}, Content: {self.content}" \ No newline at end of file + return F"Title: {self.title}, Content: {self.content}" + +class PersonalNote(Note): + user = models.ForeignKey(User, on_delete=models.CASCADE) \ No newline at end of file From ad705a7c9cdd8f4a464904c763828c6a64710125 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 20:18:57 +0000 Subject: [PATCH 10/38] (MVP DAY 2) : MVP day 2 completed --- notes/admin.py | 9 +++++++-- notes/migrations/0003_personalnote.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 notes/migrations/0003_personalnote.py diff --git a/notes/admin.py b/notes/admin.py index c9145fb1c..083ad287f 100644 --- a/notes/admin.py +++ b/notes/admin.py @@ -1,5 +1,10 @@ from django.contrib import admin -from .models import Note +from .models import Note, PersonalNote # Register Note Model -admin.site.register(Note) \ No newline at end of file +admin.site.register(Note) + +# Register PersonalNote +admin.site.register(PersonalNote) + +# MVP day 2 complete \ No newline at end of file diff --git a/notes/migrations/0003_personalnote.py b/notes/migrations/0003_personalnote.py new file mode 100644 index 000000000..a0755a479 --- /dev/null +++ b/notes/migrations/0003_personalnote.py @@ -0,0 +1,24 @@ +# Generated by Django 2.1.4 on 2018-12-03 18:41 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('notes', '0002_auto_20181203_1835'), + ] + + operations = [ + migrations.CreateModel( + name='PersonalNote', + fields=[ + ('note_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='notes.Note')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + bases=('notes.note',), + ), + ] From 408b0ebcfcc65d33b720a1599d75687eff5e2723 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 20:21:46 +0000 Subject: [PATCH 11/38] (MVP DAY 3) : installed rest framework and added it to the projects apps. also added thr permission classes --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- djorg/settings.py | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Pipfile b/Pipfile index 86e1565e3..f9bfc5b64 100644 --- a/Pipfile +++ b/Pipfile @@ -8,6 +8,7 @@ verify_ssl = true [packages] django = "*" python-decouple = "*" +djangorestframework = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index 666b71ab8..74432aa3b 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "0bb3b331760d823d01166b55e53cd9c02ded57f108f27a6e4dd739e3ed291a1f" + "sha256": "f60c55d31de62ba3f850d6c16dc83ccc1a0d6fe44e337b58447a45133fdc5bb9" }, "pipfile-spec": 6, "requires": { @@ -24,6 +24,14 @@ "index": "pypi", "version": "==2.1.4" }, + "djangorestframework": { + "hashes": [ + "sha256:607865b0bb1598b153793892101d881466bd5a991de12bd6229abb18b1c86136", + "sha256:63f76cbe1e7d12b94c357d7e54401103b2e52aef0f7c1650d6c820ad708776e5" + ], + "index": "pypi", + "version": "==3.9.0" + }, "python-decouple": { "hashes": [ "sha256:1317df14b43efee4337a4aa02914bf004f010cd56d6c4bd894e6474ec8c4fe2d" diff --git a/djorg/settings.py b/djorg/settings.py index 91d90789d..5d4387631 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -39,8 +39,15 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', ] +REST_FRAMEWORK = { + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly', + ] +} + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', From c399d3939dbc5d65287be67c6d9a6aa61cbe2ace Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 20:27:41 +0000 Subject: [PATCH 12/38] (MVP DAY 3) : created an api.py file and added a serializer class. a nested meta class and a viewset class --- notes/api.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 notes/api.py diff --git a/notes/api.py b/notes/api.py new file mode 100644 index 000000000..6f5d1b0dc --- /dev/null +++ b/notes/api.py @@ -0,0 +1,11 @@ +from rest_framework import serializers, viewsets +from .models import PersonalNote + +serializer_class = PersonalNoteSerializer +queryset = PersonalNote.objects.all() + +class PersonalNoteSerializer(serializers.HyperlinkedModelSerializer): + # Inner class nested inside PersonalNoteSerializer + class Meta: + model = PersonalNote + fields = ('title', 'content') \ No newline at end of file From 938839870d799572f1e805f81826a1f5996dac20 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 21:10:58 +0000 Subject: [PATCH 13/38] (MVP DAY 3) : tested the api get method and wrote and tested the create method --- djorg/urls.py | 8 ++++++- notes/api.py | 21 ++++++++++++----- notes/migrations/0001_initial.py | 16 +++++++++++-- notes/migrations/0002_auto_20181203_1835.py | 25 --------------------- notes/migrations/0003_personalnote.py | 24 -------------------- notes/models.py | 3 +-- 6 files changed, 37 insertions(+), 60 deletions(-) delete mode 100644 notes/migrations/0002_auto_20181203_1835.py delete mode 100644 notes/migrations/0003_personalnote.py diff --git a/djorg/urls.py b/djorg/urls.py index f4f667215..dfaaadddf 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -14,8 +14,14 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include +from rest_framework import routers +from notes.api import PersonalNoteViewSet + +router = routers.DefaultRouter() +router.register(r'notes', PersonalNoteViewSet) urlpatterns = [ path('admin/', admin.site.urls), + path('api/', include(router.urls)), ] diff --git a/notes/api.py b/notes/api.py index 6f5d1b0dc..75c5727aa 100644 --- a/notes/api.py +++ b/notes/api.py @@ -1,11 +1,20 @@ from rest_framework import serializers, viewsets -from .models import PersonalNote - -serializer_class = PersonalNoteSerializer -queryset = PersonalNote.objects.all() +from .models import PersonalNote, Note class PersonalNoteSerializer(serializers.HyperlinkedModelSerializer): - # Inner class nested inside PersonalNoteSerializer + class Meta: model = PersonalNote - fields = ('title', 'content') \ No newline at end of file + fields = ('title', 'content') + + def create(self, validated_data): + user = self.context['request'].user + note = PersonalNote.objects.create(user=user, **validated_data) + return note + +class PersonalNoteViewSet(viewsets.ModelViewSet): + serializer_class = PersonalNoteSerializer + queryset = PersonalNote.objects.all() + + + \ No newline at end of file diff --git a/notes/migrations/0001_initial.py b/notes/migrations/0001_initial.py index 26cc2a818..72aa88979 100644 --- a/notes/migrations/0001_initial.py +++ b/notes/migrations/0001_initial.py @@ -1,6 +1,8 @@ -# Generated by Django 2.1.4 on 2018-12-03 17:47 +# Generated by Django 2.1.4 on 2018-12-03 21:02 +from django.conf import settings from django.db import migrations, models +import django.db.models.deletion import uuid @@ -9,6 +11,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -18,7 +21,16 @@ class Migration(migrations.Migration): ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), ('title', models.CharField(max_length=200)), ('content', models.TextField(blank=True)), - ('pub_date', models.DateTimeField(verbose_name='date_published')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('last_modified', models.DateTimeField(auto_now=True)), ], ), + migrations.CreateModel( + name='PersonalNote', + fields=[ + ('note_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='notes.Note')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + bases=('notes.note',), + ), ] diff --git a/notes/migrations/0002_auto_20181203_1835.py b/notes/migrations/0002_auto_20181203_1835.py deleted file mode 100644 index c12e7f8b8..000000000 --- a/notes/migrations/0002_auto_20181203_1835.py +++ /dev/null @@ -1,25 +0,0 @@ -# Generated by Django 2.1.4 on 2018-12-03 18:35 - -from django.db import migrations, models -import django.utils.timezone - - -class Migration(migrations.Migration): - - dependencies = [ - ('notes', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='note', - name='created_at', - field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), - preserve_default=False, - ), - migrations.AddField( - model_name='note', - name='last_modified', - field=models.DateTimeField(auto_now=True), - ), - ] diff --git a/notes/migrations/0003_personalnote.py b/notes/migrations/0003_personalnote.py deleted file mode 100644 index a0755a479..000000000 --- a/notes/migrations/0003_personalnote.py +++ /dev/null @@ -1,24 +0,0 @@ -# Generated by Django 2.1.4 on 2018-12-03 18:41 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('notes', '0002_auto_20181203_1835'), - ] - - operations = [ - migrations.CreateModel( - name='PersonalNote', - fields=[ - ('note_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='notes.Note')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - bases=('notes.note',), - ), - ] diff --git a/notes/models.py b/notes/models.py index 58369bce8..3dde09acb 100644 --- a/notes/models.py +++ b/notes/models.py @@ -8,7 +8,6 @@ class Note(models.Model): id = models.UUIDField(primary_key=True, default=uuid4, editable=False) title = models.CharField(max_length=200) content = models.TextField(blank=True) - pub_date = models.DateTimeField('date_published') created_at = models.DateTimeField(auto_now_add=True) last_modified = models.DateTimeField(auto_now=True) @@ -16,4 +15,4 @@ def __str__(self): return F"Title: {self.title}, Content: {self.content}" class PersonalNote(Note): - user = models.ForeignKey(User, on_delete=models.CASCADE) \ No newline at end of file + user = models.ForeignKey(User, on_delete=models.CASCADE) From b0c7508fc198a90b8a75f6ad102600edc4d0dc44 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 21:22:52 +0000 Subject: [PATCH 14/38] (MVP DAY 3) : added cors to django and set it to allow all origins : MVP complete for day 3 --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- djorg/settings.py | 4 ++++ notes/api.py | 8 +++++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index f9bfc5b64..ce61f951a 100644 --- a/Pipfile +++ b/Pipfile @@ -9,6 +9,7 @@ verify_ssl = true django = "*" python-decouple = "*" djangorestframework = "*" +django-cors-headers = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index 74432aa3b..f8bed3126 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "f60c55d31de62ba3f850d6c16dc83ccc1a0d6fe44e337b58447a45133fdc5bb9" + "sha256": "d324f6a35acb89a6c6a5b432a6f79d2ba7421be2cef72d2922e5b71fdbcfe390" }, "pipfile-spec": 6, "requires": { @@ -24,6 +24,14 @@ "index": "pypi", "version": "==2.1.4" }, + "django-cors-headers": { + "hashes": [ + "sha256:5545009c9b233ea7e70da7dbab7cb1c12afa01279895086f98ec243d7eab46fa", + "sha256:c4c2ee97139d18541a1be7d96fe337d1694623816d83f53cb7c00da9b94acae1" + ], + "index": "pypi", + "version": "==2.4.0" + }, "djangorestframework": { "hashes": [ "sha256:607865b0bb1598b153793892101d881466bd5a991de12bd6229abb18b1c86136", diff --git a/djorg/settings.py b/djorg/settings.py index 5d4387631..ba6bbf6e8 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -40,6 +40,7 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'corsheaders', ] REST_FRAMEWORK = { @@ -56,9 +57,12 @@ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', ] ROOT_URLCONF = 'djorg.urls' +CORS_ORIGIN_ALLOW_ALL = True TEMPLATES = [ { diff --git a/notes/api.py b/notes/api.py index 75c5727aa..81b8393ba 100644 --- a/notes/api.py +++ b/notes/api.py @@ -14,7 +14,13 @@ def create(self, validated_data): class PersonalNoteViewSet(viewsets.ModelViewSet): serializer_class = PersonalNoteSerializer - queryset = PersonalNote.objects.all() + queryset = PersonalNote.objects.none() + def get_queryset(self): + user = self.request.user + if user.is_anonymous: + return PersonalNote.objects.none() + else: + return PersonalNote.objects.filter(user=user) \ No newline at end of file From c23785ef0462d1509e7348be2b7b87107c1bc151 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 23:03:26 +0000 Subject: [PATCH 15/38] (MVP DAY 4) : set up token authentication --- djorg/settings.py | 17 ++++++++++++----- djorg/urls.py | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/djorg/settings.py b/djorg/settings.py index ba6bbf6e8..26f5126e5 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -40,14 +40,10 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'rest_framework.authtoken', 'corsheaders', ] -REST_FRAMEWORK = { - 'DEFAULT_PERMISSION_CLASSES': [ - 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly', - ] -} MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', @@ -61,6 +57,17 @@ 'django.middleware.common.CommonMiddleware', ] +REST_FRAMEWORK = { + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly', + ], + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.BasicAuthentication', + 'rest_framework.authentication.SessionAuthentication', + 'rest_framework.authentication.TokenAuthentication', + ), +} + ROOT_URLCONF = 'djorg.urls' CORS_ORIGIN_ALLOW_ALL = True diff --git a/djorg/urls.py b/djorg/urls.py index dfaaadddf..33362487a 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -14,7 +14,8 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path, include +from django.urls import path, include, re_path +from rest_framework.authtoken import views from rest_framework import routers from notes.api import PersonalNoteViewSet From e5472656f2a1a49fc5d6529da5bec6127c619ccc Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 23:11:59 +0000 Subject: [PATCH 16/38] (MVP DAY 4) : Set up auth routes --- djorg/urls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/djorg/urls.py b/djorg/urls.py index 33362487a..c987c330d 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -25,4 +25,5 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/', include(router.urls)), + re_path(r'^api-token-auth/', views.obtain_auth_token), ] From da4e831a69813358ad5dc8575a3a2310dbe5f946 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Mon, 3 Dec 2018 23:32:12 +0000 Subject: [PATCH 17/38] (MVP DAY 4) : done for now done the mvp of day 1, 2, 3, 4 on day 1 as some stretch --- djorg/urls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/djorg/urls.py b/djorg/urls.py index c987c330d..891a68b79 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -13,6 +13,7 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +# day 4 mvp completed will work on stretch tomorrow from django.contrib import admin from django.urls import path, include, re_path from rest_framework.authtoken import views From 76418e7492a4bee5c7e7248c658f141397102159 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 00:15:19 +0000 Subject: [PATCH 18/38] (STRETCHING) : creating a polls app --- polls/__init__.py | 0 polls/admin.py | 3 +++ polls/apps.py | 5 +++++ polls/migrations/__init__.py | 0 polls/models.py | 3 +++ polls/tests.py | 3 +++ polls/views.py | 6 ++++++ 7 files changed, 20 insertions(+) create mode 100644 polls/__init__.py create mode 100644 polls/admin.py create mode 100644 polls/apps.py create mode 100644 polls/migrations/__init__.py create mode 100644 polls/models.py create mode 100644 polls/tests.py create mode 100644 polls/views.py diff --git a/polls/__init__.py b/polls/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/polls/admin.py b/polls/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/polls/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/polls/apps.py b/polls/apps.py new file mode 100644 index 000000000..d0f109e60 --- /dev/null +++ b/polls/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + name = 'polls' diff --git a/polls/migrations/__init__.py b/polls/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/polls/models.py b/polls/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/polls/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/polls/tests.py b/polls/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/polls/views.py b/polls/views.py new file mode 100644 index 000000000..dc0049bf2 --- /dev/null +++ b/polls/views.py @@ -0,0 +1,6 @@ +from django.shortcuts import render +from django.http import HttpResponse + +# Index View +def index(request): + return HttpResponse("This is the index route of the polls app") From c392bf77c47caf0f039065bcf6d261c0a6e44f9e Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 00:16:35 +0000 Subject: [PATCH 19/38] (STRETCHING) : added urls.py and initial index path to the polls app --- polls/urls.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 polls/urls.py diff --git a/polls/urls.py b/polls/urls.py new file mode 100644 index 000000000..3ef24d971 --- /dev/null +++ b/polls/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.index, name='index'), +] \ No newline at end of file From 1c567f906506bec41526b288caaac65425e4e670 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 01:10:55 +0000 Subject: [PATCH 20/38] (STRETCHING) : added the url patterns for the polls app --- djorg/urls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/djorg/urls.py b/djorg/urls.py index 891a68b79..e77341aa6 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -24,6 +24,7 @@ router.register(r'notes', PersonalNoteViewSet) urlpatterns = [ + path('polls/', include('polls.urls')), path('admin/', admin.site.urls), path('api/', include(router.urls)), re_path(r'^api-token-auth/', views.obtain_auth_token), From 9a20bac5e95e6dbebab5fb940ab85c055c2f6920 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:24:27 +0000 Subject: [PATCH 21/38] (STRETCHING) : adding ___str___ methods to the models in the polls app --- djorg/settings.py | 1 + polls/models.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/djorg/settings.py b/djorg/settings.py index 26f5126e5..ca04e0139 100644 --- a/djorg/settings.py +++ b/djorg/settings.py @@ -33,6 +33,7 @@ INSTALLED_APPS = [ 'notes.apps.NotesConfig', + 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/polls/models.py b/polls/models.py index 71a836239..b8128e904 100644 --- a/polls/models.py +++ b/polls/models.py @@ -1,3 +1,21 @@ from django.db import models -# Create your models here. +# Question Model + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date_published') + + def __str__(self): + return self.question_text + + +# Choice Model + +class Choice(models.Model): + question = models.ForeighKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __str__(self): + return self.choice_text \ No newline at end of file From 03007a7964e1cc4bb559f6fb253db1f6e49ed512 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:27:00 +0000 Subject: [PATCH 22/38] (STRETCHING) : added was_published_recently method to the Question class --- polls/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/polls/models.py b/polls/models.py index b8128e904..f47c3db83 100644 --- a/polls/models.py +++ b/polls/models.py @@ -6,6 +6,9 @@ class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date_published') + def was_published_recently(self): + return self.pub_date >= timezone.now() - datetime.timedelta(days=1) + def __str__(self): return self.question_text From 573d6b32068c981bbfcc3bf4b9bcdcbfbb3ae533 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:29:22 +0000 Subject: [PATCH 23/38] (STRETCHING) registered Question and Choice tables in the admin interface --- polls/admin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/polls/admin.py b/polls/admin.py index 8c38f3f3d..565e8ecad 100644 --- a/polls/admin.py +++ b/polls/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Question, Choice -# Register your models here. +admin.site.register(Question) +admin.site.register(Choice) From 1379cef3afb18d3b9e94d06be893c62c963cfdb6 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:34:29 +0000 Subject: [PATCH 24/38] (STRETCHING) DAY 2 : added templates to the polls app for index view --- polls/templates/polls/index.html | 9 +++++++++ polls/urls.py | 8 +++++++- polls/views.py | 8 ++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 polls/templates/polls/index.html diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html new file mode 100644 index 000000000..79823ba63 --- /dev/null +++ b/polls/templates/polls/index.html @@ -0,0 +1,9 @@ +{% if latest_question_list %} + +{% else %} +

No polls are available.

+{% endif %} \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index 3ef24d971..1b27c8a13 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -3,5 +3,11 @@ from . import views urlpatterns = [ + # index route path('', views.index, name='index'), -] \ No newline at end of file + # detail route + path('/', views.detail, name='detail'), + # results route + path('/results/', views.results, name='results'), + # vote route + path('/vote/', views.vote, name='vote'), \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index dc0049bf2..96beebf96 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,6 +1,10 @@ from django.shortcuts import render from django.http import HttpResponse -# Index View +from .models import Question + +# index view def index(request): - return HttpResponse("This is the index route of the polls app") + latest_question_list = Question.objects.order_by('-pub_date')[:5] + context = {'latest_question_list': latest_question_list} + return render(request, 'polls/index.html', context) \ No newline at end of file From 16df01543032b94b0aca17eb6a600e7f524fca6a Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:37:01 +0000 Subject: [PATCH 25/38] (POLLS APP) : refactored index view --- polls/views.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/polls/views.py b/polls/views.py index 96beebf96..36545ac12 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,10 +1,14 @@ from django.shortcuts import render from django.http import HttpResponse +from django.template import loader from .models import Question -# index view +# Index View def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] - context = {'latest_question_list': latest_question_list} - return render(request, 'polls/index.html', context) \ No newline at end of file + template = loader.get_template('polls/index.html') + context = { + 'latest_question_list': latest_question_list, + } + return HttpResponse(template.render(context, request)) \ No newline at end of file From c63e633b245ce7cb90197886cc9869de0839184b Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:38:23 +0000 Subject: [PATCH 26/38] (POLLS APP) : re added detail view with template --- polls/templates/polls/detail.html | 1 + polls/views.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 polls/templates/polls/detail.html diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html new file mode 100644 index 000000000..3bbdd2ab0 --- /dev/null +++ b/polls/templates/polls/detail.html @@ -0,0 +1 @@ +{{ question }} \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 36545ac12..95bca6b0c 100644 --- a/polls/views.py +++ b/polls/views.py @@ -11,4 +11,12 @@ def index(request): context = { 'latest_question_list': latest_question_list, } - return HttpResponse(template.render(context, request)) \ No newline at end of file + return HttpResponse(template.render(context, request)) + +# Detail View +def detail(request, question_id): + try: + question = Question.objects.get(pk=question_id) + except Question.DoesNotExist: + raise Http404("Question does not exist") + return render(request, 'polls/detail.html', {'question': question}) \ No newline at end of file From 0d36adc54ed9394deb1ecd94f8c5a3a494ab977f Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:39:43 +0000 Subject: [PATCH 27/38] (POLLS APP) : refactored the details teplate --- polls/templates/polls/detail.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index 3bbdd2ab0..4e1bd67ef 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1 +1,6 @@ -{{ question }} \ No newline at end of file +

{{ question.question_text }}

+
    +{% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }}
  • +{% endfor %} +
\ No newline at end of file From 8bf767b5e5a72da4026dcc6665e5a9d190f70861 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:40:59 +0000 Subject: [PATCH 28/38] (POLLS APP) : refacoted the index template to be more universal using variables --- polls/templates/polls/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index 79823ba63..0d002dd35 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -1,7 +1,7 @@ {% if latest_question_list %} {% else %} From b1c4e57619feb75e85089d57582f134cb9eb696d Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:43:32 +0000 Subject: [PATCH 29/38] (POLLS APP) : added namespace polls to the polls application --- polls/templates/polls/index.html | 2 +- polls/urls.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index 0d002dd35..cc41400be 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -1,7 +1,7 @@ {% if latest_question_list %} {% else %} diff --git a/polls/urls.py b/polls/urls.py index 1b27c8a13..dd799f272 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -2,6 +2,8 @@ from . import views +app_name = 'polls' + urlpatterns = [ # index route path('', views.index, name='index'), From 2e3579214d012310c3301c194ca79007a12019d8 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:44:44 +0000 Subject: [PATCH 30/38] (POLLS APP) : added a simple form to the retail template --- polls/templates/polls/detail.html | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index 4e1bd67ef..a4e063554 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1,6 +1,12 @@

{{ question.question_text }}

-
    + +{% if error_message %}

    {{ error_message }}

    {% endif %} + +
    +{% csrf_token %} {% for choice in question.choice_set.all %} -
  • {{ choice.choice_text }}
  • + +
    {% endfor %} -
\ No newline at end of file + + \ No newline at end of file From 93c0185f35d69a643987aab7d032985cd7c8bfaf Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:48:09 +0000 Subject: [PATCH 31/38] (POLLS APP) : added results view --- polls/views.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/polls/views.py b/polls/views.py index 95bca6b0c..2c8230df1 100644 --- a/polls/views.py +++ b/polls/views.py @@ -19,4 +19,21 @@ def detail(request, question_id): question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") - return render(request, 'polls/detail.html', {'question': question}) \ No newline at end of file + return render(request, 'polls/detail.html', {'question': question}) + +# Vote View +def vote(request, question_id): + question = get_object_or_404(Question, pk=question_id) + try: + selected_choice = question.choice_set.get(pk=request.POST['choice']) + except (KeyError, Choice.DoesNotExist): + # Redisplay the question voting form. + return render(request, 'polls/detail.html', { + 'question': question, + 'error_message': "You didn't select a choice.", + }) + else: + selected_choice.votes += 1 + selected_choice.save() + + return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) \ No newline at end of file From 524420e552719e2fdc6543f0e85142b43840cec4 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 12:49:33 +0000 Subject: [PATCH 32/38] (POLLS APP) : added template for results view --- polls/templates/polls/results.html | 9 +++++++++ polls/views.py | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 polls/templates/polls/results.html diff --git a/polls/templates/polls/results.html b/polls/templates/polls/results.html new file mode 100644 index 000000000..33b67b224 --- /dev/null +++ b/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ question.question_text }}

+ +
    +{% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • +{% endfor %} +
+ +Vote again? \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index 2c8230df1..ec66067fb 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,4 +1,4 @@ -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render from django.http import HttpResponse from django.template import loader @@ -36,4 +36,8 @@ def vote(request, question_id): selected_choice.votes += 1 selected_choice.save() - return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) \ No newline at end of file + return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) + + def results(request, question_id): + question = get_object_or_404(Question, pk=question_id) + return render(request, 'polls/results.html', {'question': question}) \ No newline at end of file From f47646a3251ce9526999df7a26ef4ca5cdf81b38 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 14:22:58 +0000 Subject: [PATCH 33/38] (POLLS API) added RESTful GET and POST to the Questions --- djorg/urls.py | 2 ++ polls/api.py | 19 +++++++++++++++++ polls/migrations/0001_initial.py | 36 ++++++++++++++++++++++++++++++++ polls/models.py | 12 +++++------ polls/urls.py | 3 ++- polls/views.py | 6 +++--- 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 polls/api.py create mode 100644 polls/migrations/0001_initial.py diff --git a/djorg/urls.py b/djorg/urls.py index e77341aa6..4066c9749 100644 --- a/djorg/urls.py +++ b/djorg/urls.py @@ -19,9 +19,11 @@ from rest_framework.authtoken import views from rest_framework import routers from notes.api import PersonalNoteViewSet +from polls.api import QuestionViewSet router = routers.DefaultRouter() router.register(r'notes', PersonalNoteViewSet) +router.register(r'questions', QuestionViewSet) urlpatterns = [ path('polls/', include('polls.urls')), diff --git a/polls/api.py b/polls/api.py new file mode 100644 index 000000000..7a7c4b53d --- /dev/null +++ b/polls/api.py @@ -0,0 +1,19 @@ +from rest_framework import serializers, viewsets +from .models import Question + +class QuestionSerializer(serializers.HyperlinkedModelSerializer): + + class Meta: + model = Question + fields = ('question_text', 'pub_date' ) + + def create(self, validated_data): + question = Question.objects.create(**validated_data) + return question + +class QuestionViewSet(viewsets.ModelViewSet): + serializer_class = QuestionSerializer + queryset = Question.objects.all() + + def get_queryset(self): + return Question.objects.all() \ No newline at end of file diff --git a/polls/migrations/0001_initial.py b/polls/migrations/0001_initial.py new file mode 100644 index 000000000..3084ed184 --- /dev/null +++ b/polls/migrations/0001_initial.py @@ -0,0 +1,36 @@ +# Generated by Django 2.1.4 on 2018-12-04 13:41 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ], + ), + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.AutoField(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.AddField( + model_name='choice', + name='question', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question'), + ), + ] diff --git a/polls/models.py b/polls/models.py index f47c3db83..96f5e4cc8 100644 --- a/polls/models.py +++ b/polls/models.py @@ -1,5 +1,5 @@ from django.db import models - +from django.contrib.auth.models import User # Question Model class Question(models.Model): @@ -9,16 +9,16 @@ class Question(models.Model): def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) - def __str__(self): - return self.question_text + # def __str__(self): + # return self.question_text # Choice Model class Choice(models.Model): - question = models.ForeighKey(Question, on_delete=models.CASCADE) + 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 \ No newline at end of file + # def __str__(self): + # return self.choice_text \ No newline at end of file diff --git a/polls/urls.py b/polls/urls.py index dd799f272..d89344df2 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -12,4 +12,5 @@ # results route path('/results/', views.results, name='results'), # vote route - path('/vote/', views.vote, name='vote'), \ No newline at end of file + path('/vote/', views.vote, name='vote'), +] \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index ec66067fb..d205ede18 100644 --- a/polls/views.py +++ b/polls/views.py @@ -38,6 +38,6 @@ def vote(request, question_id): return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) - def results(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, 'polls/results.html', {'question': question}) \ No newline at end of file +def results(request, question_id): + question = get_object_or_404(Question, pk=question_id) + return render(request, 'polls/results.html', {'question': question}) \ No newline at end of file From 3fdc895dee0b4315efe438b6953501c58d414c3e Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 20:17:15 +0000 Subject: [PATCH 34/38] {POLLS APP FRONT END) : got the votes button functioning and the vote view working also --- polls/models.py | 8 ++++---- polls/views.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/polls/models.py b/polls/models.py index 96f5e4cc8..393566148 100644 --- a/polls/models.py +++ b/polls/models.py @@ -9,8 +9,8 @@ class Question(models.Model): def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) - # def __str__(self): - # return self.question_text + def __str__(self): + return self.question_text # Choice Model @@ -20,5 +20,5 @@ class Choice(models.Model): choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) - # def __str__(self): - # return self.choice_text \ No newline at end of file + def __str__(self): + return self.choice_text \ No newline at end of file diff --git a/polls/views.py b/polls/views.py index d205ede18..b17e4eb8b 100644 --- a/polls/views.py +++ b/polls/views.py @@ -1,8 +1,9 @@ from django.shortcuts import get_object_or_404, render -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.template import loader +from django.urls import reverse -from .models import Question +from .models import Question, Choice # Index View def index(request): From 9c3e725c59ef6658a26ce9e7d421b999e20e1f53 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 21:08:26 +0000 Subject: [PATCH 35/38] (POLLS APP STYLES) : added a static css to the templates TODO: style the index page --- polls/static/polls/styles.css | 30 ++++++++++++++++++++++++++++++ polls/templates/polls/index.html | 10 ++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 polls/static/polls/styles.css diff --git a/polls/static/polls/styles.css b/polls/static/polls/styles.css new file mode 100644 index 000000000..258e03632 --- /dev/null +++ b/polls/static/polls/styles.css @@ -0,0 +1,30 @@ +body { + font-family: sans-serif; +} + +.container { + width: 800px; + margin: 0 auto; +} + +li a, a:visited { + font-size: 30px; + color: #fff; + background: rgba(0, 0, 0, 0.5); + display: inline-block; + text-align: center; + margin: 10px; + padding: 20px 0; + width: 400px; + border: 2px solid #333; + border-radius: 17px; + text-decoration: none; +} + +ul { + list-style: none; +} + +li { + +} \ No newline at end of file diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index cc41400be..c96336960 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -1,9 +1,15 @@ +{% load static %} + +

Questions

+ +
{% if latest_question_list %} -
    + {% else %}

    No polls are available.

    -{% endif %} \ No newline at end of file +{% endif %} +
\ No newline at end of file From 7100d8dfeb73006f293462563d3fb6840c5bfb28 Mon Sep 17 00:00:00 2001 From: Tom Tarpey Date: Tue, 4 Dec 2018 21:18:33 +0000 Subject: [PATCH 36/38] (POLL APP STYLES) : added some styles to the details and results pages still working on them --- polls/static/polls/styles.css | 16 +++++++++++++++- polls/templates/polls/detail.html | 3 +++ polls/templates/polls/index.html | 2 +- polls/templates/polls/results.html | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/polls/static/polls/styles.css b/polls/static/polls/styles.css index 258e03632..99f2afcc7 100644 --- a/polls/static/polls/styles.css +++ b/polls/static/polls/styles.css @@ -1,10 +1,24 @@ body { font-family: sans-serif; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + background: #443355; + color: #fff; } .container { width: 800px; margin: 0 auto; + text-align: center; +} + +h1 { + font-size: 3rem; + margin-left: 20px; + color: #fff; + } li a, a:visited { @@ -14,7 +28,7 @@ li a, a:visited { display: inline-block; text-align: center; margin: 10px; - padding: 20px 0; + padding: 20px 5px; width: 400px; border: 2px solid #333; border-radius: 17px; diff --git a/polls/templates/polls/detail.html b/polls/templates/polls/detail.html index a4e063554..01625bbd1 100644 --- a/polls/templates/polls/detail.html +++ b/polls/templates/polls/detail.html @@ -1,3 +1,6 @@ +{% load static %} + +

{{ question.question_text }}

{% if error_message %}

{{ error_message }}

{% endif %} diff --git a/polls/templates/polls/index.html b/polls/templates/polls/index.html index c96336960..bf29e5ce3 100644 --- a/polls/templates/polls/index.html +++ b/polls/templates/polls/index.html @@ -1,8 +1,8 @@ {% load static %} -

Questions

+

Questions

{% if latest_question_list %}