diff --git a/code/nathan/django/rest_movie/Nateflix_project/__init__.py b/code/nathan/django/rest_movie/Nateflix_project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/Nateflix_project/asgi.py b/code/nathan/django/rest_movie/Nateflix_project/asgi.py new file mode 100644 index 0000000..7fe3bfd --- /dev/null +++ b/code/nathan/django/rest_movie/Nateflix_project/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for Nateflix_project 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.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Nateflix_project.settings') + +application = get_asgi_application() diff --git a/code/nathan/django/rest_movie/Nateflix_project/settings.py b/code/nathan/django/rest_movie/Nateflix_project/settings.py new file mode 100644 index 0000000..7d1515d --- /dev/null +++ b/code/nathan/django/rest_movie/Nateflix_project/settings.py @@ -0,0 +1,135 @@ +""" +Django settings for Nateflix_project project. + +Generated by 'django-admin startproject' using Django 4.1.3. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/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.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-**kc9bu&gflb1n!$43*q27d%vx5u!3ftu-v+pyu_ou$j)+wtj5' + +# 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', + 'movies.apps.MoviesConfig', + 'api.apps.ApiConfig', + 'users.apps.UsersConfig' +] + +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 = 'Nateflix_project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [BASE_DIR / 'templates'], + '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 = 'Nateflix_project.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.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/4.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'America/North_Dakota/Center' + +USE_I18N = True + +USE_TZ = True + +STATIC_URL = '/static/' +STATICFILES_DIRS = [BASE_DIR / 'static'] + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +AUTH_USER_MODEL = 'users.CustomUser' + +LOGIN_REDIRECT_URL = 'home' +LOGOUT_REDIRECT_URL = 'home' + +LOGIN_URL = 'login' \ No newline at end of file diff --git a/code/nathan/django/rest_movie/Nateflix_project/urls.py b/code/nathan/django/rest_movie/Nateflix_project/urls.py new file mode 100644 index 0000000..58ec359 --- /dev/null +++ b/code/nathan/django/rest_movie/Nateflix_project/urls.py @@ -0,0 +1,28 @@ +"""Nateflix_project URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.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, include +from django.views.generic import TemplateView +from movies import views + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/v1/', include('api.urls')), + path('users/', include('django.contrib.auth.urls')), + path('users/', include('users.urls')), + path('', TemplateView.as_view(template_name='home.html'), name='home'), + path('movies//', views.detailMovieView, name='movie_detail') +] diff --git a/code/nathan/django/rest_movie/Nateflix_project/wsgi.py b/code/nathan/django/rest_movie/Nateflix_project/wsgi.py new file mode 100644 index 0000000..48d0fc5 --- /dev/null +++ b/code/nathan/django/rest_movie/Nateflix_project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for Nateflix_project 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.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Nateflix_project.settings') + +application = get_wsgi_application() diff --git a/code/nathan/django/rest_movie/Pipfile b/code/nathan/django/rest_movie/Pipfile new file mode 100644 index 0000000..4725933 --- /dev/null +++ b/code/nathan/django/rest_movie/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +django = "*" +djangorestframework = "*" + +[dev-packages] + +[requires] +python_version = "3.10" diff --git a/code/nathan/django/rest_movie/Pipfile.lock b/code/nathan/django/rest_movie/Pipfile.lock new file mode 100644 index 0000000..20965b4 --- /dev/null +++ b/code/nathan/django/rest_movie/Pipfile.lock @@ -0,0 +1,68 @@ +{ + "_meta": { + "hash": { + "sha256": "73653c63d46a4fc8ebd2b89822e3a608760544d16ee5f109c986963679b05ce4" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.10" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "asgiref": { + "hashes": [ + "sha256:1d2880b792ae8757289136f1db2b7b99100ce959b2aa57fd69dab783d05afac4", + "sha256:4a29362a6acebe09bf1d6640db38c1dc3d9217c68e6f9f6204d72667fc19a424" + ], + "markers": "python_version >= '3.7'", + "version": "==3.5.2" + }, + "django": { + "hashes": [ + "sha256:678bbfc8604eb246ed54e2063f0765f13b321a50526bdc8cb1f943eda7fa31f1", + "sha256:6b1de6886cae14c7c44d188f580f8ba8da05750f544c80ae5ad43375ab293cd5" + ], + "index": "pypi", + "version": "==4.1.3" + }, + "djangorestframework": { + "hashes": [ + "sha256:579a333e6256b09489cbe0a067e66abe55c6595d8926be6b99423786334350c8", + "sha256:eb63f58c9f218e1a7d064d17a70751f528ed4e1d35547fdade9aaf4cd103fd08" + ], + "index": "pypi", + "version": "==3.14.0" + }, + "pytz": { + "hashes": [ + "sha256:222439474e9c98fced559f1709d89e6c9cbf8d79c794ff3eb9f8800064291427", + "sha256:e89512406b793ca39f5971bc999cc538ce125c0e51c27941bef4568b460095e2" + ], + "version": "==2022.6" + }, + "sqlparse": { + "hashes": [ + "sha256:0323c0ec29cd52bceabc1b4d9d579e311f3e4961b98d174201d5622a23b85e34", + "sha256:69ca804846bb114d2ec380e4360a8a340db83f0ccf3afceeb1404df028f57268" + ], + "markers": "python_version >= '3.5'", + "version": "==0.4.3" + }, + "tzdata": { + "hashes": [ + "sha256:04a680bdc5b15750c39c12a448885a51134a27ec9af83667663f0b3a1bf3f342", + "sha256:91f11db4503385928c15598c98573e3af07e7229181bee5375bd30f1695ddcae" + ], + "markers": "sys_platform == 'win32'", + "version": "==2022.6" + } + }, + "develop": {} +} diff --git a/code/nathan/django/rest_movie/api/__init__.py b/code/nathan/django/rest_movie/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/api/admin.py b/code/nathan/django/rest_movie/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/code/nathan/django/rest_movie/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/code/nathan/django/rest_movie/api/apps.py b/code/nathan/django/rest_movie/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/code/nathan/django/rest_movie/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/nathan/django/rest_movie/api/migrations/__init__.py b/code/nathan/django/rest_movie/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/api/models.py b/code/nathan/django/rest_movie/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/code/nathan/django/rest_movie/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/code/nathan/django/rest_movie/api/permissions.py b/code/nathan/django/rest_movie/api/permissions.py new file mode 100644 index 0000000..bcb0598 --- /dev/null +++ b/code/nathan/django/rest_movie/api/permissions.py @@ -0,0 +1,12 @@ +from rest_framework import permissions + +class ReadOnly(permissions.BasePermission): + def has_permission(self, request, view): + return request.method in permissions.SAFE_METHODS + + +class IsAuthorOrReadOnly(permissions.BasePermission): + def has_object_permission(self, request, view, obj): + if request.method in permissions.SAFE_METHODS: + return True + return obj.author == request.user \ No newline at end of file diff --git a/code/nathan/django/rest_movie/api/serializers.py b/code/nathan/django/rest_movie/api/serializers.py new file mode 100644 index 0000000..bcb139f --- /dev/null +++ b/code/nathan/django/rest_movie/api/serializers.py @@ -0,0 +1,25 @@ +from rest_framework import serializers +from movies.models import Movie +from users.models import CustomUser +from movies.models import Movie + +class NestedMovieSerializer(serializers.ModelSerializer): + class Meta: + model = Movie + fields = ('id', 'movie_user' 'title', 'rating', 'have_watched') + +class NestedUserSerializer(serializers.ModelSerializer): + class Meta: + model = CustomUser + fields = ('id', 'username') + +class MovieSerializer(serializers.ModelSerializer): + user_detail = NestedUserSerializer(many=True, source='users', read_only=True) + class Meta: + model = Movie + fields = ('id', 'title', 'rating', 'movie_user', 'have_watched', 'user_detail') + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = CustomUser + fields = ('id', 'username') diff --git a/code/nathan/django/rest_movie/api/tests.py b/code/nathan/django/rest_movie/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/code/nathan/django/rest_movie/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/code/nathan/django/rest_movie/api/urls.py b/code/nathan/django/rest_movie/api/urls.py new file mode 100644 index 0000000..066453c --- /dev/null +++ b/code/nathan/django/rest_movie/api/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from rest_framework.routers import DefaultRouter +from . import views + +router = DefaultRouter() +router.register('movies', views.PostMovieSet, basename='movie') + +urlpatterns = router.urls + [ +] diff --git a/code/nathan/django/rest_movie/api/views.py b/code/nathan/django/rest_movie/api/views.py new file mode 100644 index 0000000..71c24d0 --- /dev/null +++ b/code/nathan/django/rest_movie/api/views.py @@ -0,0 +1,12 @@ +from django.shortcuts import render +from rest_framework import generics, viewsets +from movies.models import Movie +from .serializers import MovieSerializer + +class MovieAPIView(generics.ListAPIView): + queryset = Movie.objects.all() + serializer_class = MovieSerializer + +class PostMovieSet(viewsets.ModelViewSet): + queryset = Movie.objects.all() + serializer_class = MovieSerializer \ No newline at end of file diff --git a/code/nathan/django/rest_movie/manage.py b/code/nathan/django/rest_movie/manage.py new file mode 100644 index 0000000..54a3de7 --- /dev/null +++ b/code/nathan/django/rest_movie/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', 'Nateflix_project.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/nathan/django/rest_movie/movies/__init__.py b/code/nathan/django/rest_movie/movies/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/movies/admin.py b/code/nathan/django/rest_movie/movies/admin.py new file mode 100644 index 0000000..186b59a --- /dev/null +++ b/code/nathan/django/rest_movie/movies/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Movie + +admin.site.register(Movie) diff --git a/code/nathan/django/rest_movie/movies/apps.py b/code/nathan/django/rest_movie/movies/apps.py new file mode 100644 index 0000000..0a30ddd --- /dev/null +++ b/code/nathan/django/rest_movie/movies/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MoviesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'movies' diff --git a/code/nathan/django/rest_movie/movies/migrations/0001_initial.py b/code/nathan/django/rest_movie/movies/migrations/0001_initial.py new file mode 100644 index 0000000..b0841a5 --- /dev/null +++ b/code/nathan/django/rest_movie/movies/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.3 on 2022-12-01 01:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Movie', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('have_watched', models.BooleanField(default=False)), + ('rating', models.IntegerField(null=True)), + ], + ), + ] diff --git a/code/nathan/django/rest_movie/movies/migrations/0002_initial.py b/code/nathan/django/rest_movie/movies/migrations/0002_initial.py new file mode 100644 index 0000000..ff1fac6 --- /dev/null +++ b/code/nathan/django/rest_movie/movies/migrations/0002_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.3 on 2022-12-01 01:29 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('movies', '0001_initial'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AddField( + model_name='movie', + name='movie_user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movies', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/code/nathan/django/rest_movie/movies/migrations/__init__.py b/code/nathan/django/rest_movie/movies/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/movies/models.py b/code/nathan/django/rest_movie/movies/models.py new file mode 100644 index 0000000..6aaf726 --- /dev/null +++ b/code/nathan/django/rest_movie/movies/models.py @@ -0,0 +1,11 @@ +from django.db import models +from users.models import CustomUser + +class Movie(models.Model): + title = models.CharField(max_length=200) + have_watched = models.BooleanField(default=False) + rating = models.IntegerField(null=True) + movie_user = models.ForeignKey(CustomUser,related_name="movies",on_delete=models.CASCADE) + + def __str__(self): + return self.title \ No newline at end of file diff --git a/code/nathan/django/rest_movie/movies/tests.py b/code/nathan/django/rest_movie/movies/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/code/nathan/django/rest_movie/movies/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/code/nathan/django/rest_movie/movies/urls.py b/code/nathan/django/rest_movie/movies/urls.py new file mode 100644 index 0000000..247b341 --- /dev/null +++ b/code/nathan/django/rest_movie/movies/urls.py @@ -0,0 +1,9 @@ +# from django.urls import path +# from . import views + +# app_name = 'movies' +# urlpatterns = [ +# path('movies/', views.DetailMovieView.as_view(), name='movie_detail') +# ] + + diff --git a/code/nathan/django/rest_movie/movies/views.py b/code/nathan/django/rest_movie/movies/views.py new file mode 100644 index 0000000..63a478d --- /dev/null +++ b/code/nathan/django/rest_movie/movies/views.py @@ -0,0 +1,13 @@ +from django.shortcuts import render, get_object_or_404 +from django.views.generic import ListView +from .models import Movie + +class MovieListView(ListView): + model = Movie + template_name = 'home.html' + + +def detailMovieView(request, id): + movie = get_object_or_404(Movie, id=id) + context = {'movie': movie} + return render(request, 'detail.html', context=context) diff --git a/code/nathan/django/rest_movie/static/app.js b/code/nathan/django/rest_movie/static/app.js new file mode 100644 index 0000000..38dbde6 --- /dev/null +++ b/code/nathan/django/rest_movie/static/app.js @@ -0,0 +1,102 @@ +const app = Vue.createApp({ + delimiters: ['[[', ']]'], + data(){ + return{ + currentUser: {}, + csrfToken: "", + movies: [], + newMovie: { + 'title': '', + 'have_watched': false, + 'rating': '' + } + } + }, + methods: { + loadMovies(){ + axios({ + method: 'get', + url: 'api/v1/movies' + }).then(response => { + this.movies = response.data + this.getMovieDetails() + }) + }, + createMovie(){ + axios({ + method: 'post', + url: 'api/v1/movies/', + headers: { + 'X-CSRFToken': this.csrfToken + }, + data: { + 'title': this.newMovie.title, + 'have_watched': this.newMovie.have_watched, + 'rating': this.newMovie.rating, + 'movie_user': this.currentUser.id + } + }).then(response => { + this.loadMovies() + this.newMovie.title = "" + this.newMovie.have_watched = false + this.newMovie.rating = "" + }).catch(error => { + console.log(error.response.data) + }) + }, + deleteMovie(id){ + axios({ + method: 'delete', + url: `api/v1/movies/${id}`, + headers: { + 'X-CSRFToken': this.csrfToken + } + }) + }, + updateMovie(id){ + axios({ + method: 'put', + url: `api/v1/movies/${id}`, + headers: { + 'X-CSRFToken': this.csrfToken + }, + data: { + 'have_watched': this.newMovie.have_watched, + 'rating': this.newMovie.rating + } + }) + + }, + getMovieDetails(){ + for(let i = 0; i < this.movies.length; i++) { + this.movies[i]['hover'] = false + movieTitle = this.movies[i].title.replace(" ", "%20") + axios({ + method: 'get', + url: `https://api.themoviedb.org/3/search/movie?api_key=c77c935cf08ab8be04818ab351bd382f&language=en-US&query=${movieTitle}&page=1&include_adult=false` + }).then(response => { + response = response['data']['results'][0] + this.movies[i]['apiDetails'] = response + posterPath = response['poster_path'] + posterPath = `https://image.tmdb.org/t/p/w500/${posterPath}` + this.movies[i]['posterPath'] = posterPath + }) + } + }, + loadCurrentUser(){ + axios({ + method: 'get', + url: '/users/currentuser/' + }).then(response => { + this.currentUser = response.data + }) + } + }, + created: function() { + this.loadMovies() + this.loadCurrentUser() + }, + mounted(){ + this.csrfToken = document.querySelector("input[name=csrfmiddlewaretoken]").value + } +}) \ No newline at end of file diff --git a/code/nathan/django/rest_movie/static/app2.js b/code/nathan/django/rest_movie/static/app2.js new file mode 100644 index 0000000..475cc51 --- /dev/null +++ b/code/nathan/django/rest_movie/static/app2.js @@ -0,0 +1,69 @@ +const app = Vue.createApp({ + delimiters: ['[[', ']]'], + data(){ + return{ + currentUser: {}, + csrfToken: "", + newMovie: { + 'title': '', + 'have_watched': false, + 'rating': '' + }, + status: "" + } + }, + methods: { + deleteMovie(){ + const theMovieID = document.querySelector('#theMoviesID') + axios({ + method: 'delete', + url: `../../api/v1/movies/${theMovieID.innerHTML}`, + headers: { + 'X-CSRFToken': this.csrfToken + } + }).then( response => { + this.status = "was deleted." + }).catch(error => { + console.log(error.response) + this.status = "" + }) + }, + updateMovie(){ + const theMovieID = document.querySelector('#theMoviesID') + const theMoviesTitle = document.querySelector('#theMoviesTitle') + axios({ + method: 'put', + url: `../../api/v1/movies/${theMovieID.innerHTML}/`, + headers: { + 'X-CSRFToken': this.csrfToken + }, + data: { + 'title': theMoviesTitle.innerHTML, + 'have_watched': this.newMovie.have_watched, + 'rating': this.newMovie.rating, + 'movie_user': this.currentUser.id + } + }).then( response => { + this.status = "was updated." + }).catch(error => { + console.log(error.response.data) + this.status = "" + }) + + }, + loadCurrentUser(){ + axios({ + method: 'get', + url: '/users/currentuser/' + }).then(response => { + this.currentUser = response.data + }) + } + }, + created: function() { + this.loadCurrentUser() + }, + mounted(){ + this.csrfToken = document.querySelector("input[name=csrfmiddlewaretoken]").value + } +}) \ No newline at end of file diff --git a/code/nathan/django/rest_movie/static/css/styles.css b/code/nathan/django/rest_movie/static/css/styles.css new file mode 100644 index 0000000..64f0b3c --- /dev/null +++ b/code/nathan/django/rest_movie/static/css/styles.css @@ -0,0 +1,152 @@ +@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Rubik+Glitch&family=Rubik+Mono+One&display=swap'); + +body { + margin: 0rem; + padding: 0rem; + background-color: darkred; + color: white; +} + +#heading { + background-image: url(../images/cinema.jpg); + background-size: cover; + padding-top: 1rem; + width: 100%; + height: 100%; + font-family: 'Roboto Mono', monospace; + font-size: large; + color: white; + text-shadow: -1px 1px 0 rgb(139, 0, 0), + 1px 1px 0 rgb(139, 0, 0), + 1px -1px 0 rgb(139, 0, 0), + -1px -1px 0 rgb(139, 0, 0); + text-align: center; + border: solid darkred; +} + +#heading h1 { + font-size: 100px; + font-family: 'Rubik Mono One', sans-serif; + color: rgb(139, 0, 0); + text-shadow: -1px 1px 0 rgb(253, 253, 253), + 1px 1px 0 rgb(253, 253, 253), + 1px -1px 0 rgb(253, 253, 253), + -1px -1px 0 rgb(253, 253, 253); +} + +#heading h2 { + font-size: 40px; +} + +#login-logout { + display: flex; + justify-content: end; + gap: 2rem; + margin-right: 1rem; +} + +#login-logout a { + font-size: 50px; + font-family: 'Rubik Glitch', cursive; + text-decoration: none; + color: white; + text-shadow: -1px 1px 0 rgb(139, 0, 0), + 1px 1px 0 rgb(139, 0, 0), + 1px -1px 0 rgb(139, 0, 0), + -1px -1px 0 rgb(139, 0, 0); +} + +#login-logout a:hover { + color: rgb(139, 0, 0); + text-shadow: -1px 1px 0 rgb(253, 253, 253), + 1px 1px 0 rgb(253, 253, 253), + 1px -1px 0 rgb(253, 253, 253), + -1px -1px 0 rgb(253, 253, 253); +} + +#add-movie { + margin: 1rem; + display: flex; + gap: 1rem; + justify-content: center; +} + +#add-movie input { + padding-inline: 2rem; + padding-block: 1rem; + font-size: large; +} + +#add-movie button { + padding-inline: 2rem; + padding-block: 1rem; + font-size: large; +} + +#movie-area { + margin: 5rem; + display: flex; + flex-direction: column; + justify-content: center; + flex-wrap: wrap; + gap: 2rem; + background-color: rgba(0, 0, 0, 0.438); + padding: 1rem; + padding-top: 2rem; +} + +.api-info-plus-movie { + display: flex; + flex-wrap: wrap; + gap: 1rem; + justify-content: center; +} + +.single-movie { + display: flex; + flex-direction: column; +} + +.movie-hover { + display: flex; + flex-direction: column; + margin-left: 1rem; +} + +.movie-link:hover { + background-color: rgba(139, 0, 0, 0.308); +} + +img { + height: 25rem; +} + +#theMoviesID { + display: none; +} + +@media screen and (max-width: 1250px) { + #add-movie { + flex-direction: column; + } + + #heading h1 { + font-size: large; + } + + #heading h2 { + font-size: medium; + } + + #heading a { + font-size: large; + } + + #heading { + background-image: none; + } + + #movie-area { + margin: 0rem; + } +} \ No newline at end of file diff --git a/code/nathan/django/rest_movie/static/images/cinema.jpg b/code/nathan/django/rest_movie/static/images/cinema.jpg new file mode 100644 index 0000000..933d781 Binary files /dev/null and b/code/nathan/django/rest_movie/static/images/cinema.jpg differ diff --git a/code/nathan/django/rest_movie/templates/base.html b/code/nathan/django/rest_movie/templates/base.html new file mode 100644 index 0000000..5d4b5c4 --- /dev/null +++ b/code/nathan/django/rest_movie/templates/base.html @@ -0,0 +1,33 @@ + +{% load static %} + + + + + + + NateFlix + + + {% csrf_token %} +
+
+
+ {% if user.is_authenticated %} + Logout + {% else %} + Signup + Login + {% endif %} +
+

