Skip to content

Commit

Permalink
Library paging sort (#167)
Browse files Browse the repository at this point in the history
* Updated default sort for libraries to be created_date desc.

* Modified unit tests to work with the new default sort order.

* Expanded sorting to be more flexible.

* updated tests for new sorting.

* Fixed typo in error message. Swapped library sort order to default ascending.

* Updated ClassicUserEpic.
  • Loading branch information
tjacovich authored Apr 19, 2023
1 parent eb5d181 commit 2492de2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
64 changes: 63 additions & 1 deletion biblib/tests/unit_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,72 @@ def test_user_retrieves_correct_library_content(self):
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))


for j in range(number_of_libs):
i = len(libraries) - j - 1
for key in ['name', 'description', 'public']:
self.assertEqual(libraries[i][key],
libs[j].user_view_post_data[key])

self.assertEqual(libraries[i]['num_documents'], 0)

if libraries[i]['id'] == _lib['id']:
self.assertEqual(libraries[i]['num_users'], 2)
else:
self.assertEqual(libraries[i]['num_users'], 1)

self.assertEqual(libraries[i]['permission'], 'owner')

# Get the library created with a different sort order
with MockEmailService(stub_user_1, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user.id,
absolute_uid=user.absolute_uid,
sort_order='asc'
)

self.assertTrue(len(libraries) == number_of_libs)
for library in libraries:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))


for i in range(number_of_libs):
for key in ['name', 'description', 'public']:
self.assertEqual(libraries[i][key],
libs[i].user_view_post_data[key])

self.assertEqual(libraries[i]['num_documents'], 0)

if libraries[i]['id'] == _lib['id']:
self.assertEqual(libraries[i]['num_users'], 2)
else:
self.assertEqual(libraries[i]['num_users'], 1)

self.assertEqual(libraries[i]['permission'], 'owner')

# Get the library created with a different sort order and sort column
with MockEmailService(stub_user_1, end_type='uid'):
libraries = self.user_view.get_libraries(
service_uid=user.id,
absolute_uid=user.absolute_uid,
sort_col='date_last_modified',
sort_order='asc'
)

self.assertTrue(len(libraries) == number_of_libs)
for library in libraries:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))


for i in range(number_of_libs):
for key in ['name', 'description', 'public']:
self.assertEqual(libraries[i][key],
libs[i].user_view_post_data[key])
libs[i].user_view_post_data[key])

self.assertEqual(libraries[i]['num_documents'], 0)

Expand Down
2 changes: 1 addition & 1 deletion biblib/views/http_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
number=400
)
BAD_PARAMS_ERROR = dict(
body='You passed GET arguments that was not parseable. Please See the API documentation: {0}'
body='You passed GET arguments that were not parseable. Please See the API documentation: {0}'
.format(API_HELP),
number=400
)
Expand Down
26 changes: 22 additions & 4 deletions biblib/views/user_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,29 @@ def retrieve_user_email(owner_absolute_uid):
return response

@classmethod
def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None):
def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None, sort_col="date_created", sort_order="desc"):
"""
Get all the libraries a user has
:param service_uid: microservice UID of the user
:param absolute_uid: unique UID of the user in the API
:param start: Index of the first library to return
:param rows: Number of libraries to return (default all)
:param sort_col: Library column to sort on (date_created, date_last_modified)
:param sort_dir: Direction sort libraries (asc, desc)
:return: list of libraries in json format
"""

# Get all the permissions for a user
# This can be improved into one database call rather than having
# one per each permission, but needs some checks in place.
# The nested getattr calls allow us to request a column from the library model,
# and then request the proper sort order from that column.
with current_app.session_scope() as session:
result = session.query(Permissions, Library)\
.join(Permissions.library)\
.filter(Permissions.user_id == service_uid)\
.order_by(getattr(getattr(Library, sort_col), sort_order)())\
.all()

if rows: rows=start+rows
Expand Down Expand Up @@ -156,7 +163,8 @@ def get(self):
:param start: The index of the library list to start on (int). default: 0
:param rows: The number of rows to return from the start point (int). default: None (returns all libraries)
:param sort_col: Library column to sort on. default: date_created (date_created, date_last_modified)
:param sort_dir: Direction sort libraries. default: desc (asc, desc)
:return: list of the users libraries with the relevant information
Header:
Expand Down Expand Up @@ -200,17 +208,27 @@ def get(self):
get_params = request.args
start = get_params.get('start', default=0, type=int)
rows = get_params.get('rows', type=int)

sort_col = get_params.get('sort', default='date_created', type=str)
if sort_col not in ['date_created', 'date_last_modified']:
raise ValueError

sort_order = get_params.get('order', default='asc', type=str)
if sort_order not in ['asc', 'desc']:
raise ValueError

current_app.logger.debug("GET params: {}, start: {}, end: {}".format(get_params, start, rows))

except ValueError:
msg = "Failed to parse input parameters: {}. Please confirm request is properly formatted.".format(request)
msg = "Failed to parse input parameters: {}. Please confirm the request is properly formatted.".format(request)
current_app.logger.exception(msg)
return err(BAD_PARAMS_ERROR)

service_uid = \
self.helper_absolute_uid_to_service_uid(absolute_uid=user)

user_libraries = self.get_libraries(service_uid=service_uid,
absolute_uid=user, start=start, rows=rows)
absolute_uid=user, start=start, rows=rows, sort_col=sort_col, sort_order=sort_order)
return {'libraries': user_libraries}, 200

def post(self):
Expand Down

0 comments on commit 2492de2

Please sign in to comment.