Skip to content

Commit

Permalink
Merge cf81e93 into ad6a406
Browse files Browse the repository at this point in the history
  • Loading branch information
jma committed Jul 27, 2020
2 parents ad6a406 + cf81e93 commit f9f5122
Show file tree
Hide file tree
Showing 23 changed files with 27,478 additions and 25,628 deletions.
10,923 changes: 5,611 additions & 5,312 deletions data/holdings_big.json

Large diffs are not rendered by default.

2,249 changes: 1,066 additions & 1,183 deletions data/holdings_small.json

Large diffs are not rendered by default.

32,766 changes: 17,013 additions & 15,753 deletions data/items_big.json

Large diffs are not rendered by default.

6,564 changes: 3,193 additions & 3,371 deletions data/items_small.json

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,26 @@ def _(x):
record_serializers={
'application/json': (
'rero_ils.modules.serializers:json_v1_response'
),
'application/rero+json': (
'rero_ils.modules.items.serializers:json_item_search'
)
},
record_serializers_aliases={
search_serializers_aliases={
'json': 'application/json',
'rero+json': 'application/rero+json',
'csv': 'text/csv',
},
search_serializers={
'application/json': (
'rero_ils.modules.serializers:json_v1_search'
)
),
'application/rero+json': (
'rero_ils.modules.items.serializers:json_item_search'
),
'text/csv': (
'rero_ils.modules.items.serializers:csv_item_search'
),
},
list_route='/items/',
record_loaders={
Expand Down Expand Up @@ -1272,6 +1283,36 @@ def _(x):
_('status'): and_term_filter('holdings.items.status'),
}
),
items=dict(
aggs=dict(
library=dict(
terms=dict(
field='library.pid',
size=RERO_ILS_DEFAULT_AGGREGATION_SIZE)
),
location=dict(
terms=dict(
field='location.pid',
size=RERO_ILS_DEFAULT_AGGREGATION_SIZE)
),
item_type=dict(
terms=dict(
field='item_type.pid',
size=RERO_ILS_DEFAULT_AGGREGATION_SIZE)
),
status=dict(
terms=dict(
field='status',
size=RERO_ILS_DEFAULT_AGGREGATION_SIZE)
)
),
filters={
_('location'): and_term_filter('location.pid'),
_('library'): and_term_filter('library.pid'),
_('item_type'): and_term_filter('item_type.pid'),
_('status'): and_term_filter('status')
},
),
patrons=dict(
aggs=dict(
roles=dict(
Expand Down
9 changes: 9 additions & 0 deletions rero_ils/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class Meta:
doc_types = None


def search_document_by_pid(pid):
"""Retrieve document by pid from index."""
query = DocumentsSearch().filter('term', pid=pid)
try:
return next(query.scan())
except StopIteration:
return None


class Document(IlsRecord):
"""Document class."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
</div>
<div class="col-sm-2">
{% if item.call_number %}
{{ item.call_number }}
{{ item | format_item_call_number }}
{% endif %}
</div>
{% set locations = item|item_library_pickup_locations %}
Expand Down
5 changes: 3 additions & 2 deletions rero_ils/modules/items/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"""Item data module."""

from .api import Item, ItemsIndexer, ItemsSearch, item_id_fetcher, \
item_id_minter
item_id_minter, search_active_loans_for_item
from .circulation import ItemCirculation
from .issue import ItemIssue
from .record import ItemRecord

__all__ = (
'Item', 'ItemRecord', 'ItemCirculation', 'ItemIssue', 'ItemsSearch',
'ItemsIndexer', 'item_id_fetcher', 'item_id_minter'
'ItemsIndexer', 'item_id_fetcher', 'item_id_minter',
'search_active_loans_for_item'
)
31 changes: 29 additions & 2 deletions rero_ils/modules/items/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from functools import partial

from elasticsearch.exceptions import NotFoundError
from flask import current_app
from invenio_circulation.search.api import search_by_pid
from invenio_search import current_search

from .circulation import ItemCirculation
Expand All @@ -44,6 +46,23 @@
item_id_fetcher = partial(id_fetcher, provider=ItemProvider)


def search_active_loans_for_item(item_pid):
"""Return count and all active loans for an item."""
states = ['PENDING'] + \
current_app.config['CIRCULATION_STATES_LOAN_ACTIVE']
search = search_by_pid(
item_pid=item_pid,
filter_states=states,
sort_by_field='transaction_date',
sort_order='desc'
)
loans_count = search.count()
try:
return loans_count, search.scan()
except StopIteration:
return loans_count


class ItemsSearch(IlsRecordsSearch):
"""ItemsSearch."""

Expand Down Expand Up @@ -86,11 +105,19 @@ def extended_validation(self, **kwargs):
"""Add additional record validation.
Ensures that only one note of each type is present.
Ensures that call number is present before using second call number.
:return: False if notes array has multiple notes with same type
:returns: False if any conditions isn't respected
"""
note_types = [note.get('type') for note in self.get('notes', [])]
return len(note_types) == len(set(note_types))

if len(note_types) != len(set(note_types)):
return False

if not self.get('call_number') and self.get('second_call_number'):
return False

return True

def delete_from_index(self):
"""Delete record from index."""
Expand Down
8 changes: 8 additions & 0 deletions rero_ils/modules/items/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import json
import random
import string
from random import randint

import click
Expand Down Expand Up @@ -273,6 +274,13 @@ def create_random_item(item_pid, location_pid, missing, item_type_pid,
'faucibus'
}], k=random.randint(1, 4))

# RANDOMLY ADD SECOND CALL NUMBER
# we will add a second call number to +/- 25% of the items.
if random.random() < 0.25:
item['second_call_number'] = ''.join(
random.choices(string.ascii_uppercase + string.digits, k=5)
)

return missing, item


Expand Down
13 changes: 12 additions & 1 deletion rero_ils/modules/items/jsonschemas/items/item-v0.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"propertiesOrder": [
"barcode",
"call_number",
"second_call_number",
"item_type",
"location",
"status",
Expand Down Expand Up @@ -60,7 +61,17 @@
"call_number": {
"title": "Call number",
"type": "string",
"minLength": 4
"minLength": 4,
"form": {
"expressionProperties": {
"templateOptions.required": "field.parent.model.second_call_number !== ''"
}
}
},
"second_call_number": {
"title": "Second call number",
"type": "string",
"minLength": 1
},
"location": {
"title": "Location",
Expand Down
4 changes: 4 additions & 0 deletions rero_ils/modules/items/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ def enrich_item_data(sender, json=None, record=None, index=None,
json['organisation'] = {
'pid': org_pid
}
lib_pid = item.get_library().replace_refs()['pid']
json['library'] = {
'pid': lib_pid
}
json['available'] = item.available
10 changes: 10 additions & 0 deletions rero_ils/modules/items/mappings/v6/items/item-v0.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@
"call_number": {
"type": "keyword"
},
"second_call_number": {
"type": "keyword"
},
"location": {
"properties": {
"pid": {
"type": "keyword"
}
}
},
"library": {
"properties": {
"pid": {
"type": "keyword"
}
}
},
"document": {
"properties": {
"pid": {
Expand Down
53 changes: 53 additions & 0 deletions rero_ils/modules/items/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019 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
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Items serializers."""
from invenio_records_rest.serializers.response import record_responsify, \
search_responsify

from rero_ils.modules.items.serializers.response import search_responsify_csv
from rero_ils.modules.serializers import JSONSerializer, RecordSchemaJSONV1

from .csv import ItemCSVSerializer
from .json import ItemsJSONSerializer

csv_item = ItemCSVSerializer(
JSONSerializer,
csv_included_fields=[
'pid',
'document_pid',
'document_title',
'document_type',
'location_name',
'barcode',
'call_number',
'second_call_number',
'loans_count',
'last_transaction_date',
'status',
'created'
]
)
csv_item_response = record_responsify(csv_item, "text/csv")
csv_item_search = search_responsify_csv(csv_item, "text/csv")
"""CSV serializer."""

json_item = ItemsJSONSerializer(RecordSchemaJSONV1)
"""JSON serializer."""

json_item_search = search_responsify(json_item, 'application/rero+json')
json_item_response = record_responsify(json_item, 'application/rero+json')

0 comments on commit f9f5122

Please sign in to comment.