Skip to content

Commit c019934

Browse files
committed
Additional Analyses Fields
1 parent 81b640c commit c019934

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/senaite/jsonapi/configure.zcml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
factory=".dataproviders.DexterityDataProvider"
5454
/>
5555

56+
<!-- Data provider for Analysis content types (computed fields) -->
57+
<adapter
58+
name="senaite.jsonapi.dataproviders.AnalysisDataProvider"
59+
factory=".dataproviders.AnalysisDataProvider"
60+
/>
61+
5662

5763
<!-- DATA MANAGERS
5864
Context level interface to get and set values (by name) and get a JSON compatible

src/senaite/jsonapi/dataproviders.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from AccessControl import Unauthorized
2222
from Acquisition import aq_base
23+
from bika.lims.interfaces import IAnalysis
2324
from plone.dexterity.interfaces import IDexterityContent
2425
from Products.Archetypes.interfaces import IBaseObject
2526
from Products.CMFCore.interfaces import ISiteRoot
@@ -225,6 +226,38 @@ def __init__(self, context):
225226
self.keys = schema.keys()
226227

227228

229+
class AnalysisDataProvider(Base):
230+
"""Data provider for Analysis content types.
231+
232+
Supplements the standard ATDataProvider with computed fields that are
233+
implemented as methods rather than AT schema fields and therefore not
234+
picked up automatically.
235+
"""
236+
interface.implements(IInfo)
237+
component.adapts(IAnalysis)
238+
239+
def __init__(self, context):
240+
super(AnalysisDataProvider, self).__init__(context)
241+
# No schema keys – this provider only adds computed fields via
242+
# the attributes mapping below.
243+
self.keys = []
244+
self.attributes = {}
245+
246+
def to_dict(self):
247+
"""Return computed analysis fields."""
248+
out = {}
249+
250+
get_formatted = getattr(self.context, "getFormattedResult", None)
251+
if callable(get_formatted):
252+
out["getFormattedResult"] = get_formatted(html=False)
253+
254+
is_retest = getattr(self.context, "isRetest", None)
255+
if callable(is_retest):
256+
out["isRetest"] = is_retest()
257+
258+
return out
259+
260+
228261
class SiteRootDataProvider(Base):
229262
""" Site Root Adapter
230263
"""

0 commit comments

Comments
 (0)