From dc2fceb337fd471ee6a910582acc3ff85f650b4b Mon Sep 17 00:00:00 2001 From: Renaud Michotte Date: Wed, 28 Apr 2021 17:16:13 +0200 Subject: [PATCH] vendors: restrict deletion * Prevents the deletion of a vendor record when a holdings is attached to it. * Closes rero/rero-ils#1822. Co-Authored-by: Renaud Michotte --- rero_ils/modules/vendors/api.py | 27 +++++++++++---------------- tests/api/vendors/test_vendors.py | 3 ++- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/rero_ils/modules/vendors/api.py b/rero_ils/modules/vendors/api.py index c2b2017464..b93aae7f96 100644 --- a/rero_ils/modules/vendors/api.py +++ b/rero_ils/modules/vendors/api.py @@ -77,26 +77,21 @@ def order_email(self): 'order_contact', self.get('default_contact', {}) ).get('email') - def get_number_of_acq_orders(self): + def count_links_to_me(self, search_class): """Get number of acquisition orders.""" - return AcqOrdersSearch().filter( - 'term', vendor__pid=self.pid).source().count() - - def get_number_of_acq_invoices(self): - """Get number of acquisition invoices.""" - return AcquisitionInvoicesSearch().filter( - 'term', vendor__pid=self.pid).source().count() + return search_class()\ + .filter('term', vendor__pid=self.pid)\ + .source().count() def get_links_to_me(self): """Get number of links.""" - links = {} - acq_orders = self.get_number_of_acq_orders() - if acq_orders: - links['acq_orders'] = acq_orders - - acq_invoices = self.get_number_of_acq_invoices() - if acq_invoices: - links['acq_invoices'] = acq_invoices + from rero_ils.modules.holdings.api import HoldingsSearch + links = { + 'acq_orders': self.count_links_to_me(AcqOrdersSearch), + 'acq_invoices': self.count_links_to_me(AcquisitionInvoicesSearch), + 'holdings': self.count_links_to_me(HoldingsSearch), + } + links = {k: v for k, v in links.items() if v} return links def reasons_not_to_delete(self): diff --git a/tests/api/vendors/test_vendors.py b/tests/api/vendors/test_vendors.py index 99457e7c3e..e4b17d30d8 100644 --- a/tests/api/vendors/test_vendors.py +++ b/tests/api/vendors/test_vendors.py @@ -73,13 +73,14 @@ def test_filtered_vendors_get(client, librarian_martigny, def test_vendors_can_delete( client, vendor_martigny, acq_order_fiction_martigny, - acq_invoice_fiction_martigny): + acq_invoice_fiction_martigny, holding_lib_martigny_w_patterns): """Test can delete a vendor with a linked acquisition order.""" assert not vendor_martigny.can_delete reasons = vendor_martigny.reasons_not_to_delete() assert reasons['links']['acq_orders'] assert reasons['links']['acq_invoices'] + assert reasons['links']['holdings'] def test_vendor_post_update_delete(client, librarian_martigny,