From 06ce9bd0be4a0b7d20ccbcd30139400cc401b7ae Mon Sep 17 00:00:00 2001 From: LiamDes Date: Tue, 25 Apr 2023 17:05:52 -0700 Subject: [PATCH 1/2] initial lab parts --- code/liam/django/lab04/api/__init__.py | 0 code/liam/django/lab04/api/admin.py | 3 + code/liam/django/lab04/api/apps.py | 6 + .../django/lab04/api/migrations/__init__.py | 0 code/liam/django/lab04/api/models.py | 3 + code/liam/django/lab04/api/serializers.py | 9 ++ code/liam/django/lab04/api/tests.py | 3 + code/liam/django/lab04/api/urls.py | 8 ++ code/liam/django/lab04/api/views.py | 16 +++ code/liam/django/lab04/lab04/__init__.py | 0 code/liam/django/lab04/lab04/asgi.py | 16 +++ code/liam/django/lab04/lab04/settings.py | 131 ++++++++++++++++++ code/liam/django/lab04/lab04/urls.py | 24 ++++ code/liam/django/lab04/lab04/wsgi.py | 16 +++ code/liam/django/lab04/manage.py | 22 +++ code/liam/django/lab04/static/css/styles.css | 29 ++++ code/liam/django/lab04/static/js/app.js | 59 ++++++++ code/liam/django/lab04/students/__init__.py | 0 code/liam/django/lab04/students/admin.py | 5 + code/liam/django/lab04/students/apps.py | 6 + .../lab04/students/migrations/0001_initial.py | 26 ++++ .../lab04/students/migrations/__init__.py | 0 code/liam/django/lab04/students/models.py | 14 ++ .../templates/students/student_list.html | 52 +++++++ code/liam/django/lab04/students/tests.py | 3 + code/liam/django/lab04/students/urls.py | 6 + code/liam/django/lab04/students/views.py | 8 ++ 27 files changed, 465 insertions(+) create mode 100644 code/liam/django/lab04/api/__init__.py create mode 100644 code/liam/django/lab04/api/admin.py create mode 100644 code/liam/django/lab04/api/apps.py create mode 100644 code/liam/django/lab04/api/migrations/__init__.py create mode 100644 code/liam/django/lab04/api/models.py create mode 100644 code/liam/django/lab04/api/serializers.py create mode 100644 code/liam/django/lab04/api/tests.py create mode 100644 code/liam/django/lab04/api/urls.py create mode 100644 code/liam/django/lab04/api/views.py create mode 100644 code/liam/django/lab04/lab04/__init__.py create mode 100644 code/liam/django/lab04/lab04/asgi.py create mode 100644 code/liam/django/lab04/lab04/settings.py create mode 100644 code/liam/django/lab04/lab04/urls.py create mode 100644 code/liam/django/lab04/lab04/wsgi.py create mode 100644 code/liam/django/lab04/manage.py create mode 100644 code/liam/django/lab04/static/css/styles.css create mode 100644 code/liam/django/lab04/static/js/app.js create mode 100644 code/liam/django/lab04/students/__init__.py create mode 100644 code/liam/django/lab04/students/admin.py create mode 100644 code/liam/django/lab04/students/apps.py create mode 100644 code/liam/django/lab04/students/migrations/0001_initial.py create mode 100644 code/liam/django/lab04/students/migrations/__init__.py create mode 100644 code/liam/django/lab04/students/models.py create mode 100644 code/liam/django/lab04/students/templates/students/student_list.html create mode 100644 code/liam/django/lab04/students/tests.py create mode 100644 code/liam/django/lab04/students/urls.py create mode 100644 code/liam/django/lab04/students/views.py diff --git a/code/liam/django/lab04/api/__init__.py b/code/liam/django/lab04/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/code/liam/django/lab04/api/admin.py b/code/liam/django/lab04/api/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/code/liam/django/lab04/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/code/liam/django/lab04/api/apps.py b/code/liam/django/lab04/api/apps.py new file mode 100644 index 00000000..66656fd2 --- /dev/null +++ b/code/liam/django/lab04/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'api' diff --git a/code/liam/django/lab04/api/migrations/__init__.py b/code/liam/django/lab04/api/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/code/liam/django/lab04/api/models.py b/code/liam/django/lab04/api/models.py new file mode 100644 index 00000000..d49766e4 --- /dev/null +++ b/code/liam/django/lab04/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. \ No newline at end of file diff --git a/code/liam/django/lab04/api/serializers.py b/code/liam/django/lab04/api/serializers.py new file mode 100644 index 00000000..7c044fcc --- /dev/null +++ b/code/liam/django/lab04/api/serializers.py @@ -0,0 +1,9 @@ +from rest_framework import serializers +from students.models import Student + +class StudentSerializer(serializers.ModelSerializer): + class Meta: + model = Student + fields = ('first_name', 'last_name', 'cohort', + 'favorite_topic', 'favorite_teacher', + 'capstone') \ No newline at end of file diff --git a/code/liam/django/lab04/api/tests.py b/code/liam/django/lab04/api/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/code/liam/django/lab04/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/code/liam/django/lab04/api/urls.py b/code/liam/django/lab04/api/urls.py new file mode 100644 index 00000000..5a9939c1 --- /dev/null +++ b/code/liam/django/lab04/api/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from .views import * + +urlpatterns = [ + path('', StudentAPIView.as_view()), + path('new/', CreateStudent.as_view()), + path('//', StudentDetail.as_view()) +] \ No newline at end of file diff --git a/code/liam/django/lab04/api/views.py b/code/liam/django/lab04/api/views.py new file mode 100644 index 00000000..dda27288 --- /dev/null +++ b/code/liam/django/lab04/api/views.py @@ -0,0 +1,16 @@ +from rest_framework import generics +from students.models import Student +from .serializers import StudentSerializer + +# Create your views here. + +class StudentAPIView(generics.ListAPIView): + serializer_class = StudentSerializer + queryset = Student.objects.all() + +class CreateStudent(generics.CreateAPIView): + serializer_class = StudentSerializer + +class StudentDetail(generics.RetrieveUpdateDestroyAPIView): + serializer_class = StudentSerializer + queryset = Student.objects.all() \ No newline at end of file diff --git a/code/liam/django/lab04/lab04/__init__.py b/code/liam/django/lab04/lab04/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/code/liam/django/lab04/lab04/asgi.py b/code/liam/django/lab04/lab04/asgi.py new file mode 100644 index 00000000..5ab0fdea --- /dev/null +++ b/code/liam/django/lab04/lab04/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for lab04 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.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lab04.settings') + +application = get_asgi_application() diff --git a/code/liam/django/lab04/lab04/settings.py b/code/liam/django/lab04/lab04/settings.py new file mode 100644 index 00000000..a8f9b15c --- /dev/null +++ b/code/liam/django/lab04/lab04/settings.py @@ -0,0 +1,131 @@ +""" +Django settings for lab04 project. + +Generated by 'django-admin startproject' using Django 4.2. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +from pathlib import Path +import os + +# 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.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-yl0b@5uw)6v0b4db9&n-!nvfqx6^e_m2wlmw6a!jn11d7+^y0x' + +# 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', + 'rest_framework', + 'api.apps.ApiConfig', + 'students.apps.StudentsConfig' +] + +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 = 'lab04.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 = 'lab04.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.2/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.2/howto/static-files/ + +STATIC_URL = 'static/' + +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, 'static') +] + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/code/liam/django/lab04/lab04/urls.py b/code/liam/django/lab04/lab04/urls.py new file mode 100644 index 00000000..ab6ad482 --- /dev/null +++ b/code/liam/django/lab04/lab04/urls.py @@ -0,0 +1,24 @@ +""" +URL configuration for lab04 project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', include('api.urls')), + path('', include('students.urls')), +] diff --git a/code/liam/django/lab04/lab04/wsgi.py b/code/liam/django/lab04/lab04/wsgi.py new file mode 100644 index 00000000..93702206 --- /dev/null +++ b/code/liam/django/lab04/lab04/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for lab04 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.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lab04.settings') + +application = get_wsgi_application() diff --git a/code/liam/django/lab04/manage.py b/code/liam/django/lab04/manage.py new file mode 100644 index 00000000..79a7115a --- /dev/null +++ b/code/liam/django/lab04/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', 'lab04.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/liam/django/lab04/static/css/styles.css b/code/liam/django/lab04/static/css/styles.css new file mode 100644 index 00000000..0d9899ab --- /dev/null +++ b/code/liam/django/lab04/static/css/styles.css @@ -0,0 +1,29 @@ +#app { + background-color: #faebd7; + color: #310f0f; + margin: 1em; + padding: 1em; + border: 1px solid #310f0f; + border-radius: 3px; +} + +.studentdetails { + display: grid; + grid-template-areas: + 'info delete' + 'edit edit'; + width: 300px; +} + +.info { + grid-area: info; +} + +.delete { + grid-area: delete; + text-align: right; +} + +.edit { + grid-area: edit; +} \ No newline at end of file diff --git a/code/liam/django/lab04/static/js/app.js b/code/liam/django/lab04/static/js/app.js new file mode 100644 index 00000000..c2262b4a --- /dev/null +++ b/code/liam/django/lab04/static/js/app.js @@ -0,0 +1,59 @@ +Vue.component('StudentInfo', { + template: ` +
+ {{ student.first_name }} {{ student.last_name }} + +
+ +
+ +
+
Cohort
+
{{ student.cohort }}
+
Favorite Topic
+
{{ student.favorite_topic }}
+
Favorite Teacher
+
{{ student.favorite_teacher }}
+
Capstone URL
+
Click!
+
+ +
+ EDIT THIS INFO +

