Skip to content
Snippets Groups Projects
Commit 6a48e539 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add arg to regex field type to raise an exception if no match was found

parent 610d82bc
No related branches found
No related tags found
1 merge request!108Resolve "Reg ex field type should be able to give feedback if there was no match"
Pipeline #80616 passed
......@@ -9,6 +9,12 @@ and this project adheres to `Semantic Versioning`_.
Unreleased
----------
Added
~~~~~
* If activated, imports with regex field types will show error messages
when there is no match.
`2.3`_ - 2022-06-25
-------------------
......
......@@ -144,16 +144,26 @@ class RegExFieldType(ProcessFieldType):
data_type = str
reg_ex: str = ""
fail_if_no_match: bool = False
def get_reg_ex(self):
return self.reg_ex or self.get_args().get("reg_ex", "")
def get_fail_if_no_match(self):
return self.fail_if_no_match or self.get_args().get("fail_if_no_match", False)
def process(self, instance: Model, value):
match = re.fullmatch(self.get_reg_ex(), value)
if match:
for key, item in match.groupdict().items():
setattr(instance, key, item)
instance.save()
elif self.get_fail_if_no_match():
raise IndexError(
_("No match on {} for regular expression {} found.").format(
value, self.get_reg_ex()
)
)
@field_type_registry.register
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment