|
38 | 38 | import yaml |
39 | 39 | from celery import current_app as current_celery |
40 | 40 | from dojson.contrib.marc21.utils import create_record |
41 | | -from elasticsearch_dsl.query import Q |
42 | 41 | from flask import current_app |
43 | 42 | from flask.cli import with_appcontext |
44 | 43 | from invenio_db import db |
|
55 | 54 | from werkzeug.local import LocalProxy |
56 | 55 | from werkzeug.security import gen_salt |
57 | 56 |
|
58 | | -from rero_ils.modules.documents.api import Document, DocumentsSearch |
| 57 | +from rero_ils.modules.documents.api import Document |
59 | 58 | from rero_ils.modules.documents.dojson.contrib.marc21tojson.rero import marc21 |
60 | | -from rero_ils.modules.documents.views import get_cover_art |
| 59 | +from rero_ils.modules.documents.tasks import add_cover_urls as task_add_cover_urls |
61 | 60 | from rero_ils.modules.entities.remote_entities.api import RemoteEntity |
62 | 61 | from rero_ils.modules.files.cli import load_files |
63 | 62 | from rero_ils.modules.items.api import Item |
@@ -1579,26 +1578,25 @@ def token_create(name, user, scopes, internal, access_token): |
1579 | 1578 | click.secho("No user found", fg="red") |
1580 | 1579 |
|
1581 | 1580 |
|
1582 | | -@utils.command("add_cover_urls") |
1583 | | -@click.option("-v", "--verbose", "verbose", is_flag=True, default=False) |
| 1581 | +@utils.command() |
| 1582 | +@click.option("-c", "--commit", "commit", is_flag=True, default=False, help="Commit changes to database.") |
| 1583 | +@click.option("-v", "--verbose", "verbose", is_flag=True, default=False, help="Verbose print.") |
| 1584 | +@click.option("--cached/--no-cached", "cached", default=True, help="Use cached thumbnails.") |
| 1585 | +@click.option("-s", "--scroll", "scroll", default="60m", help="Elasticsearch scroll timeout.") |
| 1586 | +@click.option( |
| 1587 | + "-p", "--pids", "pids_file", type=click.File("r"), default=None, help="File with document pids to process." |
| 1588 | +) |
1584 | 1589 | @with_appcontext |
1585 | | -def add_cover_urls(verbose): |
| 1590 | +def add_cover_urls(commit, verbose, cached, scroll, pids_file): |
1586 | 1591 | """Add cover urls to all documents with isbns.""" |
1587 | | - click.secho("Add cover urls.", fg="green") |
1588 | | - search = ( |
1589 | | - DocumentsSearch() |
1590 | | - .filter("term", identifiedBy__type="bf:Isbn") |
1591 | | - .filter("bool", must_not=[Q("term", electronicLocator__content="coverImage")]) |
1592 | | - .params(preserve_order=True) |
1593 | | - .sort({"pid": {"order": "asc"}}) |
1594 | | - .source("pid") |
1595 | | - ) |
1596 | | - for idx, hit in enumerate(search.scan()): |
1597 | | - pid = hit.pid |
1598 | | - record = Document.get_record_by_pid(pid) |
1599 | | - url = get_cover_art(record=record, save_cover_url=True) |
| 1592 | + |
| 1593 | + pids = None |
| 1594 | + if pids_file: |
| 1595 | + pids = [line.strip() for line in pids_file if line.strip()] |
1600 | 1596 | if verbose: |
1601 | | - click.echo(f"{idx}:\tdocument: {pid}\t{url}") |
| 1597 | + click.secho(f"Processing {len(pids)} documents from file", fg="green") |
| 1598 | + |
| 1599 | + return task_add_cover_urls(commit=commit, verbose=verbose, cached=cached, scroll=scroll, pids=pids) |
1602 | 1600 |
|
1603 | 1601 |
|
1604 | 1602 | @utils.command() |
|
0 commit comments