From 32d2050b410291a29f19120d73711c1194f5895d Mon Sep 17 00:00:00 2001 From: Jerrica Bobadilla Date: Tue, 6 Oct 2020 12:52:23 -0700 Subject: [PATCH] contacts api set up for deployment --- .env.sample | 1 + .gitignore | 1 + Pipfile | 19 +++ Pipfile.lock | 138 ++++++++++++++++++++++ Procfile | 1 + contacts_api/__init__.py | 0 contacts_api/admin.py | 5 + contacts_api/apps.py | 5 + contacts_api/migrations/0001_initial.py | 22 ++++ contacts_api/migrations/__init__.py | 0 contacts_api/models.py | 11 ++ contacts_api/serializers.py | 11 ++ contacts_api/tests.py | 3 + contacts_api/urls.py | 8 ++ contacts_api/views.py | 11 ++ django_rest_api/__init__.py | 0 django_rest_api/asgi.py | 16 +++ django_rest_api/settings.py | 147 ++++++++++++++++++++++++ django_rest_api/urls.py | 24 ++++ django_rest_api/wsgi.py | 16 +++ manage.py | 22 ++++ settings.sql | 3 + 22 files changed, 464 insertions(+) create mode 100644 .env.sample create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 Procfile create mode 100644 contacts_api/__init__.py create mode 100644 contacts_api/admin.py create mode 100644 contacts_api/apps.py create mode 100644 contacts_api/migrations/0001_initial.py create mode 100644 contacts_api/migrations/__init__.py create mode 100644 contacts_api/models.py create mode 100644 contacts_api/serializers.py create mode 100644 contacts_api/tests.py create mode 100644 contacts_api/urls.py create mode 100644 contacts_api/views.py create mode 100644 django_rest_api/__init__.py create mode 100644 django_rest_api/asgi.py create mode 100644 django_rest_api/settings.py create mode 100644 django_rest_api/urls.py create mode 100644 django_rest_api/wsgi.py create mode 100755 manage.py create mode 100644 settings.sql diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..ffd6eaa --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +SECRET_KEY=Putsomesecrethere \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..1e862b0 --- /dev/null +++ b/Pipfile @@ -0,0 +1,19 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +django = "*" +psycopg2-binary = "*" +django-extensions = "*" +djangorestframework = "*" +dj-database-url = "*" +python-dotenv = "*" +gunicorn = "*" +whitenoise = "*" + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..2ef0291 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,138 @@ +{ + "_meta": { + "hash": { + "sha256": "a25ca89be3f4cece40a21706dbfcbe4881c85256c7952735821d2ff0b3feac6f" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "asgiref": { + "hashes": [ + "sha256:7e51911ee147dd685c3c8b805c0ad0cb58d360987b56953878f8c06d2d1c6f1a", + "sha256:9fc6fb5d39b8af147ba40765234fa822b39818b12cc80b35ad9b0cef3a476aed" + ], + "markers": "python_version >= '3.5'", + "version": "==3.2.10" + }, + "dj-database-url": { + "hashes": [ + "sha256:4aeaeb1f573c74835b0686a2b46b85990571159ffc21aa57ecd4d1e1cb334163", + "sha256:851785365761ebe4994a921b433062309eb882fedd318e1b0fcecc607ed02da9" + ], + "index": "pypi", + "version": "==0.5.0" + }, + "django": { + "hashes": [ + "sha256:a2127ad0150ec6966655bedf15dbbff9697cc86d61653db2da1afa506c0b04cc", + "sha256:c93c28ccf1d094cbd00d860e83128a39e45d2c571d3b54361713aaaf9a94cac4" + ], + "index": "pypi", + "version": "==3.1.2" + }, + "django-extensions": { + "hashes": [ + "sha256:6809c89ca952f0e08d4e0766bc0101dfaf508d7649aced1180c091d737046ea7", + "sha256:dc663652ac9460fd06580a973576820430c6d428720e874ae46b041fa63e0efa" + ], + "index": "pypi", + "version": "==3.0.9" + }, + "djangorestframework": { + "hashes": [ + "sha256:5c5071fcbad6dce16f566d492015c829ddb0df42965d488b878594aabc3aed21", + "sha256:d54452aedebb4b650254ca092f9f4f5df947cb1de6ab245d817b08b4f4156249" + ], + "index": "pypi", + "version": "==3.12.1" + }, + "gunicorn": { + "hashes": [ + "sha256:1904bb2b8a43658807108d59c3f3d56c2b6121a701161de0ddf9ad140073c626", + "sha256:cd4a810dd51bf497552cf3f863b575dabd73d6ad6a91075b65936b151cbf4f9c" + ], + "index": "pypi", + "version": "==20.0.4" + }, + "psycopg2-binary": { + "hashes": [ + "sha256:0deac2af1a587ae12836aa07970f5cb91964f05a7c6cdb69d8425ff4c15d4e2c", + "sha256:0e4dc3d5996760104746e6cfcdb519d9d2cd27c738296525d5867ea695774e67", + "sha256:11b9c0ebce097180129e422379b824ae21c8f2a6596b159c7659e2e5a00e1aa0", + "sha256:1fabed9ea2acc4efe4671b92c669a213db744d2af8a9fc5d69a8e9bc14b7a9db", + "sha256:2dac98e85565d5688e8ab7bdea5446674a83a3945a8f416ad0110018d1501b94", + "sha256:42ec1035841b389e8cc3692277a0bd81cdfe0b65d575a2c8862cec7a80e62e52", + "sha256:6a32f3a4cb2f6e1a0b15215f448e8ce2da192fd4ff35084d80d5e39da683e79b", + "sha256:7312e931b90fe14f925729cde58022f5d034241918a5c4f9797cac62f6b3a9dd", + "sha256:7d92a09b788cbb1aec325af5fcba9fed7203897bbd9269d5691bb1e3bce29550", + "sha256:833709a5c66ca52f1d21d41865a637223b368c0ee76ea54ca5bad6f2526c7679", + "sha256:8cd0fb36c7412996859cb4606a35969dd01f4ea34d9812a141cd920c3b18be77", + "sha256:950bc22bb56ee6ff142a2cb9ee980b571dd0912b0334aa3fe0fe3788d860bea2", + "sha256:a0c50db33c32594305b0ef9abc0cb7db13de7621d2cadf8392a1d9b3c437ef77", + "sha256:a0eb43a07386c3f1f1ebb4dc7aafb13f67188eab896e7397aa1ee95a9c884eb2", + "sha256:aaa4213c862f0ef00022751161df35804127b78adf4a2755b9f991a507e425fd", + "sha256:ac0c682111fbf404525dfc0f18a8b5f11be52657d4f96e9fcb75daf4f3984859", + "sha256:ad20d2eb875aaa1ea6d0f2916949f5c08a19c74d05b16ce6ebf6d24f2c9f75d1", + "sha256:b4afc542c0ac0db720cf516dd20c0846f71c248d2b3d21013aa0d4ef9c71ca25", + "sha256:b8a3715b3c4e604bcc94c90a825cd7f5635417453b253499664f784fc4da0152", + "sha256:ba28584e6bca48c59eecbf7efb1576ca214b47f05194646b081717fa628dfddf", + "sha256:ba381aec3a5dc29634f20692349d73f2d21f17653bda1decf0b52b11d694541f", + "sha256:bd1be66dde2b82f80afb9459fc618216753f67109b859a361cf7def5c7968729", + "sha256:c2507d796fca339c8fb03216364cca68d87e037c1f774977c8fc377627d01c71", + "sha256:cec7e622ebc545dbb4564e483dd20e4e404da17ae07e06f3e780b2dacd5cee66", + "sha256:d14b140a4439d816e3b1229a4a525df917d6ea22a0771a2a78332273fd9528a4", + "sha256:d1b4ab59e02d9008efe10ceabd0b31e79519da6fb67f7d8e8977118832d0f449", + "sha256:d5227b229005a696cc67676e24c214740efd90b148de5733419ac9aaba3773da", + "sha256:e1f57aa70d3f7cc6947fd88636a481638263ba04a742b4a37dd25c373e41491a", + "sha256:e74a55f6bad0e7d3968399deb50f61f4db1926acf4a6d83beaaa7df986f48b1c", + "sha256:e82aba2188b9ba309fd8e271702bd0d0fc9148ae3150532bbb474f4590039ffb", + "sha256:ee69dad2c7155756ad114c02db06002f4cded41132cc51378e57aad79cc8e4f4", + "sha256:f5ab93a2cb2d8338b1674be43b442a7f544a0971da062a5da774ed40587f18f5" + ], + "index": "pypi", + "version": "==2.8.6" + }, + "python-dotenv": { + "hashes": [ + "sha256:8c10c99a1b25d9a68058a1ad6f90381a62ba68230ca93966882a4dbc3bc9c33d", + "sha256:c10863aee750ad720f4f43436565e4c1698798d763b63234fb5021b6c616e423" + ], + "index": "pypi", + "version": "==0.14.0" + }, + "pytz": { + "hashes": [ + "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed", + "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048" + ], + "version": "==2020.1" + }, + "sqlparse": { + "hashes": [ + "sha256:022fb9c87b524d1f7862b3037e541f68597a730a8843245c349fc93e1643dc4e", + "sha256:e162203737712307dfe78860cc56c8da8a852ab2ee33750e33aeadf38d12c548" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==0.3.1" + }, + "whitenoise": { + "hashes": [ + "sha256:05ce0be39ad85740a78750c86a93485c40f08ad8c62a6006de0233765996e5c7", + "sha256:05d00198c777028d72d8b0bbd234db605ef6d60e9410125124002518a48e515d" + ], + "index": "pypi", + "version": "==5.2.0" + } + }, + "develop": {} +} diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..c6d3572 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn django_rest_api.wsgi \ No newline at end of file diff --git a/contacts_api/__init__.py b/contacts_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contacts_api/admin.py b/contacts_api/admin.py new file mode 100644 index 0000000..3ad3bd1 --- /dev/null +++ b/contacts_api/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Contact + +# Register your models here. +admin.site.register(Contact) \ No newline at end of file diff --git a/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..3dc51b7 --- /dev/null +++ b/contacts_api/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 3.1.2 on 2020-10-06 19:07 + +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=100)), + ('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/models.py b/contacts_api/models.py new file mode 100644 index 0000000..d53ab58 --- /dev/null +++ b/contacts_api/models.py @@ -0,0 +1,11 @@ +from django.db import models + +# Create your models here. + +## Contacts Model +class Contact(models.Model): + name = models.CharField(max_length=100) + age = models.IntegerField() + + def __str__(self): + return self.name \ No newline at end of file diff --git a/contacts_api/serializers.py b/contacts_api/serializers.py new file mode 100644 index 0000000..92d6dad --- /dev/null +++ b/contacts_api/serializers.py @@ -0,0 +1,11 @@ +from rest_framework import serializers +from .models import Contact + +class ContactSerializer(serializers.HyperlinkedModelSerializer): + contact_url = serializers.ModelSerializer.serializer_url_field( + view_name = 'contact_detail' + ) + + class Meta: + model = Contact + fields = ('id', 'contact_url', 'name', 'age',) \ No newline at end of file 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..5c51fcd --- /dev/null +++ b/contacts_api/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views +from rest_framework.routers import DefaultRouter + +urlpatterns = [ + path('api/contacts', views.ContactList.as_view(), name='contact_list'), + path('api/contacts/', views.ContactDetail.as_view(), name='contact_detail'), +] \ No newline at end of file diff --git a/contacts_api/views.py b/contacts_api/views.py new file mode 100644 index 0000000..7ec23e0 --- /dev/null +++ b/contacts_api/views.py @@ -0,0 +1,11 @@ +from rest_framework import generics +from .serializers import ContactSerializer +from .models import Contact + +class ContactList(generics.ListCreateAPIView): + queryset = Contact.objects.all() + serializer_class = ContactSerializer + +class ContactDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = Contact.objects.all() + serializer_class = ContactSerializer \ No newline at end of file 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/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..349e065 --- /dev/null +++ b/django_rest_api/settings.py @@ -0,0 +1,147 @@ +""" +Django settings for django_rest_api project. + +Generated by 'django-admin startproject' using Django 3.1.2. + +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/ +""" +import os +import dj_database_url + +from pathlib import Path + +# .env config +from dotenv import load_dotenv, find_dotenv +load_dotenv(find_dotenv()) + +# 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/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ['SECRET_KEY'] + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['localhost', 'afternoon-escarpment-71350.herokuapp.com'] + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'django_extensions', + 'rest_framework', + 'contacts_api', +] + +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', + 'whitenoise.middleware.WhiteNoiseMiddleware', +] + +# Simplified static file serving. +# https://warehouse.python.org/project/whitenoise/ +STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' + +ROOT_URLCONF = 'django_rest_api.urls' + +REST_FRAMEWORK = { + # Use Django's standard `django.contrib.auth` permissions, + # or allow read-only access for unauthenticated users. + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ] +} + +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': 'djangouser', + 'PASSWORD': 'django', + 'HOST': 'localhost' + } +} + +# Updating database when deployed +db_from_env = dj_database_url.config(conn_max_age=600) +DATABASES['default'].update(db_from_env) + +# 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/' +STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') diff --git a/django_rest_api/urls.py b/django_rest_api/urls.py new file mode 100644 index 0000000..5e055e0 --- /dev/null +++ b/django_rest_api/urls.py @@ -0,0 +1,24 @@ +"""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.conf.urls import include +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('contacts_api.urls')), + path('api-auth', include('rest_framework.urls', namespace='rest_framework')) +] 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() diff --git a/settings.sql b/settings.sql new file mode 100644 index 0000000..d345066 --- /dev/null +++ b/settings.sql @@ -0,0 +1,3 @@ +CREATE DATABASE django_contacts; +CREATE USER djangouser WITH PASSWORD 'django'; +GRANT ALL PRIVILEGES ON DATABASE tunr TO djangouser; \ No newline at end of file