Skip to content

Commit f59f9f1

Browse files
Joe WassJoe Wass
Joe Wass
authored and
Joe Wass
committed
First commit
0 parents  commit f59f9f1

11 files changed

+332
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
.pyc
4+
__pycache__

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.7
3+
4+
# Set environment varibles
5+
ENV PYTHONUNBUFFERED 1
6+
ENV DJANGO_ENV dev
7+
8+
COPY ./requirements.txt /code/requirements.txt
9+
RUN pip install --upgrade pip
10+
RUN pip install -r /code/requirements.txt
11+
RUN pip install gunicorn
12+
13+
COPY . /code/
14+
WORKDIR /code/
15+
16+
EXPOSE 8000
17+
18+
# default run, can be overridden in docker compose.
19+
CMD gunicorn demoproject.wsgi:application --bind 0.0.0.0:8000 --workers 3

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Django Demo Project
2+
3+
Quick demo project to show Django.
4+
5+
- Runs in Docker
6+
- Hello World response
7+
- Naive JSON response
8+
- Django REST API based response.
9+
- Swagger schema
10+
11+
12+
To run:
13+
14+
docker-compose build
15+
16+
(Run this again if you change your dependencies or Dockerfile)
17+
18+
19+
To run development:
20+
21+
docker-compose run --service-ports dev
22+
23+
Then visit
24+
25+
26+
- <http://localhost:8000/>
27+
- <http://localhost:8000/naive/numbers> - JSON response done naively.
28+
- <http://localhost:8000/naive/numbers/28>
29+
- <http://localhost:8000/numbers/> - API with properly defined entities.
30+
- <http://localhost:8000/numbers/28/ >
31+
- <http://localhost:8000/docs/> - Auto generated Docs.
32+

demoproject/__init__.py

Whitespace-only changes.

demoproject/settings.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
Django settings for demoproject project.
3+
4+
Generated by 'django-admin startproject' using Django 2.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.2/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '!^)x7f&ng*&8-3q7xrohpdgqr3^t@b%pbk7r$bnb%jub_9jht4'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = [
29+
"0.0.0.0",
30+
"localhost"
31+
]
32+
33+
34+
# Application definition
35+
36+
INSTALLED_APPS = [
37+
'django.contrib.auth',
38+
'django.contrib.contenttypes',
39+
'django.contrib.sessions',
40+
'django.contrib.messages',
41+
'django.contrib.staticfiles',
42+
'rest_framework',
43+
'coreapi'
44+
]
45+
46+
MIDDLEWARE = [
47+
'django.middleware.security.SecurityMiddleware',
48+
'django.contrib.sessions.middleware.SessionMiddleware',
49+
'django.middleware.common.CommonMiddleware',
50+
'django.middleware.csrf.CsrfViewMiddleware',
51+
'django.contrib.auth.middleware.AuthenticationMiddleware',
52+
'django.contrib.messages.middleware.MessageMiddleware',
53+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
54+
]
55+
56+
ROOT_URLCONF = 'demoproject.urls'
57+
58+
TEMPLATES = [
59+
{
60+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
61+
'DIRS': [],
62+
'APP_DIRS': True,
63+
'OPTIONS': {
64+
'context_processors': [
65+
'django.template.context_processors.debug',
66+
'django.template.context_processors.request',
67+
'django.contrib.auth.context_processors.auth',
68+
'django.contrib.messages.context_processors.messages',
69+
],
70+
},
71+
},
72+
]
73+
74+
WSGI_APPLICATION = 'demoproject.wsgi.application'
75+
76+
77+
# Database
78+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
79+
80+
DATABASES = {
81+
}
82+
83+
84+
# Password validation
85+
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
86+
87+
AUTH_PASSWORD_VALIDATORS = [
88+
{
89+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
90+
},
91+
{
92+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
93+
},
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
99+
},
100+
]
101+
102+
103+
# Internationalization
104+
# https://docs.djangoproject.com/en/2.2/topics/i18n/
105+
106+
LANGUAGE_CODE = 'en-us'
107+
108+
TIME_ZONE = 'UTC'
109+
110+
USE_I18N = True
111+
112+
USE_L10N = True
113+
114+
USE_TZ = True
115+
116+
117+
# Static files (CSS, JavaScript, Images)
118+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
119+
120+
STATIC_URL = '/static/'

