Skip to content

Commit

Permalink
items: create items dump functionality
Browse files Browse the repository at this point in the history
* Adds organisation $ref field to item json schema.
* Populates the organisation field from the location organisation field.
* Adds item availability to item index.

Co-Authored-by: Aly Badr <[email protected]>
  • Loading branch information
Aly Badr committed Nov 22, 2019
1 parent 87be6be commit 327a4b7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
24 changes: 24 additions & 0 deletions rero_ils/modules/items/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def delete_from_index(self):
def create(cls, data, id_=None, delete_pid=False,
dbcommit=False, reindex=False, **kwargs):
"""Create item record."""
cls._item_build_org_ref(data)
record = super(Item, cls).create(
data, id_, delete_pid, dbcommit, reindex, **kwargs)
if not data.get('holding'):
Expand All @@ -248,6 +249,23 @@ def update(self, data, dbcommit=False, reindex=False):

return self

@classmethod
def _item_build_org_ref(cls, data):
"""Build $ref for the organisation of the item."""
loc_pid = data.get('location', {}).get('pid')
if not loc_pid:
loc_pid = data.get('location').get('$ref').split('locations/')[1]
org_pid = Location.get_record_by_pid(loc_pid).organisation_pid
base_url = current_app.config.get('RERO_ILS_APP_BASE_URL')
url_api = '{base_url}/api/{doc_type}/{pid}'
org_ref = {
'$ref': url_api.format(
base_url=base_url,
doc_type='organisations',
pid=org_pid or cls.organisation_pid)
}
data['organisation'] = org_ref

def item_link_to_holding(self):
"""Link an item to a holding record."""
from ..holdings.api import \
Expand Down Expand Up @@ -947,3 +965,9 @@ def item_has_active_loan_or_request(self):
)
search_result = search.execute()
return search_result.hits.total

def dumps(self, **kwargs):
"""Return pure Python dictionary with record metadata."""
dump = super(Item, self).dumps(**kwargs)
dump['available'] = self.available
return dump
11 changes: 11 additions & 0 deletions rero_ils/modules/items/jsonschemas/items/item-v0.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@
"pattern": "^https://ils.rero.ch/api/holdings/.+?$"
}
}
},
"organisation": {
"title": "Organisation",
"type": "object",
"properties": {
"$ref": {
"title": "Organisation URI",
"type": "string",
"pattern": "^https://ils.rero.ch/api/organisations/.*?$"
}
}
}
}
}
1 change: 1 addition & 0 deletions rero_ils/modules/items/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ def enrich_item_data(sender, json=None, record=None, index=None,
json['organisation'] = {
'pid': org_pid
}
json['available'] = item.available
6 changes: 4 additions & 2 deletions tests/api/test_items_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ def test_items_post_put_delete(client, document, loc_public_martigny,
assert res.status_code == 201

# Check that the returned record matches the given data
del data['metadata']['holding']
for deleted_key in ['holding', 'available']:
data['metadata'].pop(deleted_key)
assert data['metadata'] == item_lib_martigny_data

res = client.get(item_url)
assert res.status_code == 200

data = get_json(res)
del data['metadata']['holding']
for deleted_key in ['holding', 'available']:
data['metadata'].pop(deleted_key)
assert item_lib_martigny_data == data['metadata']

# Update record/PUT
Expand Down
22 changes: 21 additions & 1 deletion tests/api/test_items_rest_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,34 @@
import pytest
from flask import url_for
from invenio_accounts.testutils import login_user_via_session
from utils import postdata
from utils import get_json, postdata

from rero_ils.modules.documents.views import item_status_text
from rero_ils.modules.errors import InvalidRecordID
from rero_ils.modules.items.api import Item, ItemStatus
from rero_ils.modules.loans.api import LoanAction


def test_item_dumps(client, item_lib_martigny, org_martigny,
librarian_martigny_no_email):
"""Test item dumps and elastic search version."""
item_dumps = Item(item_lib_martigny.dumps()).replace_refs()

assert item_dumps.get('available')
assert item_dumps.get('organisation').get('pid') == org_martigny.pid

login_user_via_session(client, librarian_martigny_no_email.user)
record_url = url_for('invenio_records_rest.item_item',
pid_value=item_lib_martigny.pid)

res = client.get(record_url)
assert res.status_code == 200

item_es = Item(get_json(res).get('metadata'))
assert item_es.available
assert item_es.organisation_pid == org_martigny.pid


def test_checkout_no_loan_given(client, librarian_martigny_no_email,
patron_martigny_no_email, loc_public_martigny,
item_lib_martigny, json_header,
Expand Down

0 comments on commit 327a4b7

Please sign in to comment.