Skip to content

Commit

Permalink
feat(item): add extra data on item dumper
Browse files Browse the repository at this point in the history
Add the name of the temporary location as well as
the name of the collections.

* Closes #1321.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Apr 2, 2024
1 parent 0d5194c commit 20f7c0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
14 changes: 13 additions & 1 deletion rero_ils/modules/collections/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2022 RERO
# Copyright (C) 2019-2024 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand All @@ -17,6 +17,7 @@

"""API for manipulating collections."""

from datetime import datetime, timezone
from functools import partial

from rero_ils.modules.items.api import Item
Expand Down Expand Up @@ -49,6 +50,17 @@ class Meta:
index = 'collections'
doc_types = None

def active_by_item_pid(self, item_pid):
"""Search for active collections by item pid.
:param item_pid: string - the item to filter with.
:return: An ElasticSearch query to get hits related the entity.
"""
return self \
.filter('term', items__pid=item_pid) \
.filter('range', end_date={'gte': datetime.now(timezone.utc)}) \
.sort({'end_date': {'order': 'asc'}})


class Collection(IlsRecord):
"""Collection class."""
Expand Down
14 changes: 13 additions & 1 deletion rero_ils/modules/items/dumpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2023 RERO
# Copyright (C) 2019-2024 RERO
# Copyright (C) 2019-2023 UCLouvain
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -21,6 +21,7 @@

from invenio_records.dumpers import Dumper as InvenioRecordsDumper

from rero_ils.modules.collections.api import CollectionsSearch
from rero_ils.modules.commons.exceptions import MissingDataException
from rero_ils.modules.documents.api import Document
from rero_ils.modules.documents.dumpers import \
Expand Down Expand Up @@ -152,4 +153,15 @@ def dump(self, record, data):
data['pending_loans'] = [
first_request.dumps(LoanCirculationDumper())
]
# add temporary location name
if temporary_location_pid := item.get('temporary_location', {}).get(
'pid'
):
data['temporary_location']['name'] = Location.get_record_by_pid(
temporary_location_pid).get('name')
# add collections
results = CollectionsSearch().active_by_item_pid(item['pid'])\
.params(preserve_order=True).source('title').scan()
if collections := [collection.title for collection in results]:
data['collections'] = collections
return data
38 changes: 35 additions & 3 deletions tests/ui/items/test_items_dumpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2022 RERO
# Copyright (C) 2022-2024 RERO
# Copyright (C) 2022 UCLouvain
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -18,10 +18,13 @@

"""Items Record dumper tests."""
from copy import deepcopy
from datetime import date

import pytest
from dateutil.relativedelta import relativedelta
from utils import item_record_to_a_specific_loan_state

from rero_ils.modules.collections.api import Collection
from rero_ils.modules.commons.exceptions import MissingDataException
from rero_ils.modules.holdings.api import Holding
from rero_ils.modules.items.dumpers import CirculationActionDumper, \
Expand All @@ -33,7 +36,8 @@

def test_item_action_circulation_dumper(
item_lib_martigny, patron_martigny, loc_public_martigny,
librarian_martigny, circulation_policies):
librarian_martigny, circulation_policies, loc_public_martigny_bourg,
coll_martigny_1):
"""Test item circulation action dumper."""
params = {
'patron_pid': patron_martigny.pid,
Expand All @@ -43,7 +47,8 @@ def test_item_action_circulation_dumper(
}
item, _ = item_record_to_a_specific_loan_state(
item=item_lib_martigny, loan_state=LoanState.PENDING,
params=params, copy_item=True)
params=params, copy_item=False)

data = item.dumps(CirculationActionDumper())
# $ref resolution
assert data['library']['pid']
Expand All @@ -69,6 +74,33 @@ def test_item_action_circulation_dumper(
# number of pending requests
assert data['current_pending_requests'] == 1

# not temporary location
assert 'temporary_location' not in data

# not collections
assert 'collections' not in data

# update end date on the collection
collection = Collection.get_record_by_pid(coll_martigny_1["pid"])
next_end_date = date.today() + relativedelta(months=2)
collection["end_date"] = next_end_date.strftime("%Y-%m-%d")
collection.update(collection, dbcommit=True, reindex=True)

# add temporary location data
item["temporary_location"] = {
"pid": "loc15",
"type": "loc"
}

data = item.dumps(CirculationActionDumper())

# temporary location name
assert data['temporary_location']['name'] == loc_public_martigny_bourg\
.get('name')

# Collection title
assert data['collections'][0] == coll_martigny_1.get('title')


def test_item_circulation_dumper(item_lib_martigny):
"""Test item circulation dumper."""
Expand Down

0 comments on commit 20f7c0d

Please sign in to comment.