demoproject/urls.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.urls import path
2+
from django.conf.urls import url, include
3+
from rest_framework.documentation import include_docs_urls
4+
5+
from . import views
6+
7+
urlpatterns = [
8+
path('', views.home, name='home'),
9+
10+
# Naive...
11+
path('naive/numbers', views.naive_numbers),
12+
path('naive/numbers/<int:num_id>', views.naive_number),
13+
14+
# Using REST API
15+
url(r'^', include(views.numbers_router.urls)),
16+
url(r'^docs/', include_docs_urls(title='My Catalogue of Numbers')),
17+
18+
]

demoproject/views.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from django.shortcuts import render
2+
from django.http import HttpResponse
3+
from django.http import JsonResponse
4+
from django.http import Http404
5+
from rest_framework import serializers, viewsets, routers
6+
from rest_framework.renderers import JSONRenderer
7+
from rest_framework.response import Response
8+
9+
# This would be served from a database normally.
10+
# However, as we probably won't use one, this demo shows in-memory data.
11+
NUMBERS = {1002: {"id": 1002, "name": "One Thousand and two"},
12+
39949: {"id": 39949, "name": "Thirty-nine thousand, nine hundred and forty nine"},
13+
55: {"id": 55, "name": "Fiffty-five"},
14+
28: {"id": 28, "name": "Twenty-eight"}}
15+
16+
17+
# Friendly greeting.
18+
19+
def home(request):
20+
return HttpResponse("Hello, world.")
21+
22+
23+
# Naive JSON API
24+
# Just to show it's doable.
25+
26+
def naive_numbers(request):
27+
return JsonResponse({'numbers': NUMBERS})
28+
29+
def naive_number(request, num_id):
30+
number_data = NUMBERS[num_id]
31+
try:
32+
number_data = NUMBERS[num_id]
33+
return JsonResponse({'number': number_data})
34+
except KeyError:
35+
raise Http404("Number does not exist!")
36+
37+
# Numbers using Using REST API
38+
39+
# The objects we're dealing with.
40+
class Number(object):
41+
def __init__(self, data):
42+
self.num_id = data["id"]
43+
self.num_name = data["name"]
44+
45+
# For auto API and schema generation.
46+
class NumberSerializer(serializers.Serializer):
47+
num_id = serializers.IntegerField()
48+
num_name = serializers.CharField()
49+
50+
class NumberViewSet(viewsets.ViewSet):
51+
serializer_class = NumberSerializer
52+
53+
def list(self, request):
54+
# Construct an object for each.
55+
nums = [Number(x) for x in NUMBERS.values()]
56+
57+
serializer = NumberSerializer(nums, many=True)
58+
return Response(serializer.data)
59+
60+
def retrieve(self, request, pk=None):
61+
# Look up and construct.
62+
number = Number(NUMBERS[int(pk)])
63+
64+
serializer = NumberSerializer(number)
65+
return Response(serializer.data)
66+
67+
68+
# Attach the endpoints.
69+
numbers_router = routers.DefaultRouter()
70+
numbers_router.register(r'numbers', NumberViewSet, basename='number')
71+

demoproject/wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for demoproject project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demoproject.settings')
15+
16+
application = get_wsgi_application()

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '2'
2+
services:
3+
4+
# Development server.
5+
dev:
6+
build: .
7+
volumes:
8+
- .:/code
9+
ports:
10+
- "8000:8000"
11+
working_dir: /code
12+
command: "python manage.py runserver 0.0.0.0:8000"
13+
14+
# Production server.
15+
prod:
16+
build: .
17+
volumes:
18+
- .:/code
19+
ports:
20+
- "8000:8000"
21+
working_dir: /code
22+
command: gunicorn demoproject.wsgi:application --bind 0.0.0.0:8000 --workers 3
23+
24+

manage.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demoproject.settings')
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == '__main__':
21+
main()

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Django==2.2
2+
3+
4+
djangorestframework
5+
markdown
6+
django-filter
7+
coreapi

0 commit comments

Comments
 (0)