Welcome to NateFlix

+

A place to keep track of movies you've seen

+

and movies you want to see.

+ + {% block content%} + {% endblock %} +
+
+ + + \ No newline at end of file diff --git a/code/nathan/django/rest_movie/templates/detail.html b/code/nathan/django/rest_movie/templates/detail.html new file mode 100644 index 0000000..f5a72e5 --- /dev/null +++ b/code/nathan/django/rest_movie/templates/detail.html @@ -0,0 +1,32 @@ +{% extends 'base.html' %} +{% load static %} + +{% block content %} +
+

{{ movie.title }}

+

[[status]]

+

{{movie.id}}

+
+ {% if not movie.have_watched %} + I've seen this movie +
+ +
+ + {% endif %} + +
+
+ + + + + + + + + + + + +{% endblock %} \ No newline at end of file diff --git a/code/nathan/django/rest_movie/templates/home.html b/code/nathan/django/rest_movie/templates/home.html new file mode 100644 index 0000000..4bf6a4d --- /dev/null +++ b/code/nathan/django/rest_movie/templates/home.html @@ -0,0 +1,68 @@ +{% extends 'base.html' %} +{% block content%} +{% load static %} +
+
+ {% if user.is_authenticated %} +

Add a Movie

+
+ + I've seen this movie +
+ +
+ +
+
+
+

