Skip to content

Commit

Permalink
Type param (#177)
Browse files Browse the repository at this point in the history
* added the type param and fixed tests

* fixing count error

* improving efficiency a little bit

* fixing bugs
  • Loading branch information
femalves authored Feb 5, 2024
1 parent 71e7026 commit d1c886d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion biblib/tests/functional_tests/test_deletion_abuse_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_deletion_abuse_epic(self):
url,
headers=stub_owner.headers
)
self.assertTrue(response.json['libraries_count'] == 0)
self.assertTrue(response.json['count'] == 0)
self.assertTrue(len(response.json['libraries']) == 0)
if __name__ == '__main__':
unittest.main(verbosity=2)
4 changes: 2 additions & 2 deletions biblib/tests/functional_tests/test_deletion_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_deletion_epic(self):
url,
headers=stub_user.headers
)
self.assertTrue(response.json['libraries_count'] == 1)
self.assertTrue(response.json['count'] == 1)

# Deletes the first library
url = url_for('documentview', library=library_id_1)
Expand All @@ -110,7 +110,7 @@ def test_deletion_epic(self):
url,
headers=stub_user.headers
)
self.assertTrue(response.json['libraries_count'] == 0)
self.assertTrue(response.json['count'] == 0)

if __name__ == '__main__':
unittest.main(verbosity=2)
4 changes: 2 additions & 2 deletions biblib/tests/functional_tests/test_returned_data_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_returned_data_user_view_epic(self):
headers=user_dave.headers
)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.json['libraries_count'] == 1)
self.assertTrue(response.json['count'] == 1)
self.assertTrue(
response.json['libraries'][0]['num_documents']
== (number_of_documents+number_of_documents_second)
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_returned_data_user_view_epic(self):
self.assertEqual(response.status_code, 200)
libraries = response.json

self.assertTrue(libraries['libraries_count'] == 1)
self.assertTrue(libraries['count'] == 1)
self.assertTrue(
libraries['libraries'][0]['num_documents'] == number_of_documents+1
)
Expand Down
13 changes: 6 additions & 7 deletions biblib/tests/unit_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_user_can_retrieve_library(self):
absolute_uid=user.absolute_uid
)

self.assertEqual(libraries['libraries_count'], number_of_libs)
self.assertEqual(libraries['count'], number_of_libs)

def test_user_can_retrieve_rows_number_of_libraries(self):
"""
Expand Down Expand Up @@ -474,8 +474,7 @@ def test_user_can_retrieve_all_libraries_by_paging(self):
rows=10
)
libraries += curr_libraries['libraries']
total_libraries = curr_libraries['libraries_count']

total_libraries = curr_libraries['count']
self.assertEqual(total_libraries, 100)
self.assertEqual(libraries_full, libraries)

Expand Down Expand Up @@ -562,7 +561,7 @@ def test_user_retrieves_correct_library_content_from_userview(self):
absolute_uid=user.absolute_uid
)

self.assertTrue(libraries['libraries_count'] == number_of_libs)
self.assertTrue(libraries['count'] == number_of_libs)

for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
Expand Down Expand Up @@ -592,7 +591,7 @@ def test_user_retrieves_correct_library_content_from_userview(self):
sort_order='asc'
)

self.assertTrue(libraries['libraries_count'] == number_of_libs)
self.assertTrue(libraries['count'] == number_of_libs)
for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
Expand Down Expand Up @@ -621,7 +620,7 @@ def test_user_retrieves_correct_library_content_from_userview(self):
sort_order='asc'
)

self.assertTrue(libraries['libraries_count'] == number_of_libs)
self.assertTrue(libraries['count'] == number_of_libs)
for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
Expand Down Expand Up @@ -650,7 +649,7 @@ def test_user_retrieves_correct_library_content_from_userview(self):
absolute_uid=user_other.absolute_uid
)

self.assertTrue(libraries['libraries_count'] == 2)
self.assertTrue(libraries['count'] == 2)

def test_dates_of_updates_change_correctly(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions biblib/tests/unit_tests/test_webservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ def test_can_remove_a_library(self):
url,
headers=stub_user.headers
)
self.assertTrue(response.json['libraries_count'] == 0,
self.assertTrue(response.json['count'] == 0,
response.json)

# Check there is no document content
Expand Down Expand Up @@ -3173,7 +3173,7 @@ def test_when_user_has_classic_credentials_and_library_returns(self):
url,
headers=stub_user.headers
)
self.assertTrue(response.json['libraries_count'] > 0,
self.assertTrue(response.json['count'] > 0,
msg='No libraries returned: {}'.format(response.json))
self.assertEqual(response.json['libraries'][0]['name'], stub_library.name)
self.assertEqual(response.json['libraries'][0]['description'],
Expand Down
68 changes: 41 additions & 27 deletions biblib/views/user_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,35 @@ def retrieve_user_email(owner_absolute_uid):
service
)
return response

@classmethod
def get_user_libraries(cls, session, service_uid, sort_col, sort_order, access_type, start=0, rows=None):

query = session.query(Permissions, Library)\
.join(Permissions.library)\
.filter(Permissions.user_id == service_uid)

if access_type == 'owner':
query = query.filter(Permissions.permissions['owner'].astext.cast(Boolean).is_(True))
elif access_type == 'collaborator':
query = query.filter(Permissions.permissions['owner'].astext.cast(Boolean).is_(False))

# Sorting
query = query.order_by(getattr(getattr(Library, sort_col), sort_order)())

count = query.count()

# Pagination
if start > 0:
query = query.offset(start)
if rows:
query = query.limit(rows)

return query.all(), count


@classmethod
def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None, sort_col="date_created", sort_order="desc", ownership=False):
def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None, sort_col="date_created", sort_order="desc", access_type="all"):
"""
Get all the libraries a user has
:param service_uid: microservice UID of the user
Expand All @@ -79,19 +105,11 @@ def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None, sort_col="
# 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:
user_libraries = session.query(Permissions, Library)\
.join(Permissions.library)\
.filter(Permissions.user_id == service_uid)\
.order_by(getattr(getattr(Library, sort_col), sort_order)())\
.all()

