Skip to content

Commit

Permalink
Fix jsonapi returns None for sample's DateOfBirth field (#114)
Browse files Browse the repository at this point in the history
* Added a field manager for AgeDateOfBirthField

* Fix NameError: name 'IAgeDateOfBirthField' is not defined

* Changelog
  • Loading branch information
xispa authored Oct 26, 2024
1 parent 4d93120 commit bb507f9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.5.0 (unreleased)
------------------

- #114 Fix jsonapi returns None for sample's DateOfBirth field
- #113 Added estimated birthdate field to Patient content type
- #112 Use default TZ when calculating birthdate if `on_date` param is not set
- #111 Allow/Disallow the introduction of future dates of birth
Expand Down
1 change: 1 addition & 0 deletions src/senaite/patient/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<include package=".browser" />
<include package=".catalog" />
<include package=".content" />
<include package=".jsonapi" />
<include package=".monkeys" />
<include package=".subscribers" />
<include package=".upgrade" />
Expand Down
4 changes: 4 additions & 0 deletions src/senaite/patient/content/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Some rights reserved, see README and LICENSE.

import six

from AccessControl import ClassSecurityInfo
from archetypes.schemaextender.field import ExtensionField
from Products.Archetypes.Field import ObjectField
Expand All @@ -29,6 +30,8 @@
from senaite.patient.browser.widgets import TemporaryIdentifierWidget
from senaite.patient.config import AUTO_ID_MARKER
from senaite.patient.config import PATIENT_CATALOG
from senaite.patient.interfaces import IAgeDateOfBirthField
from zope.interface import implementer


class TemporaryIdentifierField(ExtensionField, ObjectField):
Expand Down Expand Up @@ -122,6 +125,7 @@ def get_fullname(self, instance):
return " ".join(filter(None, [firstname, middlename, lastname]))


@implementer(IAgeDateOfBirthField)
class AgeDateOfBirthField(ExtensionField, ObjectField):
"""ObjectField extender that stores a tuple (dob, from_age, estimated)
"""
Expand Down
7 changes: 7 additions & 0 deletions src/senaite/patient/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ class IRoutineAnalysis(interface.Interface):
"""Marker interface for routine analyses, that is required for the
patient's dynamic result range adapter to work properly
"""


class IAgeDateOfBirthField(interface.Interface):
"""Marker interface for AT-custom AgeDateOfBirthField. This interface is
necessary for AgeDateOfBirthFieldManager, required by senaite.jsonapi to
properly retrieve and jsonify the value
"""
1 change: 1 addition & 0 deletions src/senaite/patient/jsonapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
8 changes: 8 additions & 0 deletions src/senaite/patient/jsonapi/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<configure xmlns="http://namespaces.zope.org/zope">

<!-- Adapter for senaite.patient's AT AgeDateOfBirthField -->
<adapter
for="senaite.patient.interfaces.IAgeDateOfBirthField"
factory=".fieldmanagers.AgeDateOfBirthFieldManager"/>

</configure>
21 changes: 21 additions & 0 deletions src/senaite/patient/jsonapi/fieldmanagers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

from senaite.jsonapi import api
from senaite.jsonapi.fieldmanagers import ATFieldManager
from senaite.jsonapi.interfaces import IFieldManager
from zope import interface


class AgeDateOfBirthFieldManager(ATFieldManager):
"""Adapter to get/set the value of AT's AgeDateOfBirthField
"""
interface.implements(IFieldManager)

def json_data(self, instance, default=None):
"""Get a JSON compatible value
"""
dob = self.field.get_date_of_birth(instance)
from_age = self.field.get_from_age(instance)
estimated = self.field.get_estimated(instance)

return [api.to_iso_date(dob), from_age, estimated]

0 comments on commit bb507f9

Please sign in to comment.