|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# RERO ILS |
| 4 | +# Copyright (C) 2019-2022 RERO |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as published by |
| 8 | +# the Free Software Foundation, version 3 of the License. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Affero General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Affero General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +"""Click command-line interface for item record management.""" |
| 19 | + |
| 20 | +from io import BytesIO |
| 21 | +from random import choice, shuffle |
| 22 | + |
| 23 | +import click |
| 24 | +from flask import current_app |
| 25 | +from flask.cli import with_appcontext |
| 26 | +from invenio_access.permissions import system_identity |
| 27 | +from invenio_db import db |
| 28 | +from invenio_search import current_search |
| 29 | +from rero_invenio_files.pdf import PDFGenerator |
| 30 | + |
| 31 | +from rero_ils.modules.documents.api import Document |
| 32 | +from rero_ils.modules.documents.dojson.contrib.jsontodc import dublincore |
| 33 | +from rero_ils.modules.libraries.api import Library |
| 34 | + |
| 35 | + |
| 36 | +def create_pdf_file(document): |
| 37 | + """Create a pdf file from a document record. |
| 38 | +
|
| 39 | + :param doc: Document - a given bibliographic document |
| 40 | + :returns: a byte stream of the pdf content |
| 41 | + """ |
| 42 | + # get the dublin core format of the given document |
| 43 | + dc = dublincore.do(document, "english") |
| 44 | + data = dict(header=f"Document ({document.pid})") |
| 45 | + if titles := dc.get("titles"): |
| 46 | + data["title"] = "\n".join(titles) |
| 47 | + if contributors := dc.get("contributors"): |
| 48 | + data["authors"] = contributors |
| 49 | + # Some fields are not well converted |
| 50 | + # TODO: remove this when the dc conversion will be fixed |
| 51 | + try: |
| 52 | + if descriptions := dc.get("descriptions"): |
| 53 | + data["summary"] = "\n".join(descriptions) |
| 54 | + except Exception: |
| 55 | + pass |
| 56 | + generator = PDFGenerator(data) |
| 57 | + generator.render() |
| 58 | + return generator.output() |
| 59 | + |
| 60 | + |
| 61 | +def create_pdf_record_files(document, metadata, flush=False): |
| 62 | + """Creates and attach a pdf file to a given document. |
| 63 | +
|
| 64 | + :param document: Document - the document record. |
| 65 | + :param metadata: dict - file metadata. |
| 66 | + """ |
| 67 | + # add document link |
| 68 | + metadata.setdefault("links", []).append(f"doc_{document.pid}") |
| 69 | + ext = current_app.extensions["rero-invenio-files"] |
| 70 | + # get services |
| 71 | + record_service = ext.records_service |
| 72 | + file_service = ext.records_files_service |
| 73 | + # generate the PDF file |
| 74 | + stream = BytesIO(create_pdf_file(document)) |
| 75 | + # create the record file |
| 76 | + record = record_service.record_cls.create(data={"metadata": metadata}) |
| 77 | + record.commit() |
| 78 | + recid = record["id"] |
| 79 | + # index the file record |
| 80 | + record_service.indexer.index_by_id(record.id) |
| 81 | + if flush: |
| 82 | + current_search.flush_and_refresh(record_service.record_cls.index._name) |
| 83 | + # attach the file record to the document |
| 84 | + file_name = f"doc_{document.pid}.pdf" |
| 85 | + file_service.init_files(system_identity, recid, [{"key": file_name}]) |
| 86 | + file_service.set_file_content(system_identity, recid, file_name, stream) |
| 87 | + file_service.commit_file(system_identity, recid, file_name) |
| 88 | + db.session.commit() |
| 89 | + |
| 90 | + |
| 91 | +@click.command("create_files") |
| 92 | +@click.argument("number", type=int) |
| 93 | +@with_appcontext |
| 94 | +def create_files(number): |
| 95 | + """Create attached files. |
| 96 | +
|
| 97 | + :param number: integer - number of the files to generate |
| 98 | + """ |
| 99 | + collections = ["col1", "col2", "col3"] |
| 100 | + doc_pids = list(Document.get_all_pids()) |
| 101 | + lib_pids = list(Library.get_all_pids()) |
| 102 | + shuffle(doc_pids) |
| 103 | + |
| 104 | + for _ in range(0, number): |
| 105 | + pid = choice(doc_pids) |
| 106 | + click.echo(f"Create file for {pid}") |
| 107 | + doc = Document.get_record_by_pid(pid) |
| 108 | + lib_pid = choice(lib_pids) |
| 109 | + metadata = dict( |
| 110 | + collections=[choice(collections)], |
| 111 | + owners=[f"lib_{lib_pid}"] |
| 112 | + ) |
| 113 | + create_pdf_record_files(document=doc, metadata=metadata) |
0 commit comments