Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #52, #53, and #55 #54

Merged
merged 7 commits into from
Sep 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
[submodule "codingsystems"]
path = codingsystems
url = git://github.com/chb/indivo_codingsystems.git
[submodule "indivo/tests/client"]
path = indivo/tests/client
url = git://github.com/chb/indivo_client_py.git
[submodule "sample_data"]
path = sample_data
url = git://github.com/chb/indivo_sample_data.git
Expand Down
2 changes: 1 addition & 1 deletion indivo/fields/dummy_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CodeProvenanceField(DummyField):
replacements = {
'source_code': (models.CharField, {'max_length':200, 'null':True, 'db_column':'sc'}),
'title': (models.CharField, {'max_length':200, 'null':True}),
'translation_fidelity': (models.CharField, {'max_length':50, 'null':True, 'db_column':'tf'}),
'translation_fidelity': (models.CharField, {'max_length':200, 'null':True, 'db_column':'tf'}),
}

class CodedValueField(DummyField):
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion indivo/models/demographics.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def from_xml(klass, xml):
return klass(**attrs)

def uri(self):
return "http://indivo.org/records/%s/demographics"%(self.record.id)
return "%s/records/%s/demographics"%(settings.SITE_URL_PREFIX, self.record.id)

def as_json(self):
"""JSON string representation of Demographics instance"""
Expand Down
3 changes: 2 additions & 1 deletion indivo/models/fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from lxml import etree

from django.conf import settings
from django.core import serializers
from django.core.exceptions import ValidationError
from django.db import models
Expand All @@ -28,7 +29,7 @@ def __unicode__(self):
def uri(self, modelname=None):
if not modelname:
modelname = self.__class__.__name__.lower() + 's'
return "http://indivo.org/records/%s/%s/%s"%(self.record.id, modelname, self.id)
return "%s/records/%s/%s/%s"%(settings.SITE_URL_PREFIX, self.record.id, modelname, self.id)

#Meta = BaseMeta(True)

Expand Down
18 changes: 18 additions & 0 deletions indivo/schemas/data/core/sdmx/transform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from indivo.document_processing import BaseTransform
from lxml import etree

INDIVO_DOC_NS = "{http://indivo.org/vocab/xml/documents#}"

Expand All @@ -10,4 +11,21 @@ def to_sdmx(self, doc_etree):
# the indivo namespace, so we need to strip the NS
for el in doc_etree.iter(tag="%s*"%INDIVO_DOC_NS):
el.tag = el.tag.replace(INDIVO_DOC_NS, '')

# Look for a preprocessor for the SDMX transform, then call it
for model_etree in doc_etree.iter(tag='Model'):
model_name = model_etree.get('name')
if model_name:
try:
model_class = getattr(__import__('indivo.models', fromlist=[model_name]), model_name, None)
except ImportError:
model_class = None

if model_class:
func = getattr(model_class, 'to_sdmx', None)
if func:
ret = func(model_etree)
if isinstance(ret, etree._ElementTree):
doc_etree[doc_etree.index(model_etree)] = ret

return doc_etree
12 changes: 8 additions & 4 deletions indivo/serializers/datamodel_hooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ATTACHABLE_ATTRS = ['to_rdf', 'to_xml', 'to_json',]
ATTACHABLE_ATTRS = ['to_rdf', 'to_xml', 'to_json', 'to_sdmx',]

class DataModelSerializers(object):
""" Abstract base class for defining serializers that should be attached to a data model class.
Expand All @@ -14,7 +14,10 @@ class DataModelSerializers(object):
* ``to_json(queryset, result_count, record=None, carenet=None)``: takes a queryset, and formats it as a valid
`JSON <http://www.json.org/>`_ string.

In order to be called, the methods must be attached to that data model class by calling the
* ``to_sdmx(doc_etree)``: takes an etree, and returns another etree as a valid Simple Data Model XML.
Actually, this method is not a serializer, but a preprocessor for the :ref:`SDMX <sdmx>` transform.

In order to be called, the methods must be attached to that data model class by calling the
``attach_to_data_model()`` method.

"""
Expand All @@ -27,10 +30,11 @@ def attach_to_data_model(cls, data_model_cls):
attr_val = getattr(cls, attr_name, None)
if attr_val:
# unbind the method from our class
unbound_func = attr_val.__func__
def unbound_func_factory(unbound_func):
return lambda cls, *args, **kwargs: unbound_func(*args, **kwargs)

# Wrap it as a classmethod
cm = classmethod(lambda cls, *args, **kwargs: unbound_func(*args, **kwargs))
cm = classmethod(unbound_func_factory(attr_val.__func__))

# And bind it to our data model
setattr(data_model_cls, attr_name, cm)
31 changes: 0 additions & 31 deletions indivo/tests/data/sdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,37 +319,6 @@
}
''',
'''
{
"__modelname__": "TestMedication2",
"name": "ibuprofen",
"date_started": "2010-10-01T1a00:34Z",
"date_stopped": "2010-10-31T00:00:00Z",
"brand_name": "Advil",
"route": "Oral",
"prescription": {
"__modelname__": "TestPrescription2",
"prescribed_by_name": "Kenneth D. Mandl",
"prescribed_by_institution": "Children's Hospital Boston",
"prescribed_on": "2010-09-30T00:00:00Z",
"prescribed_stop_on": "2010-10-31T00:00:00Z"
},
"fills": [
{
"__modelname__": "TestFill2",
"date_filled": "2010-10-01T00:00:00Z",
"supply_days": "15",
"filled_at_name": "CVS"
},
{
"__modelname__": "TestFill2",
"date_filled": "2010-10-16T00:00:00Z",
"supply_days": "15",
"filled_at_name": "CVS"
}
]
}
''',
'''
{
"__modelname__": "TestMedication2",
"name": "ibuprofen",
Expand Down
7 changes: 3 additions & 4 deletions indivo/tests/unit/models/fact.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.conf import settings
from indivo.tests.internal_tests import InternalTests
from indivo.models import Fact
from indivo.tests.data.record import TEST_RECORDS
from indivo.tests.data.document import TEST_R_DOCS
from indivo.fields import CodedValueField
from django.db import models

URI_PREFIX = "http://indivo.org/"

class FactModelUnitTests(InternalTests):
def setUp(self):
super(FactModelUnitTests, self).setUp()
Expand Down Expand Up @@ -68,8 +67,8 @@ def test_uri(self):
instance.save() # because we'll need the fact to have an id

# URI should have 'facts' in it by default
self.assertEqual(instance.uri(), URI_PREFIX + "records/%s/facts/%s"%(self.record.id, instance.id))
self.assertEqual(instance.uri(), settings.SITE_URL_PREFIX + "/records/%s/facts/%s"%(self.record.id, instance.id))

# But we can override it
self.assertEqual(instance.uri('medications'), URI_PREFIX + "records/%s/medications/%s"%(self.record.id, instance.id))
self.assertEqual(instance.uri('medications'), settings.SITE_URL_PREFIX + "/records/%s/medications/%s"%(self.record.id, instance.id))