-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcustom.py
More file actions
1386 lines (1175 loc) · 47.6 KB
/
Copy pathcustom.py
File metadata and controls
1386 lines (1175 loc) · 47.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
from collections.abc import Mapping, MutableMapping, Sequence
from copy import deepcopy
from datetime import date, datetime
from typing import Protocol, assert_never
from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.validators import (
MaxValueValidator,
MinValueValidator,
RegexValidator,
validate_email,
)
from django.utils import timezone
from django.utils.crypto import constant_time_compare
from django.utils.html import format_html
from django.utils.translation import get_language, gettext as _
import structlog
from glom import Path, glom
from rest_framework import ISO_8601, serializers
from rest_framework.fields import get_error_detail
from rest_framework.request import Request
from openforms.api.geojson import (
GeoJsonGeometryPolymorphicSerializer,
GeoJsonGeometryTypes,
)
from openforms.authentication.service import AuthAttribute
from openforms.config.constants import FamilyMembersDataAPIChoices
from openforms.config.models import GlobalConfiguration, MapTileLayer, MapWMSTileLayer
from openforms.contrib.customer_interactions.update import (
update_customer_interaction_data,
)
from openforms.formio.typing.map import Overlay
from openforms.formio.validators import EmailVerificationValidator
from openforms.forms.models import FormVariable
from openforms.prefill.contrib.family_members.plugin import (
PLUGIN_IDENTIFIER as FM_PLUGIN_IDENTIFIER,
)
from openforms.submissions.models import Submission
from openforms.typing import JSONObject, JSONValue, VariableValue
from openforms.utils.date import TIMEZONE_AMS, datetime_in_amsterdam
from openforms.utils.json_schema import GEO_JSON_COORDINATE_SCHEMAS, to_multiple
from openforms.utils.validators import BSNValidator, IBANValidator
from openforms.validations.service import PluginValidator
from openforms.variables.constants import FormVariableDataTypes, FormVariableSources
from ..datastructures import FormioData
from ..dynamic_config.date import mutate as mutate_min_max_validation
from ..formatters.custom import (
AddressNLFormatter,
CosignFormatter,
CustomerProfileFormatter,
DateFormatter,
DateTimeFormatter,
MapFormatter,
)
from ..formatters.formio import (
DefaultFormatter,
TextFieldFormatter,
)
from ..registry import BasePlugin, ComponentPreRegistrationResult, register
from ..typing import (
AddressNLComponent,
ChildrenComponent,
Component,
CustomerProfileComponent,
DateComponent,
DatetimeComponent,
MapComponent,
)
from ..typing.custom import DigitalAddress, SupportedChannels
from ..utils import conform_to_mask
from .np_family_members.haal_centraal import get_np_family_members_haal_centraal
from .np_family_members.stuf_bg import get_np_family_members_stuf_bg
from .utils import _normalize_pattern, salt_location_message
logger = structlog.stdlib.get_logger(__name__)
GEO_JSON_TYPE_TO_INTERACTION = {
GeoJsonGeometryTypes.point: "marker",
GeoJsonGeometryTypes.polygon: "polygon",
GeoJsonGeometryTypes.line_string: "polyline",
}
POSTCODE_REGEX = r"^[1-9][0-9]{3} ?(?!sa|sd|ss|SA|SD|SS)[a-zA-Z]{2}$"
HOUSE_NUMBER_REGEX = r"^\d{1,5}$"
HOUSE_LETTER_REGEX = r"^[a-zA-Z]$"
HOUSE_NUMBER_ADDITION_REGEX = r"^[a-zA-Z0-9]{1,4}$"
class FormioDateField(serializers.DateField):
def validate_empty_values(self, data):
is_empty, data = super().validate_empty_values(data)
# base field only treats `None` as empty, but formio uses empty strings
if data == "":
if self.required:
self.fail("required")
return (True, "")
return is_empty, data
@register("date")
class Date(BasePlugin[DateComponent]):
formatter = DateFormatter
data_type = FormVariableDataTypes.date
empty_value = ""
def mutate_config_dynamically(
self, component: DateComponent, submission: Submission, data: FormioData
) -> None:
"""
Implement the behaviour for our custom date component options.
In the JS, this component type inherits from Formio datetime component. See
``src/openforms/js/components/form/date.js`` for the various configurable options.
"""
mutate_min_max_validation(component, data)
# inject the translated placeholder for the formio DateField component
component["placeholder"] = _("dd-mm-yyyy")
def build_serializer_field(
self, component: DateComponent
) -> FormioDateField | serializers.ListField:
"""
Accept date values.
Additional validation is taken from the datePicker configuration, which is also
set dynamically through our own backend (see :meth:`mutate_config_dynamically`).
"""
# relevant validators: required, datePicker.minDate and datePicker.maxDate
multiple = component.get("multiple", False)
validate = component.get("validate", {})
required = validate.get("required", False)
date_picker = component.get("datePicker") or {}
validators = []
if min_date := date_picker.get("minDate"):
min_value = datetime_in_amsterdam(datetime.fromisoformat(min_date)).date()
validators.append(MinValueValidator(min_value))
if max_date := date_picker.get("maxDate"):
max_value = datetime_in_amsterdam(datetime.fromisoformat(max_date)).date()
validators.append(MaxValueValidator(max_value))
base = FormioDateField(
required=required,
allow_null=not required,
validators=validators,
)
return serializers.ListField(child=base) if multiple else base
@staticmethod
def as_json_schema(component: DateComponent) -> JSONObject:
label = component.get("label", "Date")
multiple = component.get("multiple", False)
base = {"title": label, "format": "date", "type": "string"}
return to_multiple(base) if multiple else base
class FormioDateTimeField(serializers.DateTimeField):
def validate_empty_values(self, data):
is_empty, data = super().validate_empty_values(data)
# base field only treats `None` as empty, but formio uses empty strings
if data == "":
if self.required:
self.fail("required")
return (True, "")
return is_empty, data
def to_internal_value(self, value):
# we *only* accept datetimes in ISO-8601 format. Python will happily parse a
# YYYY-MM-DD string as a datetime (with hours/minutes set to 0). For a component
# specifically aimed at datetimes, this is not a valid input.
if value and isinstance(value, str) and "T" not in value:
self.fail("invalid", format="YYYY-MM-DDTHH:mm:ss+XX:YY")
return super().to_internal_value(value)
def _normalize_validation_datetime(value: str) -> datetime:
"""
Takes a string expected to contain an ISO-8601 datetime and normalizes it.
Seconds and time zone information may be missing. If it is, assume Europe/Amsterdam.
:return: Time-zone aware datetime.
"""
parsed = datetime.fromisoformat(value)
if timezone.is_naive(parsed):
parsed = timezone.make_aware(parsed, timezone=TIMEZONE_AMS)
return parsed
@register("datetime")
class Datetime(BasePlugin):
formatter = DateTimeFormatter
data_type = FormVariableDataTypes.datetime
empty_value = ""
def mutate_config_dynamically(
self,
component: DatetimeComponent,
submission: Submission,
data: FormioData,
) -> None:
"""
Implement the behaviour for our custom datetime component options.
"""
mutate_min_max_validation(component, data)
# inject the translated placeholder for the formio DateTimeField component
component["placeholder"] = _("dd-mm-yyyy HH:mm")
def build_serializer_field(
self, component: DateComponent
) -> FormioDateTimeField | serializers.ListField:
"""
Accept datetime values.
Additional validation is taken from the datePicker configuration, which is also
set dynamically through our own backend (see :meth:`mutate_config_dynamically`).
"""
# relevant validators: required, datePicker.minDate and datePicker.maxDate
multiple = component.get("multiple", False)
validate = component.get("validate", {})
required = validate.get("required", False)
date_picker = component.get("datePicker") or {}
validators = []
if min_date := date_picker.get("minDate"):
min_value = _normalize_validation_datetime(min_date)
validators.append(MinValueValidator(min_value))
if max_date := date_picker.get("maxDate"):
max_value = _normalize_validation_datetime(max_date)
validators.append(MaxValueValidator(max_value))
base = FormioDateTimeField(
input_formats=[ISO_8601],
required=required,
allow_null=not required,
validators=validators,
)
return serializers.ListField(child=base) if multiple else base
@staticmethod
def as_json_schema(component: Component) -> JSONObject:
label = component.get("label", "Date time")
multiple = component.get("multiple", False)
base = {"title": label, "format": "date-time", "type": "string"}
return to_multiple(base) if multiple else base
@register("map")
class Map(BasePlugin[MapComponent]):
formatter = MapFormatter
data_type = FormVariableDataTypes.object
empty_value = None
def mutate_config_dynamically(
self, component: MapComponent, submission: Submission, data: FormioData
) -> None:
if (identifier := component.get("tileLayerIdentifier")) is not None:
tile_layer = MapTileLayer.objects.filter(identifier=identifier).first()
if tile_layer is not None:
# Add the tile layer url information
component["tileLayerUrl"] = tile_layer.url
if overlays := component.get("overlays", []):
# inject the map layer URLs for the SDK
wms_uuids = (
overlay["uuid"] for overlay in overlays if overlay["type"] == "wms"
)
wms_layers: Mapping[str, str] = {
str(uuid): str(url)
for (uuid, url) in MapWMSTileLayer.objects.filter(
uuid__in=wms_uuids
).values_list("uuid", "url")
}
updated_overlays: list[Overlay] = [
{**overlay, "url": wms_layer_url}
for overlay in overlays
# only keep overlays that don't have stale UUID references
if (wms_layer_url := wms_layers.get(overlay["uuid"]))
]
component["overlays"] = updated_overlays
@staticmethod
def rewrite_for_request(component: MapComponent, request: Request):
if component.get("useConfigDefaultMapSettings", False):
config = GlobalConfiguration.get_solo()
component["defaultZoom"] = config.form_map_default_zoom_level
component.setdefault("initialCenter", {})
component["initialCenter"]["lat"] = config.form_map_default_latitude
component["initialCenter"]["lng"] = config.form_map_default_longitude
def build_serializer_field(
self, component: MapComponent
) -> GeoJsonGeometryPolymorphicSerializer:
validate = component.get("validate", {})
required = validate.get("required", False)
return GeoJsonGeometryPolymorphicSerializer(
required=required, allow_null=not required
)
@staticmethod
def as_json_schema(component: MapComponent) -> JSONObject:
label = component.get("label", "Map")
interactions = component["interactions"]
properties = [
{
"properties": {
"type": {"type": "string", "const": geometry_type},
"coordinates": GEO_JSON_COORDINATE_SCHEMAS[geometry_type],
},
"additionalProperties": False,
}
for geometry_type in GeoJsonGeometryTypes.values
# Only include the schema of types that are allowed
if interactions.get(GEO_JSON_TYPE_TO_INTERACTION[geometry_type], False)
]
schema = {
"title": label,
"type": "object",
"required": ["type", "coordinates"],
}
if len(properties) == 1:
schema.update(properties[0])
else:
schema["oneOf"] = properties
return schema
@register("postcode")
class Postcode(BasePlugin[Component]):
formatter = TextFieldFormatter
data_type = FormVariableDataTypes.string
empty_value = ""
@staticmethod
def normalizer(component: Component, value: str) -> str:
if not value:
return value
input_mask = component.get("inputMask")
if not input_mask:
return value
try:
return conform_to_mask(value, input_mask)
except ValueError as exc:
logger.warning(
"formio.postcode_to_mask_failure",
input_mask=input_mask,
value=value,
component=component,
exc_info=exc,
)
return value
def build_serializer_field(
self, component: Component
) -> serializers.CharField | serializers.ListField:
multiple = component.get("multiple", False)
validate = component.get("validate", {})
required = validate.get("required", False)
# dynamically add in more kwargs based on the component configuration
extra = {}
validators = []
# adding in the validator is more explicit than changing to
# serializers.RegexField, which essentially does the same.
if pattern := validate.get("pattern"):
validators.append(
RegexValidator(
_normalize_pattern(pattern),
message=_("This value does not match the required pattern."),
)
)
if plugin_ids := validate.get("plugins", []):
validators.append(PluginValidator(plugin_ids))
if validators:
extra["validators"] = validators
base = serializers.CharField(
required=required, allow_blank=not required, **extra
)
return serializers.ListField(child=base) if multiple else base
@staticmethod
def as_json_schema(component: Component) -> JSONObject:
label = component.get("label", "Postcode")
multiple = component.get("multiple", False)
validate = component.get("validate", {})
base = {
"title": label,
"type": "string",
"pattern": validate.get("pattern", POSTCODE_REGEX),
}
return to_multiple(base) if multiple else base
class FamilyMembersHandler(Protocol):
def __call__(
self,
bsn: str,
include_children: bool,
include_partner: bool,
submission: Submission | None = ...,
) -> list[tuple[str, str]]: ...
@register("npFamilyMembers")
class NPFamilyMembers(BasePlugin):
# not actually relevant, as we transform the component into a different type
formatter = DefaultFormatter
data_type = FormVariableDataTypes.object
empty_value = {}
def build_serializer_field(self, component: Component) -> serializers.Field:
raise NotImplementedError()
@staticmethod
def _get_handler() -> FamilyMembersHandler:
handlers = {
FamilyMembersDataAPIChoices.haal_centraal: get_np_family_members_haal_centraal,
FamilyMembersDataAPIChoices.stuf_bg: get_np_family_members_stuf_bg,
}
config = GlobalConfiguration.get_solo()
return handlers[config.family_members_data_api]
def mutate_config_dynamically(
self, component: Component, submission: Submission, data: FormioData
) -> None:
# Check authentication details/status before proceeding
has_bsn = (
submission.is_authenticated
and submission.auth_info.attribute == AuthAttribute.bsn
)
if not has_bsn:
component.update(
{
"type": "content",
"html": format_html(
"<p>{message}</p>",
message=_(
"Selecting family members is currently not available."
),
),
"input": False,
}
)
return
bsn = submission.auth_info.value
component.update(
{
"type": "selectboxes",
"fieldSet": False,
"inline": False,
"inputType": "checkbox",
}
)
if "mask" in component:
del component["mask"]
existing_values = component.get("values", [])
empty_option = {
"label": "",
"value": "",
}
if not existing_values or existing_values[0] == empty_option:
handler = self._get_handler()
# make the API call
# TODO: this should eventually be replaced with logic rules/variables that
# retrieve data from an "arbitrary source", which will cause the data to
# become available in the ``data`` argument instead.
child_choices = handler(
bsn,
include_children=component.get("includeChildren", True),
include_partners=component.get("includePartners", True),
submission=submission,
)
component["values"] = [
{
"label": label,
"value": value,
}
for value, label in child_choices
]
@staticmethod
def as_json_schema(component: Component) -> JSONObject:
# This component plugin is transformed into a SelectBoxes component, so a schema
# is not relevant here
raise NotImplementedError()
@register("bsn")
class BSN(BasePlugin[Component]):
formatter = TextFieldFormatter
data_type = FormVariableDataTypes.string
empty_value = ""
def build_serializer_field(
self, component: Component
) -> serializers.CharField | serializers.ListField:
multiple = component.get("multiple", False)
validate = component.get("validate", {})
required = validate.get("required", False)
# dynamically add in more kwargs based on the component configuration
extra = {}
validators = [BSNValidator()]
if plugin_ids := validate.get("plugins", []):
validators.append(PluginValidator(plugin_ids))
extra["validators"] = validators
base = serializers.CharField(
required=required,
allow_blank=not required,
# FIXME: should always be False, but formio client sends `null` for
# untouched fields :( See #4068
allow_null=multiple,
**extra,
)
return serializers.ListField(child=base) if multiple else base
@staticmethod
def as_json_schema(component: Component) -> JSONObject:
label = component.get("label", "BSN")
multiple = component.get("multiple", False)
base = {
"title": label,
"type": "string",
"pattern": r"^\d{9}$",
"format": "nl-bsn",
}
return to_multiple(base) if multiple else base
class AddressValueSerializer(serializers.Serializer):
postcode = serializers.RegexField(POSTCODE_REGEX)
houseNumber = serializers.RegexField(HOUSE_NUMBER_REGEX)
houseLetter = serializers.RegexField(
HOUSE_LETTER_REGEX, required=False, allow_blank=True
)
houseNumberAddition = serializers.RegexField(
HOUSE_NUMBER_ADDITION_REGEX,
required=False,
allow_blank=True,
)
streetName = serializers.CharField(
label=_("street name"),
help_text=_("Derived street name"),
required=False,
allow_blank=True,
)
city = serializers.CharField(
label=_("city"),
help_text=_("Derived city"),
required=False,
allow_blank=True,
)
secretStreetCity = serializers.CharField(
label=_("city and street name secret"),
help_text=_("Secret for the combination of city and street name"),
required=False,
allow_blank=True,
)
def __init__(self, **kwargs):
self.derive_address = kwargs.pop("derive_address", None)
self.component = kwargs.pop("component", None)
super().__init__(**kwargs)
def get_fields(self):
fields = super().get_fields()
# if the field as a whole is not required, postcode & house number become
# optional
postcode = fields["postcode"]
assert isinstance(postcode, serializers.RegexField)
postcode.allow_blank = not self.required
house_number = fields["houseNumber"]
assert isinstance(house_number, serializers.RegexField)
house_number.allow_blank = not self.required
return fields
def validate_city(self, value: str) -> str:
if city_regex := glom(
self.component, "openForms.components.city.validate.pattern", default=""
):
if not re.fullmatch(city_regex, value):
language_code = get_language().split("-")[0]
error_message = glom(
self.component,
Path(
"openForms",
"components",
"city",
"translatedErrors",
language_code,
"pattern",
),
default=_("City does not match the specified pattern."),
)
raise serializers.ValidationError(error_message, code="invalid")
return value
def validate_postcode(self, value: str) -> str:
"""Normalize the postcode so that it matches the regex from the BRK API."""
if postcode_regex := glom(
self.component, "openForms.components.postcode.validate.pattern", default=""
):
if not re.fullmatch(postcode_regex, value):
language_code = get_language().split("-")[0]
error_message = glom(
self.component,
Path(
"openForms",
"components",
"postcode",
"translatedErrors",
language_code,
"pattern",
),
default=_("Postcode does not match the specified pattern."),
)
raise serializers.ValidationError(error_message, code="invalid")
return value.upper().replace(" ", "")
def validate(self, attrs):
attrs = super().validate(attrs)
city = attrs.get("city", "")
street_name = attrs.get("streetName", "")
if self.derive_address:
existing_hmac = attrs.get("secretStreetCity", "")
postcode = attrs.get("postcode", "")
number = attrs.get("houseNumber", "")
computed_hmac = salt_location_message(
{
"postcode": postcode,
"number": number,
"city": city,
"street_name": street_name,
}
)
if not constant_time_compare(existing_hmac, computed_hmac):
raise serializers.ValidationError(
_("Invalid secret city - street name combination"),
code="invalid",
)
return attrs
@register("addressNL")
class AddressNL(BasePlugin[AddressNLComponent]):
formatter = AddressNLFormatter
data_type = FormVariableDataTypes.object
# addressNL is a composite field, and rendering it in the UI will cause the
# sub-paths to be set. We can't use `null` as empty value, as that would
# lead to inconsistent empty-checks, as you want to reliably point to
# {"var": "myComponent.postcode"} for a test against empty string
empty_value = {
"postcode": "",
"houseNumber": "",
"houseLetter": "",
"houseNumberAddition": "",
"streetName": "",
"city": "",
"secretStreetCity": "",
"autoPopulated": False,
}
def build_serializer_field(
self, component: AddressNLComponent
) -> AddressValueSerializer:
validate = component.get("validate", {})
required = validate.get("required", False)
extra = {}
validators = []
if plugin_ids := validate.get("plugins", []):
validators.append(PluginValidator(plugin_ids))
extra["validators"] = validators
return AddressValueSerializer(
derive_address=component["deriveAddress"],
required=required,
allow_null=not required,
component=component,
**extra,
)
@staticmethod
def as_json_data(component: Component, value: VariableValue) -> VariableValue:
"""
Drop internal keys from the data.
"""
assert isinstance(value, dict)
value = value.copy()
# remove internal data
value.pop("secretStreetCity", None)
return value
@staticmethod
def as_json_schema(component: AddressNLComponent) -> JSONObject:
label = component.get("label", "Address NL")
components = component.get("openForms", {}).get("components", {})
postcode_validate = components.get("postcode", {}).get("validate", {})
city_validate = components.get("city", {}).get("validate", {})
base = {
"title": label,
"type": "object",
"properties": {
"city": {"type": "string"},
"houseLetter": {"type": "string", "pattern": HOUSE_LETTER_REGEX},
"houseNumber": {"type": "string", "pattern": HOUSE_NUMBER_REGEX},
"houseNumberAddition": {
"type": "string",
"pattern": HOUSE_NUMBER_ADDITION_REGEX,
},
"postcode": {
"type": "string",
"pattern": postcode_validate.get("pattern", POSTCODE_REGEX),
},
"streetName": {"type": "string"},
"autoPopulated": {"type": "boolean"},
},
"required": ["houseNumber", "postcode"],
}
if city_pattern := city_validate.get("pattern"):
base["properties"]["city"]["pattern"] = city_pattern
return base
class PartnerSerializer(serializers.Serializer):
bsn = serializers.CharField(
label=_("bsn"),
max_length=9,
help_text=_("The BSN of the partner"),
validators=[BSNValidator()],
)
initials = serializers.CharField(
label=_("initials"),
help_text=_("The initials of the partner"),
allow_blank=True,
)
affixes = serializers.CharField(
label=_("affixes"),
help_text=_("The affixes of the partner"),
allow_blank=True,
)
lastName = serializers.CharField(
label=_("last name"),
help_text=_("The last name of the partner"),
allow_blank=True,
)
dateOfBirth = FormioDateField(
label=_("date of birth"),
help_text=_("The date of birth of the partner"),
)
def __init__(self, **kwargs):
self.component = kwargs.pop("component", None)
super().__init__(**kwargs)
class PartnerListField(serializers.Field):
def __init__(self, component, **kwargs):
self.component = component
super().__init__(**kwargs)
def to_internal_value(self, data):
if not isinstance(data, list):
raise serializers.ValidationError("Expected a list of partners.")
serializer = PartnerSerializer(
data=data,
many=True,
component=self.component,
)
serializer.is_valid(raise_exception=True)
validated = serializer.validated_data
self.validate_list(validated)
return validated
def to_representation(self, value):
return PartnerSerializer(value, many=True, component=self.component).data
def validate_list(self, partners):
component_key = self.component["key"]
submission = self.context["submission"]
state = submission.variables_state
prefill_data = state.get_prefilled_data()
fm_immutable_variable = FormVariable.objects.filter(
source=FormVariableSources.user_defined,
prefill_plugin=FM_PLUGIN_IDENTIFIER,
prefill_options__mutable_data_form_variable=component_key,
form=submission.form,
).first()
if fm_immutable_variable:
# we do not receive these fields from the frontend (since they are not used
# for now) so we have to exclude them from the data that needs validation
initial_value = [
{
key: (
# date format for StUF-BG (yyyymmdd) is different from HaalCentraal
# (yyyy-MM-dd)
date.fromisoformat(value)
if key == "dateOfBirth" and value
else value
)
for key, value in partner.items()
if key not in ("dateOfBirthPrecision", "firstNames", "deceased")
}
for partner in prefill_data[fm_immutable_variable.key]
]
if initial_value and initial_value != partners:
raise serializers.ValidationError(
_("The family members prefill data may not be altered.")
)
@register("partners")
class Partners(BasePlugin[Component]):
formatter = DefaultFormatter
data_type = FormVariableDataTypes.array
data_subtype = FormVariableDataTypes.partners
empty_value = []
def build_serializer_field(self, component: Component) -> PartnerListField:
return PartnerListField(component=component)
@staticmethod
def as_json_data(component: Component, value: VariableValue) -> VariableValue:
"""
Drop the internal data keys from the partner objects.
"""
assert isinstance(value, Sequence)
value = deepcopy(value)
for partner in value:
assert isinstance(partner, MutableMapping)
partner.pop("dateOfBirthPrecision", None)
partner.pop("__addedManually", None)
return value
@staticmethod
def as_json_schema(component: Component) -> JSONObject:
label = component.get("label", "Partners")
schema = {
"title": label,
"type": "array",
"items": {
"type": "object",
"required": ["bsn"],
"properties": {
"bsn": {
"type": "string",
"pattern": r"^\d{9}$",
"format": "nl-bsn",
},
"initials": {"type": "string"},
"affixes": {"type": "string"},
"lastName": {"type": "string"},
"dateOfBirth": {"type": "string", "format": "date"},
# obtained from prefill, not exposed in the UI.
"firstNames": {"type": "string"},
},
"additionalProperties": False,
},
}
return schema
class ChildSerializer(serializers.Serializer):
bsn = serializers.CharField(
label=_("bsn"),
max_length=9,
help_text=_("The BSN of the child"),
validators=[BSNValidator()],
)
firstNames = serializers.CharField(
label=_("firstNames"),
help_text=_("The first names of the child"),
allow_blank=True,
)
dateOfBirth = FormioDateField(
label=_("date of birth"),
help_text=_("The date of birth of the child"),
)
selected = serializers.BooleanField(
label=_("selected"),
allow_null=True,
help_text=_(
"Whether the child is selected by the user or not for further processing"
),
)
def __init__(self, **kwargs):
self.component = kwargs.pop("component", None)
super().__init__(**kwargs)
class ChildListField(serializers.Field):
def __init__(self, component, **kwargs):
self.component = component
super().__init__(**kwargs)
def to_internal_value(self, data):
if not isinstance(data, list):
raise serializers.ValidationError("Expected a list of children.")
serializer = ChildSerializer(
data=data,
many=True,
component=self.component,
)
serializer.is_valid(raise_exception=True)
validated = serializer.validated_data
self.validate_list(validated)
return validated
def to_representation(self, value):
return ChildSerializer(value, many=True, component=self.component).data
# TODO
# Add proper type hints when #2324 is completed (sync the frontend with the backend)
def validate_list(self, children):
component_key = self.component["key"]
submission = self.context["submission"]
state = submission.variables_state
prefill_data = state.get_prefilled_data()
fm_immutable_variable = FormVariable.objects.filter(
source=FormVariableSources.user_defined,
prefill_plugin=FM_PLUGIN_IDENTIFIER,
prefill_options__mutable_data_form_variable=component_key,
form=submission.form,
).first()
if fm_immutable_variable:
# we do not receive these fields from the frontend (since they are not used
# for now) so we have to exclude them from the data that needs validation.
initial_value = [
{
key: (
# date format for StUF-BG (yyyymmdd) is different from HaalCentraal
# (yyyy-MM-dd)
date.fromisoformat(value)
if key == "dateOfBirth" and value
else value
)
for key, value in child.items()
if key
not in (
"dateOfBirthPrecision",
"lastName",
"affixes",
"initials",
"deceased",
)
}
for child in prefill_data[fm_immutable_variable.key]
]