Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/issue_837'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Nov 12, 2024
2 parents c8bc120 + 65b372d commit 01b77bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
25 changes: 23 additions & 2 deletions workbench
Original file line number Diff line number Diff line change
Expand Up @@ -3112,7 +3112,6 @@ def update_terms():
)
csv_data = get_csv_data(config)
csv_column_headers = csv_data.fieldnames
# invalid_target_ids = []

if config["log_term_creation"] is False:
logging.info(
Expand Down Expand Up @@ -3183,13 +3182,23 @@ def update_terms():
if len(row["description"].strip()) != 0:
term["description"] = [{"value": row["description"]}]

if "published" in csv_column_headers:
if len(row["published"].strip()) != 0:
term["status"] = [{"value": row["published"]}]

# Add custom (non-required) fields.
required_fields = ["term_id"]
custom_fields = list(set(csv_column_headers) - set(required_fields))
for custom_field in custom_fields:
term_has_all_fields = True
# If node doesn't have the field, log that fact and skip updating the field.
reserved_fields = ["parent", "weight", "description", "term_name"]
reserved_fields = [
"parent",
"weight",
"description",
"term_name",
"published",
]
if (
custom_field not in json.loads(term_response.text)
and custom_field not in reserved_fields
Expand All @@ -3216,6 +3225,10 @@ def update_terms():
if custom_field == "description":
continue

# 'published' is a reserved CSV field.
if custom_field == "published":
continue

# Assemble Drupal field structures from CSV data. If new field types are added to
# workbench_fields.py, they need to be registered in the following if/elif/else block.

Expand Down Expand Up @@ -3313,6 +3326,14 @@ def update_terms():
"Term %s updated.",
config["host"] + "/taxonomy/term/" + row["term_id"],
)
else:
message = (
f'Term {config["host"]}/taxonomy/term/{row["term_id"]} not updated.'
)
print("Warning: " + message)
logging.warning(
f"{message} HTTP response code was {term_response.status_code}. Response body was {term_response.text}."
)

if config["progress_bar"] is True:
row_count += 1
Expand Down
19 changes: 17 additions & 2 deletions workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,7 @@ def check_input(config, args):
"parent",
"weight",
"description",
"published",
]
drupal_fieldnames = []
for drupal_fieldname in field_definitions:
Expand Down Expand Up @@ -6487,9 +6488,10 @@ def create_term(config, vocab_id, term_name, term_csv_row=None):
return tid
else:
logging.warning(
"Term '%s' not created, HTTP response code was %s.",
"Term '%s' not created, HTTP response code was %s, response body was %s.",
term_name,
response.status_code,
response.text,
)
return False

Expand All @@ -6516,9 +6518,18 @@ def get_term_field_data(config, vocab_id, term_name, term_csv_row):
The dict containing the term field data, or False if this is not possible.
@note: reason why creating JSON is not possible should be logged in this function.
"""
if (
term_csv_row is not None
and "published" in term_csv_row.keys()
and len(term_csv_row["published"]) > 0
):
published_status = term_csv_row["published"]
else:
published_status = True

# 'vid' and 'name' are added in create_term().
term_field_data = {
"status": [{"value": True}],
"status": [{"value": published_status}],
"description": [{"value": "", "format": None}],
"weight": [{"value": 0}],
"parent": [{"target_type": "taxonomy_term", "target_id": None}],
Expand Down Expand Up @@ -6546,6 +6557,10 @@ def get_term_field_data(config, vocab_id, term_name, term_csv_row):
if field_name == "term_name":
continue

# "published" is a reserved column name in the vocabulary CSV and not a field in the term JSON, so skip it.
if field_name == "published":
continue

# 'parent' field is present and not empty, so we need to look up the parent term. All terms
# that are parents will have already been created back in workbench.create_terms() as long as
# they preceded the children. If they come after the children in the CSV, we create the child
Expand Down

0 comments on commit 01b77bc

Please sign in to comment.