Skip to content

Commit

Permalink
holdings: allow deletion of serials holdings
Browse files Browse the repository at this point in the history
* Closes rero#1720.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep and rerowep committed Jul 22, 2021
1 parent 50dae7a commit 02695b4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
30 changes: 27 additions & 3 deletions rero_ils/modules/holdings/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from rero_ils.modules.items.models import ItemIssueStatus

from .models import HoldingIdentifier, HoldingMetadata, HoldingTypes
from ..api import IlsRecord, IlsRecordsIndexer
from ..api import IlsRecord, IlsRecordError, IlsRecordsIndexer
from ..documents.api import Document
from ..errors import MissingRequiredParameterError, RegularReceiveNotAllowed
from ..fetchers import id_fetcher
Expand Down Expand Up @@ -173,6 +173,20 @@ def extended_validation(self, **kwargs):
return _('Can not have multiple notes of same type.')
return True

def delete(self, force=False, dbcommit=False, delindex=False):
"""Delete record and persistent identifier."""
can, _ = self.can_delete
if can:
if self.is_serial:
# Delete all attached items
for item in self.get_items:
item.delete(
force=force, dbcommit=dbcommit, delindex=delindex)
return super().delete(
force=force, dbcommit=dbcommit, delindex=delindex)
else:
raise IlsRecordError.NotDeleted()

@property
def is_serial(self):
"""Shortcut to check if holding is a serial holding record."""
Expand Down Expand Up @@ -346,7 +360,8 @@ def get_items(self):
item = Item.get_record_by_pid(item_pid)
if not item.issue_status or \
item.issue_status == ItemIssueStatus.RECEIVED:
# inherit holdings first call# for issues with no 1st call#.
# inherit holdings first call#
# for issues with no 1st call#.
issue_call_number = item.issue_inherited_first_call_number
if issue_call_number:
item['call_number'] = issue_call_number
Expand Down Expand Up @@ -375,7 +390,16 @@ def get_links_to_me(self):
def reasons_not_to_delete(self):
"""Get reasons not to delete record."""
cannot_delete = {}
links = self.get_links_to_me()
links = {}
if self.is_serial:
# Find out if we can delete all items
not_deleteable_items = [
item for item in self.get_items if item.reasons_not_to_delete()
]
if not_deleteable_items:
links['items'] = len(not_deleteable_items)
else:
links = self.get_links_to_me()
if links:
cannot_delete['links'] = links
return cannot_delete
Expand Down
4 changes: 0 additions & 4 deletions rero_ils/modules/items/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ def reasons_not_to_delete(self):
links = self.get_links_to_me()
if links:
cannot_delete['links'] = links
if self.item_record_type == 'issue' and self.issue_is_regular:
cannot_delete['others'] = dict(
regular_issue_cannot_be_deleted=True
)
return cannot_delete

def in_collection(self, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions tests/api/items/test_items_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_issues_permissions(client, json_header,
assert issue_item is not None
assert issue_item.issue_is_regular

# a regular issue cannot be deleted
res = client.get(
url_for(
'api_blueprint.permissions',
Expand All @@ -57,4 +56,4 @@ def test_issues_permissions(client, json_header,
)
assert res.status_code == 200
data = get_json(res)
assert not data["delete"]["can"]
assert data["delete"]["can"]
18 changes: 3 additions & 15 deletions tests/ui/holdings/test_holdings_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from invenio_accounts.testutils import login_user_via_session
from jsonschema.exceptions import ValidationError

from rero_ils.modules.api import IlsRecordError
from rero_ils.modules.holdings.api import Holding
from rero_ils.modules.holdings.models import HoldingNoteTypes
from rero_ils.modules.items.api import Item
Expand Down Expand Up @@ -560,20 +559,9 @@ def test_regular_issue_creation_update_delete_api(
issue_display, expected_date = holding._get_next_issue_display_text(
holding.get('patterns'))
issue = holding.receive_regular_issue(dbcommit=True, reindex=True)
item = deepcopy(issue)
item['issue']['status'] = ItemIssueStatus.DELETED
issue.update(data=item, dbcommit=True, reindex=True)
created_issue = Item.get_record_by_pid(issue.pid)
assert created_issue.get('issue').get('status') == ItemIssueStatus.DELETED
# Unable to delete a regular issue
with pytest.raises(IlsRecordError.NotDeleted):
created_issue.delete(dbcommit=True, delindex=True)

# no errors when deleting an irregular issue
pid = created_issue.pid
created_issue.get('issue')['regular'] = False
created_issue.delete(dbcommit=True, delindex=True)
assert not Item.get_record_by_pid(pid)
issue_pid = issue.pid
assert holding.delete(dbcommit=True, delindex=True)
assert not Item.get_record_by_pid(issue_pid)


def test_holding_notes(client, librarian_martigny,
Expand Down

0 comments on commit 02695b4

Please sign in to comment.