From dd0041cd3b7ff3ab76e7279e4b9321e84680479f Mon Sep 17 00:00:00 2001 From: hayley-leblanc Date: Mon, 29 Jul 2019 11:22:12 +0200 Subject: [PATCH] Formatting --- .../lib/activity_streams_session_extension.py | 4 +- ckan/lib/changes.py | 516 ++++++++++++------ ckan/lib/helpers.py | 30 +- ckan/views/dataset.py | 40 +- 4 files changed, 400 insertions(+), 190 deletions(-) diff --git a/ckan/lib/activity_streams_session_extension.py b/ckan/lib/activity_streams_session_extension.py index decdeeb6cde..8a9d3791e1d 100644 --- a/ckan/lib/activity_streams_session_extension.py +++ b/ckan/lib/activity_streams_session_extension.py @@ -70,7 +70,7 @@ def before_commit(self, session): # object is a package. # Don't create activities for private datasets. - #if obj.private: + # if obj.private: # continue activities[obj.id] = activity @@ -106,7 +106,7 @@ def before_commit(self, session): continue # Don't create activities for private datasets. - #if package.private: + # if package.private: # continue if package.id in activities: diff --git a/ckan/lib/changes.py b/ckan/lib/changes.py index 87403cde0a4..e1ae63c6f81 100644 --- a/ckan/lib/changes.py +++ b/ckan/lib/changes.py @@ -1,10 +1,11 @@ ''' -Functions used by the helper function compare_pkg_dicts() to analyze the differences -between two versions of a dataset. +Functions used by the helper function compare_pkg_dicts() to analyze +the differences between two versions of a dataset. ''' from helpers import url_for + def _extras_to_dict(extras_list): ''' Takes a list of dictionaries with the following format: @@ -35,16 +36,17 @@ def _extras_to_dict(extras_list): return ret_dict -def _check_resource_changes(change_list, original, new, new_pkg, old_activity_id): + +def _check_resource_changes(change_list, original, new, new_pkg, + old_activity_id): ''' - Checks whether a dataset's resources have changed - whether new ones have been uploaded, - existing ones have been deleted, or existing ones have been edited. For existing - resources, checks whether their names, formats, and/or descriptions have changed, as well - as whether a new file has been uploaded for the resource. + Checks whether a dataset's resources have changed - whether new ones have + been uploaded, existing ones have been deleted, or existing ones have + been edited. For existing resources, checks whether their names, formats, + and/or descriptions have changed, as well as whether a new file has been + uploaded for the resource. ''' - # TODO: clean this up - # make a set of the resource IDs present in original and new original_resource_set = set() original_resource_dict = {} @@ -54,18 +56,21 @@ def _check_resource_changes(change_list, original, new, new_pkg, old_activity_id for resource in original['resources']: original_resource_set.add(resource['id']) - original_resource_dict[resource['id']] = {'name': resource['name'], - 'url': resource['url'], - 'description': resource['description'], - 'format': resource['format']} - + original_resource_dict[resource['id']] = { + 'name': resource['name'], + 'url': resource['url'], + 'description': resource['description'], + 'format': resource['format'] + } for resource in new['resources']: new_resource_set.add(resource['id']) - new_resource_dict[resource['id']] = {'name': resource['name'], - 'url': resource['url'], - 'description': resource['description'], - 'format': resource['format']} + new_resource_dict[resource['id']] = { + 'name': resource['name'], + 'url': resource['url'], + 'description': resource['description'], + 'format': resource['format'] + } # get the IDs of the resources that have been added between the versions new_resources = list(new_resource_set - original_resource_set) @@ -73,91 +78,151 @@ def _check_resource_changes(change_list, original, new, new_pkg, old_activity_id seq2 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Added resource", s.join(seq2), "to", new_pkg]) + change_list.append(["Added resource", + s.join(seq2), "to", + new_pkg]) # get the IDs of resources that have been deleted between versions deleted_resources = list(original_resource_set - new_resource_set) for resource_id in deleted_resources: seq2 = ("", + action="read", id=original['id'], resource_id=resource_id) + + "?activity_id=" + old_activity_id, "\">", original_resource_dict[resource_id]['name'], "") change_list.append(["Deleted resource", s.join(seq2), "from", new_pkg]) - # now check the resources that are in both and see if any have been changed - - # TODO: only one resource can be edited at a time like this right? - # so we could stop once we find the one that is edited + # now check the resources that are in both and see if any + # have been changed resources = new_resource_set.intersection(original_resource_set) for resource_id in resources: original_metadata = original_resource_dict[resource_id] new_metadata = new_resource_dict[resource_id] if original_metadata['name'] != new_metadata['name']: - seq2 = ("", + seq2 = ("", original_resource_dict[resource_id]['name'], "") - seq3 = ("", + seq3 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Renamed resource", s.join(seq2), "to", s.join(seq3), "in", new_pkg]) + change_list.append(["Renamed resource", s.join(seq2), + "to", s.join(seq3), "in", new_pkg]) + + # you can't remove a format, but if a resource's format isn't + # recognized, it won't have one set - # you can't remove a format, but if a resource's format isn't recognized, it won't have one set # if a format was not originally set and the user set one if not original_metadata['format'] and new_metadata['format']: - seq2 = ("", + seq2 = ("", new_resource_dict[resource_id]['name'], "") - seq3 = ("", + seq3 = ("", new_metadata['format'], "") - change_list.append(["Set format of resource", s.join(seq2), "to", s.join(seq3), "in", new_pkg]) + change_list.append(["Set format of resource", s.join(seq2), + "to", s.join(seq3), "in", new_pkg]) # if both versions have a format but the format changed elif original_metadata['format'] != new_metadata['format']: - seq2 = ("", + seq2 = ("", original_resource_dict[resource_id]['name'], "") - seq3 = ("", + seq3 = ("", new_metadata['format'], "") - seq4 = ("", + seq4 = ("", original_metadata['format'], "") - change_list.append(["Set format of resource", s.join(seq2), "to", s.join(seq3), "(previously", s.join(seq4) + ")", "in", new_pkg]) + change_list.append(["Set format of resource", + s.join(seq2), + "to", s.join(seq3), + "(previously", s.join(seq4) + ")", + "in", new_pkg]) # if the description changed - if not original_metadata['description'] and new_metadata['description']: - seq2 = ("", + if not original_metadata['description'] and + new_metadata['description']: + seq2 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Updated description of resource", s.join(seq2), "in", + change_list.append(["Updated description of resource", + s.join(seq2), "in", new_pkg, "to
", - "
" + new_metadata['description'] + "
"]) + "
" + new_metadata['description'] + + "
"]) # if there was a description but the user removed it - elif original_metadata['description'] and not new_metadata['description']: - seq2 = ("", + elif original_metadata['description'] and + not new_metadata['description']: + seq2 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Removed description from resource", s.join(seq2), "in", new_pkg]) + change_list.append(["Removed description from resource", + s.join(seq2), "in", new_pkg]) # if both have descriptions but they are different elif original_metadata['description'] != new_metadata['description']: - seq2 = ("", + seq2 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Updated description of resource", s.join(seq2), "in", - new_pkg, "from
", - "
" + original_metadata['description'] + "
", + change_list.append(["Updated description of resource", + s.join(seq2), "in", + new_pkg, + "from
", + "
" + + original_metadata['description'] + + "
", "to
", - "
" + new_metadata['description'] + "
"]) + "
" + + new_metadata['description'] + + "
"]) # check if the user uploaded a new file # TODO: use regular expressions to determine the actual name of the new and old files if original_metadata['url'] != new_metadata['url']: - seq2 = ("", + seq2 = ("", new_resource_dict[resource_id]['name'], "") - change_list.append(["Uploaded a new file to resource", s.join(seq2), "in", new_pkg]) + change_list.append(["Uploaded a new file to resource", + s.join(seq2), "in", new_pkg]) + def _check_metadata_changes(change_list, original, new, new_pkg): ''' @@ -191,7 +256,8 @@ def _check_metadata_changes(change_list, original, new, new_pkg): # if the visibility of the dataset changed if original['private'] != new['private']: - change_list.append(["Set visibility of", new_pkg, "to", "Private" if new['private'] else "Public"]) + change_list.append(["Set visibility of", new_pkg, "to", + "Private" if new['private'] else "Public"]) # if the description of the dataset changed if original['notes'] != new['notes']: @@ -209,11 +275,13 @@ def _check_metadata_changes(change_list, original, new, new_pkg): _license_change(change_list, original, new, new_pkg) # if the name of the dataset has changed - # this is only visible to the user via the dataset's URL, so display the change using that + # this is only visible to the user via the dataset's URL, + # so display the change using that if original['name'] != new['name']: _name_change(change_list, original, new) - # if the source URL (metadata value, not the actual URL of the dataset) has changed + # if the source URL (metadata value, not the actual URL of the dataset) + # has changed if original['url'] != new['url']: _source_url_change(change_list, original, new, new_pkg) @@ -221,8 +289,8 @@ def _check_metadata_changes(change_list, original, new, new_pkg): if original['version'] != new['version']: _version_change(change_list, original, new, new_pkg) - # check whether fields added by extensions or custom fields (in the "extras" field) - # have been changed + # check whether fields added by extensions or custom fields + # (in the "extras" field) have been changed _extension_fields(change_list, original, new, new_pkg) _extra_fields(change_list, original, new, new_pkg) @@ -236,25 +304,34 @@ def _title_change(change_list, original, new): seq2 = ("", new['title'], "") - change_list.append(["Changed title to", s.join(seq2), "(previously", original['title'] + ")"]) + change_list.append(["Changed title to", s.join(seq2), + "(previously", original['title'] + ")"]) + def _org_change(change_list, original, new, new_pkg): ''' - Appends a summary of a change to a dataset's organization between two versions - (original and new) to change_list. + Appends a summary of a change to a dataset's organization between + two versions (original and new) to change_list. ''' s = "" - seq2 = ("", + seq2 = ("", original['organization']['title'], "") - seq3 = ("", + seq3 = ("", new['organization']['title'], "") change_list.append(["Moved", new_pkg, - "from organization", - s.join(seq2), - "to organization", - s.join(seq3)]) + "from organization", + s.join(seq2), + "to organization", + s.join(seq3)]) + def _maintainer_change(change_list, original, new, new_pkg): ''' @@ -263,27 +340,36 @@ def _maintainer_change(change_list, original, new, new_pkg): ''' # if the original dataset had a maintainer if original['maintainer'] and new['maintainer']: - change_list.append(["Set maintainer of", new_pkg, "to", new['maintainer'], "(previously", original['maintainer'] + ")"]) + change_list.append(["Set maintainer of", new_pkg, + "to", new['maintainer'], + "(previously", original['maintainer'] + ")"]) elif not new['maintainer']: change_list.append(["Removed maintainer from", new_pkg]) else: change_list.append(["Set maintainer of", new_pkg, "to", new['maintainer']]) + def _maintainer_email_change(change_list, original, new, new_pkg): ''' - Appends a summary of a change to a dataset's maintainer e-mail address field - between two versions (original and new) to change_list. + Appends a summary of a change to a dataset's maintainer e-mail address + field between two versions (original and new) to change_list. ''' s = "" - seq2 = ("", new['maintainer_email'], "") + seq2 = ("", + new['maintainer_email'], "") # if the original dataset had a maintainer email if original['maintainer_email'] and new['maintainer_email']: - seq3 = ("", original['maintainer_email'], "") - change_list.append(["Set maintainer e-mail of", new_pkg, "to", s.join(seq2), "(previously", s.join(seq3) + ")"]) + seq3 = ("", + original['maintainer_email'], "") + change_list.append(["Set maintainer e-mail of", + new_pkg, "to", s.join(seq2), + "(previously", s.join(seq3) + ")"]) elif not new['maintainer_email']: change_list.append(["Removed maintainer e-mail from", new_pkg]) else: - change_list.append(["Set maintainer e-mail of", new_pkg, "to", s.join(seq2)]) + change_list.append(["Set maintainer e-mail of", + new_pkg, "to", s.join(seq2)]) + def _author_change(change_list, original, new, new_pkg): ''' @@ -292,27 +378,34 @@ def _author_change(change_list, original, new, new_pkg): ''' # if the original dataset had an author if original['author'] and new['author']: - change_list.append(["Set author of", new_pkg, "to", new['author'], "(previously", original['author'] + ")"]) + change_list.append(["Set author of", new_pkg, "to", new['author'], + "(previously", original['author'] + ")"]) elif not new['author']: change_list.append(["Removed author from", new_pkg]) else: change_list.append(["Set author of", new_pkg, "to", new['author']]) + def _author_email_change(change_list, original, new, new_pkg): ''' Appends a summary of a change to a dataset's author e-mail address field between two versions (original and new) to change_list. ''' s = "" - seq2 = ("", new['author_email'], "") + seq2 = ("", + new['author_email'], "") # if the original dataset had a author email if original['author_email'] and new['author_email']: - seq3 = ("", original['author_email'], "") - change_list.append(["Set author e-mail of", new_pkg, "to", s.join(seq2), "(previously", s.join(seq3) + ")"]) + seq3 = ("", + original['author_email'], "") + change_list.append(["Set author e-mail of", new_pkg, "to", + s.join(seq2), "(previously", s.join(seq3) + ")"]) elif not new['author_email']: change_list.append(["Removed author e-mail from", new_pkg]) else: - change_list.append(["Set author e-mail of", new_pkg, "to", s.join(seq2)]) + change_list.append(["Set author e-mail of", new_pkg, + "to", s.join(seq2)]) + def _description_change(change_list, original, new, new_pkg): ''' @@ -325,41 +418,70 @@ def _description_change(change_list, original, new, new_pkg): # if the original dataset had a description if original['notes'] and new['notes']: change_list.append(["Updated description of", new_pkg, - "from
", "
" + original['notes'] + "
", - "to
", "
" + new['notes'] + "
"]) + "from
", + "
" + + original['notes'] + + "
", + "to
", + "
" + + new['notes'] + + "
"]) elif not new['notes']: change_list.append(["Removed description from", new_pkg]) else: change_list.append(["Updated description of", new_pkg, - "to
", "
" + new['notes'] + "
"]) + "to
", + "
" + + new['notes'] + + "
"]) + def _tag_change(change_list, new_tags, original_tags, new_pkg): ''' - Appends a summary of a change to a dataset's tag list between two versions - (original and new) to change_list. + Appends a summary of a change to a dataset's tag list between two + versions (original and new) to change_list. ''' s = "" deleted_tags = original_tags - new_tags deleted_tags_list = list(deleted_tags) if len(deleted_tags) == 1: - seq2 = ("", + seq2 = ("", deleted_tags_list[0], "") change_list.append(["Removed tag", s.join(seq2), "from", new_pkg]) elif len(deleted_tags) > 1: - seq2 = ["
  • " + deleted_tags_list[i] + "
  • " + seq2 = ["
  • " + deleted_tags_list[i] + "
  • " for i in range(0, len(deleted_tags))] - change_list.append(["Removed the following tags from", new_pkg, ""]) + change_list.append(["Removed the following tags from", new_pkg, + ""]) added_tags = new_tags - original_tags added_tags_list = list(added_tags) if len(added_tags) == 1: - seq2 = ("", + seq2 = ("", added_tags_list[0], "") change_list.append(["Added tag", s.join(seq2), "to", new_pkg]) elif len(added_tags) > 1: - seq2 = ["
  • " + added_tags_list[i] + "
  • " + seq2 = ["
  • " + added_tags_list[i] + "
  • " for i in range(0, len(added_tags))] - change_list.append(["Added the following tags to", new_pkg, ""]) + change_list.append(["Added the following tags to", new_pkg, + ""]) + def _license_change(change_list, original, new, new_pkg): ''' @@ -371,72 +493,97 @@ def _license_change(change_list, original, new, new_pkg): seq3 = () # if the license has a URL, use it if 'license_url' in original and original['license_url']: - seq2 = ("", original['license_title'], "") + seq2 = ("", + original['license_title'], "") else: seq2 = (original['license_title']) if 'license_url' in new and new['license_url']: - seq3 = ("", new['license_title'], "") + seq3 = ("", + new['license_title'], "") else: seq3 = (new['license_title']) - change_list.append(["Changed the license of", new_pkg, "to", s.join(seq3), "(previously", s.join(seq2) + ")"]) + change_list.append(["Changed the license of", new_pkg, "to", + s.join(seq3), "(previously", s.join(seq2) + ")"]) + def _name_change(change_list, original, new): ''' - Appends a summary of a change to a dataset's name (and thus the URL it can - be accessed at) between two versions (original and new) to change_list. + Appends a summary of a change to a dataset's name (and thus the URL it + can be accessed at) between two versions (original and new) to + change_list. ''' s = "" - old_url = url_for(qualified=True, controller="dataset", - action="read", id=original['name']) - new_url = url_for(qualified=True, controller="dataset", - action="read", id=new['name']) + old_url = url_for(qualified=True, + controller="dataset", + action="read", + id=original['name']) + new_url = url_for(qualified=True, + controller="dataset", + action="read", + id=new['name']) seq2 = ("", old_url, "") seq3 = ("", new_url, "") - change_list.append(["Moved the dataset from", s.join(seq2), "to", s.join(seq3)]) + change_list.append(["Moved the dataset from", s.join(seq2), + "to", s.join(seq3)]) + def _source_url_change(change_list, original, new, new_pkg): ''' - Appends a summary of a change to a dataset's source URL (metadata field, not - its actual URL in the datahub) between two versions (original and new) to - change_list. + Appends a summary of a change to a dataset's source URL (metadata field, + not its actual URL in the datahub) between two versions (original and + new) to change_list. ''' s = "" seq2 = ("", original['url'], "") seq3 = ("", new['url'], "") if original['url'] and new['url']: - change_list.append(["Changed the source URL of", new_pkg, "from", s.join(seq2), "to", s.join(seq3)]) + change_list.append(["Changed the source URL of", new_pkg, + "from", s.join(seq2), "to", s.join(seq3)]) elif not new['url']: change_list.append(["Removed source URL from", new_pkg]) else: - change_list.append(["Changed the source URL of", new_pkg, "to", s.join(seq3)]) + change_list.append(["Changed the source URL of", + new_pkg, "to", s.join(seq3)]) + def _version_change(change_list, original, new, new_pkg): ''' - Appends a summary of a change to a dataset's version field (inputted by the user, - not from version control) between two versions (original and new) to change_list. + Appends a summary of a change to a dataset's version field (inputted + by the user, not from version control) between two versions (original + and new) to change_list. ''' if original['version'] and new['url']: - change_list.append(["Changed the version of", new_pkg, "from", original['version'], "to", new['version']]) + change_list.append(["Changed the version of", new_pkg, + "from", original['version'], + "to", new['version']]) elif not new['url']: change_list.append(["Removed version number from", new_pkg]) else: - change_list.append(["Changed the version of", new_pkg, "to", new['version']]) + change_list.append(["Changed the version of", new_pkg, + "to", new['version']]) + def _extension_fields(change_list, original, new, new_pkg): ''' - Checks whether any fields that have been added to the package dictionaries - by CKAN extensions have been changed between versions. If there have been - any changes between the two versions (original and new), a general summary - of the change is appended to change_list. This function does not produce - summaries for fields added or deleted by extensions, since these changes are - not triggered by the user in the web interface or API. + Checks whether any fields that have been added to the package + dictionaries by CKAN extensions have been changed between versions. + If there have been any changes between the two versions (original and + new), a general summary of the change is appended to change_list. This + function does not produce summaries for fields added or deleted by + extensions, since these changes are not triggered by the user in the web + interface or API. ''' # list of the default metadata fields for a dataset - # any fields that are not part of this list are custom fields added by a user or extension - fields = ['owner_org', 'maintainer', 'maintainer_email', 'relationships_as_object', 'private', 'num_tags', - 'id', 'metadata_created', 'metadata_modified', 'author', 'author_email', 'state', 'version', - 'license_id', 'type', 'resources', 'num_resources', 'tags', 'title', 'groups', 'creator_user_id', - 'relationships_as_subject', 'name', 'isopen', 'url', 'notes', 'license_title', 'extras', + # any fields that are not part of this list are custom fields added by a + #user or extension + fields = ['owner_org', 'maintainer', 'maintainer_email', + 'relationships_as_object', 'private', 'num_tags', + 'id', 'metadata_created', 'metadata_modified', + 'author', 'author_email', 'state', 'version', + 'license_id', 'type', 'resources', 'num_resources', + 'tags', 'title', 'groups', 'creator_user_id', + 'relationships_as_subject', 'name', 'isopen', 'url', + 'notes', 'license_title', 'extras', 'license_url', 'organization', 'revision_id'] fields_set = set(fields) @@ -445,29 +592,36 @@ def _extension_fields(change_list, original, new, new_pkg): original_set = set(original.keys()) new_set = set(new.keys()) - addl_fields_new = new_set - fields_set # set of additional fields in the new dictionary - addl_fields_original = original_set - fields_set # set of additional fields in the original dictionary - addl_fields = addl_fields_new.intersection(addl_fields_original) # set of additional fields in both + # set of additional fields in the new dictionary + addl_fields_new = new_set - fields_set + # set of additional fields in the original dictionary + addl_fields_original = original_set - fields_set + # set of additional fields in both + addl_fields = addl_fields_new.intersection(addl_fields_original) - # do NOT display a change if any additional fields have been added or deleted, - # since that is not a change made by the user from the web interface + # do NOT display a change if any additional fields have been + # added or deleted, since that is not a change made by the user + # from the web interface # if additional fields have been changed addl_fields_list = list(addl_fields) for field in addl_fields_list: if original[field] != new[field]: if original[field]: - change_list.append(["Changed value of field", field.capitalize(), "to", new[field], "(previously", original[field] + ")", "in", new_pkg]) + change_list.append(["Changed value of field", + field.capitalize(), "to", + new[field], "(previously", + original[field] + ")", "in", new_pkg]) else: - change_list.append(["Changed value of field", field.capitalize(), "to", new[field], "in", new_pkg]) + change_list.append(["Changed value of field", + field.capitalize(), "to", + new[field], "in", new_pkg]) + def _extra_fields(change_list, original, new, new_pkg): - # check the extras field to see if anything has been added, deleted, or changed - # that is where custom fields added via the web interface go - they do not become - # actual fields in a package dict ''' - Checks whether a user has added, removed, or changed any custom fields from - the web interface (or API?) and appends a summary of each change to + Checks whether a user has added, removed, or changed any custom fields + from the web interface (or API?) and appends a summary of each change to change_list. ''' @@ -476,8 +630,8 @@ def _extra_fields(change_list, original, new, new_pkg): extra_fields_new = _extras_to_dict(new['extras']) extra_new_set = set(extra_fields_new.keys()) - # if the original version has an extra fields, we need to compare the new version's - # extras to the original ones + # if the original version has an extra fields, we need + # to compare the new version'sextras to the original ones if 'extras' in original: extra_fields_original = _extras_to_dict(original['extras']) extra_original_set = set(extra_fields_original.keys()) @@ -486,61 +640,91 @@ def _extra_fields(change_list, original, new, new_pkg): new_fields = list(extra_new_set - extra_original_set) if len(new_fields) == 1: if extra_fields_new[new_fields[0]]: - change_list.append(["Added field", s.join(("", new_fields[0], "")), - "with value", s.join(("", extra_fields_new[new_fields[0]], "")), + change_list.append(["Added field", s.join(("", + new_fields[0], "")), + "with value", s.join(("", + extra_fields_new[new_fields[0]], + "")), "to", new_pkg]) else: - change_list.append(["Added field", s.join(("", new_fields[0], "")), + change_list.append(["Added field", s.join(("", + new_fields[0], "")), "to", new_pkg]) elif len(new_fields) > 1: - seq2 = ["
  • " + new_fields[i] + " with value " + extra_fields_new[new_fields[i]] + "
  • " if extra_fields_new[new_fields[i]] + seq2 = ["
  • " + new_fields[i] + " with value " + + extra_fields_new[new_fields[i]] + "
  • " + if extra_fields_new[new_fields[i]] else "
  • " + new_fields[i] + "
  • " for i in range(0, len(new_fields))] - change_list.append(["Added the following fields to", new_pkg, ""]) + change_list.append(["Added the following fields to", + new_pkg, ""]) # if some fields were deleted deleted_fields = list(extra_original_set - extra_new_set) if len(deleted_fields) == 1: - change_list.append(["Removed field", s.join(("", deleted_fields[0], "")), "from", new_pkg]) + change_list.append(["Removed field", s.join(("", + deleted_fields[0], "")), + "from", new_pkg]) elif len(deleted_fields) > 1: - seq2 = ["
  • " + deleted_fields[i] + "
  • " for i in range(0, len(deleted_fields))] - change_list.append(["Removed the following fields from", new_pkg, ""]) + seq2 = ["
  • " + deleted_fields[i] + "
  • " + for i in range(0, len(deleted_fields))] + change_list.append(["Removed the following fields from", + new_pkg, ""]) # if some existing fields were changed - extra_fields = list(extra_new_set.intersection(extra_original_set)) # list of extra fields in both the original and new versions + # list of extra fields in both the original and new versions + extra_fields = list(extra_new_set.intersection(extra_original_set)) for field in extra_fields: if extra_fields_original[field] != extra_fields_new[field]: if extra_fields_original[field]: - change_list.append(["Changed value of field", s.join(("", field, "")), - "to", s.join(("", extra_fields_new[field], "")), - "(previously", s.join(("", extra_fields_original[field], "")) + ")", + change_list.append(["Changed value of field", + s.join(("", field, "")), + "to", s.join(("", + extra_fields_new[field], "")), + "(previously", s.join(("", + extra_fields_original[field], + "")) + ")", "in", new_pkg]) else: - change_list.append(["Changed value of field", s.join(("", field, "")), - "to", s.join(("", extra_fields_new[field], "")), + change_list.append(["Changed value of field", + s.join(("", field, "")), + "to", s.join(("", + extra_fields_new[field], "")), "in", new_pkg]) - # if the original version didn't have an extras field, the user could only have added a field (not changed or deleted) + # if the original version didn't have an extras field, + # the user could only have added a field (not changed or deleted) else: new_fields = list(extra_new_set) if len(new_fields) == 1: if extra_fields_new[new_fields[0]]: - change_list.append(["Added field", s.join(("", new_fields[0], "")), - "with value", s.join(("", extra_fields_new[new_fields[0]], "")), + change_list.append(["Added field", s.join(("", + new_fields[0], "")), + "with value", s.join(("", + extra_fields_new[new_fields[0]], + "")), "to", new_pkg]) else: - change_list.append(["Added field", s.join(("", new_fields[0], "")), + change_list.append(["Added field", s.join(("", + new_fields[0], "")), "to", new_pkg]) elif len(new_fields) > 1: - seq2 = ["
  • " + new_fields[i] + " with value " + extra_fields_new[new_fields[i]] + "
  • " if extra_fields_new[new_fields[i]] + seq2 = ["
  • " + new_fields[i] + " with value " + + extra_fields_new[new_fields[i]] + "
  • " + if extra_fields_new[new_fields[i]] else "
  • " + new_fields[i] + "
  • " for i in range(0, len(new_fields))] - change_list.append(["Added the following fields to", new_pkg, ""]) + change_list.append(["Added the following fields to", + new_pkg, ""]) elif 'extras' in original: deleted_fields = _extras_to_dict(original['extras']).keys() if len(deleted_fields) == 1: - change_list.append(["Removed field", s.join(("", deleted_fields[0], "")), "from", new_pkg]) + change_list.append(["Removed field", s.join(("", + deleted_fields[0], "")), "from", + new_pkg]) elif len(deleted_fields) > 1: - seq2 = ["
  • " + deleted_fields[i] + "
  • " for i in range(0, len(deleted_fields))] - change_list.append(["Removed the following fields from", new_pkg, ""]) + seq2 = ["
  • " + deleted_fields[i] + + "
  • " for i in range(0, len(deleted_fields))] + change_list.append(["Removed the following fields from", + new_pkg, ""]) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index b4f384b3118..88122286160 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -2705,33 +2705,45 @@ def compare_pkg_dicts(original, new, old_activity_id): s = "" seq1 = ("", - new['title'], "") + action="read", id=new['id']), "\">", + new['title'], "") new_pkg = s.join(seq1) _check_metadata_changes(change_list, original, new, new_pkg) - _check_resource_changes(change_list, original, new, new_pkg, old_activity_id) + _check_resource_changes(change_list, original, new, new_pkg, + old_activity_id) # if the dataset was updated but none of the fields we check were changed, # display a message stating that if len(change_list) == 0: - change_list.append(["No fields were updated. See metadata diff for more details."]) + change_list.append(["No fields were updated. \ + See metadata diff for more details."]) return change_list @core_helper def activity_list_select(pkg_activity_list, current_activity_id): ''' - Builds an HTML formatted list of options for the select lists on the "Changes" - summary page. + Builds an HTML formatted list of options for the select lists + on the "Changes" summary page. ''' select_list = [] for activity in pkg_activity_list: - entry = render_datetime(activity['timestamp'], with_hours=True, with_seconds=True) + entry = render_datetime(activity['timestamp'], + with_hours=True, + with_seconds=True) if activity['id'] == current_activity_id: - select_list.append("") + select_list.append("") else: - select_list.append("") + select_list.append("") return select_list diff --git a/ckan/views/dataset.py b/ckan/views/dataset.py index 0c5712e0cfc..c1231a6e59b 100644 --- a/ckan/views/dataset.py +++ b/ckan/views/dataset.py @@ -1104,7 +1104,8 @@ def changes(id, package_type=None): # changed, and we need a link to it which works pkg_id = activity_diff[u'activities'][1][u'data'][u'package'][u'id'] current_pkg_dict = get_action(u'package_show')(context, {u'id': pkg_id}) - pkg_activity_list = get_action(u'package_activity_list')(context, {u'id': pkg_id, u'limit': 100}) + pkg_activity_list = get_action(u'package_activity_list')(context, + {u'id': pkg_id, u'limit': 100}) return base.render( u'package/changes.html', { @@ -1114,11 +1115,13 @@ def changes(id, package_type=None): } ) + def change_range(package_type=None): ''' - Called when a user specifies a range of versions they want to look at changes between. - Verifies that the range is valid and finds the set of activity diffs for the changes - in the given version range, then re-renders changes.html with the list. + Called when a user specifies a range of versions they want to look at + changes between. Verifies that the range is valid and finds the set of + activity diffs for the changes in the given version range, then + re-renders changes.html with the list. ''' newest_id = h.get_request_param('newest_id') @@ -1129,9 +1132,16 @@ def change_range(package_type=None): u'user': g.user, u'auth_user_obj': g.userobj } - # check to ensure that the old activity is actually older than the new activity - old_activity = get_action(u'activity_show')(context, {u'id': oldest_id, u'include_data': False}) - new_activity = get_action('activity_show')(context, {'id': newest_id, u'include_data': False}) + # check to ensure that the old activity is actually older than + # the new activity + old_activity = get_action(u'activity_show')(context, { + u'id': oldest_id, + u'include_data': False + }) + new_activity = get_action('activity_show')(context, { + u'id': newest_id, + u'include_data': False + }) old_timestamp = old_activity['timestamp'] new_timestamp = new_activity['timestamp'] @@ -1142,9 +1152,9 @@ def change_range(package_type=None): time_diff = t2 - t1 # if the time difference is negative, just return the change that put us # at the more recent ID we were just looking at - # TODO: do something better here - go back to the previous page, display a warning - # that the user can't look at a sequence where the newest item is older than the - # oldest one, etc + # TODO: do something better here - go back to the previous page, + # display a warning that the user can't look at a sequence where + # the newest item is older than the oldest one, etc if time_diff.total_seconds() < 0: return changes(h.get_request_param('current_new_id')) @@ -1152,13 +1162,14 @@ def change_range(package_type=None): current_id = newest_id diff_list = [] - while done == False: + while not done: try: activity_diff = get_action(u'activity_diff')( context, {u'id': current_id, u'object_type': u'package', u'diff_type': u'html'}) except NotFound as e: - log.info(u'Activity not found: {} - {}'.format(str(e), current_id)) + log.info(u'Activity not found: {} - {}'.format(str(e), + current_id)) return base.abort(404, _(u'Activity not found')) except NotAuthorized: return base.abort(403, _(u'Unauthorized to view activity data')) @@ -1172,7 +1183,10 @@ def change_range(package_type=None): pkg_id = diff_list[0][u'activities'][1][u'data'][u'package'][u'id'] current_pkg_dict = get_action(u'package_show')(context, {u'id': pkg_id}) - pkg_activity_list = get_action(u'package_activity_list')(context, {u'id': pkg_id, u'limit': 100}) + pkg_activity_list = get_action(u'package_activity_list')(context, { + u'id': pkg_id, + u'limit': 100 + }) return base.render( u'package/changes.html', {