To watch list

+
+
+
+
+ movie poster +

[[movie.title]]

+
+
+

The world's rating:
[[movie.apiDetails.vote_average]] out of 10

+

Release Date:
[[movie.apiDetails.release_date]]

+
+
+
+
+

Watched Movies

+
+
+
+
+ movie poster +

[[movie.title]]

+
+
+

Your rating:
[[movie.rating]] out of 10

+

The world's rating:
[[movie.apiDetails.vote_average]] out of 10

+

Release Date:
[[movie.apiDetails.release_date]]

+
+
+
+
+
+ {% endif %} +
+ + + + + + + + + + + + + + + {% endblock %} + + diff --git a/code/nathan/django/rest_movie/templates/registration/login.html b/code/nathan/django/rest_movie/templates/registration/login.html new file mode 100644 index 0000000..11119c5 --- /dev/null +++ b/code/nathan/django/rest_movie/templates/registration/login.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% load static%} + +{% block content%} +

Sign in

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ +{% endblock %} \ No newline at end of file diff --git a/code/nathan/django/rest_movie/templates/registration/signup.html b/code/nathan/django/rest_movie/templates/registration/signup.html new file mode 100644 index 0000000..a838ac5 --- /dev/null +++ b/code/nathan/django/rest_movie/templates/registration/signup.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} +{% load static%} + +{% block content%} + +

