Add validate_fields to ModelStorage

issue11303
review386191002
This commit is contained in:
Cédric Krier
2022-04-11 23:24:21 +02:00
parent ab378ef13e
commit 055bebb564
3 changed files with 36 additions and 26 deletions
+15 -12
View File
@@ -352,22 +352,25 @@ ${COUNTRY}"""
cls._get_format_cache.clear()
@classmethod
def validate(cls, formats):
super(AddressFormat, cls).validate(formats)
for format_ in formats:
format_.check_format()
def validate_fields(cls, formats, field_names):
super().validate_fields(formats, field_names)
cls.check_format(formats, field_names)
def check_format(self):
@classmethod
def check_format(cls, formats, field_names=None):
pool = Pool()
Address = pool.get('party.address')
if field_names and 'format_' not in field_names:
return
address = Address()
try:
Template(self.format_).substitute(
**address._get_address_substitutions())
except Exception as exception:
raise InvalidFormat(gettext('party.invalid_format',
format=self.format_,
exception=exception)) from exception
substitutions = address._get_address_substitutions()
for format_ in formats:
try:
Template(format_.format_).substitute(**substitutions)
except Exception as exception:
raise InvalidFormat(gettext('party.invalid_format',
format=format_.format_,
exception=exception)) from exception
@classmethod
def get_format(cls, address, pattern=None):
+6 -4
View File
@@ -65,13 +65,15 @@ class Configuration(ModelSingleton, ModelSQL, ModelView, MultiValueMixin):
ModelView._fields_view_get_cache.clear()
@classmethod
def validate(cls, records):
super().validate(records)
cls(1).check_identifier_types()
def validate_fields(cls, records, field_names):
super().validate_fields(records, field_names)
cls(1).check_identifier_types(field_names)
def check_identifier_types(self):
def check_identifier_types(self, field_names=None):
pool = Pool()
Identifier = pool.get('party.identifier')
if field_names and 'identifier_types' not in field_names:
return
if self.identifier_types:
identifier_types = [None, ''] + list(self.identifier_types)
identifiers = Identifier.search([
+15 -10
View File
@@ -262,18 +262,23 @@ class ContactMechanism(
cls._format_values(all_mechanisms)
@classmethod
def validate(cls, mechanisms):
super(ContactMechanism, cls).validate(mechanisms)
for mechanism in mechanisms:
mechanism.check_valid_phonenumber()
def validate_fields(cls, mechanisms, field_names):
super().validate_fields(mechanisms, field_names)
cls.check_valid_phonenumber(mechanisms, field_names)
def check_valid_phonenumber(self):
if not phonenumbers or self.type not in _PHONE_TYPES:
@classmethod
def check_valid_phonenumber(cls, mechanisms, field_names=None):
if field_names and not (field_names & {'type', 'value'}):
return
if not self._parse_phonenumber(self.value):
raise InvalidPhoneNumber(
gettext('party.msg_invalid_phone_number',
phone=self.value, party=self.party.rec_name))
if not phonenumbers:
return
for mechanism in mechanisms:
if mechanism.type not in _PHONE_TYPES:
continue
if not mechanism._parse_phonenumber(mechanism.value):
raise InvalidPhoneNumber(
gettext('party.msg_invalid_phone_number',
phone=mechanism.value, party=mechanism.party.rec_name))
@classmethod
def usages(cls, _fields=None):