Backend

We developed a JSON-based API as a backend for our Google-Glass-App. Therefore we used the Django-Web-Framework with the Tastypie-Package and Virtualenv

 

First steps

We named our django project glass and created an app named tummedical within the project.

These steps are very well explained in the official django-docs. Now our project structure looks like this:

python manage.py startproject glass
python manage.py startapp tummedical

Project-Structure

  • glass
    • __init__.py
    • settings.py
    • urls.py
    • wsgi.py
  • tummedical
    • migrations
    • __init.py
    • admin.py
    • api.py
    • models.py
    • test.py
    • views.py
  • manage.py

After that we can create our models withon the tummedical application.

models.py

from django.contrib.auth.models import User
from django.db import models


# Create your models here.
    class Patient(models.Model):
        first_name = models.CharField(max_length=200)
        last_name = models.CharField(max_length=200)
        barcode = models.CharField(max_length=200,blank=True,null=True)
        diagnosis = models.CharField(max_length=200,blank=True,null=True)

        birth_date = models.CharField(max_length=200, blank=True, null=True)

        # Timestamps
        created_at = models.DateTimeField(auto_now_add=True, null=True)  # set when it's created
        updated_at = models.DateTimeField(auto_now=True, null=True)  # set every time it's updated

        def __unicode__(self):
            return u'%s, %s' % (self.last_name, self.first_name)


class Medicine(models.Model):
    name =  models.CharField(max_length=200)
    barcode = models.CharField(max_length=200,blank=True,null=True)
    dose = models.CharField(max_length=200,blank=True,null=True)
    type = models.CharField(max_length=200,blank=True,null=True)
    image = models.CharField(max_length=200,blank=True,null=True)

    # Timestamps
    created_at = models.DateTimeField(auto_now_add=True, null=True)  # set when it's created
    updated_at = models.DateTimeField(auto_now=True, null=True)  # set every time it's updated

    def __unicode__(self):
        return u'%s' % self.name


class Treatment(models.Model):
    amount  = models.CharField(max_length=200, blank=True, null=True)
    daytime = models.CharField(max_length=200, blank=True, null=True)
    date    = models.CharField(max_length=200, blank=True, null=True)

    KEY_TYPES = (("1","Morning"),("2","Midday"),("3","Evening"),("4","Night"))
    date = models.CharField(max_length=200, choices=KEY_TYPES, null=True)

    medicine = models.ForeignKey(Medicine)
    patient =  models.ForeignKey(Patient)

    # Timestamps
    created_at = models.DateTimeField(auto_now_add=True, null=True)  # set when it's created
    updated_at = models.DateTimeField(auto_now=True, null=True)  # set every time it's updated

    def __unicode__(self):
        return u'%s (%s)' % (self.medicine, self.patient)

To implement the API-functionality we use Tastipie.
Tastipie can be installed via pip

pip install django-tastipie

To enable tummedical and tastipie we have to add it in the settings.py.
Here is the also the place to configure your database connection.

settings.py

[...]
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tummedical',
    'tastypie',
)
[...]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '/tmp/mysql.sock',
        }
}
[...]

Remember to adjust name, user, password and host to your specific settings. Depending on choosen database you need to install some additional packages. In case of MySQL you may need:

pip install MySQL-python

Having a database connected and the tummedical and tastypie app´s enabled we can initialize the database.

python manage.py syncdb

Keep in mind that the manage.py need to be executed from the project root.

As mentioned before we use tastypie for the API-functionality. The configuration of tastypie is done in api.py.
For the authentication of our Wearable Devices we use the ApiKeyAuthentication(). After a successful authentication we give the device access to all the functionality (authorization = Authorization()). We create the ApiKey later in the django admin interface.
We want to allow filtering on the barcode field:

filtering = {
            "barcode": ALL,
        }

api.py

from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import Authorization
from tastypie.resources import ModelResource
from tummedical.models import Patient, Medicine, Treatment
from tastypie.resources import ALL

class PatientResource(ModelResource):
    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = Authorization()
        filtering = {
            "barcode": ALL,
        }
        queryset = Patient.objects.all()
        resource_name = 'patient'

class MedicineResource(ModelResource):
    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = Authorization()
        queryset = Medicine.objects.all()
        filtering = {
            "barcode": ALL,
        }
        resource_name = 'medicine'

class TreatmentResource(ModelResource):
    medicine = fields.ForeignKey(MedicineResource, 'medicine', full=False)
    patient = fields.ForeignKey(PatientResource, 'patient', full=False)

    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = Authorization()
        queryset = Treatment.objects.all()
        resource_name = 'treatment'

Make json the dedault format in settings.py

TASTYPIE_DEFAULT_FORMATS = ['json']

We enable the django admin interface by registering the models in admin.py
admin.py

from django.contrib import admin
from tummedical.models import Patient, Medicine, Treatment

# Register your models here.
admin.site.register(Patient)
admin.site.register(Medicine)
admin.site.register(Treatment)

To access the admin interface we have to create a superuser first

python manage.py createsuperuser

With this user we can log into the admin interface(servername.de/admin) and create ApiKeys for our Wearable devices.

Django administration
Add api key

There is one more step we need before we can use the API: We need to register the API-Endpoints in the urls.py

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from tummedical.api import PatientResource, MedicineResource, TreatmentResource

patient_resource = PatientResource()
medicine_resource = MedicineResource()
treatment_resource = TreatmentResource()

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', include(patient_resource.urls)),
    url(r'^api/', include(medicine_resource.urls)),
    url(r'^api/', include(treatment_resource.urls)),
]

If you want to give your Patients, Medicine and Treatment a more advanced interface you can implement this functionality in the views.py.