|
| 1 | +from typing import TypeAlias |
| 2 | + |
| 3 | +from elinkapi import Elink |
| 4 | +from elinkapi.record import RecordResponse |
| 5 | +from pymongo import MongoClient |
| 6 | + |
| 7 | +import requests |
| 8 | +from elinkapi.utils import Validation |
| 9 | + |
| 10 | + |
| 11 | +from mp_cite.doi_builder import MinimumDARecord |
| 12 | + |
| 13 | +from typing import Literal |
| 14 | + |
| 15 | +OstiID: TypeAlias = int |
| 16 | + |
| 17 | + |
| 18 | +def find_out_of_date_doi_entries( |
| 19 | + rc_client: MongoClient, |
| 20 | + doi_client: MongoClient, |
| 21 | + robocrys_db: str, |
| 22 | + robocrys_collection: str, |
| 23 | + doi_db: str, |
| 24 | + doi_collection: str, |
| 25 | +) -> list[OstiID]: |
| 26 | + """ |
| 27 | + find_out_of_date_doi_entries queries MP's mongo collections to find all robocrys documents that were updated less recently than the latest doi document |
| 28 | +
|
| 29 | + :rc_client is the MongoClient used to access the robocrys collection |
| 30 | + :doi_client is the MongoClient used to access the doi collection (since a new doi collection is planned, they clients are passed separately, though in the future they may be the same client.) |
| 31 | + :robocrys_db is the name of the database the robocrys collection is in |
| 32 | + :robocrys_collection is the name of the robocrys collection |
| 33 | + :doi_db is the name of the database the doi collection is in |
| 34 | + :doi_collection is the name of the doi collection |
| 35 | +
|
| 36 | + returns a list containing all OSTI IDs associated with out-of-date doi entries. |
| 37 | + """ |
| 38 | + robocrys = rc_client[robocrys_db][robocrys_collection] |
| 39 | + dois = doi_client[doi_db][doi_collection] |
| 40 | + |
| 41 | + latest_doi = next( |
| 42 | + dois.aggregate( |
| 43 | + [ |
| 44 | + {"$project": {"_id": 0, "date_metadata_updated": 1}}, |
| 45 | + {"$sort": {"date_metadata_updated": -1}}, |
| 46 | + {"$limit": 1}, |
| 47 | + ] |
| 48 | + ) |
| 49 | + )["date_metadata_updated"] |
| 50 | + |
| 51 | + material_ids_to_update = list( |
| 52 | + map( |
| 53 | + lambda x: x["material_id"], |
| 54 | + robocrys.find( |
| 55 | + {"last_updated": {"$gt": latest_doi}}, {"_id": 0, "material_id": 1} |
| 56 | + ), |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + return list( |
| 61 | + map( |
| 62 | + lambda x: x["osti_id"], |
| 63 | + dois.find( |
| 64 | + {"material_id": {"$in": material_ids_to_update}}, |
| 65 | + {"_id": 0, "osti_id": 1}, |
| 66 | + ), |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def update_existing_osti_record( |
| 72 | + elinkapi: Elink, osti_id: OstiID, new_values: dict |
| 73 | +) -> RecordResponse: |
| 74 | + """ |
| 75 | + update_existing_osti_record allows users to provide a dictionary of keywords and new values, which will replace the old values under the same keywords in the record with the given osti id |
| 76 | +
|
| 77 | + :elinkapi is the instance of the elinkapi associated with the environment in which the record is held (e.g. either production or review environment) |
| 78 | + :osti_id is the osti id of the record which ought to be updated |
| 79 | + :new_values is a dictionary of keywords (which should exist in ELink's record model) and new value pairs. |
| 80 | +
|
| 81 | + N.B., it is currently assumed that the user will handle the "sponsor identifier bug" |
| 82 | + --- in which the retreived record responses of validated records from the E-Link production environment seemingly |
| 83 | + lack the required Sponsor Organization identifiers which were necessary for their submission (due to rearrangement of metadata |
| 84 | + on E-Link's side) --- before calling this function. |
| 85 | +
|
| 86 | + Otherwise, the following code excerpt would need to be added to retroactively fix the issue with the sponsor organization's identifiers |
| 87 | + for entry in record.organizations: |
| 88 | + if entry.type == "SPONSOR": |
| 89 | + entry.identifiers = [{"type": 'CN_DOE', "value": 'AC02-05CH11231'}] |
| 90 | + break |
| 91 | +
|
| 92 | + Instead, we leave this for the user. |
| 93 | + """ |
| 94 | + |
| 95 | + record_on_elink = elinkapi.get_single_record(osti_id) |
| 96 | + |
| 97 | + for keyword in new_values: |
| 98 | + setattr(record_on_elink, keyword, new_values[keyword]) |
| 99 | + |
| 100 | + return elinkapi.update_record( |
| 101 | + osti_id, record_on_elink, state="save" |
| 102 | + ) # user should use update_state_of_osti_record to submit instead |
| 103 | + |
| 104 | + |
| 105 | +def submit_new_osti_record( |
| 106 | + elinkapi: Elink, |
| 107 | + new_values: dict, |
| 108 | + state="submit", |
| 109 | +) -> RecordResponse: |
| 110 | + """ |
| 111 | + submit_new_osti_record generates a new record based on the provided keyword-value pairs in the new_values dict and the default minimum DA Record metadata necessary for submission |
| 112 | +
|
| 113 | + :elinkapi is the elinkapi (see previous) |
| 114 | + :new_values is the dictionary of keywords and values which want to be included in the submitted record (besides or in lieu of default values). The title MUST be provided. |
| 115 | + :state defaults to "submit" but the user can simply "save" if desired. This is done given our assumption that there is |
| 116 | + no need to both with saving, rather, just only send new record to osti when it's ready for submission. |
| 117 | +
|
| 118 | + returns the record response after submission |
| 119 | + """ |
| 120 | + |
| 121 | + # template for all repeated stuff |
| 122 | + # only submit |
| 123 | + new_record = MinimumDARecord( |
| 124 | + **new_values |
| 125 | + ) # record is an instance of the MinimumDARecord model which gives default values to all necessary fields (EXCEPT Title) |
| 126 | + record_response = elinkapi.post_new_record(new_record, state) |
| 127 | + |
| 128 | + return record_response |
| 129 | + |
| 130 | + |
| 131 | +def update_state_of_osti_record( |
| 132 | + elinkapi: Elink, osti_id: OstiID, new_state: Literal["save", "submit"] |
| 133 | +) -> RecordResponse: |
| 134 | + """ |
| 135 | + update_state_of_osti_record allows a user to update the state of a record with provided osti_id to either "save" or "submit" (the two valid states) |
| 136 | +
|
| 137 | + :elinkapi is the elinkapi (see previous) |
| 138 | + :osti_id is the OSTI ID associated with the record of which to update state. |
| 139 | + :new_state is a Literal object, in this case a subtype of strings (either "save" or "submit"). |
| 140 | +
|
| 141 | + returns the record response after updating the state. |
| 142 | + """ |
| 143 | + record = elinkapi.get_single_record(osti_id) |
| 144 | + return elinkapi.update_record(osti_id, record, new_state) |
| 145 | + |
| 146 | + |
| 147 | +def delete_osti_record(elinkapi: Elink, osti_id: OstiID, reason: str) -> bool: |
| 148 | + """ |
| 149 | + Delete a record by its OSTI ID. |
| 150 | +
|
| 151 | + :elinkapi is the elinkapi |
| 152 | + :osti_id is the osti_id associated with the record which ought to be deleted |
| 153 | + :reason is a str object which explains in words why the record is to be deleted (necessary for the http request) |
| 154 | +
|
| 155 | + returns true if deleted successfully, else false, which indicates a bad status_code |
| 156 | + """ |
| 157 | + response = requests.delete( |
| 158 | + f"{elinkapi.target}records/{osti_id}?reason={reason}", |
| 159 | + headers={"Authorization": f"Bearer {elinkapi.token}"}, |
| 160 | + ) |
| 161 | + Validation.handle_response(response) |
| 162 | + return response.status_code == 204 # True if deleted successfully |
0 commit comments