Sign up

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ +{% endblock %} \ No newline at end of file diff --git a/code/nathan/django/rest_movie/users/__init__.py b/code/nathan/django/rest_movie/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/users/admin.py b/code/nathan/django/rest_movie/users/admin.py new file mode 100644 index 0000000..603dfa7 --- /dev/null +++ b/code/nathan/django/rest_movie/users/admin.py @@ -0,0 +1,12 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .forms import CustomUserCreationForm, CustomUserChangeForm +from .models import CustomUser + +class CustomUserAdmin(UserAdmin): + add_form = CustomUserCreationForm + form = CustomUserChangeForm + model = CustomUser + list_display = ['username', 'first_name', 'last_name', 'email'] + +admin.site.register(CustomUser, CustomUserAdmin) diff --git a/code/nathan/django/rest_movie/users/apps.py b/code/nathan/django/rest_movie/users/apps.py new file mode 100644 index 0000000..72b1401 --- /dev/null +++ b/code/nathan/django/rest_movie/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users' diff --git a/code/nathan/django/rest_movie/users/forms.py b/code/nathan/django/rest_movie/users/forms.py new file mode 100644 index 0000000..a71f358 --- /dev/null +++ b/code/nathan/django/rest_movie/users/forms.py @@ -0,0 +1,13 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm, UserChangeForm +from .models import CustomUser + +class CustomUserCreationForm(UserCreationForm): + class Meta: + model = CustomUser + fields = ('username', 'first_name', 'last_name', 'email') + +class CustomUserChangeForm(UserChangeForm): + class Meta: + model = CustomUser + fields = ('username', 'first_name', 'last_name', 'email') \ No newline at end of file diff --git a/code/nathan/django/rest_movie/users/migrations/0001_initial.py b/code/nathan/django/rest_movie/users/migrations/0001_initial.py new file mode 100644 index 0000000..363851a --- /dev/null +++ b/code/nathan/django/rest_movie/users/migrations/0001_initial.py @@ -0,0 +1,43 @@ +# Generated by Django 4.1.3 on 2022-12-01 01:29 + +import django.contrib.auth.models +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='CustomUser', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('username', models.CharField(max_length=50, unique=True)), + ('first_name', models.CharField(max_length=50, null=True)), + ('last_name', models.CharField(max_length=50, null=True)), + ('email', models.EmailField(max_length=254, null=True)), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/code/nathan/django/rest_movie/users/migrations/__init__.py b/code/nathan/django/rest_movie/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/nathan/django/rest_movie/users/models.py b/code/nathan/django/rest_movie/users/models.py new file mode 100644 index 0000000..bb90d38 --- /dev/null +++ b/code/nathan/django/rest_movie/users/models.py @@ -0,0 +1,12 @@ +from django.db import models +from django.contrib.auth.models import AbstractUser +from datetime import date + +class CustomUser(AbstractUser): + username = models.CharField(max_length=50, unique=True) + first_name = models.CharField(max_length=50, null=True) + last_name = models.CharField(max_length=50, null=True) + email = models.EmailField(max_length=254, null=True) + + def __str__(self): + return self.username diff --git a/code/nathan/django/rest_movie/users/tests.py b/code/nathan/django/rest_movie/users/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/code/nathan/django/rest_movie/users/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/code/nathan/django/rest_movie/users/urls.py b/code/nathan/django/rest_movie/users/urls.py new file mode 100644 index 0000000..4f0d194 --- /dev/null +++ b/code/nathan/django/rest_movie/users/urls.py @@ -0,0 +1,12 @@ +from django.urls import path +from rest_framework.routers import DefaultRouter +from . import views +from .views import SignUpView + +router = DefaultRouter() +router.register('users', views.UserViewSet, basename='users') + +urlpatterns = [ + path('signup/', SignUpView.as_view(), name='signup'), + path('currentuser/', views.CurrentUserView.as_view()) +] \ No newline at end of file diff --git a/code/nathan/django/rest_movie/users/views.py b/code/nathan/django/rest_movie/users/views.py new file mode 100644 index 0000000..46b1f4f --- /dev/null +++ b/code/nathan/django/rest_movie/users/views.py @@ -0,0 +1,23 @@ +from django.shortcuts import render +from rest_framework import generics, viewsets, permissions +from django.views.generic.edit import CreateView +from django.urls import reverse_lazy +from users.models import CustomUser +from api.serializers import UserSerializer +from api.permissions import IsAuthorOrReadOnly +from .forms import CustomUserCreationForm, CustomUserChangeForm + +class SignUpView(CreateView): + form_class = CustomUserCreationForm + template_name = 'registration/signup.html' + success_url = reverse_lazy('login') + +class CurrentUserView(generics.RetrieveUpdateAPIView): + serializer_class = UserSerializer + def get_object(self): + return self.request.user + +class UserViewSet(viewsets.ReadOnlyModelViewSet): + queryset = CustomUser + serializer_class = UserSerializer + permission_classes = [permissions.IsAuthenticatedOrReadOnly]