Skip to content

Commit

Permalink
vendors: restrict deletion
Browse files Browse the repository at this point in the history
* Prevents the deletion of a vendor record when a holdings is attached
  to it.
* Closes rero#1822.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Apr 30, 2021
1 parent 714fed6 commit dc2fceb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
27 changes: 11 additions & 16 deletions rero_ils/modules/vendors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/api/vendors/test_vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dc2fceb

Please sign in to comment.