Coverage for gws-app/gws/plugin/model_validator/date_range/__init__.py: 0%
31 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""Validator for date ranges.
3Checks that the field value is a date between ``min`` and ``max``, both
4inclusive. The bounds are model values, so they can be fixed dates or
5computed, e.g. the current date. A bound that computes to a string is
6compared as an ISO date string. A value that is not a date fails the check.
8Example::
10 fields+ {
11 name "inspection_date"
12 type "date"
13 validators+ {
14 type "dateRange"
15 min { type "static" value "2000-01-01" }
16 max { type "currentTimestamp" }
17 }
18 }
19"""
21from typing import Optional
23import gws
24import gws.base.model.validator
25import gws.lib.datetimex as dt
28@gws.ext.config.modelValidator('dateRange')
29class Config(gws.base.model.validator.Config):
30 """Checks that a date lies within a range."""
32 min: Optional[gws.ext.config.modelValue]
33 """Earliest allowed date."""
34 max: Optional[gws.ext.config.modelValue]
35 """Latest allowed date."""
38@gws.ext.object.modelValidator('dateRange')
39class Object(gws.base.model.validator.Object):
40 """Date range validator object."""
42 minVal: Optional[gws.ModelValue]
43 """Value that computes the earliest allowed date."""
44 maxVal: Optional[gws.ModelValue]
45 """Value that computes the latest allowed date."""
47 def configure(self):
48 self.minVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('min'))
49 self.maxVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('max'))
51 def validate(self, field, feature, mc):
52 val = feature.attributes.get(field.name)
53 if not dt.is_date(val):
54 return False
56 d = dt.to_iso_date_string(val)
58 if self.minVal:
59 v = self.minVal.compute(field, feature, mc)
60 s = v if isinstance(v, str) else dt.to_iso_date_string(v)
61 if d < s:
62 return False
64 if self.maxVal:
65 v = self.maxVal.compute(field, feature, mc)
66 s = v if isinstance(v, str) else dt.to_iso_date_string(v)
67 if d > s:
68 return False
70 return True