Skip to content

Commit

Permalink
Merge pull request #645 from hassanelsheikha/main
Browse files Browse the repository at this point in the history
Add Support for Creating Revision Log Messages with Update Tasks
  • Loading branch information
mjordan authored Aug 28, 2023
2 parents 1d17473 + 294647b commit 575b7ba
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 55 additions & 1 deletion workbench
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ def update():
if custom_field == 'uid':
continue

if custom_field == 'revision_log':
continue

# Entity reference fields (taxonomy term and node).
if field_definitions[custom_field]['field_type'] == 'entity_reference':
entity_reference_field = workbench_fields.EntityReferenceField()
Expand Down Expand Up @@ -547,6 +550,22 @@ def update():
if node_has_all_fields is True:
node_endpoint = config['host'] + '/node/' + row['node_id'] + '?_format=json'
node_headers = {'Content-Type': 'application/json'}
# Make a GET request to the content type
content_type_endpoint = f"{config['host']}/entity/node_type/{config['content_type']}/?_format=json"
response = issue_request(config, 'GET', content_type_endpoint).text
# See if revisions are enabled for the content type and create a new revision log message if so
revisions_enabled = json.loads(response)['new_revision']
if revisions_enabled:
# Add revision information to node
revision_log_message = row['revision_log'] if 'revision_log' in row and row['revision_log'] != '' else 'Updated by Islandora Workbench.'
revision_json = {
"revision_log": [
{
"value": revision_log_message
}
]
}
node.update(revision_json)
node_response = issue_request(config, 'PATCH', node_endpoint, node_headers, node)

if node_response.status_code == 200:
Expand Down Expand Up @@ -998,6 +1017,36 @@ def update_media() -> None:
{'value': status}
]
}

def is_revisions_enabled(config: dict, media_type: str):
"""Return True if the media type has revisions enabled, False otherwise.
Parameters:
- config: The global configuration object.
- media_type: The media entity's type (e.g., 'image').
Returns:
True if the media type has revisions enabled, False otherwise.
"""
media_type_endpoint = f"{config['host']}/entity/media_type/{media_type}/?_format=json"
response = issue_request(config, 'GET', media_type_endpoint).text
return json.loads(response)['new_revision']

def patch_media_revision_log_message(revision_log_message) -> dict:
"""Return the JSON object for a PATCH request required to patch the media entity's revision log message.
Parameters:
- revision_log_message: The revision log message.
Returns:
The JSON request for a PATCH request required to patch the media entity's revision log message.
"""
return {
'revision_log_message': [
{'value': revision_log_message}
]
}

# ========================================================= Main Logic =========================================================

# TODO: Updating the media file and the name simultaneously does not work. The media takes the name of the new file and not the specified name.
Expand Down Expand Up @@ -1229,6 +1278,11 @@ def update_media() -> None:
pbar(row_position)
continue

# Update revision log message
if is_revisions_enabled(config, media_type):
revision_log_message = row['revision_log'] if 'revision_log' in row and row['revision_log'] != '' else 'Updated by Islandora Workbench.'
patch_request_json.update(patch_media_revision_log_message(revision_log_message))

# Make the PATCH request
if config['standalone_media_url'] is True:
update_media_url = config['host'] + '/media/' + media_id + '?_format=json'
Expand Down Expand Up @@ -2205,4 +2259,4 @@ except KeyboardInterrupt:
try:
sys.exit(0)
except SystemExit:
os._exit(0)
os._exit(0)
4 changes: 3 additions & 1 deletion workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ def check_input(config, args):
base_fields = ['title', 'status', 'promote', 'sticky', 'uid', 'created', 'published']
# Any new reserved columns introduced into the CSV need to be removed here. 'langcode' is a standard Drupal field
# but it doesn't show up in any field configs.
reserved_fields = ['file', 'media_use_tid', 'checksum', 'node_id', 'url_alias', 'image_alt_text', 'parent_id', 'langcode']
reserved_fields = ['file', 'media_use_tid', 'checksum', 'node_id', 'url_alias', 'image_alt_text', 'parent_id', 'langcode', 'revision_log']
entity_fields = get_entity_fields(config, 'node', config['content_type'])
if config['id_field'] not in entity_fields:
reserved_fields.append(config['id_field'])
Expand Down Expand Up @@ -1898,6 +1898,8 @@ def check_input(config, args):
csv_column_headers.remove('image_alt_text')
if 'media_use_tid' in csv_column_headers:
csv_column_headers.remove('media_use_tid')
if 'revision_log' in csv_column_headers:
csv_column_headers.remove('revision_log')
if 'file' in csv_column_headers:
message = 'Error: CSV column header "file" is not allowed in update tasks.'
logging.error(message)
Expand Down

0 comments on commit 575b7ba

Please sign in to comment.