libraries_response = {'libraries_count': len(user_libraries)}

if rows: rows=start+rows

my_libraries = []
shared_with_me = []
for permission, library in user_libraries[start:rows]:

user_libraries, count = cls.get_user_libraries(session, service_uid, sort_col, sort_order, access_type, start, rows)

libraries = []
for permission, library in user_libraries:

# For this library get all the people who have permissions
users = session.query(Permissions).filter_by(
Expand Down Expand Up @@ -153,16 +171,9 @@ def get_libraries(cls, service_uid, absolute_uid, start=0, rows=None, sort_col="
owner=owner
)

if (ownership and main_permission in ['owner']) or not ownership:
my_libraries.append(payload)
elif ownership and main_permission in ['admin', 'read', 'write']:
shared_with_me.append(payload)

if ownership:
libraries_response['my_libraries'] = my_libraries
libraries_response['shared_with_me'] = shared_with_me
else:
libraries_response['libraries'] = my_libraries
libraries.append(payload)

libraries_response = {'count': count, 'libraries': libraries}

return libraries_response

Expand All @@ -176,6 +187,8 @@ def get(self):
:param rows: The number of rows to return from the start point (int). default: None (returns all libraries)
:param sort: Library column to sort on. default: date_created (date_created, date_last_modified, name)
:param order: Direction sort libraries. default: desc (asc, desc)
:param access_type: Level of library ownership. default: all (all, owner, collaborator)
:return: list of the users libraries with the relevant information
Header:
Expand Down Expand Up @@ -228,8 +241,9 @@ def get(self):
if sort_order not in ['asc', 'desc']:
raise ValueError

ownership = get_params.get('ownership', default=False, type=check_boolean)
current_app.logger.debug("GET params: {}, start: {}, end: {}".format(get_params, start, rows))
access_type = get_params.get('access_type', default='all', type=str)
if access_type not in ['all', 'owner', 'collaborator']:
raise ValueError

except ValueError:
msg = "Failed to parse input parameters: {}. Please confirm the request is properly formatted.".format(request)
Expand All @@ -245,7 +259,7 @@ def get(self):
rows=rows,
sort_col=sort_col,
sort_order=sort_order,
ownership=ownership)
access_type=access_type)
return response, 200

def post(self):
Expand Down

0 comments on commit d1c886d

Please sign in to comment.