From 7a117ce6ce2c900e73f3a76fa904fc9f581c815d Mon Sep 17 00:00:00 2001 From: DaeronAlagos Date: Thu, 12 Mar 2020 09:13:18 +0000 Subject: [PATCH] Mint Django app and start filling in some stuff --- .dockerignore | 1 + .gitignore | 136 ++++++++++++++++++++++ django/C19/C19/__init__.py | 0 django/C19/C19/asgi.py | 16 +++ django/C19/C19/settings.py | 132 +++++++++++++++++++++ django/C19/C19/urls.py | 22 ++++ django/C19/C19/wsgi.py | 16 +++ django/C19/api/__init__.py | 0 django/C19/api/admin.py | 3 + django/C19/api/apps.py | 5 + django/C19/api/migrations/__init__.py | 0 django/C19/api/models.py | 3 + django/C19/api/serializers.py | 36 ++++++ django/C19/api/tests.py | 3 + django/C19/api/urls.py | 16 +++ django/C19/api/views.py | 15 +++ django/C19/ehrbase_connector/__init__.py | 1 + django/C19/ehrbase_connector/connector.py | 16 +++ django/C19/manage.py | 21 ++++ django/Dockerfile | 8 ++ django/requirements.txt | 5 + docker-compose.yml | 62 ++++++++++ 22 files changed, 517 insertions(+) create mode 100644 .dockerignore create mode 100644 django/C19/C19/__init__.py create mode 100644 django/C19/C19/asgi.py create mode 100644 django/C19/C19/settings.py create mode 100644 django/C19/C19/urls.py create mode 100644 django/C19/C19/wsgi.py create mode 100644 django/C19/api/__init__.py create mode 100644 django/C19/api/admin.py create mode 100644 django/C19/api/apps.py create mode 100644 django/C19/api/migrations/__init__.py create mode 100644 django/C19/api/models.py create mode 100644 django/C19/api/serializers.py create mode 100644 django/C19/api/tests.py create mode 100644 django/C19/api/urls.py create mode 100644 django/C19/api/views.py create mode 100644 django/C19/ehrbase_connector/__init__.py create mode 100644 django/C19/ehrbase_connector/connector.py create mode 100755 django/C19/manage.py create mode 100644 django/Dockerfile create mode 100644 django/requirements.txt create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..553f5d4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +django/Dockerfile diff --git a/.gitignore b/.gitignore index 4d29575..8953230 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,139 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + + +# Created by .ignore support plugin (hsz.mobi) +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Custom +.idea/ diff --git a/django/C19/C19/__init__.py b/django/C19/C19/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/C19/C19/asgi.py b/django/C19/C19/asgi.py new file mode 100644 index 0000000..bc67800 --- /dev/null +++ b/django/C19/C19/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for C19 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.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'C19.settings') + +application = get_asgi_application() diff --git a/django/C19/C19/settings.py b/django/C19/C19/settings.py new file mode 100644 index 0000000..a49f0e3 --- /dev/null +++ b/django/C19/C19/settings.py @@ -0,0 +1,132 @@ +""" +Django settings for C19 project. + +Generated by 'django-admin startproject' using Django 3.0.4. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/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/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 't8z$5)6b-4y_liyeo@rh=e=z=0loz!(_6lhaw9(as+k&3!f=x0' + +# 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', + 'rest_framework', +] + +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', +] + +ROOT_URLCONF = 'C19.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 = 'C19.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.environ['DJANGO_POSTGRES_DB'], + 'USER': os.environ['DJANGO_POSTGRES_USER'], + 'PASSWORD': os.environ['DJANGO_POSTGRES_PASSWORD'], + 'HOST': 'django_postgres', + 'PORT': 5432, + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/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', + }, +] + +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' + ] +} + +# Internationalization +# https://docs.djangoproject.com/en/3.0/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.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/django/C19/C19/urls.py b/django/C19/C19/urls.py new file mode 100644 index 0000000..bd1d470 --- /dev/null +++ b/django/C19/C19/urls.py @@ -0,0 +1,22 @@ +"""C19 URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/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 path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', include('api.urls')) +] diff --git a/django/C19/C19/wsgi.py b/django/C19/C19/wsgi.py new file mode 100644 index 0000000..7649688 --- /dev/null +++ b/django/C19/C19/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for C19 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.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'C19.settings') + +application = get_wsgi_application() diff --git a/django/C19/api/__init__.py b/django/C19/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/C19/api/admin.py b/django/C19/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/django/C19/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/django/C19/api/apps.py b/django/C19/api/apps.py new file mode 100644 index 0000000..d87006d --- /dev/null +++ b/django/C19/api/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + name = 'api' diff --git a/django/C19/api/migrations/__init__.py b/django/C19/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/C19/api/models.py b/django/C19/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/django/C19/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/django/C19/api/serializers.py b/django/C19/api/serializers.py new file mode 100644 index 0000000..8d8dcda --- /dev/null +++ b/django/C19/api/serializers.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +from rest_framework import serializers + + +class BodyTempSerializer(serializers.Serializer): + magnitude = serializers.CharField() + units = serializers.CharField() + + +class OtherSymptomSerializer(serializers.Serializer): + symptomNameText = serializers.CharField() + symptomNameCode = serializers.CharField() + symptomNameTerminology = serializers.CharField() + + +class SymptomsSerializer(serializers.Serializer): + firstSymptomsPresenceCode = serializers.CharField() + firstSymptomsPresenceText = serializers.CharField() + dateOfOnset = serializers.CharField() + coughPresenceCode = serializers.CharField() + coughPresenceText = serializers.CharField() + feverPresenceCode = serializers.CharField() + feverPresenceValue = serializers.CharField() + difficultyBreathingPresenceCode = serializers.CharField() + difficultyBreathingPresenceText = serializers.CharField() + soreThroatPresenceCode = serializers.CharField() + soreThroatPresenceText = serializers.CharField() + otherSymptom = OtherSymptomSerializer() + + +class Covid19Serializer(serializers.Serializer): + clinicalAuthorName = serializers.CharField() + clinicalAuthorId = serializers.CharField() + documentTime = serializers.CharField() + symptoms = SymptomsSerializer() + bodyTemp = BodyTempSerializer() diff --git a/django/C19/api/tests.py b/django/C19/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django/C19/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django/C19/api/urls.py b/django/C19/api/urls.py new file mode 100644 index 0000000..47fcb8f --- /dev/null +++ b/django/C19/api/urls.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from django.urls import include, path +from rest_framework import routers +from api import views +from rest_framework.urlpatterns import format_suffix_patterns + + +router = routers.DefaultRouter() + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), + path('covid-screens/', views.CovidScreenListView.as_view()) +] + diff --git a/django/C19/api/views.py b/django/C19/api/views.py new file mode 100644 index 0000000..032303f --- /dev/null +++ b/django/C19/api/views.py @@ -0,0 +1,15 @@ +from django.http import Http404 +from api.serializers import Covid19Serializer +from ehrbase_connector.connector import OpenEHRConnector +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status + + +class CovidScreenListView(APIView): + + def post(self, request, format=None): + serializer = Covid19Serializer(data=request.data) + if serializer.is_valid(): + ehrbase = OpenEHRConnector() + ehrbase.post(data=serializer.data) diff --git a/django/C19/ehrbase_connector/__init__.py b/django/C19/ehrbase_connector/__init__.py new file mode 100644 index 0000000..40a96af --- /dev/null +++ b/django/C19/ehrbase_connector/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/django/C19/ehrbase_connector/connector.py b/django/C19/ehrbase_connector/connector.py new file mode 100644 index 0000000..a913b25 --- /dev/null +++ b/django/C19/ehrbase_connector/connector.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +import os +import requests + + +class OpenEHRConnector(object): + _host = 'http://localhost' + _username = os.environ['EHRBASE_USER'] + _password = os.environ['EHRBASE_PASSWORD'] + + def get_token(self): + response = requests.get(self._host, self._username, self._password) + return response.get('token') + + def get(self, path): + return requests.get(self._host + path) diff --git a/django/C19/manage.py b/django/C19/manage.py new file mode 100755 index 0000000..20a76b9 --- /dev/null +++ b/django/C19/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'C19.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/django/Dockerfile b/django/Dockerfile new file mode 100644 index 0000000..88794ee --- /dev/null +++ b/django/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3 +ENV PYTHONUNBUFFERED 1 +RUN mkdir /app +WORKDIR /app +COPY requirements.txt /app/ +RUN pip install -r requirements.txt +COPY . /app/ +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] \ No newline at end of file diff --git a/django/requirements.txt b/django/requirements.txt new file mode 100644 index 0000000..a32379e --- /dev/null +++ b/django/requirements.txt @@ -0,0 +1,5 @@ +Django +psycopg2 +djangorestframework +requests +pydevd-pycharm~=193.6494.30 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d3c4358 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,62 @@ +version: "3.7" +services: + django: + image: quay.io/opusvl/c19:dev + container_name: c19-django + environment: + DJANGO_POSTGRES_USER: ${DJANGO_POSTGRES_USER} + DJANGO_POSTGRES_PASSWORD: ${DJANGO_POSTGRES_PASSWORD} + DJANGO_POSTGRES_DB: ${DJANGO_POSTGRES_DB} + EHRBASE_USER: ${EHRBASE_USER} + EHRBASE_PASSWORD: ${EHRBASE_PASSWORD} + volumes: + - ./django/C19:/app + ports: + - 8000:8000 + depends_on: + - django_postgres + networks: + - django_net + django_postgres: + image: postgres:12 + container_name: c19-django-postgres + environment: + POSTGRES_USER: ${DJANGO_POSTGRES_USER} + POSTGRES_PASSWORD: ${DJANGO_POSTGRES_PASSWORD} + networks: + - django_net + + ehrdb: + image: ehrbaseorg/ehrbase-postgres:latest + volumes: + - "./postgres/:/var/lib/postgresql/data/:rw" + healthcheck: + test: [ "CMD", "pg_isready", "-q", "-d", "${EHRBASE_DB}", "-U", "${EHRBASE_USER}"] + timeout: 45s + interval: 10s + retries: 10 + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + EHRBASE_USER: ${EHRBASE_USER} + EHRBASE_PASSWORD: ${EHRBASE_PASSWORD} + networks: + - ehrbase_net + restart: always + + ehrbase: + image: ehrbaseorg/ehrbase:next + environment: + DB_URL: ${DB_URL} + DB_USER: ${DB_USER} + DB_PASS: ${DB_PASS} + SYSTEM_NAME: ${SYSTEM_NAME} + ports: + - "8080:8080" + networks: + - ehrbase_net + restart: always + +networks: + django_net: + ehrbase_net: \ No newline at end of file