commit 12ff9e7843d71fbb42a9b336a0f8fa13f7beaed5 Author: Matt Huntington Date: Wed Feb 17 22:02:50 2021 -0500 all the ham diff --git a/contacts_api/__init__.py b/contacts_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contacts_api/__pycache__/__init__.cpython-39.pyc b/contacts_api/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..751078f Binary files /dev/null and b/contacts_api/__pycache__/__init__.cpython-39.pyc differ diff --git a/contacts_api/__pycache__/admin.cpython-39.pyc b/contacts_api/__pycache__/admin.cpython-39.pyc new file mode 100644 index 0000000..2268ddd Binary files /dev/null and b/contacts_api/__pycache__/admin.cpython-39.pyc differ diff --git a/contacts_api/__pycache__/models.cpython-39.pyc b/contacts_api/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..446fa74 Binary files /dev/null and b/contacts_api/__pycache__/models.cpython-39.pyc differ diff --git a/contacts_api/__pycache__/serializers.cpython-39.pyc b/contacts_api/__pycache__/serializers.cpython-39.pyc new file mode 100644 index 0000000..904a2e4 Binary files /dev/null and b/contacts_api/__pycache__/serializers.cpython-39.pyc differ diff --git a/contacts_api/__pycache__/urls.cpython-39.pyc b/contacts_api/__pycache__/urls.cpython-39.pyc new file mode 100644 index 0000000..a4c4bb1 Binary files /dev/null and b/contacts_api/__pycache__/urls.cpython-39.pyc differ diff --git a/contacts_api/__pycache__/views.cpython-39.pyc b/contacts_api/__pycache__/views.cpython-39.pyc new file mode 100644 index 0000000..9191b29 Binary files /dev/null and b/contacts_api/__pycache__/views.cpython-39.pyc differ diff --git a/contacts_api/admin.py b/contacts_api/admin.py new file mode 100644 index 0000000..c451ace --- /dev/null +++ b/contacts_api/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin + +# Register your models here. +from .models import Contact +admin.site.register(Contact) diff --git a/contacts_api/apps.py b/contacts_api/apps.py new file mode 100644 index 0000000..bda4cc0 --- /dev/null +++ b/contacts_api/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ContactsApiConfig(AppConfig): + name = 'contacts_api' diff --git a/contacts_api/migrations/0001_initial.py b/contacts_api/migrations/0001_initial.py new file mode 100644 index 0000000..7ff514d --- /dev/null +++ b/contacts_api/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 3.1.6 on 2021-02-18 02:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Contact', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=32)), + ('age', models.IntegerField()), + ], + ), + ] diff --git a/contacts_api/migrations/__init__.py b/contacts_api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contacts_api/migrations/__pycache__/0001_initial.cpython-39.pyc b/contacts_api/migrations/__pycache__/0001_initial.cpython-39.pyc new file mode 100644 index 0000000..e1d91bb Binary files /dev/null and b/contacts_api/migrations/__pycache__/0001_initial.cpython-39.pyc differ diff --git a/contacts_api/migrations/__pycache__/__init__.cpython-39.pyc b/contacts_api/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..305195a Binary files /dev/null and b/contacts_api/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/contacts_api/models.py b/contacts_api/models.py new file mode 100644 index 0000000..4f90f18 --- /dev/null +++ b/contacts_api/models.py @@ -0,0 +1,7 @@ +from django.db import models + +# Create your models here. +class Contact(models.Model): + name = models.CharField(max_length=32) + age = models.IntegerField() + diff --git a/contacts_api/serializers.py b/contacts_api/serializers.py new file mode 100644 index 0000000..28e909d --- /dev/null +++ b/contacts_api/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Contact + +class ContactSerializer(serializers.HyperlinkedModelSerializer): # serializers.HyperlinkedModelSerializer just tells django to convert sql to JSON + class Meta: + model = Contact # tell django which model to use + fields = ('id', 'name', 'age',) # tell django which fields to include + diff --git a/contacts_api/tests.py b/contacts_api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/contacts_api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/contacts_api/urls.py b/contacts_api/urls.py new file mode 100644 index 0000000..56d6cd1 --- /dev/null +++ b/contacts_api/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('api/contacts', views.ContactList.as_view(), name='contact_list'), # api/contacts will be routed to the ContactList view for handling + path('api/contacts/', views.ContactDetail.as_view(), name='contact_detail'), # api/contacts will be routed to the ContactDetail view for handling +] + diff --git a/contacts_api/views.py b/contacts_api/views.py new file mode 100644 index 0000000..c776b71 --- /dev/null +++ b/contacts_api/views.py @@ -0,0 +1,12 @@ +from rest_framework import generics +from .serializers import ContactSerializer +from .models import Contact + +class ContactList(generics.ListCreateAPIView): + queryset = Contact.objects.all() # tell django how to retrieve all objects from the DB + serializer_class = ContactSerializer # tell django what serializer to use + +class ContactDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = Contact.objects.all() + serializer_class = ContactSerializer + diff --git a/django_rest_api/.settings.py.swp b/django_rest_api/.settings.py.swp new file mode 100644 index 0000000..4188d2a Binary files /dev/null and b/django_rest_api/.settings.py.swp differ diff --git a/django_rest_api/__init__.py b/django_rest_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_rest_api/__pycache__/__init__.cpython-39.pyc b/django_rest_api/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..005c481 Binary files /dev/null and b/django_rest_api/__pycache__/__init__.cpython-39.pyc differ diff --git a/django_rest_api/__pycache__/settings.cpython-39.pyc b/django_rest_api/__pycache__/settings.cpython-39.pyc new file mode 100644 index 0000000..ad772fd Binary files /dev/null and b/django_rest_api/__pycache__/settings.cpython-39.pyc differ diff --git a/django_rest_api/__pycache__/urls.cpython-39.pyc b/django_rest_api/__pycache__/urls.cpython-39.pyc new file mode 100644 index 0000000..4feb8df Binary files /dev/null and b/django_rest_api/__pycache__/urls.cpython-39.pyc differ diff --git a/django_rest_api/__pycache__/wsgi.cpython-39.pyc b/django_rest_api/__pycache__/wsgi.cpython-39.pyc new file mode 100644 index 0000000..9172031 Binary files /dev/null and b/django_rest_api/__pycache__/wsgi.cpython-39.pyc differ diff --git a/django_rest_api/asgi.py b/django_rest_api/asgi.py new file mode 100644 index 0000000..5575bfe --- /dev/null +++ b/django_rest_api/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for django_rest_api 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/3.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_api.settings') + +application = get_asgi_application() diff --git a/django_rest_api/settings.py b/django_rest_api/settings.py new file mode 100644 index 0000000..e632e4c --- /dev/null +++ b/django_rest_api/settings.py @@ -0,0 +1,129 @@ +""" +Django settings for django_rest_api project. + +Generated by 'django-admin startproject' using Django 3.1.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.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/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '6d*ktt0=6-!)i1bdxk4(^)*ls-eseg(k12m2vefor1-g@vc5kn' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'corsheaders', # add this + 'contacts_api', + 'rest_framework', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'corsheaders.middleware.CorsMiddleware', # this makes the cors package run for all requests. A bit like app.use() in express + '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', +] + +CORS_ALLOW_ALL_ORIGINS = True # add this + +ROOT_URLCONF = 'django_rest_api.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 = 'django_rest_api.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'django_contacts', + 'USER': '', + 'PASSWORD': '', + 'HOST': 'localhost' + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.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/3.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/3.1/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/django_rest_api/urls.py b/django_rest_api/urls.py new file mode 100644 index 0000000..c5d3dd4 --- /dev/null +++ b/django_rest_api/urls.py @@ -0,0 +1,23 @@ +"""django_rest_api URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.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 +from django.conf.urls import include # add this + +urlpatterns = [ + path('', include('contacts_api.urls')), # add this + path('admin/', admin.site.urls), +] diff --git a/django_rest_api/wsgi.py b/django_rest_api/wsgi.py new file mode 100644 index 0000000..6dbc105 --- /dev/null +++ b/django_rest_api/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for django_rest_api 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/3.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_api.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..087e21d --- /dev/null +++ b/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', 'django_rest_api.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()