diff --git a/rero_ils/modules/patrons/views.py b/rero_ils/modules/patrons/views.py index 0fd6e4794e..b378bbf468 100644 --- a/rero_ils/modules/patrons/views.py +++ b/rero_ils/modules/patrons/views.py @@ -339,6 +339,8 @@ 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: @@ -346,23 +348,29 @@ def get_institution_code(institution): 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 = [] diff --git a/tests/api/patrons/test_patrons_rest.py b/tests/api/patrons/test_patrons_rest.py index 276a8f9262..d1670f1141 100644 --- a/tests/api/patrons/test_patrons_rest.py +++ b/tests/api/patrons/test_patrons_rest.py @@ -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, @@ -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 @@ -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."""