diff --git a/docs/manual/forms/logic.rst b/docs/manual/forms/logic.rst index 513e128095..182227193a 100644 --- a/docs/manual/forms/logic.rst +++ b/docs/manual/forms/logic.rst @@ -368,15 +368,70 @@ Het is mogelijk om de datum / tijd component te vergelijken met de huidige datum Bijvoorbeeld, deze trigger checkt of de datum / tijd van een evenement meer dan 24u in de toekomst is: +.. tabs:: + + .. tab:: "duration" operator + + .. code:: json + + { + ">": [ + {"datetime": {"var": "datumTijdEvenement"}}, + {"datetime": {"+": [{"var": "now"}, {"duration": "PT24H"}]}}, + ] + } + + .. tab:: "rdelta" operator + + Deze operator is verouderd en het is beter om de "duration" operator te gebruiken. + + .. code:: json + + { + ">": [ + {"datetime": {"var": "datumTijdEvenement"}}, + {"+": [{"var": "now"}, {"rdelta": [0, 0, 0, 24]}]}, + ] + } + +Met ``duration`` is het mogelijk om berekeningen met datums te doen. De ``duration`` operator moet een `ISO 8601 'Duration'`_ +bevatten. In de trigger hierboven, wordt een periode van 24 uren toegevoegd aan de datum / tijd van nu (``{"var": "now"}``). + +``duration`` mag ook met datums worden gebruikt. Bijvoorbeeld: + + .. code:: json { ">": [ - {"datetime": {"var": "datumTijdEvenement"}}, - {"+": [{"var": "now"}, {"rdelta": [0, 0, 0, 24]}]}, + {"date": {"var": "datumEvenement"}}, + {"date": {"+": [{"var": "today"}, {"duration": "P1M"}]}}, ] } -Met ``rdelta`` is het mogelijk om berekeningen met datums te doen. De ``rdelta`` operator mag tot 6 argumenten te hebben. -Deze zijn dan de waarde van ``[jaren, maanden, dagen, uren, minuten, seconden]``. In de trigger hierboven, worden 0 jaren, -0 maanden, 0 dagen en 24 uren toegevoegd aan de datum / tijd van nu (``{"var": "now"}``). +In de trigger hierboven, wordt een periode van 1 maand toegevoegd aan de datum van vandaag (``{"var": "today"}``). +Als vandaag ``"2023-02-01"`` is, dan is ``"2023-03-01"`` het resultaat van +``{"+": [{"var": "today"}, {"duration": "P1M"}]}``. + +.. _ISO 8601 'Duration': https://en.wikipedia.org/wiki/ISO_8601#Durations + + +Voorbeeld met vergelijking van tijdsperiodes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Het is ook mogelijk om tijdsperiodes te vergelijken. + +Bijvoorbeeld, deze trigger checkt of de periode tussen ``geboorteDatum`` en ``evenementDatum`` meer dan 18 jaren is. + +.. code:: json + + { + "<": [ + { + "duration": { + "-": [{"date": {"var": "geboorteDatum"}}, {"date": {"var": "evenementDatum"}}] + } + }, + {"duration": "P18Y"}, + ] + } diff --git a/requirements/base.txt b/requirements/base.txt index ce4cdf5ccb..66ec9314a8 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -268,6 +268,7 @@ inflection==0.5.1 # drf-spectacular isodate==0.6.1 # via + # maykin-json-logic-py # maykin-python3-saml # zeep josepy==1.8.0 @@ -301,7 +302,7 @@ mail-parser==3.15.0 # via django-yubin maykin-django-two-factor-auth[phonenumbers]==2.0.4 # via -r requirements/base.in -maykin-json-logic-py==0.11.0 +maykin-json-logic-py==0.13.0 # via -r requirements/base.in maykin-python3-saml==1.14.0.post0 # via django-digid-eherkenning diff --git a/requirements/ci.txt b/requirements/ci.txt index 378cc5bff9..480a87f0cf 100644 --- a/requirements/ci.txt +++ b/requirements/ci.txt @@ -488,6 +488,7 @@ isodate==0.6.1 # via # -c requirements/base.txt # -r requirements/base.txt + # maykin-json-logic-py # maykin-python3-saml # zeep isort==5.10.1 @@ -554,7 +555,7 @@ maykin-django-two-factor-auth[phonenumbers]==2.0.4 # via # -c requirements/base.txt # -r requirements/base.txt -maykin-json-logic-py==0.11.0 +maykin-json-logic-py==0.13.0 # via # -c requirements/base.txt # -r requirements/base.txt diff --git a/requirements/dev.txt b/requirements/dev.txt index 65f89a4089..4867bb956e 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -556,6 +556,7 @@ isodate==0.6.1 # via # -c requirements/ci.txt # -r requirements/ci.txt + # maykin-json-logic-py # maykin-python3-saml # zeep isort==5.10.1 @@ -634,7 +635,7 @@ maykin-django-two-factor-auth[phonenumbers]==2.0.4 # via # -c requirements/ci.txt # -r requirements/ci.txt -maykin-json-logic-py==0.11.0 +maykin-json-logic-py==0.13.0 # via # -c requirements/ci.txt # -r requirements/ci.txt diff --git a/requirements/extensions.txt b/requirements/extensions.txt index c4bb02844e..6df958f247 100644 --- a/requirements/extensions.txt +++ b/requirements/extensions.txt @@ -408,6 +408,7 @@ inflection==0.5.1 isodate==0.6.1 # via # -r requirements/base.txt + # maykin-json-logic-py # maykin-python3-saml # zeep josepy==1.8.0 @@ -459,7 +460,7 @@ maykin-django-two-factor-auth[phonenumbers]==2.0.4 # via # -c requirements/base.in # -r requirements/base.txt -maykin-json-logic-py==0.11.0 +maykin-json-logic-py==0.13.0 # via # -c requirements/base.in # -r requirements/base.txt diff --git a/src/openforms/submissions/tests/form_logic/test_modify_variables.py b/src/openforms/submissions/tests/form_logic/test_modify_variables.py index a3a3b7166a..329a5acecc 100644 --- a/src/openforms/submissions/tests/form_logic/test_modify_variables.py +++ b/src/openforms/submissions/tests/form_logic/test_modify_variables.py @@ -354,3 +354,50 @@ def test_logic_with_repeating_groups(self): self.assertEqual(2, variables_state.variables["numberOfCars"].value) self.assertEqual(5000, variables_state.variables["totalPrice"].value) self.assertEqual(0, variables_state.variables["priceOfThirdCar"].value) + + def test_dates_and_timedeltas(self): + form = FormFactory.create( + generate_minimal_setup=True, + formstep__form_definition__configuration={ + "components": [ + { + "type": "date", + "key": "datum", + }, + ] + }, + ) + FormVariableFactory.create( + key="timedelta", + data_type=FormVariableDataTypes.string, + user_defined=True, + form=form, + initial_value="", + ) + FormLogicFactory.create( + form=form, + json_logic_trigger=True, + actions=[ + { + "variable": "timedelta", + "action": { + "type": "variable", + "value": { + "-": [{"date": {"var": "datum"}}, {"date": "2022-09-09"}] + }, + }, + } + ], + ) + + submission = SubmissionFactory.create(form=form) + submission_step = SubmissionStepFactory.create( + submission=submission, + data={"datum": "2023-09-12"}, + ) + + evaluate_form_logic(submission, submission_step, submission.data) + + variables_state = submission.load_submission_value_variables_state() + + self.assertEqual("P368D", variables_state.variables["timedelta"].value)