Skip to content

Commit

Permalink
operation logs: update loan logs
Browse files Browse the repository at this point in the history
* Converts birthdate to age in patron.
* Adds local codes in patron.

Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze authored and jma committed Jun 12, 2021
1 parent f0102ed commit 3ac71fe
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
21 changes: 19 additions & 2 deletions rero_ils/modules/loans/logs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""Loans logs API."""

import hashlib
from datetime import date

from invenio_search import RecordsSearch

Expand Down Expand Up @@ -166,14 +167,30 @@ def _get_patron_data(cls, patron_pid):
patron_type = PatronType.get_record_by_pid(
extracted_data_from_ref(patron['patron']['type']['$ref']))

return {
def get_age(birth_date):
"""Calculate age from a birthdate.
:param Date birth_date: Date of birth.
:returns: Age
:rtype: int
"""
today = date.today()
return today.year - birth_date.year - (
(today.month, today.day) < (birth_date.month, birth_date.day))

data = {
'name': patron.formatted_name,
'type': patron_type['name'] if patron_type else None,
'birth_date': str(patron.user.profile.birth_date),
'age': get_age(patron.user.profile.birth_date),
'postal_code': patron.user.profile.postal_code,
'gender': patron.user.profile.gender or 'other'
}

if patron.get('local_codes'):
data['local_codes'] = patron['local_codes']

return data

@classmethod
def get_logs_by_record_pid(cls, pid):
"""Get all logs for a given record PID.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"index_patterns": [
"operation_logs*"
],
"mappings": {
"date_detection": false,
"numeric_detection": false,
Expand Down Expand Up @@ -75,14 +78,17 @@
"type": {
"type": "keyword"
},
"birth_date": {
"type": "date"
"age": {
"type": "short"
},
"postal_code": {
"type": "keyword"
},
"gender": {
"type": "keyword"
},
"local_codes": {
"type": "keyword"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@
"type": "string",
"minLength": 1
},
"birth_date": {
"title": "Birthdate",
"type": "string",
"minLength": 1,
"format": "yyyy-MM-dd"
"age": {
"title": "Age",
"type": "number",
"minLength": 1
},
"postal_code": {
"title": "Postal code",
Expand All @@ -153,12 +152,23 @@
"female",
"other"
]
},
"local_codes": {
"title": "Local codes",
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"title": "Local code",
"type": "string",
"minLength": 1
}
}
},
"required": [
"name",
"type",
"birth_date",
"age",
"postal_code",
"gender"
]
Expand Down
3 changes: 3 additions & 0 deletions tests/data/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,9 @@
"username": "louis",
"email": "lroduit@gmail.com",
"home_phone": "+41324993156",
"local_codes": [
"code1"
],
"keep_history": true,
"roles": [
"patron"
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/loans/test_loans_operation_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ def test_loan_operation_log(client, operation_log_data,
assert log_data['loan']['patron'] == {
'name': 'Roduit, Louis',
'type': 'children',
'birth_date': '1947-06-07',
'age': 74,
'postal_code': '1920',
'gender': 'other'
'gender': 'other',
'local_codes': ['code1']
}
assert log_data['loan']['item'] == {
'category': 'standard',
Expand Down

0 comments on commit 3ac71fe

Please sign in to comment.