Skip to content

Commit

Permalink
Adiciona error_level como parâmetros de AffiliationValidation.validat…
Browse files Browse the repository at this point in the history
…e_main_affiliations e validate_translated_affiliations (#719)

* Adiciona AffiliationValidation.validate_label; Condiciona o retorno das respostas quando há erro ou quando o error_level é INFO; Adiciona parâmetros de error_level para cada componente da afiliação

* Ajusta os testes de aff
  • Loading branch information
robertatakenaka authored Oct 8, 2024
1 parent 484149b commit 10c3300
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 173 deletions.
295 changes: 163 additions & 132 deletions packtools/sps/validation/aff.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, xml_tree, country_codes_list=None):

def validate_affiliations_list(self, country_codes_list=None):
"""
Validate the list of affiliations against a list of country codes.
Validate all the document affiliations
Parameters
----------
Expand All @@ -55,17 +55,35 @@ def validate_affiliations_list(self, country_codes_list=None):
yield from self.validate_main_affiliations(country_codes_list)
yield from self.validate_translated_affiliations(country_codes_list)

def validate_main_affiliations(self, country_codes_list=None):
for affiliation in self.affiliations.article_affs():
def validate_main_affiliations(self, country_codes_list=None, id_error_level=None, label_error_level=None, original_error_level=None, orgname_error_level=None, country_error_level=None, country_code_error_level=None, state_error_level=None, city_error_level=None):
items = list(self.affiliations.article_affs())
total = len(items)
if total == 1:
label_error_level = "INFO"
for affiliation in items:
yield from AffiliationValidation(
affiliation, country_codes_list
).validate_main_affiliation()
).validate(
id_error_level, label_error_level, original_error_level, orgname_error_level, country_error_level, country_code_error_level, state_error_level, city_error_level
)

def validate_translated_affiliations(self, country_codes_list=None):
for affiliation in self.affiliations.sub_article_translation_affs():
def validate_translated_affiliations(self, country_codes_list=None, id_error_level=None, label_error_level=None, original_error_level=None, orgname_error_level=None, country_error_level=None, country_code_error_level=None, state_error_level=None, city_error_level=None):
orgname_error_level = orgname_error_level or "INFO"
country_error_level = country_error_level or "INFO"
country_code_error_level = country_code_error_level or "INFO"
state_error_level = state_error_level or "INFO"
city_error_level = city_error_level or "INFO"

items = list(self.affiliations.sub_article_translation_affs())
total = len(items)
if total == 1:
label_error_level = "INFO"
for affiliation in items:
yield from AffiliationValidation(
affiliation, country_codes_list
).validate_translated_affiliation()
).validate(
id_error_level, label_error_level, original_error_level, orgname_error_level, country_error_level, country_code_error_level, state_error_level, city_error_level
)

def validate(self, data):
"""
Expand Down Expand Up @@ -162,19 +180,20 @@ def validate_original(self, error_level=None):
"""
original = self.affiliation.get("original")
error_level = error_level or "ERROR"
return build_response(
title="original",
parent=self.affiliation,
item="institution",
sub_item='@content-type="original"',
validation_type="exist",
is_valid=bool(original),
expected=_("original affiliation"),
obtained=original,
advice=_("provide the original affiliation"),
data=self.affiliation,
error_level=error_level,
)
if not original or error_level == "INFO":
yield build_response(
title="original",
parent=self.affiliation,
item="institution",
sub_item='@content-type="original"',
validation_type="exist",
is_valid=bool(original),
expected=_("original affiliation"),
obtained=original,
advice=_("provide the original affiliation"),
data=self.affiliation,
error_level=error_level,
)

def validate_orgname(self, error_level=None):
"""
Expand All @@ -192,19 +211,51 @@ def validate_orgname(self, error_level=None):
"""
orgname = self.affiliation.get("orgname")
error_level = error_level or "CRITICAL"
return build_response(
title="orgname",
parent=self.affiliation,
item="institution",
sub_item='@content-type="orgname"',
validation_type="exist",
is_valid=bool(orgname),
expected=_("orgname"),
obtained=orgname,
advice=_("provide the orgname"),
data=self.affiliation,
error_level=error_level,
)
if not orgname or error_level == "INFO":
yield build_response(
title="orgname",
parent=self.affiliation,
item="institution",
sub_item='@content-type="orgname"',
validation_type="exist",
is_valid=bool(orgname),
expected=_("orgname"),
obtained=orgname,
advice=_("provide the orgname"),
data=self.affiliation,
error_level=error_level,
)

def validate_label(self, error_level=None):
"""
Validate the presence of the label affiliation content.
Parameters
----------
error_level : str, optional
The level of error to be reported. Default is "CRITICAL".
Yields
------
dict
A dictionary containing the validation result for the orgname affiliation.
"""
label = self.affiliation.get("label")
error_level = error_level or "CRITICAL"
if not label or error_level == "INFO":
yield build_response(
title="label",
parent=self.affiliation,
item="aff",
sub_item="label",
validation_type="exist",
is_valid=bool(label),
expected=_("label"),
obtained=label,
advice=_("provide the label"),
data=self.affiliation,
error_level=error_level,
)

def validate_country(self, error_level=None):
"""
Expand All @@ -222,19 +273,20 @@ def validate_country(self, error_level=None):
"""
country = self.affiliation.get("country_name")
error_level = error_level or "CRITICAL"
return build_response(
title="country name",
parent=self.affiliation,
item="aff",
sub_item="country",
validation_type="exist",
is_valid=bool(country),
expected=_("country name"),
obtained=country,
advice=_("provide the country name"),
data=self.affiliation,
error_level=error_level,
)
if not country or error_level == "INFO":
yield build_response(
title="country name",
parent=self.affiliation,
item="aff",
sub_item="country",
validation_type="exist",
is_valid=bool(country),
expected=_("country name"),
obtained=country,
advice=_("provide the country name"),
data=self.affiliation,
error_level=error_level,
)

def validate_country_code(self, error_level=None):
"""
Expand All @@ -259,19 +311,20 @@ def validate_country_code(self, error_level=None):
)
country_code = self.affiliation.get("country_code")
error_level = error_level or "CRITICAL"
return build_response(
title="country code",
parent=self.affiliation,
item="country",
sub_item="@country",
validation_type="value in list",
is_valid=country_code in country_codes_list,
expected=self.country_codes_list,
obtained=country_code,
advice=_("provide a valid @country"),
data=self.affiliation,
error_level=error_level,
)
if (not country_code in country_codes_list) or (error_level == "INFO"):
yield build_response(
title="country code",
parent=self.affiliation,
item="country",
sub_item="@country",
validation_type="value in list",
is_valid=country_code in country_codes_list,
expected=self.country_codes_list,
obtained=country_code,
advice=_("provide a valid @country"),
data=self.affiliation,
error_level=error_level,
)