First Name: [___]

+

Last Name: [___]

+

Cohort Name: [___]

+

Favorite Topic: [___]

+

Favorite Teacher: [___]

+

Capstone URL: [___]

+

+
+
+ `, + props: ['student'], +}) + +new Vue({ + el: '#app', + delimiters: ['[[', ']]'], + data: { + students: [], + token: '', + newFirstName: '', + newLastName: '', + newCohort: '', + newFavThing: '', + newFavTeacher: '', + newCapstone: '', + }, + methods: { + getStudents() { + axios.get('/api/') + .then(res => this.students = res.data) + } + }, + mounted() { + this.getStudents() + this.token = document.querySelector('input[name=csrfmiddlewaretoken]').value + } +}) \ No newline at end of file diff --git a/code/liam/django/lab04/students/__init__.py b/code/liam/django/lab04/students/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/code/liam/django/lab04/students/admin.py b/code/liam/django/lab04/students/admin.py new file mode 100644 index 00000000..21dde3a7 --- /dev/null +++ b/code/liam/django/lab04/students/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Student + +# Register your models here. +admin.site.register(Student) \ No newline at end of file diff --git a/code/liam/django/lab04/students/apps.py b/code/liam/django/lab04/students/apps.py new file mode 100644 index 00000000..c242c4de --- /dev/null +++ b/code/liam/django/lab04/students/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class StudentsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'students' diff --git a/code/liam/django/lab04/students/migrations/0001_initial.py b/code/liam/django/lab04/students/migrations/0001_initial.py new file mode 100644 index 00000000..b5e68b78 --- /dev/null +++ b/code/liam/django/lab04/students/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2 on 2023-04-25 22:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Student', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=20, verbose_name='first name')), + ('last_name', models.CharField(max_length=25, verbose_name='last name')), + ('cohort', models.CharField(max_length=25, verbose_name='cohort')), + ('favorite_topic', models.CharField(max_length=200, verbose_name='favorite topic')), + ('favorite_teacher', models.CharField(max_length=20, verbose_name='favorite teacher')), + ('capstone', models.URLField(verbose_name='capstone project')), + ], + ), + ] diff --git a/code/liam/django/lab04/students/migrations/__init__.py b/code/liam/django/lab04/students/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/code/liam/django/lab04/students/models.py b/code/liam/django/lab04/students/models.py new file mode 100644 index 00000000..248daba3 --- /dev/null +++ b/code/liam/django/lab04/students/models.py @@ -0,0 +1,14 @@ +from django.db import models + +# Create your models here. + +class Student(models.Model): + first_name = models.CharField('first name',max_length=20) + last_name = models.CharField('last name',max_length=25) + cohort = models.CharField('cohort',max_length=25) + favorite_topic = models.CharField('favorite topic',max_length=200) + favorite_teacher = models.CharField('favorite teacher', max_length=20) + capstone = models.URLField('capstone project') + + def __str__(self) -> str: + return f'{self.first_name} {self.last_name}' \ No newline at end of file diff --git a/code/liam/django/lab04/students/templates/students/student_list.html b/code/liam/django/lab04/students/templates/students/student_list.html new file mode 100644 index 00000000..39464a0f --- /dev/null +++ b/code/liam/django/lab04/students/templates/students/student_list.html @@ -0,0 +1,52 @@ +{% load static %} + + + + + + + + + + + + Students API + + + {% csrf_token %} +
+
+ +
+ +

Student List

+ +
+ +
+ +
+ MAKE UP A STUDENT. GO AHEAD. +
+ + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/code/liam/django/lab04/students/tests.py b/code/liam/django/lab04/students/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/code/liam/django/lab04/students/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/code/liam/django/lab04/students/urls.py b/code/liam/django/lab04/students/urls.py new file mode 100644 index 00000000..a3671e62 --- /dev/null +++ b/code/liam/django/lab04/students/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import StudentListView + +urlpatterns = [ + path('', StudentListView.as_view(), name='home') +] \ No newline at end of file diff --git a/code/liam/django/lab04/students/views.py b/code/liam/django/lab04/students/views.py new file mode 100644 index 00000000..a74f6420 --- /dev/null +++ b/code/liam/django/lab04/students/views.py @@ -0,0 +1,8 @@ +from django.shortcuts import render +from django.views.generic import ListView +from .models import Student + +# Create your views here. +class StudentListView(ListView): + model = Student + template_name = 'student_list.html' \ No newline at end of file From a54705cb64231fce1809a68f46492fa4b0ff40b6 Mon Sep 17 00:00:00 2001 From: LiamDes Date: Wed, 26 Apr 2023 12:26:04 -0700 Subject: [PATCH 2/2] lab04! --- code/liam/django/lab04/api/serializers.py | 2 +- code/liam/django/lab04/api/urls.py | 1 + code/liam/django/lab04/api/views.py | 9 +++ code/liam/django/lab04/static/css/styles.css | 27 ++++++- code/liam/django/lab04/static/js/app.js | 80 ++++++++++++++++--- .../templates/students/student_list.html | 23 +++--- 6 files changed, 120 insertions(+), 22 deletions(-) diff --git a/code/liam/django/lab04/api/serializers.py b/code/liam/django/lab04/api/serializers.py index 7c044fcc..94068b38 100644 --- a/code/liam/django/lab04/api/serializers.py +++ b/code/liam/django/lab04/api/serializers.py @@ -4,6 +4,6 @@ class StudentSerializer(serializers.ModelSerializer): class Meta: model = Student - fields = ('first_name', 'last_name', 'cohort', + fields = ('id', 'first_name', 'last_name', 'cohort', 'favorite_topic', 'favorite_teacher', 'capstone') \ No newline at end of file diff --git a/code/liam/django/lab04/api/urls.py b/code/liam/django/lab04/api/urls.py index 5a9939c1..9f81e5f5 100644 --- a/code/liam/django/lab04/api/urls.py +++ b/code/liam/django/lab04/api/urls.py @@ -3,6 +3,7 @@ urlpatterns = [ path('', StudentAPIView.as_view()), + path('search/', StudentSearch.as_view()), path('new/', CreateStudent.as_view()), path('//', StudentDetail.as_view()) ] \ No newline at end of file diff --git a/code/liam/django/lab04/api/views.py b/code/liam/django/lab04/api/views.py index dda27288..dd9656f0 100644 --- a/code/liam/django/lab04/api/views.py +++ b/code/liam/django/lab04/api/views.py @@ -8,6 +8,15 @@ class StudentAPIView(generics.ListAPIView): serializer_class = StudentSerializer queryset = Student.objects.all() +class StudentSearch(generics.ListAPIView): + serializer_class = StudentSerializer + def get_queryset(self): + queryset = Student.objects.all() + name = self.request.query_params.get('first_name') + if name is not None: + queryset = queryset.filter(first_name__icontains=name) + return queryset + class CreateStudent(generics.CreateAPIView): serializer_class = StudentSerializer diff --git a/code/liam/django/lab04/static/css/styles.css b/code/liam/django/lab04/static/css/styles.css index 0d9899ab..0fba29f1 100644 --- a/code/liam/django/lab04/static/css/styles.css +++ b/code/liam/django/lab04/static/css/styles.css @@ -1,10 +1,25 @@ +html { + background-color: #242c25; +} + #app { - background-color: #faebd7; + background-color: #e1e6e2; + font-family: sans-serif; color: #310f0f; - margin: 1em; + margin: auto; padding: 1em; border: 1px solid #310f0f; border-radius: 3px; + max-width: 700px; + min-width: 330px; + + filter: drop-shadow(400px 400px 4px #839689) drop-shadow(-400px -400px 4px #839689); +} + +.studentCards { + display: flex; + flex-wrap: wrap; + margin-bottom: 2em; } .studentdetails { @@ -15,6 +30,10 @@ width: 300px; } +.name { + font-weight: 800; +} + .info { grid-area: info; } @@ -26,4 +45,8 @@ .edit { grid-area: edit; +} + +fieldset>div { + margin: 0.5em; } \ No newline at end of file diff --git a/code/liam/django/lab04/static/js/app.js b/code/liam/django/lab04/static/js/app.js index c2262b4a..804eef8e 100644 --- a/code/liam/django/lab04/static/js/app.js +++ b/code/liam/django/lab04/static/js/app.js @@ -1,10 +1,10 @@ Vue.component('StudentInfo', { template: `
- {{ student.first_name }} {{ student.last_name }} + {{ student.first_name }} {{ student.last_name }}
- +
@@ -20,22 +20,26 @@ Vue.component('StudentInfo', {
EDIT THIS INFO -

First Name: [___]

-

Last Name: [___]

-

Cohort Name: [___]

-

Favorite Topic: [___]

-

Favorite Teacher: [___]

-

Capstone URL: [___]

-

+

First Name:

+

Last Name:

+

Cohort Name:

+

Favorite Topic:

+

Favorite Teacher:

+

Capstone URL:

+

`, props: ['student'], + methods: { + edit() { + this.$parent.editStudent(this.student) + }, + } }) new Vue({ el: '#app', - delimiters: ['[[', ']]'], data: { students: [], token: '', @@ -45,11 +49,67 @@ new Vue({ newFavThing: '', newFavTeacher: '', newCapstone: '', + changeFirst: undefined, + changeLast: undefined, + changeCohort: undefined, + changeFavThing: undefined, + changeFavTeacher: undefined, + changeCapstone: undefined, + searchPhrase: undefined }, methods: { getStudents() { axios.get('/api/') .then(res => this.students = res.data) + }, + addStudent() { + axios.post('api/new/', { + 'first_name': this.newFirstName, + 'last_name': this.newLastName, + 'cohort': this.newCohort, + 'favorite_topic': this.newFavThing, + 'favorite_teacher': this.newFavTeacher, + 'capstone': this.newCapstone, + }, { + headers: { 'X-CSRFToken': this.token } + }).then(() => this.getStudents()) + this.newFirstName = '', + this.newLastName = '', + this.newCohort = '', + this.newFavThing = '', + this.newFavTeacher = '', + this.newCapstone = '' + }, + editStudent(student) { + console.log(student) + axios.patch(`api/${student.id}/${student.first_name}/`, { + 'first_name': this.changeFirst, + 'last_name': this.changeLast, + 'cohort': this.changeCohort, + 'favorite_topic': this.changeFavThing, + 'favorite_teacher': this.changeFavTeacher, + 'capstone': this.changeCapstone, + }, { + headers: { 'X-CSRFToken': this.token } + }).then(() => this.getStudents()) + this.changeFirst = undefined, + this.changeLast = undefined, + this.changeCohort = undefined, + this.changeFavThing = undefined, + this.changeFavTeacher = undefined, + this.changeCapstone = undefined + }, + deleteStudent(student) { + axios.delete(`api/${student.id}/${student.first_name}`, { + headers: { 'X-CSRFToken': this.token } + }).then(() => this.getStudents()) + }, + searchStudents(search) { + axios.get(`api/search`, { + params: { first_name: search }, + headers: { 'X-CSRFToken': this.token } + }).then(res => this.students = res.data) + this.searchPhrase = undefined } }, mounted() { diff --git a/code/liam/django/lab04/students/templates/students/student_list.html b/code/liam/django/lab04/students/templates/students/student_list.html index 39464a0f..1475aff7 100644 --- a/code/liam/django/lab04/students/templates/students/student_list.html +++ b/code/liam/django/lab04/students/templates/students/student_list.html @@ -16,13 +16,18 @@ {% csrf_token %}
- + + +

Student List

- -
- + +
+
+ +
@@ -31,19 +36,19 @@

Student List

- +
- +
- +
- +
- +