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

serializers: fix error when no bf:Publication #3139

Merged
merged 1 commit into from
Oct 27, 2022
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
33 changes: 18 additions & 15 deletions rero_ils/modules/items/serializers/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,25 @@ def _build_doc(data):
for edition in edition_statement.get('_text', [])
)
# process provision activity
provision_activity = next(
filter(lambda x: x.get('type')
== 'bf:Publication', data.get('provisionActivity'))
)
start_date = provision_activity.get('startDate', '')
end_date = provision_activity.get('endDate')
document_data['document_publication_year'] = \
f'{start_date} - {end_date}' \
if end_date else start_date

document_data['document_publisher'] = cls.separator.join(
data['value']
for stmt in provision_activity.get('statement', [])
for data in stmt.get('label', [])
if stmt['type'] == 'bf:Agent'
)
# we only use the first provision activity of type
# bf:publication
if any(
(provision_activity := prov).get('type' == 'bf:Publication')
for prov in data.get('provisionActivity', [])
):
Comment on lines +149 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First time I see this structure ! Nice 👍

start_date = provision_activity.get('startDate', '')
end_date = provision_activity.get('endDate')
document_data['document_publication_year'] = \
f'{start_date} - {end_date}' \
if end_date else start_date

document_data['document_publisher'] = cls.separator.join(
data['value']
for stmt in provision_activity.get('statement', [])
for data in stmt.get('label', [])
if stmt['type'] == 'bf:Agent'
)
return document_data

doc_search = DocumentsSearch() \
Expand Down
14 changes: 14 additions & 0 deletions tests/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_document_and_holdings_serializers(
def test_items_serializers(
client,
item_lib_martigny, # on shelf
document,
item_lib_fully, # on loan
csv_header,
json_header,
Expand Down Expand Up @@ -208,6 +209,19 @@ def test_items_serializers(
for field in fields:
assert field in data

# test provisionActivity without type bf:Publication
document['provisionActivity'][0]['type'] = 'bf:Manufacture'
document.update(document, True, True, True)

list_url = url_for('api_item.inventory_search')
response = client.get(list_url, headers=csv_header)
assert response.status_code == 200
data = get_csv(response)
assert data

document['provisionActivity'][0]['type'] = 'bf:Publication'
document.update(document, True, True, True)


def test_loans_serializers(
client,
Expand Down