Skip to content

Commit

Permalink
patron: fix API /info
Browse files Browse the repository at this point in the history
* Fixes API /info for users without patron.
* Closes: rero#3607.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Mar 4, 2024
1 parent 0b24b26 commit acf40ef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
32 changes: 20 additions & 12 deletions rero_ils/modules/patrons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,30 +339,38 @@ def get_institution_code(institution):
# TODO: make this non rero specific using a configuration
return institution['code'] if institution['code'] != 'nj' else 'rbnj'

user = User.get_record(current_user.id).dumps_metadata()

# Process for all patrons
patrons = copy.deepcopy(current_patrons)
for patron in patrons:
patron['institution'] = patron.organisation
patron['patron']['type'] = PatronType.get_record_by_pid(
extracted_data_from_ref(patron['patron']['type']['$ref']))

# Stores the main patron
patron = get_main_patron(patrons)

# Birthdate
data = {}
birthdate = current_user.user_profile.get('birth_date')
if 'birthdate' in token_scopes and birthdate:
data['birthdate'] = birthdate
# Full name
name_parts = [
current_user.user_profile.get('last_name', '').strip(),
current_user.user_profile.get('first_name', '').strip()
]
fullname = ', '.join(filter(None, name_parts))
if 'fullname' in token_scopes and fullname:
data['fullname'] = fullname

# No patrons found for user
if not patrons:
return jsonify(data)

# Get the main patron
patron = get_main_patron(patrons)
# Barcode
if patron.get('patron', {}).get('barcode'):
data['barcode'] = patron['patron']['barcode'][0]

# Full name
if 'fullname' in token_scopes:
data['fullname'] = patron.formatted_name

# Birthdate
if 'birthdate' in token_scopes:
data['birthdate'] = current_user.user_profile.get('birth_date')

# Patron types
if 'patron_types' in token_scopes:
patron_types = []
Expand Down
23 changes: 23 additions & 0 deletions tests/api/patrons/test_patrons_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ def test_patron_info(app, client, patron_martigny, librarian_martigny):
_redirect_uris='')

# token with all scopes
librarian_token = Token(
client=oauth_client,
user=librarian_martigny.user,
token_type='bearer',
access_token='test_librarian_access',
expires=None,
is_personal=False,
is_internal=False,
_scopes=' '.join(scopes))

token = Token(
client=oauth_client,
user=patron_martigny.user,
Expand All @@ -511,7 +521,9 @@ def test_patron_info(app, client, patron_martigny, librarian_martigny):
is_internal=False)

db.session.add(oauth_client)
db.session.add(librarian_token)
db.session.add(token)
db.session.add(no_scope_token)
db.session.commit()

# denied with a wrong token
Expand Down Expand Up @@ -547,6 +559,17 @@ def test_patron_info(app, client, patron_martigny, librarian_martigny):
}]
}

# librarian information with all scopes
res = client.get(
url_for('api_patrons.info', access_token=librarian_token.access_token))
assert res.status_code == 200
assert res.json == {
'birthdate':
'1965-02-07',
'fullname':
'Pedronni, Marie'
}


def test_patrons_search(client, librarian_martigny):
"""Test patron search."""
Expand Down

0 comments on commit acf40ef

Please sign in to comment.