Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vendors: restrict deletion #1854

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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