Skip to content

Commit

Permalink
Resolves #838: Added support for 'published' column in CSV for create…
Browse files Browse the repository at this point in the history
…_terms and update_termns.
  • Loading branch information
mjordan committed Nov 3, 2024
1 parent 107087b commit 584ca09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 15 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
16 changes: 15 additions & 1 deletion workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,7 @@ def check_input(config, args):
"parent",
"weight",
"description",
"published",
]
drupal_fieldnames = []
for drupal_fieldname in field_definitions:
Expand Down Expand Up @@ -6508,9 +6509,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 @@ -6538,6 +6548,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 584ca09

Please sign in to comment.