Skip to content

Commit

Permalink
and now translates all opal models to be python2/3 unicoded
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Aug 29, 2017
1 parent 793b8b4 commit 7409db4
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 41 deletions.
83 changes: 54 additions & 29 deletions opal/models.py
@@ -1,6 +1,7 @@
"""
Opal Django Models
"""
from __future__ import unicode_literals
import datetime
import functools
import hashlib
Expand Down Expand Up @@ -32,6 +33,7 @@
from opal.core.subrecords import (
episode_subrecords, patient_subrecords, get_subrecord_from_api_name
)
from django.utils.encoding import python_2_unicode_compatible

warnings.simplefilter('once', DeprecationWarning)

Expand Down Expand Up @@ -461,14 +463,18 @@ def update_from_dict(self, data):
self.save()


@python_2_unicode_compatible
class ContactNumber(models.Model):
name = models.CharField(max_length=255)
number = models.CharField(max_length=255)

def __unicode__(self):
return '{0}: {1}'.format(self.name, self.number)
def __str__(self):
return '{0}: {1} - {2}'.format(
self.__class__.__name__, self.name, self.number
)


@python_2_unicode_compatible
class Synonym(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType)
Expand All @@ -478,10 +484,14 @@ class Synonym(models.Model):
class Meta:
unique_together = (('name', 'content_type'))

def __unicode__(self):
return self.name
def __str__(self):
return "{0}: {1}".format(
self.__class__.__name__,
self.name
)


@python_2_unicode_compatible
class Macro(models.Model):
"""
A Macro is a user-expandable text sequence that allows us to
Expand All @@ -494,8 +504,8 @@ class Macro(models.Model):
title = models.CharField(max_length=200, help_text=HELP_TITLE)
expanded = models.TextField(help_text=HELP_EXPANDED)

def __unicode__(self):
return self.title
def __str__(self):
return "{0}: {1}".format(self.__class__.__name__, self.title)

@classmethod
def to_dict(klass):
Expand All @@ -506,20 +516,25 @@ def to_dict(klass):
for m in klass.objects.all()]


@python_2_unicode_compatible
class Patient(models.Model):

objects = managers.PatientQueryset.as_manager()

def __unicode__(self):
def __str__(self):
try:
demographics = self.demographics_set.get()
return '%s | %s %s' % (
return '%s: %s - %s %s' % (
self.__class__.__name__,
demographics.hospital_number,
demographics.first_name,
demographics.surname
)
except models.ObjectDoesNotExist:
return 'Patient {0}'.format(self.id)
return '{0}: {1}'.format(
self.__class__.__name__,
self.id
)
except:
print(self.id)
raise
Expand Down Expand Up @@ -688,6 +703,7 @@ def set_created(self, incoming_value, user, *args, **kwargs):
self.created = timezone.now()


@python_2_unicode_compatible
class Episode(UpdatesFromDictMixin, TrackedModel):
"""
An individual episode of care.
Expand All @@ -712,19 +728,21 @@ class Episode(UpdatesFromDictMixin, TrackedModel):

objects = managers.EpisodeQueryset.as_manager()

def __unicode__(self):
def __str__(self):
try:
demographics = self.patient.demographics_set.get()

