├── README.md └── samsung ├── db.sqlite3 ├── employees ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── admin.cpython-36.pyc │ ├── apps.cpython-36.pyc │ ├── models.cpython-36.pyc │ ├── serializers.cpython-36.pyc │ ├── urls.cpython-36.pyc │ └── views.cpython-36.pyc ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-36.pyc │ │ └── __init__.cpython-36.pyc ├── models.py ├── serializers.py ├── tests.py ├── urls.py └── views.py ├── manage.py └── samsung ├── __pycache__ ├── __init__.cpython-36.pyc ├── settings.cpython-36.pyc ├── urls.cpython-36.pyc └── wsgi.cpython-36.pyc ├── settings.py ├── urls.py └── wsgi.py /README.md: -------------------------------------------------------------------------------- 1 | # rest_api 2 | a sample REST API in Django and Python for employees. Supports GET, POST, PUT, DEL 3 | Some notes on usage: 4 | - GET /employees/ 5 | - GET /employees/3/ 6 | - PUT /employees/3/ and must include all 3 key/value parameters (name, position, department) 7 | - POST /employees/ and must include all 3 key/value parameters (name, position, department) 8 | - DEL /employees/3/ 9 | -------------------------------------------------------------------------------- /samsung/db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/db.sqlite3 -------------------------------------------------------------------------------- /samsung/employees/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/admin.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/admin.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/apps.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/apps.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/models.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/models.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/serializers.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/serializers.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/urls.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/urls.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/__pycache__/views.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/__pycache__/views.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Employee 3 | 4 | # Register your models here. 5 | admin.site.register(Employee) -------------------------------------------------------------------------------- /samsung/employees/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class EmployeesConfig(AppConfig): 5 | name = 'employees' 6 | -------------------------------------------------------------------------------- /samsung/employees/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.1 on 2018-08-21 23:31 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Employee', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=40)), 19 | ('position', models.CharField(max_length=20)), 20 | ('department', models.CharField(max_length=20)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /samsung/employees/migrations/__pycache__/0001_initial.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/migrations/__pycache__/0001_initial.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/migrations/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/employees/migrations/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/employees/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class Employee(models.Model): 4 | name = models.CharField(max_length=40) 5 | position = models.CharField(max_length=20) 6 | department = models.CharField(max_length=20) 7 | 8 | def __str__ (self): 9 | return (str(self.id) + ': ' + self.name) -------------------------------------------------------------------------------- /samsung/employees/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from .models import Employee 3 | 4 | class EmployeeSerializer (serializers.ModelSerializer): 5 | class Meta: 6 | model = Employee 7 | fields = ('id', 'name', 'position', 'department') -------------------------------------------------------------------------------- /samsung/employees/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /samsung/employees/urls.py: -------------------------------------------------------------------------------- 1 | # employees URLs 2 | from employees import views 3 | from django.conf.urls import url 4 | 5 | urlpatterns = [ 6 | # url(r'^employees/$', views.employee_list), 7 | # url(r'^employees/(?P[0-9]+)/$', views.employee_detail), 8 | ] -------------------------------------------------------------------------------- /samsung/employees/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import get_object_or_404 2 | from rest_framework.response import Response 3 | from rest_framework.decorators import api_view 4 | from .models import Employee 5 | from .serializers import EmployeeSerializer 6 | from rest_framework import status 7 | 8 | 9 | @api_view(['GET', 'POST']) 10 | def employee_list(request): 11 | if request.method == 'GET': 12 | all_employees = Employee.objects.all() 13 | serializer = EmployeeSerializer(all_employees, many=True) 14 | return Response(serializer.data) 15 | 16 | elif request.method == 'POST': 17 | serializer = EmployeeSerializer(data=request.data) 18 | if serializer.is_valid(): 19 | serializer.save() 20 | return Response(serializer.data, status=status.HTTP_201_CREATED) 21 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 22 | 23 | @api_view(['GET', 'PUT', 'DELETE']) 24 | def employee_detail(request, id): 25 | employee = get_object_or_404(Employee, id = id) 26 | 27 | if request.method == 'GET': 28 | serializer = EmployeeSerializer(employee) 29 | return Response(serializer.data) 30 | 31 | elif request.method == 'PUT': 32 | serializer = EmployeeSerializer(employee, data = request.data) 33 | if serializer.is_valid(): 34 | serializer.save() 35 | return Response(serializer.data) 36 | return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST) 37 | 38 | elif request.method == 'DELETE': 39 | employee.delete() 40 | return Response(status=status.HTTP_204_NO_CONTENT) 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /samsung/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == '__main__': 6 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'samsung.settings') 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError as exc: 10 | raise ImportError( 11 | "Couldn't import Django. Are you sure it's installed and " 12 | "available on your PYTHONPATH environment variable? Did you " 13 | "forget to activate a virtual environment?" 14 | ) from exc 15 | execute_from_command_line(sys.argv) 16 | -------------------------------------------------------------------------------- /samsung/samsung/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/samsung/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/samsung/__pycache__/settings.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/samsung/__pycache__/settings.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/samsung/__pycache__/urls.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/samsung/__pycache__/urls.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/samsung/__pycache__/wsgi.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joeyajames/rest_api/27d81384d4251381f066fd2f2e6cab9cef8ecd3a/samsung/samsung/__pycache__/wsgi.cpython-36.pyc -------------------------------------------------------------------------------- /samsung/samsung/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for samsung project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.1/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.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'kvwb!ny)m(x#m=)m+!--h$p2=d@-!w3hi1=w2r%rjit#*w7l$&' 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 | 'employees.apps.EmployeesConfig', 35 | 'rest_framework', 36 | 'django.contrib.admin', 37 | 'django.contrib.auth', 38 | 'django.contrib.contenttypes', 39 | 'django.contrib.sessions', 40 | 'django.contrib.messages', 41 | 'django.contrib.staticfiles', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'samsung.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'samsung.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/2.1/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/2.1/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/2.1/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | -------------------------------------------------------------------------------- /samsung/samsung/urls.py: -------------------------------------------------------------------------------- 1 | # samsung URL Configuration 2 | from django.contrib import admin 3 | from django.urls import path, re_path, include 4 | from employees import views 5 | 6 | urlpatterns = [ 7 | path('admin/', admin.site.urls), 8 | path('employees/', views.employee_list, name='employee_list'), 9 | re_path(r'^employees/(?P[0-9]+)/$', views.employee_detail, name='employee_detail'), 10 | ] -------------------------------------------------------------------------------- /samsung/samsung/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for samsung 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.1/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', 'samsung.settings') 15 | 16 | application = get_wsgi_application() 17 | --------------------------------------------------------------------------------