def validate_state(self, error_level=None):
"""
Expand All @@ -289,19 +342,20 @@ def validate_state(self, error_level=None):
"""
state = self.affiliation.get("state")
error_level = error_level or "ERROR"
return build_response(
title="state",
parent=self.affiliation,
item="addr-line",
sub_item="state",
validation_type="exist",
is_valid=bool(state),
expected=_("state"),
obtained=state,
advice=_("provide the state"),
data=self.affiliation,
error_level=error_level,
)
if not state or error_level == "INFO":
yield build_response(
title="state",
parent=self.affiliation,
item="addr-line",
sub_item="state",
validation_type="exist",
is_valid=bool(state),
expected=_("state"),
obtained=state,
advice=_("provide the state"),
data=self.affiliation,
error_level=error_level,
)

def validate_city(self, error_level=None):
"""
Expand All @@ -319,19 +373,20 @@ def validate_city(self, error_level=None):
"""
city = self.affiliation.get("city")
error_level = error_level or "ERROR"
return build_response(
title="city",
parent=self.affiliation,
item="addr-line",
sub_item="city",
validation_type="exist",
is_valid=bool(city),
expected=_("city"),
obtained=city,
advice=_("provide the city"),
data=self.affiliation,
error_level=error_level,
)
if not city or error_level == "INFO":
yield build_response(
title="city",
parent=self.affiliation,
item="addr-line",
sub_item="city",
validation_type="exist",
is_valid=bool(city),
expected=_("city"),
obtained=city,
advice=_("provide the city"),
data=self.affiliation,
error_level=error_level,
)

def validate_id(self, error_level=None):
"""
Expand All @@ -349,33 +404,22 @@ def validate_id(self, error_level=None):
"""
aff_id = self.affiliation.get("id")
error_level = error_level or "CRITICAL"
return build_response(
title="id",
parent=self.affiliation,
item="aff",
sub_item="@id",
validation_type="exist",
is_valid=bool(aff_id),
expected=_("affiliation ID"),
obtained=aff_id,
advice=_("provide the affiliation ID"),
data=self.affiliation,
error_level=error_level,
)

def validate_affiliation(self):
"""
Validate the affiliation
Yields
------
dict
A dictionary containing the validation results for the affiliation.
"""
yield self.validate_main_affiliation()
yield self.validate_translated_affiliation()
if not aff_id or error_level == "INFO":
yield build_response(
title="id",
parent=self.affiliation,
item="aff",
sub_item="@id",
validation_type="exist",
is_valid=bool(aff_id),
expected=_("affiliation ID"),
obtained=aff_id,
advice=_("provide the affiliation ID"),
data=self.affiliation,
error_level=error_level,
)

def validate_main_affiliation(self, id_error_level=None, original_error_level=None, orgname_error_level=None, country_error_level=None, country_code_error_level=None, state_error_level=None, city_error_level=None):
def validate(self, id_error_level=None, label_error_level=None, original_error_level=None, orgname_error_level=None, country_error_level=None, country_code_error_level=None, state_error_level=None, city_error_level=None):
"""
Validate the affiliation
Expand All @@ -384,24 +428,11 @@ def validate_main_affiliation(self, id_error_level=None, original_error_level=No
dict
A dictionary containing the validation results for the affiliation.
"""
if self.affiliation.get("parent_article_type") != "translation":
yield self.validate_id(id_error_level)
yield self.validate_original(original_error_level)
yield self.validate_orgname(orgname_error_level)
yield self.validate_country(country_error_level)
yield self.validate_country_code(country_code_error_level)
yield self.validate_state(state_error_level)
yield self.validate_city(city_error_level)

def validate_translated_affiliation(self, id_error_level=None, original_error_level=None):
"""
Validate the translated affiliation
Yields
------
dict
A dictionary containing the validation results for the translated affiliation.
"""
if self.affiliation.get("parent_article_type") == "translation":
yield self.validate_id(id_error_level)
yield self.validate_original(original_error_level)
yield from self.validate_id(id_error_level)
yield from self.validate_label(label_error_level)
yield from self.validate_original(original_error_level)
yield from self.validate_orgname(orgname_error_level)
yield from self.validate_country(country_error_level)
yield from self.validate_country_code(country_code_error_level)
yield from self.validate_state(state_error_level)
yield from self.validate_city(city_error_level)
Loading

0 comments on commit 10c3300

Please sign in to comment.