Skip to content

Support DX Duration (Timedelta) fields via a field manager - #102

Open
ramonski wants to merge 2 commits into
feature/encode-at-string-valuesfrom
feature/dx-duration-fieldmanager
Open

Support DX Duration (Timedelta) fields via a field manager#102
ramonski wants to merge 2 commits into
feature/encode-at-string-valuesfrom
feature/dx-duration-fieldmanager

Conversation

@ramonski

Copy link
Copy Markdown
Contributor

What

Adds a field manager for the DX DurationField so duration values can be set and read through the JSON API.

Why

senaite.core.schema.DurationField is a zope.schema.Timedelta; its value is a datetime.timedelta, which JSON cannot represent. Setting e.g. SamplePoint.sampling_frequency failed:

POST .../SamplePoint/update  {"sampling_frequency": {"days": 7}}
-> {"sampling_frequency": "wrong type"}

because the raw mapping went through the setSamplingFrequency mutator unchanged and was then rejected by the field. There was no field manager registered for IDurationField, so the generic one just stored the dict.

Change

  • DurationFieldManager (a ZopeSchemaFieldManager) converts a {days, hours, minutes, seconds} mapping (or a number of minutes, or a timedelta) to a timedelta on set, and serializes a timedelta back to that mapping in json_data.
  • Registered as the adapter for senaite.core.schema.interfaces.IDurationField.
  • DexterityDataManager.set routes duration fields through the field manager (extending the same "normalizing managers bypass the raw setter" rule introduced for UID references in Normalize UID references through the field manager, not the setter #100), so the mutator does not store the dict unconverted.

Testing

New create doctest posts a JSON body with sampling_frequency: {"days": 7} and asserts the stored value is datetime.timedelta(7). Full suite green (31/0).

Stacking

On top of #101.

ramonski added 2 commits July 27, 2026 13:56
A DX DurationField is a zope.schema Timedelta, which JSON cannot carry,
so fields like SamplePoint.sampling_frequency could not be set via the
API: a raw {days, hours, minutes} mapping went through the set<Name>
mutator and was later rejected as "wrong type".

Add a DurationFieldManager that converts a {days, hours, minutes,
seconds} mapping to/from a timedelta, register it for IDurationField,
and route duration fields through the field manager in
DexterityDataManager.set (as done for UID references).
@ramonski
ramonski requested a review from xispa July 27, 2026 12:02
@ramonski ramonski added the Enhancement ✨ Improvement to existing functionality label Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement ✨ Improvement to existing functionality

Development

Successfully merging this pull request may close these issues.

1 participant