Big changes
This commit is contained in:
parent
4240ad38e2
commit
e3f0d63fec
99 changed files with 8804 additions and 1731 deletions
|
|
@ -16,94 +16,6 @@ from app.services.domains.publications.queries import (
|
|||
unread_item_from_row,
|
||||
)
|
||||
from app.services.domains.publications.types import PublicationListItem, UnreadPublicationItem
|
||||
from app.services.domains.unpaywall.application import resolve_publication_oa_metadata
|
||||
from sqlalchemy import update
|
||||
from app.db.models import Publication
|
||||
|
||||
|
||||
def _with_oa_overrides(
|
||||
rows: list[PublicationListItem],
|
||||
oa_data: dict[int, tuple[str | None, str | None]],
|
||||
) -> list[PublicationListItem]:
|
||||
return [
|
||||
PublicationListItem(
|
||||
publication_id=row.publication_id,
|
||||
scholar_profile_id=row.scholar_profile_id,
|
||||
scholar_label=row.scholar_label,
|
||||
title=row.title,
|
||||
year=row.year,
|
||||
citation_count=row.citation_count,
|
||||
venue_text=row.venue_text,
|
||||
pub_url=row.pub_url,
|
||||
doi=(oa_data.get(row.publication_id) or (None, None))[0] or row.doi,
|
||||
pdf_url=(oa_data.get(row.publication_id) or (None, None))[1] or row.pdf_url,
|
||||
is_read=row.is_read,
|
||||
first_seen_at=row.first_seen_at,
|
||||
is_new_in_latest_run=row.is_new_in_latest_run,
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
|
||||
def _resolved_fields(
|
||||
*,
|
||||
row: PublicationListItem,
|
||||
resolved: tuple[str | None, str | None] | None,
|
||||
) -> tuple[str | None, str | None]:
|
||||
if resolved is None:
|
||||
return row.doi, row.pdf_url
|
||||
resolved_doi, resolved_pdf = resolved
|
||||
return resolved_doi or row.doi, resolved_pdf or row.pdf_url
|
||||
|
||||
|
||||
async def _persist_resolved_metadata(
|
||||
db_session: AsyncSession,
|
||||
*,
|
||||
rows: list[PublicationListItem],
|
||||
oa_data: dict[int, tuple[str | None, str | None]],
|
||||
) -> None:
|
||||
by_id = {row.publication_id: row for row in rows}
|
||||
updates: list[tuple[int, str | None, str | None]] = []
|
||||
for publication_id, resolved in oa_data.items():
|
||||
existing = by_id.get(publication_id)
|
||||
if existing is None:
|
||||
continue
|
||||
next_doi, next_pdf = _resolved_fields(row=existing, resolved=resolved)
|
||||
if next_doi == existing.doi and next_pdf == existing.pdf_url:
|
||||
continue
|
||||
updates.append((publication_id, next_doi, next_pdf))
|
||||
for publication_id, doi, pdf_url in updates:
|
||||
await db_session.execute(
|
||||
update(Publication)
|
||||
.where(Publication.id == publication_id)
|
||||
.values(doi=doi, pdf_url=pdf_url)
|
||||
)
|
||||
if updates:
|
||||
await db_session.commit()
|
||||
|
||||
|
||||
def missing_pdf_items(
|
||||
rows: list[PublicationListItem],
|
||||
*,
|
||||
limit: int,
|
||||
) -> list[PublicationListItem]:
|
||||
bounded_limit = max(0, int(limit))
|
||||
if bounded_limit == 0:
|
||||
return []
|
||||
return [row for row in rows if not row.pdf_url][:bounded_limit]
|
||||
|
||||
|
||||
async def resolve_and_persist_oa_metadata(
|
||||
db_session: AsyncSession,
|
||||
*,
|
||||
rows: list[PublicationListItem],
|
||||
unpaywall_email: str | None = None,
|
||||
) -> dict[int, tuple[str | None, str | None]]:
|
||||
if not rows:
|
||||
return {}
|
||||
oa_data = await resolve_publication_oa_metadata(rows, request_email=unpaywall_email)
|
||||
await _persist_resolved_metadata(db_session, rows=rows, oa_data=oa_data)
|
||||
return oa_data
|
||||
|
||||
|
||||
async def list_for_user(
|
||||
|
|
@ -112,7 +24,9 @@ async def list_for_user(
|
|||
user_id: int,
|
||||
mode: str = MODE_ALL,
|
||||
scholar_profile_id: int | None = None,
|
||||
favorite_only: bool = False,
|
||||
limit: int = 300,
|
||||
offset: int = 0,
|
||||
) -> list[PublicationListItem]:
|
||||
resolved_mode = resolve_publication_view_mode(mode)
|
||||
latest_run_id = await get_latest_completed_run_id_for_user(db_session, user_id=user_id)
|
||||
|
|
@ -122,7 +36,9 @@ async def list_for_user(
|
|||
mode=resolved_mode,
|
||||
latest_run_id=latest_run_id,
|
||||
scholar_profile_id=scholar_profile_id,
|
||||
favorite_only=favorite_only,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
)
|
||||
rows = [
|
||||
|
|
@ -138,22 +54,13 @@ async def retry_pdf_for_user(
|
|||
user_id: int,
|
||||
scholar_profile_id: int,
|
||||
publication_id: int,
|
||||
unpaywall_email: str | None = None,
|
||||
) -> PublicationListItem | None:
|
||||
item = await get_publication_item_for_user(
|
||||
return await get_publication_item_for_user(
|
||||
db_session,
|
||||
user_id=user_id,
|
||||
scholar_profile_id=scholar_profile_id,
|
||||
publication_id=publication_id,
|
||||
)
|
||||
if item is None:
|
||||
return None
|
||||
oa_data = await resolve_and_persist_oa_metadata(
|
||||
db_session,
|
||||
rows=[item],
|
||||
unpaywall_email=unpaywall_email,
|
||||
)
|
||||
return _with_oa_overrides([item], oa_data)[0]
|
||||
|
||||
|
||||
async def list_unread_for_user(
|
||||
|
|
@ -168,7 +75,9 @@ async def list_unread_for_user(
|
|||
mode=MODE_UNREAD,
|
||||
latest_run_id=None,
|
||||
scholar_profile_id=None,
|
||||
favorite_only=False,
|
||||
limit=limit,
|
||||
offset=0,
|
||||
)
|
||||
)
|
||||
return [unread_item_from_row(row) for row in result.all()]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue