Repository: internetarchive/openlibrary

Issue:
Author create edit record should show source link
Edition records include a link to the source MARC record that was used to create them. Author records used to include the same, but don't any more. This isn't a new issue because I've seen 2017 author records which also exhibit the issue.

Here's a recent one which does. The comment on this record: https://openlibrary.org/authors/OL7433603A/Stefan_Ap%C2%A9%C5%81thy

should look like it's complementary edition record: https://openlibrary.org/books/OL26582866M/Die_Mikrotechnik_der_thierischen_Morphologie

which points back to: https://openlibrary.org/show-records/ia:diemikrotechnikd2190apth

Author create edit record should show source link
Edition records include a link to the source MARC record that was used to create them. Author records used to include the same, but don't any more. This isn't a new issue because I've seen 2017 author records which also exhibit the issue.

Here's a recent one which does. The comment on this record: https://openlibrary.org/authors/OL7433603A/Stefan_Ap%C2%A9%C5%81thy

should look like it's complementary edition record: https://openlibrary.org/books/OL26582866M/Die_Mikrotechnik_der_thierischen_Morphologie

which points back to: https://openlibrary.org/show-records/ia:diemikrotechnikd2190apth



Discussion:
I suspect this is because the importer is not including the correct `machine_comment` in the update.
Here's an earlier example:
https://openlibrary.org/authors/OL2666900A.json?m=history

```
{
comment: "initial import",
author: "/people/ImportBot",
ip: "127.0.0.1",
created: "2008-10-19T23:55:47.958486",
data: "{"machine_comment": "marc_university_of_toronto/uoft.marc:2679117007:1081"}",
key: "/authors/OL2666900A",
action: "update",
author_id: 17874995,
changes: "[{"key": "/authors/OL2666900A", "revision": 2}]",
id: 26984722,
machine_comment: "marc_university_of_toronto/uoft.marc:2679117007:1081",
revision: 2
},
```

This gets rendered in the web interface as:

October 19, 2008 | Edited by ImportBot | Found a [matching record](https://openlibrary.org/show-records/marc_university_of_toronto/uoft.marc:2679117007:1081) from [University of Toronto](https://archive.org/details/marc_university_of_toronto) .



Some authors have `source_records` as a field, which is a general source field which does not have to be added at the first import. 
https://openlibrary.org/authors/OL104493A.json

Having all these `source_records` displayed consistently on the Works, Editions, and Authors pages would be nice.

This `machine_comment` isn't well named, but I guess indicates where the _first_ import came from and therefore is appropriate to be shown in the history.
I suspect this is because the importer is not including the correct `machine_comment` in the update.
Here's an earlier example:
https://openlibrary.org/authors/OL2666900A.json?m=history

```
{
comment: "initial import",
author: "/people/ImportBot",
ip: "127.0.0.1",
created: "2008-10-19T23:55:47.958486",
data: "{"machine_comment": "marc_university_of_toronto/uoft.marc:2679117007:1081"}",
key: "/authors/OL2666900A",
action: "update",
author_id: 17874995,
changes: "[{"key": "/authors/OL2666900A", "revision": 2}]",
id: 26984722,
machine_comment: "marc_university_of_toronto/uoft.marc:2679117007:1081",
revision: 2
},
```

This gets rendered in the web interface as:

October 19, 2008 | Edited by ImportBot | Found a [matching record](https://openlibrary.org/show-records/marc_university_of_toronto/uoft.marc:2679117007:1081) from [University of Toronto](https://archive.org/details/marc_university_of_toronto) .



Some authors have `source_records` as a field, which is a general source field which does not have to be added at the first import. 
https://openlibrary.org/authors/OL104493A.json

Having all these `source_records` displayed consistently on the Works, Editions, and Authors pages would be nice.

This `machine_comment` isn't well named, but I guess indicates where the _first_ import came from and therefore is appropriate to be shown in the history.

Resolving patch:
diff --git a/openlibrary/catalog/add_book/__init__.py b/openlibrary/catalog/add_book/__init__.py
--- a/openlibrary/catalog/add_book/__init__.py
+++ b/openlibrary/catalog/add_book/__init__.py
@@ -175,23 +175,25 @@ def find_matching_work(e):
                 return wkey
 
 
-def build_author_reply(author_in, edits):
+def build_author_reply(authors_in, edits, source):
     """
     Steps through an import record's authors, and creates new records if new,
     adding them to 'edits' to be saved later.
 
-    :param list author_in: List of import sourced author dicts [{"name:" "Some One"}, ...], possibly with dates
+    :param list authors_in: import author dicts [{"name:" "Bob"}, ...], maybe dates
     :param list edits: list of Things to be saved later. Is modfied by this method.
+    :param str source: Source record e.g. marc:marc_ex/part01.dat:26456929:680
     :rtype: tuple
-    :return: (list, list) authors [{"key": "/author/OL..A"}, ...], author_reply the JSON status response to return for each author
+    :return: (list, list) authors [{"key": "/author/OL..A"}, ...], author_reply
     """
 
     authors = []
     author_reply = []
-    for a in author_in:
+    for a in authors_in:
         new_author = 'key' not in a
         if new_author:
             a['key'] = web.ctx.site.new_key('/type/author')
+            a['source_records'] = [source]
             edits.append(a)
         authors.append({'key': a['key']})
         author_reply.append({
@@ -280,11 +282,13 @@ def get_ia_item(ocaid):
     item = ia.get_item(ocaid, config=cfg)
     return item
 
+
 def modify_ia_item(item, data):
     access_key = lending.config_ia_ol_metadata_write_s3 and lending.config_ia_ol_metadata_write_s3['s3_key']
     secret_key = lending.config_ia_ol_metadata_write_s3 and lending.config_ia_ol_metadata_write_s3['s3_secret']
     return item.modify_metadata(data, access_key=access_key, secret_key=secret_key)
 
+
 def create_ol_subjects_for_ocaid(ocaid, subjects):
     item = get_ia_item(ocaid)
     openlibrary_subjects = copy(item.metadata.get('openlibrary_subject')) or []
@@ -302,6 +306,7 @@ def create_ol_subjects_for_ocaid(ocaid, subjects):
     else:
         return ("success for %s" % item.identifier)
 
+
 def update_ia_metadata_for_ol_edition(edition_id):
     """
     Writes the Open Library Edition and Work id to a linked
@@ -577,7 +582,8 @@ def load_data(rec, account=None):
     # TOFIX: edition.authors has already been processed by import_authors() in build_query(), following line is a NOP?
     author_in = [import_author(a, eastern=east_in_by_statement(rec, a)) for a in edition.get('authors', [])]
     # build_author_reply() adds authors to edits
-    (authors, author_reply) = build_author_reply(author_in, edits)
+    (authors, author_reply) = build_author_reply(author_in, edits,
+                                                 rec['source_records'][0])
 
     if authors:
         edition['authors'] = authors


Test patch: