Initial Work Commit
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
*.pyc
|
||||
*.AppleDouble/
|
||||
.DS_Store
|
||||
__pycache__/
|
||||
|
||||
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
||||
FROM python:3
|
||||
LABEL maintainer="Michael Rest <mr@mir.systems>"
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends git && apt-get clean
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
RUN mkdir /code
|
||||
WORKDIR /code
|
||||
ADD requirements.txt /code/
|
||||
RUN pip install -r requirements.txt
|
||||
ADD . /code/
|
||||
0
animals/__init__.py
Normal file
0
animals/__init__.py
Normal file
3
animals/admin.py
Normal file
3
animals/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
animals/apps.py
Normal file
5
animals/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AnimalsConfig(AppConfig):
|
||||
name = 'animals'
|
||||
25
animals/migrations/0001_initial.py
Normal file
25
animals/migrations/0001_initial.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# Generated by Django 2.1.4 on 2019-01-01 16:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Animal',
|
||||
fields=[
|
||||
('animalnr', models.IntegerField(primary_key=True, serialize=False)),
|
||||
('earmark', models.CharField(max_length=25)),
|
||||
('rfid', models.CharField(max_length=25)),
|
||||
('forbidmilk', models.CharField(max_length=1)),
|
||||
('forbidmilkstart', models.DateTimeField(null=True)),
|
||||
('forbidmilkend', models.DateTimeField(null=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
animals/migrations/__init__.py
Normal file
0
animals/migrations/__init__.py
Normal file
26
animals/models.py
Normal file
26
animals/models.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
#animalnr INTEGER UNIQUE NOT NULL,
|
||||
#earmark CHAR (25) NOT NULL,
|
||||
#rfid INTEGER,
|
||||
#forbidmilk CHAR (1) default '0'
|
||||
#, tsforbidstart timestamp (14), tsforbidend timestamp (14));
|
||||
|
||||
class Animal (models.Model):
|
||||
"""
|
||||
Amimal Model
|
||||
Defines the attributes of a animal
|
||||
"""
|
||||
animalnr = models.IntegerField (primary_key = True)
|
||||
earmark = models.CharField (max_length = 25)
|
||||
rfid = models.CharField (max_length = 25)
|
||||
forbidmilk = models.CharField (max_length = 1)
|
||||
forbidmilkstart = models.DateTimeField (auto_now_add = False, null = True)
|
||||
forbidmilkend = models.DateTimeField (auto_now = False, null = True)
|
||||
|
||||
def get_forbidmilk (self):
|
||||
return self.forbidmilk
|
||||
|
||||
def __repr__(self):
|
||||
return self.animalnr + ' is added.'
|
||||
8
animals/serializers.py
Normal file
8
animals/serializers.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Animal
|
||||
|
||||
|
||||
class AnimalSerializer (serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Animal
|
||||
fields = ('animalnr', 'earmark', 'rfid', 'forbidmilk', 'forbidmilkstart', 'forbidmilkend')
|
||||
0
animals/tests/__init__.py
Normal file
0
animals/tests/__init__.py
Normal file
25
animals/tests/test_models.py
Normal file
25
animals/tests/test_models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.test import TestCase
|
||||
from ..models import Animal
|
||||
|
||||
|
||||
class AnimalTest (TestCase):
|
||||
""" Test module for Animal model """
|
||||
def setUp (self):
|
||||
Animal.objects.create ( animalnr = 100,
|
||||
earmark = 'abcdefg',
|
||||
rfid = 123,
|
||||
forbidmilk = '0')
|
||||
|
||||
Animal.objects.create ( animalnr = 101,
|
||||
earmark = 'bbbbb',
|
||||
rfid = 124,
|
||||
forbidmilk = '1')
|
||||
|
||||
|
||||
def test_animal_milkforbid (self):
|
||||
animal100 = Animal.objects.get (animalnr = 100)
|
||||
animal101 = Animal.objects.get (animalnr = 101)
|
||||
self.assertEqual (
|
||||
animal100.get_forbidmilk (), '0')
|
||||
self.assertEqual (
|
||||
animal101.get_forbidmilk (), '1')
|
||||
113
animals/tests/test_views.py
Normal file
113
animals/tests/test_views.py
Normal file
@@ -0,0 +1,113 @@
|
||||
import json
|
||||
from rest_framework import status
|
||||
from django.test import TestCase, Client
|
||||
from django.urls import reverse
|
||||
from ..models import Animal
|
||||
from ..serializers import AnimalSerializer
|
||||
|
||||
|
||||
# initialize the APIClient app
|
||||
client = Client ()
|
||||
|
||||
|
||||
class GetAllAnimalsTest (TestCase):
|
||||
""" Test module for GET all Animals API """
|
||||
|
||||
def setUp(self):
|
||||
Animal.objects.create ( animalnr = 100,
|
||||
earmark = 'aaaa',
|
||||
rfid = 123,
|
||||
forbidmilk = '0')
|
||||
|
||||
Animal.objects.create ( animalnr = 101,
|
||||
earmark = 'bbbbb',
|
||||
rfid = 124,
|
||||
forbidmilk = '1',
|
||||
forbidmilkstart = '2017-10-01 00:00:00')
|
||||
|
||||
Animal.objects.create ( animalnr = 102,
|
||||
earmark = 'ccccc',
|
||||
rfid = 125,
|
||||
forbidmilk = '1',
|
||||
forbidmilkstart = '2017-10-01 00:00:00',
|
||||
forbidmilkend = '2017-10-21 00:00:00')
|
||||
|
||||
def test_get_all_animals (self):
|
||||
""" get API response """
|
||||
response = client.get (reverse ('get_post_animals'))
|
||||
# get data from db
|
||||
animals = Animal.objects.all ()
|
||||
serializer = AnimalSerializer (animals, many=True)
|
||||
self.assertEqual (response.data, serializer.data)
|
||||
self.assertEqual (response.status_code, status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GetSingleAnimalTest (TestCase):
|
||||
""" Test module for GET single Animal API """
|
||||
|
||||
def setUp(self):
|
||||
self.animal100 = Animal.objects.create ( animalnr = 100,
|
||||
earmark = 'aaaa',
|
||||
rfid = 123,
|
||||
forbidmilk = '0')
|
||||
|
||||
self.animal101 = Animal.objects.create ( animalnr = 101,
|
||||
earmark = 'bbbbb',
|
||||
rfid = 124,
|
||||
forbidmilk = '1',
|
||||
forbidmilkstart = '2017-10-01 00:00:00')
|
||||
|
||||
self.animal102 = Animal.objects.create ( animalnr = 102,
|
||||
earmark = 'ccccc',
|
||||
rfid = 125,
|
||||
forbidmilk = '1',
|
||||
forbidmilkstart = '2017-10-01 00:00:00',
|
||||
forbidmilkend = '2017-10-21 00:00:00')
|
||||
|
||||
def test_get_valid_single_animal (self):
|
||||
""" get API response """
|
||||
# get data from db
|
||||
animals = Animal.objects.all ()
|
||||
response = client.get (
|
||||
reverse ('get_delete_update_animal', kwargs = {'pk': self.animal100.pk}))
|
||||
animal = Animal.objects.get (pk = self.animal100.pk)
|
||||
serializer = AnimalSerializer (animal)
|
||||
self.assertEqual (response.data, serializer.data)
|
||||
self.assertEqual (response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_get_invalid_single_animal (self):
|
||||
response = client.get (
|
||||
reverse ('get_delete_update_animal', kwargs = {'pk': 1239}))
|
||||
self.assertEqual (response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
class CreateNewAnimalTest(TestCase):
|
||||
""" Test module for inserting a new animal """
|
||||
def setUp (self):
|
||||
self.valid_payload = { 'animalnr' : 100,
|
||||
'earmark' : 'aaaa',
|
||||
'rfid' : 123,
|
||||
'forbidmilk' : '0'}
|
||||
|
||||
#FixMe
|
||||
self.invalid_payload = { 'animalnr' : '0',
|
||||
'earmark' : 'bbbbb',
|
||||
'rfid' : 124,
|
||||
'forbidmilk' : '1',
|
||||
'forbidmilkstart' : '2017-10-01 00:00:00'}
|
||||
|
||||
|
||||
def test_create_valid_animal (self):
|
||||
response = client.post (
|
||||
reverse ('get_post_animals'),
|
||||
data = json.dumps (self.valid_payload),
|
||||
content_type = 'application/json'
|
||||
)
|
||||
self.assertEqual (response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
def test_create_invalid_animal (self):
|
||||
response = client.post (
|
||||
reverse ('get_post_animals'),
|
||||
data = json.dumps (self.invalid_payload),
|
||||
content_type = 'application/json'
|
||||
)
|
||||
self.assertEqual (response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
17
animals/urls.py
Normal file
17
animals/urls.py
Normal file
@@ -0,0 +1,17 @@
|
||||
#from django.conf.urls import url,path
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path (
|
||||
'api/v1/animals/<int:pk>',
|
||||
views.get_delete_update_animal,
|
||||
name = 'get_delete_update_animal'
|
||||
),
|
||||
path (
|
||||
'api/v1/animals/',
|
||||
views.get_post_animals,
|
||||
name = 'get_post_animals'
|
||||
)
|
||||
]
|
||||
47
animals/views.py
Normal file
47
animals/views.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from .models import Animal
|
||||
from .serializers import AnimalSerializer
|
||||
|
||||
|
||||
#@api_view(['GET', 'DELETE', 'PUT'])
|
||||
@api_view(['GET', 'PUT'])
|
||||
def get_delete_update_animal (request, pk):
|
||||
try:
|
||||
animal = Animal.objects.get (pk = pk)
|
||||
except Animal.DoesNotExist:
|
||||
return Response (status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# get details of a single Animal
|
||||
if request.method == 'GET':
|
||||
serializer = AnimalSerializer (animal)
|
||||
return Response (serializer.data)
|
||||
# delete a single puppy
|
||||
elif request.method == 'DELETE':
|
||||
return Response ({})
|
||||
# update details of a single puppy
|
||||
elif request.method == 'PUT':
|
||||
return Response ({})
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
def get_post_animals (request):
|
||||
# get all Animals
|
||||
if request.method == 'GET':
|
||||
animals = Animal.objects.all ()
|
||||
serializer = AnimalSerializer (animals, many=True)
|
||||
return Response (serializer.data)
|
||||
# insert a new record for a Animal
|
||||
elif request.method == 'POST':
|
||||
data = { 'animalnr': int (request.data.get ('animalnr')),
|
||||
'earmark': request.data.get ('earmark'),
|
||||
'rfid': request.data.get ('rfid'),
|
||||
'forbidmilk': '0'
|
||||
}
|
||||
serializer = AnimalSerializer (data = data)
|
||||
if serializer.is_valid () and data ['animalnr'] > 0 :
|
||||
serializer.save ()
|
||||
return Response (serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
command: python3 manage.py runserver 0.0.0.0:8000
|
||||
volumes:
|
||||
- .:/code
|
||||
ports:
|
||||
- "8000:8000"
|
||||
0
lactorapi/__init__.py
Normal file
0
lactorapi/__init__.py
Normal file
133
lactorapi/settings.py
Normal file
133
lactorapi/settings.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
Django settings for lactorapi project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 2.1.4.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/2.1/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/2.1/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# 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/2.1/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = '(a^(q85$5tn=(9wmg6c$*lrkpbg^iw%v2-!!!1$965pqzdbm#-'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'animals',
|
||||
'rest_framework',
|
||||
'rest_framework_swagger',
|
||||
]
|
||||
|
||||
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',
|
||||
]
|
||||
REST_FRAMEWORK = {
|
||||
# Use Django's standard `django.contrib.auth` permissions,
|
||||
# or allow read-only access for unauthenticated users.
|
||||
'DEFAULT_PERMISSION_CLASSES': [],
|
||||
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
|
||||
}
|
||||
|
||||
ROOT_URLCONF = 'lactorapi.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 = 'lactorapi.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
},
|
||||
'animaldb': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': os.path.join(BASE_DIR, 'animaldb.sqlite3'),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/2.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/2.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/2.1/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
31
lactorapi/urls.py
Normal file
31
lactorapi/urls.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""lactorapi URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/2.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 include,path
|
||||
|
||||
#Create Swagger View
|
||||
from rest_framework_swagger.views import get_swagger_view
|
||||
schema_view = get_swagger_view (title='Lactor API')
|
||||
|
||||
urlpatterns = [
|
||||
path('', schema_view),
|
||||
path ('', include ('animals.urls')),
|
||||
path (
|
||||
'api-auth/',
|
||||
include ('rest_framework.urls', namespace = 'rest_framework')
|
||||
),
|
||||
path ('admin/', admin.site.urls),
|
||||
]
|
||||
16
lactorapi/wsgi.py
Normal file
16
lactorapi/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for lactorapi 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/2.1/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lactorapi.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
15
manage.py
Executable file
15
manage.py
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lactorapi.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)
|
||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Django
|
||||
djangorestframework
|
||||
django-rest-swagger
|
||||
Reference in New Issue
Block a user