Skip to content

Commit 1a673ba

Browse files
committed
Firsr mock endpoints
1 parent fa1b6f4 commit 1a673ba

File tree

14 files changed

+331
-0
lines changed

14 files changed

+331
-0
lines changed

demo/__init__.py

Whitespace-only changes.

demo/api/__init__.py

Whitespace-only changes.

demo/api/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

demo/api/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ApiConfig(AppConfig):
5+
name = 'api'

demo/api/migrations/__init__.py

Whitespace-only changes.

demo/api/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

demo/api/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

demo/api/views.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import json
2+
3+
from django.http import JsonResponse, HttpResponse
4+
from django.views.decorators.csrf import csrf_exempt
5+
6+
7+
ANNA = {
8+
"uuid": "68af402f-1084-40a4-b9b2-6bb5c2d11559",
9+
"name": "Anna",
10+
"yearsOfExperience": 5,
11+
"languages": ["python", "c"],
12+
"newOpportunities": True,
13+
}
14+
15+
LOUIS = {
16+
"uuid": "0d1bd106-c585-4d6b-b3a4-d72dedf7190e",
17+
"name": "Louis",
18+
"yearsOfExperience": 3,
19+
"languages": ["java"],
20+
"newOpportunities": True,
21+
}
22+
23+
MARCUS = {
24+
"uuid": "129e8cb2-d19c-41ad-9921-cea329bed7f0",
25+
"name": "Marcus",
26+
"yearsOfExperience": 4,
27+
"languages": ["c"],
28+
"newOpportunities": False,
29+
}
30+
31+
DEVS = [ANNA, LOUIS, MARCUS]
32+
33+
34+
def health(request):
35+
if request.method == "GET":
36+
return HttpResponse("OK!")
37+
38+
return http_method_now_allowed(request.method)
39+
40+
41+
@csrf_exempt
42+
def dev_list(request):
43+
if request.method == "GET":
44+
newOpportunities = request.GET.get("newOpportunities")
45+
devs = filter_devs_by_new_opportunities(newOpportunities)
46+
47+
return JsonResponse(devs, safe=False)
48+
49+
if request.method == "POST":
50+
body = json.loads(str(request.body, encoding="utf-8"))
51+
return JsonResponse(body, status=201, safe=False)
52+
53+
return http_method_now_allowed(request.method)
54+
55+
56+
@csrf_exempt
57+
def dev_details(request, identifier):
58+
uuid = str(identifier)
59+
dev = get_dev(uuid)
60+
if request.method == "GET":
61+
if not dev:
62+
return dev_not_found(uuid)
63+
64+
return JsonResponse(dev, status=200, safe=False)
65+
66+
if request.method == "DELETE":
67+
if not dev:
68+
return dev_not_found(uuid)
69+
70+
return JsonResponse({"deleted": f"{uuid}"}, status=204, safe=False)
71+
72+
return http_method_now_allowed(request.method)
73+
74+
75+
def dev_details_languages(request, identifier):
76+
uuid = str(identifier)
77+
dev = get_dev(uuid)
78+
if request.method == "GET":
79+
if not dev:
80+
return dev_not_found(uuid)
81+
82+
return JsonResponse(dev["languages"], status=204, safe=False)
83+
84+
return http_method_now_allowed(request.method)
85+
86+
87+
def language_list(request):
88+
return JsonResponse(["c", "go", "java", "python", "ruby"], safe=False)
89+
90+
91+
def filter_devs_by_new_opportunities(newOpportunities):
92+
if newOpportunities in ("False", "false", 0):
93+
return [dev for dev in DEVS if dev["newOpportunities"] == False]
94+
95+
if newOpportunities in ("True", "true", 1):
96+
return [dev for dev in DEVS if dev["newOpportunities"] == True]
97+
98+
return DEVS
99+
100+
101+
def get_dev(uuid):
102+
for dev in DEVS:
103+
if uuid == dev["uuid"]:
104+
return dev
105+
106+
107+
def dev_not_found(uuid):
108+
return HttpResponse(f"uuid {uuid} not found", status=404)
109+
110+
111+
def http_method_now_allowed(request_method):
112+
return JsonResponse({"error": f"{request_method} Method not allowed"})

demo/asgi.py

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

demo/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 demo project.
3+
4+
Generated by 'django-admin startproject' using Django 3.0.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.0/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/3.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '(5yq@u@s)rg^!cs@o7dsyi5b=yni)sh030a(a$m_-9j@ov7ruy'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
]
41+
42+
MIDDLEWARE = [
43+
'django.middleware.security.SecurityMiddleware',
44+
'django.contrib.sessions.middleware.SessionMiddleware',
45+
'django.middleware.common.CommonMiddleware',
46+
'django.middleware.csrf.CsrfViewMiddleware',
47+
'django.contrib.auth.middleware.AuthenticationMiddleware',
48+
'django.contrib.messages.middleware.MessageMiddleware',
49+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
50+
]
51+
52+
ROOT_URLCONF = 'demo.urls'
53+
54+
TEMPLATES = [
55+
{
56+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
57+
'DIRS': [],
58+
'APP_DIRS': True,
59+
'OPTIONS': {
60+
'context_processors': [
61+
'django.template.context_processors.debug',
62+
'django.template.context_processors.request',
63+
'django.contrib.auth.context_processors.auth',
64+
'django.contrib.messages.context_processors.messages',
65+
],
66+
},
67+
},
68+
]
69+
70+
WSGI_APPLICATION = 'demo.wsgi.application'
71+
72+
73+
# Database
74+
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
75+
76+
DATABASES = {
77+
'default': {
78+
'ENGINE': 'django.db.backends.sqlite3',
79+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
80+
}
81+
}
82+
83+
84+
# Password validation
85+
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/howto/static-files/
119+
120+
STATIC_URL = '/static/'

0 commit comments

Comments
 (0)