feat(backend): detalle ORCID en export y sync sin borrar datos
Enriquece obras al exportar SWORD/ZIP, pide detalle en obras nuevas (con tope), preserva campos de detalle en re-sync y desenvuelve la respuesta work de ORCID.
This commit is contained in:
@@ -10,6 +10,8 @@ from app.core.rate_limit import limiter
|
||||
from app.db.models import Publication, PublicationDownload, Researcher
|
||||
from app.db.session import get_db
|
||||
from app.security.export_auth import require_export_access
|
||||
from app.services.orcid_client import get_display_name
|
||||
from app.services.publication_enrichment import enrich_publications_from_orcid
|
||||
from app.services.sword_generator import SWORDGenerator
|
||||
from app.services.zip_generator import ZIPGenerator
|
||||
from app.utils.orcid_validator import ORCID_PATTERN, is_valid_orcid
|
||||
@@ -51,6 +53,22 @@ def _record_downloads(db: Session, current: Researcher, pubs: Iterable[Publicati
|
||||
db.commit()
|
||||
|
||||
|
||||
def _prepare_researcher_and_publications_for_export(
|
||||
db: Session,
|
||||
researcher: Researcher,
|
||||
pubs: List[Publication],
|
||||
) -> None:
|
||||
"""Nombre del investigador y detalle ORCID de obras antes de generar SWORD/ZIP."""
|
||||
if not researcher.name:
|
||||
display_name = get_display_name(researcher.orcid_id)
|
||||
if display_name:
|
||||
researcher.name = display_name
|
||||
db.commit()
|
||||
db.refresh(researcher)
|
||||
|
||||
enrich_publications_from_orcid(db, researcher, pubs)
|
||||
|
||||
|
||||
def _validate_pub_ids(pub_ids: List[UUID]) -> List[UUID]:
|
||||
if len(pub_ids) > settings.MAX_PUB_IDS_BATCH:
|
||||
raise HTTPException(status_code=413, detail="Too many publication IDs")
|
||||
@@ -98,6 +116,10 @@ async def export_multiple_sword(
|
||||
raise HTTPException(status_code=404, detail="No publications found")
|
||||
|
||||
researcher = db.query(Researcher).filter_by(id=pubs[0].researcher_id).first()
|
||||
if not researcher:
|
||||
raise HTTPException(status_code=404, detail="Researcher not found")
|
||||
|
||||
_prepare_researcher_and_publications_for_export(db, researcher, pubs)
|
||||
|
||||
xml_bytes = SWORDGenerator.generate_feed_xml(researcher, pubs)
|
||||
if current:
|
||||
@@ -129,6 +151,8 @@ async def export_researcher_sword(
|
||||
if not pubs:
|
||||
raise HTTPException(status_code=404, detail="No publications found for this researcher")
|
||||
|
||||
_prepare_researcher_and_publications_for_export(db, researcher, pubs)
|
||||
|
||||
xml_bytes = SWORDGenerator.generate_feed_xml(researcher, pubs)
|
||||
if current:
|
||||
_record_downloads(db, current, pubs)
|
||||
@@ -156,6 +180,10 @@ async def export_multiple_zip(
|
||||
raise HTTPException(status_code=404, detail="No publications found")
|
||||
|
||||
researcher = db.query(Researcher).filter_by(id=pubs[0].researcher_id).first()
|
||||
if not researcher:
|
||||
raise HTTPException(status_code=404, detail="Researcher not found")
|
||||
|
||||
_prepare_researcher_and_publications_for_export(db, researcher, pubs)
|
||||
|
||||
zip_bytes = ZIPGenerator.generate_zip(researcher, pubs)
|
||||
if current:
|
||||
@@ -187,6 +215,8 @@ async def export_researcher_zip(
|
||||
if not pubs:
|
||||
raise HTTPException(status_code=404, detail="No publications found for this researcher")
|
||||
|
||||
_prepare_researcher_and_publications_for_export(db, researcher, pubs)
|
||||
|
||||
zip_bytes = ZIPGenerator.generate_zip(researcher, pubs)
|
||||
if current:
|
||||
_record_downloads(db, current, pubs)
|
||||
|
||||
Reference in New Issue
Block a user