Skip to content

Commit

Permalink
Merge pull request #3521 from open-formulieren/feature/2953-timedelta
Browse files Browse the repository at this point in the history
[#2953] Support date periods
  • Loading branch information
SilviaAmAm authored Oct 10, 2023
2 parents 87503a8 + fd0e873 commit 253ff8a
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 9 deletions.
65 changes: 60 additions & 5 deletions docs/manual/forms/logic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
]
}
3 changes: 2 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements/extensions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 253ff8a

Please sign in to comment.