|
| 1 | +import psycopg2 |
| 2 | +import psycopg2.extras |
| 3 | + |
| 4 | + |
| 5 | +def neo_contributors(con: psycopg2.connect, self) -> list: |
| 6 | + """_Obtain a list of the dataset contributors by activity for a dataset._ |
| 7 | +
|
| 8 | + Args: |
| 9 | + con (psycopg2.connect): _A valid connection the the Neotoma Database server_ |
| 10 | +
|
| 11 | + Returns: |
| 12 | + list: _A list of Neotoma contributors, including external identifiers when available._ |
| 13 | + """ |
| 14 | + query = """ |
| 15 | + WITH chronfolk AS ( |
| 16 | + SELECT DISTINCT contactid, |
| 17 | + 'Researcher'::text AS contributorType |
| 18 | + FROM ndb.datasets AS d |
| 19 | + JOIN ndb.chronologies AS chron ON d.collectionunitid = chron.collectionunitid |
| 20 | + WHERE d.datasetid = %(datasetid)s |
| 21 | + ), |
| 22 | + collfolk AS ( |
| 23 | + SELECT DISTINCT contactid, 'DataCollector'::text AS contributortype |
| 24 | + FROM ndb.datasets AS d |
| 25 | + JOIN ndb.collectors AS coll ON d.collectionunitid = coll.collectionunitid |
| 26 | + WHERE d.datasetid = %(datasetid)s |
| 27 | + ), |
| 28 | + dpi AS ( |
| 29 | + SELECT DISTINCT contactid, |
| 30 | + 'ProjectLeader'::text AS contributortype |
| 31 | + FROM ndb.datasetpis WHERE datasetpis.datasetid = %(datasetid)s |
| 32 | + ), |
| 33 | + curator AS ( |
| 34 | + /* In the DB stuff this should be a 'DataSteward' */ |
| 35 | + SELECT DISTINCT contactid, 'DataCurator'::text AS contributortype |
| 36 | + FROM ndb.datasetsubmissions |
| 37 | + WHERE datasetsubmissions.datasetid = %(datasetid)s |
| 38 | + ), |
| 39 | + coauth AS ( |
| 40 | + SELECT DISTINCT contactid, |
| 41 | + 'Researcher'::text AS contributortype |
| 42 | + FROM ndb.datasetpublications AS d |
| 43 | + JOIN ndb.publicationauthors AS paut ON d.publicationid = paut.publicationid |
| 44 | + WHERE d.datasetid = %(datasetid)s |
| 45 | + ), |
| 46 | + analyst AS ( |
| 47 | + SELECT DISTINCT sana.contactid, |
| 48 | + /* In the DB stuff this should be a 'DataAnalyst' */ |
| 49 | + 'DataCollector'::text AS contributortype |
| 50 | + FROM ndb.samples AS samp |
| 51 | + JOIN ndb.sampleanalysts AS sana ON samp.sampleid = sana.sampleid |
| 52 | + WHERE samp.datasetid = %(datasetid)s |
| 53 | + ) |
| 54 | + SELECT DISTINCT cts.contactname AS name, |
| 55 | + -- cts.address AS affiliation, |
| 56 | + lister.contributortype as "contributorType", |
| 57 | + jsonb_agg(DISTINCT |
| 58 | + jsonb_build_object('nameIdentifier', exct.identifier, |
| 59 | + 'nameIdentifierScheme', exdb.extdatabasename, |
| 60 | + 'schemeUri', exdb.url)) AS "nameIdentifiers" |
| 61 | + FROM (SELECT * FROM analyst |
| 62 | + UNION ALL |
| 63 | + (SELECT * FROM coauth) |
| 64 | + UNION ALL |
| 65 | + (SELECT * FROM curator) |
| 66 | + UNION ALL |
| 67 | + (SELECT * FROM dpi) |
| 68 | + UNION ALL |
| 69 | + (SELECT * FROM collfolk) |
| 70 | + UNION ALL |
| 71 | + (SELECT * FROM chronfolk)) AS lister |
| 72 | + JOIN ndb.contacts AS cts ON cts.contactid = lister.contactid |
| 73 | + LEFT JOIN ndb.externalcontacts AS exct ON exct.contactid = cts.contactid |
| 74 | + LEFT JOIN ndb.externaldatabases AS exdb ON exdb.extdatabaseid = exct.extdatabaseid |
| 75 | + GROUP BY cts.contactid, lister.contributortype; |
| 76 | + """ |
| 77 | + with con.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur: |
| 78 | + cur.execute(query, {"datasetid": self.datasetid}) |
| 79 | + response = cur.fetchall() |
| 80 | + contributors = [] |
| 81 | + for i in response: |
| 82 | + creator = dict(i) |
| 83 | + if not all( |
| 84 | + [i.get("nameIdentifier") for i in creator.get("nameIdentifiers")] |
| 85 | + ): |
| 86 | + _ = creator.pop("nameIdentifiers", None) |
| 87 | + contributors.append(creator) |
| 88 | + return contributors |
0 commit comments