Skip to content
Snippets Groups Projects
Commit 1ba3c1d6 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Merge branch...

Merge branch '50-reg-ex-field-type-should-be-able-to-give-feedback-if-there-was-no-match' into 'master'

Resolve "Reg ex field type should be able to give feedback if there was no match"

Closes #50

See merge request !108
parents 610d82bc 6a48e539
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 #80696 failed
Pipeline: AlekSIS

#80701

    ...@@ -9,6 +9,12 @@ and this project adheres to `Semantic Versioning`_. ...@@ -9,6 +9,12 @@ and this project adheres to `Semantic Versioning`_.
    Unreleased Unreleased
    ---------- ----------
    Added
    ~~~~~
    * If activated, imports with regex field types will show error messages
    when there is no match.
    `2.3`_ - 2022-06-25 `2.3`_ - 2022-06-25
    ------------------- -------------------
    ......
    ...@@ -144,16 +144,26 @@ class RegExFieldType(ProcessFieldType): ...@@ -144,16 +144,26 @@ class RegExFieldType(ProcessFieldType):
    data_type = str data_type = str
    reg_ex: str = "" reg_ex: str = ""
    fail_if_no_match: bool = False
    def get_reg_ex(self): def get_reg_ex(self):
    return self.reg_ex or self.get_args().get("reg_ex", "") 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): def process(self, instance: Model, value):
    match = re.fullmatch(self.get_reg_ex(), value) match = re.fullmatch(self.get_reg_ex(), value)
    if match: if match:
    for key, item in match.groupdict().items(): for key, item in match.groupdict().items():
    setattr(instance, key, item) setattr(instance, key, item)
    instance.save() 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 @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