Skip to content

Commit

Permalink
Merge pull request #912 from watchdogpolska/institution-import
Browse files Browse the repository at this point in the history
Some improvements to institution import script.
  • Loading branch information
dzejkobi committed Nov 26, 2020
2 parents 4e30d2f + ffa6892 commit 75e0b80
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions api_examples/insert_institution_csv.py
Expand Up @@ -92,6 +92,14 @@ def get_build_args(self, argv):
action="store",
default=None,
)
parser.add_argument(
"--starting-row",
required=False,
help="CSV input file row to start export",
dest="starting_row",
type=int,
default=1,
)
parser.add_argument(
"--name-exact",
required=False,
Expand All @@ -109,11 +117,20 @@ def get_build_args(self, argv):
default="",
)
parser.add_argument(
"--name-suffix-field",
"--name-prefix-no-check",
required=False,
help="Should name prefix be compared with GUS data "
"or only used to get suffix",
dest="name_prefix_no_check",
action="store_true",
default=False,
)
parser.add_argument(
"--name-suffix-gus-field",
required=False,
help="In what field of GUS REGON data should institution suffix "
help="In which field of GUS REGON data should institution suffix "
"be looked for",
dest="name_suffix_field",
dest="name_suffix_gus_field",
action="store",
default=None,
)
Expand Down Expand Up @@ -223,6 +240,7 @@ def _get_and_validate_regon_data(self, data):

if (
self.args.name_prefix
and not self.args.name_prefix_no_check
and self.args.name_prefix.upper() not in regon_data_name
):
print(
Expand All @@ -238,13 +256,13 @@ def _get_and_validate_regon_data(self, data):
expected_suffix = (
data["name"].upper().replace(self.args.name_prefix.upper(), "").strip()
)
regon_suffix = regon_data.get(self.args.name_suffix_field, "").upper()
gus_suffix_field = regon_data.get(self.args.name_suffix_gus_field, "").upper()

if self.args.name_suffix_field and expected_suffix not in regon_suffix:
if self.args.name_suffix_gus_field and expected_suffix not in gus_suffix_field:
print(
'Warning! GUS field "{}" does not contain '
'expected value "{}" but "{}".'.format(
self.args.name_suffix_field, expected_suffix, regon_suffix
self.args.name_suffix_gus_field, expected_suffix, gus_suffix_field
)
)
if self.args.explicit:
Expand Down Expand Up @@ -336,7 +354,7 @@ def _push_to_api(self, data, regon_data, obj_id):
data["name"], response.status_code
)
print(error_msg, file=sys.stderr)
print(response.text, file=sys.stderr)
print("\n{}\n".format(response.text), file=sys.stderr)
if self.args.explicit:
self._print_gus_data(data["name"], regon_data)
raise ScriptError(error_msg)
Expand Down Expand Up @@ -403,6 +421,7 @@ def run(self):
return 5

data = list(reader)
data = data[self.args.starting_row - 1 :]
self.total_count = len(data)
for row in data:
normalized_row = self._normalize_row(**row)
Expand Down

0 comments on commit 75e0b80

Please sign in to comment.