Skip to content

Commit

Permalink
add description and enum fields to the opal schema and the title fiel…
Browse files Browse the repository at this point in the history
…d to tagging
  • Loading branch information
fredkingham committed Mar 16, 2017
1 parent aab98b5 commit 5442a79
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
59 changes: 45 additions & 14 deletions opal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ def _get_field_default(cls, name):

return default

@classmethod
def get_field_description(cls, name):
field = cls._get_field(name)
description = getattr(field, 'help_text', "")
if description:
return description

@classmethod
def get_field_enum(cls, name):
field = cls._get_field(name)
choices = getattr(field, "choices", [])

if choices:
return [i[1] for i in choices]

@classmethod
def build_field_schema(cls):
field_schema = []
Expand All @@ -184,14 +199,18 @@ def build_field_schema(cls):
)
title = cls._get_field_title(fieldname)
default = cls._get_field_default(fieldname)

field_schema.append({'name': fieldname,
'title': title,
'type': field_type,
'lookup_list': lookup_list,
'default': default,
'model': cls.__name__
})
field = {
'name': fieldname,
'title': title,
'type': field_type,
'lookup_list': lookup_list,
'default': default,
'model': cls.__name__,
'description': cls.get_field_description(fieldname),
'enum': cls.get_field_enum(fieldname)
}

field_schema.append(field)
return field_schema


Expand Down Expand Up @@ -1011,8 +1030,15 @@ def get_form_template(team=None, subteam=None):

@staticmethod
def build_field_schema():
return [{'name': t, 'type': 'boolean'} for t in
patient_lists.TaggedPatientList.get_tag_names()]
# t.title is wrong, but its the better than nothing
result = []
for tag in patient_lists.TaggedPatientList.get_tag_names():
result.append({
'name': tag,
'type': 'boolean',
'title': tag.replace("_", " ").title()
})
return result


"""
Expand Down Expand Up @@ -1280,23 +1306,27 @@ class Demographics(PatientSubrecord):

hospital_number = models.CharField(max_length=255, blank=True)
nhs_number = models.CharField(
max_length=255, blank=True, null=True, verbose_name="NHS Number"
max_length=255, blank=True, null=True, verbose_name="NHS Number",
)

surname = models.CharField(max_length=255, blank=True)
first_name = models.CharField(max_length=255, blank=True)
middle_name = models.CharField(max_length=255, blank=True, null=True)
title = ForeignKeyOrFreeText(Title)
date_of_birth = models.DateField(null=True, blank=True)
date_of_birth = models.DateField(
null=True, blank=True, verbose_name="Date of Birth"
)
marital_status = ForeignKeyOrFreeText(MaritalStatus)
religion = models.CharField(max_length=255, blank=True, null=True)
date_of_death = models.DateField(null=True, blank=True)
date_of_death = models.DateField(
null=True, blank=True, verbose_name="Date of Death"
)
post_code = models.CharField(max_length=20, blank=True, null=True)
gp_practice_code = models.CharField(
max_length=20, blank=True, null=True, verbose_name="GP Practice Code"
)
birth_place = ForeignKeyOrFreeText(Destination,
verbose_name="Country Of Birth")
verbose_name="Country of Birth")
ethnicity = ForeignKeyOrFreeText(Ethnicity)
death_indicator = models.BooleanField(default=False)

Expand Down Expand Up @@ -1540,6 +1570,7 @@ class InpatientAdmission(PatientSubrecord, ExternallySourcedModel):
_title = "Inpatient Admissions"
_icon = 'fa fa-map-marker'
_sort = "-admitted"
_advanced_searchable = False

datetime_of_admission = models.DateTimeField(blank=True, null=True)
datetime_of_discharge = models.DateTimeField(blank=True, null=True)
Expand Down
12 changes: 12 additions & 0 deletions opal/tests/test_core_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,46 @@
'type': 'date_time',
'name': 'created',
'default': None,
'enum': None,
'description': None,
'title': 'Created'},
{'model': 'Colour',
'lookup_list': None,
'type': 'date_time',
'name': 'updated',
'default': None,
'enum': None,
'description': None,
'title': 'Updated'},
{'model': 'Colour',
'lookup_list': None,
'default': None,
'name': 'created_by_id',
'title': 'Created By',
'enum': None,
'description': None,
'type': 'forei'},
{'model': 'Colour',
'lookup_list': None,
'default': None,
'enum': None,
'description': None,
'name': 'updated_by_id',
'title': 'Updated By',
'type': 'forei'},
{'model': 'Colour',
'lookup_list': None,
'default': None,
'enum': None,
'description': None,
'name': 'consistency_token',
'title': 'Consistency Token',
'type': 'token'},
{'model': 'Colour',
'lookup_list': None,
'default': None,
'enum': None,
'description': None,
'name': 'name',
'title': 'Name',
'type': 'string'},
Expand Down
20 changes: 11 additions & 9 deletions opal/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
from mock import patch, MagicMock

from django.conf import settings
from django.contrib.auth.models import User
from django.utils import timezone

from opal import models
from opal.core import exceptions
from opal.models import (
Subrecord, Tagging, Patient, InpatientAdmission, Symptom,
SymptomComplex, UserProfile
)
from opal.core.test import OpalTestCase
import opal.tests.test_patient_lists # To make sure test tagged lists are pulled in
from opal.core import patient_lists
from opal.tests import test_patient_lists
from opal.tests.models import (
FamousLastWords, PatientColour, ExternalSubRecord, SymptomComplex,
PatientConsultation, Birthday, DogOwner, HatWearer, HouseOwner, HoundOwner,
Colour
)


class PatientRecordAccessTestCase(OpalTestCase):

def test_to_dict(self):
Expand Down Expand Up @@ -746,13 +746,15 @@ def test_display_template(self):
def test_form_template(self):
self.assertEqual('tagging_modal.html', Tagging.get_form_template())

def test_field_schema(self):
names = ['eater', 'herbivore', 'carnivore']
fields = [{'name': tagname, 'type': 'boolean'} for tagname in names]
@patch.object(patient_lists.TaggedPatientList, "list")
def test_field_schema(self, patient_list):
patient_list.return_value = [test_patient_lists.TaggingTestPatientList]
expected = [
{'name': 'eater', 'title': 'Eater', 'type': 'boolean'},
{'type': 'boolean', 'name': 'herbivore', 'title': 'Herbivore'}
]
schema = Tagging.build_field_schema()
for field in fields:
self.assertIn(field, schema)

self.assertEqual(expected, schema)


class AbstractDemographicsTestCase(OpalTestCase):
Expand Down
5 changes: 5 additions & 0 deletions opal/tests/test_models_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def test_build_field_schema(self):
expected = [
{
'model': 'SerialisableModel',
'description': None,
'enum': None,
'lookup_list': None,
'type': 'string',
'name': 'pid',
Expand All @@ -69,6 +71,8 @@ def test_build_field_schema(self):
},
{
'model': 'SerialisableModel',
'description': None,
'enum': None,
'lookup_list': 'hat',
'type': 'string',
'name': 'hatty',
Expand All @@ -89,6 +93,7 @@ def test_getter_is_used(self):
dict(foo="gotten", id=None)
)


class UpdatesFromDictMixinTestCase(OpalTestCase):
def setUp(self):
self.model = UpdatableModelInstance
Expand Down

0 comments on commit 5442a79

Please sign in to comment.