def update_authorships(cursor: psql_cursor, authorships_limit: int = -1): people = db.get_people(cursor) affiliations = db.get_affiliations(cursor) known = db.get_confirmed_publications(cursor) authorships_updated = db.get_pubmed_authorships_updates(cursor) authorships: typing.Dict[int, typing.List[str]] = {} for person_id, info in people.items(): if authorships_limit == 0: log("Reached the limit of authorship searches for this run") break latest_update = authorships_updated.get(person_id, None) if latest_update and too_recent(latest_update): log(f"{person_id}: skipping author whose publications were " "downloaded recently") continue person = classes.Person(person_id=str(person_id), first_name=info[0], last_name=info[1], display_name=info[2], email=info[3], phone=info[4], withheld=info[5], overview=info[6]) if person.withheld: log(f"{person_id}: skipping withheld author") continue if person_id not in affiliations: log(f"{person_id}: skipping author without affiliations") continue log(f"{person_id}: " f"fetching PMIDs for {person.first_name} {person.last_name}.") if person_id in known: pmids = catalyst.fetch_ids(person, list(affiliations[person_id]), include_pmids=list(known[person_id][0]), exclude_pmids=list(known[person_id][1])) else: pmids = get_pubmed_ids(person.first_name, person.last_name, list(affiliations[person_id])) authorships[person_id] = pmids log(f"{person_id}: found {len(pmids)} publications.") authorships_limit -= 1 if not authorships: return count = db.update_authorships(cursor, authorships) log(f"Number of authorships updated: {count}")
def get_people(sup_cur: db.Cursor) -> Dict[int, Person]: print("Gathering People") people: Dict[int, Person] = {} records = db.get_people(sup_cur) for pid, (first, last, display, email, phone, withheld, overview) in records.items(): person = Person(person_id=str(pid), first_name=first, last_name=last, display_name=display, email=email, phone=phone, withheld=withheld, overview=overview) people[pid] = person print(f"There are {len(people)} people.") return people
def person_overview(): # POST json => json if request.method == 'POST': formdata: Optional[Dict[str, Any]] = request.json if not formdata: return jsonify(error='no data'), HTTPStatus.BAD_REQUEST if 'id' not in formdata or 'overview' not in formdata: return (jsonify(error='id and overview required'), HTTPStatus.BAD_REQUEST) try: person_id = int(formdata['id']) assert person_id > 0 overview = str(formdata['overview']) except (ValueError, AssertionError): return jsonify(error="bad id"), HTTPStatus.BAD_REQUEST try: with conn.cursor() as cur: assert db.update_overview(cur, person_id, overview) conn.commit() return jsonify(msg='Successfully updated overview'), HTTPStatus.OK except Exception: conn.rollback() logging.exception("person_overview") return (jsonify(error='Error updating overview'), HTTPStatus.BAD_GATEWAY) try: person_id = int(request.args.get('person_id', '0')) except ValueError: person_id = 0 # GET ?person_id=DDD => JSON if person_id > 0: try: with conn.cursor() as cur: overview = db.get_overview(cur, person_id) return jsonify(overview=overview) except Exception: logging.exception() return (jsonify(error='Failed to get overview'), HTTPStatus.BAD_GATEWAY) # GET => HTML with conn.cursor() as cur: people = db.get_people(cur) ppl: List[Tuple[id, str, bool]] = [] for person_id, data in people.items(): first_name, last_name, display_name, email, phone, withheld, _ = data ppl.append((person_id, f"{first_name} {last_name}".strip(), withheld)) ppl.append((person_id, display_name, withheld)) ppl = sorted(ppl, key=operator.itemgetter(0, 2, 1)) names: List[Tuple[id, str]] = [] for person_id, name, withheld in ppl: name = f"{name} (#{person_id})" if withheld: name = f"{name} - WITHHELD" if (person_id, name) not in names: names.append((person_id, name)) return render_template('personoverview.html', names=names)