return 'episode: %s %s %s %s %s' % (
return '%s: %s - %s - %s %s - %s' % (
self.__class__.__name__,
self.id,
demographics.hospital_number,
demographics.first_name,
demographics.surname,
self.start
)
except models.ObjectDoesNotExist:
return 'episode: %s %s' % (
return '%s: %s - %s' % (
self.__class__.__name__,
self.id,
self.start
)
Expand Down Expand Up @@ -881,6 +899,7 @@ def to_dict(self, user, shallow=False):
return d


@python_2_unicode_compatible
class Subrecord(UpdatesFromDictMixin, ToDictMixin, TrackedModel, models.Model):
consistency_token = models.CharField(max_length=8)
_is_singleton = False
Expand All @@ -890,13 +909,12 @@ class Subrecord(UpdatesFromDictMixin, ToDictMixin, TrackedModel, models.Model):
class Meta:
abstract = True

def __unicode__(self):
if self.created:
return '{0}: {1} {2}'.format(
self.get_api_name(), self.id, self.created
)
else:
return '{0}: {1}'.format(self.get_api_name(), self.id)
def __str__(self):
return '%s: %s - %s' % (
self.__class__.__name__,
self.id,
self.created
)

@classmethod
def get_api_name(cls):
Expand Down Expand Up @@ -1058,6 +1076,7 @@ class Meta:
abstract = True


@python_2_unicode_compatible
class Tagging(TrackedModel, models.Model):
_is_singleton = True
_advanced_searchable = True
Expand All @@ -1071,13 +1090,19 @@ class Tagging(TrackedModel, models.Model):
class Meta:
unique_together = (('value', 'episode', 'user'))

def __unicode__(self):
def __str__(self):
if self.user is not None:
return 'User: %s - %s - archived: %s' % (
self.user.username, self.value, self.archived
return '%s: User: %s - archived: %s' % (
self.__class__.__name__,
self.user.username,
self.archived
)
else:
return "%s - archived: %s" % (self.value, self.archived)
return "%s: %s - archived: %s" % (
self.__class__.__name__,
self.value,
self.archived
)

@staticmethod
def get_api_name():
Expand Down Expand Up @@ -1414,6 +1439,7 @@ class Meta:
abstract = True


@python_2_unicode_compatible
class Location(EpisodeSubrecord):
_is_singleton = True
_icon = 'fa fa-map-marker'
Expand All @@ -1428,11 +1454,9 @@ class Location(EpisodeSubrecord):
class Meta:
abstract = True

def __unicode__(self):
demographics = self.episode.patient.demographics_set.get()
return 'Location for {0}({1}) {2} {3} {4} {5}'.format(
demographics.name,
demographics.hospital_number,
def __str__(self):
return '{0}: {1} {2} {3} {4}'.format(
self.__class__.__name__,
self.category,
self.hospital,
self.ward,
Expand Down Expand Up @@ -1477,6 +1501,7 @@ class Meta:
abstract = True


@python_2_unicode_compatible
class Diagnosis(EpisodeSubrecord):
"""
This is a working-diagnosis list, will often contain things that are
Expand All @@ -1498,7 +1523,7 @@ class Diagnosis(EpisodeSubrecord):
class Meta:
abstract = True

def __unicode__(self):
def __str__(self):
return 'Diagnosis for {0}: {1} - {2}'.format(
self.episode.patient.demographics_set.get().name,
self.condition,
Expand Down
11 changes: 11 additions & 0 deletions opal/tests/test_core_fields.py
Expand Up @@ -37,6 +37,7 @@ def test_enum(self):
)
self.assertEqual(choices, fields.enum('one', '2', 'III'))


class TestForeignKeyOrFreeText(OpalTestCase):

def test_unset_verbose_name(self):
Expand All @@ -60,6 +61,16 @@ def test_get_raises(self):
result = field.__get__(self, ForeignKeyOrFreeText)
self.assertEqual(result, 'Unknown Lookuplist Entry')

def test_synonym_string(self):
ct = ContentType.objects.get_for_model(
test_models.Dog
)
alsation = test_models.Dog.objects.create(name="Alsation")
synonym = Synonym.objects.create(
content_type=ct, name="German Shepherd", object_id=alsation.id
)
self.assertEqual(str(synonym), "Synonym: German Shepherd")

def test_synonyms_addition(self):
ct = ContentType.objects.get_for_model(
test_models.Dog
Expand Down
17 changes: 9 additions & 8 deletions opal/tests/test_episode.py
Expand Up @@ -6,6 +6,7 @@

from django.contrib.auth.models import User

from six import text_type
from opal.core.episodes import InpatientEpisode
from opal.core.test import OpalTestCase
from opal.models import Patient, Episode, Tagging, UserProfile
Expand All @@ -30,33 +31,33 @@ def test_unicode(self):
first_name="Wilma", surname="Flintstone", hospital_number="123"
)
self.assertEqual(
unicode(self.episode),
"episode: 1 123 Wilma Flintstone 2017-01-01"
text_type(self.episode),
"Episode: 1 - 123 - Wilma Flintstone - 2017-01-01"
)

def test_unicode_without_start(self):
self.patient.demographics_set.update(
first_name="Wilma", surname="Flintstone", hospital_number="123"
)
self.assertEqual(
unicode(self.episode),
"episode: 1 123 Wilma Flintstone None"
text_type(self.episode),
"Episode: 1 - 123 - Wilma Flintstone - None"
)

def test_unicode_without_demographics(self):
self.patient.demographics_set.all().delete()
self.episode.start = datetime.date(2017, 1, 1)
self.episode.save()
self.assertEqual(
unicode(self.episode),
"episode: 1 2017-01-01"
text_type(self.episode),
"Episode: 1 - 2017-01-01"
)

def test_unicode_without_demographics_without_start(self):
self.patient.demographics_set.all().delete()
self.assertEqual(
unicode(self.episode),
"episode: 1 None"
text_type(self.episode),
"Episode: 1 - None"
)

def test_singleton_subrecord_created(self):
Expand Down
6 changes: 6 additions & 0 deletions opal/tests/test_macros.py
Expand Up @@ -2,6 +2,7 @@

from opal.models import Macro


class MacroTest(TestCase):
def setUp(self):
self.m1 = Macro(title="hai", expanded="Why Hello there!")
Expand All @@ -19,3 +20,8 @@ def test_to_dict(self):
dict(label="brb", expanded="Be right back...")
]
self.assertEqual(serialised, Macro.to_dict())

def test_to_string(self):
self.assertEqual(
str(self.m1), "Macro: hai"
)
37 changes: 36 additions & 1 deletion opal/tests/test_models.py
Expand Up @@ -11,7 +11,7 @@
from opal import models
from opal.core import exceptions
from opal.models import (
Subrecord, Tagging, Patient, InpatientAdmission, Symptom,
Subrecord, Tagging, Patient, InpatientAdmission, Symptom, ContactNumber
)
from opal.core.test import OpalTestCase
from opal.core import patient_lists
Expand Down Expand Up @@ -742,6 +742,29 @@ def test_field_schema(self, patient_list):
schema = Tagging.build_field_schema()
self.assertEqual(expected, schema)

def test_tagging_with_user_to_string(self):
_, episode = self.new_patient_and_episode_please()
self.user.username = "test_user"
tag = Tagging.objects.create(
user=self.user,
episode=episode,
archived=True
)
self.assertEqual(
str(tag), "Tagging: User: test_user - archived: True"
)

def test_tagging_without_user_to_string(self):
_, episode = self.new_patient_and_episode_please()
tag = Tagging.objects.create(
value="A&E",
episode=episode,
archived=True
)
self.assertEqual(
str(tag), "Tagging: A&E - archived: True"
)


class AbstractDemographicsTestCase(OpalTestCase):
def test_name(self):
Expand All @@ -757,3 +780,15 @@ def test_get_footer(self):
ExternalSubRecord.get_modal_footer_template(),
"partials/_sourced_modal_footer.html"
)


class TestContactNumber(OpalTestCase):
def test_to_string(self):
contact_number = ContactNumber.objects.create(
name="Wilma",
number="Bedrock 243"
)
self.assertEqual(
str(contact_number),
"ContactNumber: Wilma - Bedrock 243"
)

0 comments on commit 7409db4

Please sign in to comment.