From 908e65983a563de9ac45d92f5f8151ea5a6e58af Mon Sep 17 00:00:00 2001 From: Peter Weber Date: Mon, 4 Jun 2018 14:21:01 +0200 Subject: [PATCH] fix: fix rero/reroils-app/#26 edit of items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Peter Weber Signed-off-by: Johnny MariƩthoz --- reroils_data/documents_items/api.py | 7 ++--- reroils_data/documents_items/utils.py | 42 ++++++++++++++++++++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/reroils_data/documents_items/api.py b/reroils_data/documents_items/api.py index 745dd77..dab171d 100644 --- a/reroils_data/documents_items/api.py +++ b/reroils_data/documents_items/api.py @@ -85,7 +85,6 @@ def remove_item(self, item, force=False, delindex=False): @classmethod def get_document_by_itemid(cls, id_, with_deleted=False): """Retrieve the record by id.""" - doc_item = cls.metadata.query.filter_by(item_id=id_).one() - doc_id = doc_item.document_id - doc = DocumentsWithItems.get_record_by_id(doc_id) - return doc + return super(DocumentsWithItems, cls).get_record_by_elementid( + id_, with_deleted + ) diff --git a/reroils_data/documents_items/utils.py b/reroils_data/documents_items/utils.py index 3ffdfa7..4e43b89 100644 --- a/reroils_data/documents_items/utils.py +++ b/reroils_data/documents_items/utils.py @@ -36,12 +36,15 @@ def delete_item(record_type, pid, record_indexer, parent_pid): The item is marked as deleted in the db, his pid as well. The document is reindexed. """ - document = DocumentsWithItems.get_record_by_pid(parent_pid) item = Item.get_record_by_pid(pid) + document = get_document(parent_pid, item.id) persistent_identifier = item.persistent_identifier - document.remove_item(item, delindex=True) - - _next = url_for('invenio_records_ui.doc', pid_value=parent_pid) + if document: + document.remove_item(item, delindex=True) + _next = url_for('invenio_records_ui.doc', pid_value=document.pid) + else: + item.delete(delindex=True) + _next = url_for('invenio_records_ui') return _next, persistent_identifier @@ -52,14 +55,31 @@ def save_item(data, record_type, fetcher, minter, If the item does not exists, it well be created and attached to the parent document. """ - document = DocumentsWithItems.get_record_by_pid(parent_pid) - pid = data.get('pid') - if pid: - item = Item.get_record_by_pid(pid) + item_pid = data.get('pid') + document = None + if item_pid: + item = Item.get_record_by_pid(item_pid) + document = get_document(parent_pid, item.id) item.update(data, dbcommit=True) else: item = Item.create(data, dbcommit=True) - document.add_item(item, dbcommit=True) - document.reindex() - _next = url_for('invenio_records_ui.doc', pid_value=parent_pid) + document = get_document(parent_pid, item.id) + if document: + document.add_item(item, dbcommit=True) + if document: + document.reindex() + if parent_pid: + _next = url_for('invenio_records_ui.doc', pid_value=parent_pid) + else: + _next = url_for('invenio_records_ui.item', pid_value=item.pid) return _next, item.persistent_identifier + + +def get_document(document_pid, item_id): + """Get document from document or item pid.""" + if document_pid: + return DocumentsWithItems.get_record_by_pid(document_pid) + elif item_id: + return DocumentsWithItems.get_document_by_itemid(item_id) + else: + return None