diff --git a/pubs/server.py b/pubs/server.py index 34e39d4..b091dcb 100644 --- a/pubs/server.py +++ b/pubs/server.py @@ -125,7 +125,7 @@ async def count_pubs(self): match, _ = self.args_to_match_query() return await self.db.publications.count_documents(match) - async def get_pubs(self, mongoid=False): + async def get_pubs(self, mongoid=False, sortby='date'): match, args = self.args_to_match_query() kwargs = {} @@ -139,7 +139,10 @@ async def get_pubs(self, mongoid=False): pubs = [] i = -1 - async for row in self.db.publications.find(match, **kwargs).sort('date', pymongo.DESCENDING): + cursor = self.db.publications.find(match, **kwargs) + if sortby: + cursor = cursor.sort(sortby, pymongo.DESCENDING) + async for row in cursor: i += 1 if mongoid: row['_id'] = str(row['_id']) @@ -179,7 +182,8 @@ async def get(self): class CSV(BaseHandler): async def get(self): - pubs = await self.get_pubs() + sortby = self.get_argument('sort', 'date') + pubs = await self.get_pubs(sortby=sortby) f = StringIO() writer = csv.DictWriter(f, fieldnames=FIELDS) diff --git a/pubs/templates/main.html b/pubs/templates/main.html index 596aaf0..4948b0e 100644 --- a/pubs/templates/main.html +++ b/pubs/templates/main.html @@ -56,9 +56,32 @@

Selected Publications:

{% if pub.get('abstract', '') %} -
Abstract:
{{ pub['abstract'] }}
+
Abstract: + +
{{ pub['abstract'] }}
+
{% end %} {% end %} +{% end %} + +{% block scripts %} + + {% end %} \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py index 9316b45..e6d6a43 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -266,4 +266,65 @@ async def test_csv(server): s = AsyncSession(retries=0, backoff_factor=1) r = await asyncio.wrap_future(s.get(url+'/csv')) r.raise_for_status() - \ No newline at end of file + + +@pytest.mark.asyncio +async def test_csv_sorted(server): + db, url = server + + await add_pub(db, title='Test Title1', authors=['auth1'], abstract='', + pub_type="journal", citation="TestJournal", date='2024-01-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title2', authors=['auth2'], abstract='', + pub_type="proceeding", citation="TestJournal", date='2024-02-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title3', authors=['auth1', 'auth3'], abstract='', + pub_type="thesis", citation="TestJournal", date='2024-03-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title4', authors=['auth1', 'auth4'], abstract='the abstract', + pub_type="internal", citation="TestReport", date='2023-01-01T01:00:00', + downloads=[], projects=['icecube']) + + s = AsyncSession(retries=0, backoff_factor=1) + r = await asyncio.wrap_future(s.get(url+'/csv')) + r.raise_for_status() + + lines = r.text.split('\n') + assert 'Test Title3' in lines[1] + assert 'Test Title2' in lines[2] + assert 'Test Title1' in lines[3] + assert 'Test Title4' in lines[4] + + +@pytest.mark.asyncio +async def test_csv_unsorted(server): + db, url = server + + await add_pub(db, title='Test Title1', authors=['auth1'], abstract='', + pub_type="journal", citation="TestJournal", date='2024-01-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title2', authors=['auth2'], abstract='', + pub_type="proceeding", citation="TestJournal", date='2024-02-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title3', authors=['auth1', 'auth3'], abstract='', + pub_type="thesis", citation="TestJournal", date='2024-03-01T01:00:00', + downloads=[], projects=['icecube']) + + await add_pub(db, title='Test Title4', authors=['auth1', 'auth4'], abstract='the abstract', + pub_type="internal", citation="TestReport", date='2023-01-01T01:00:00', + downloads=[], projects=['icecube']) + + s = AsyncSession(retries=0, backoff_factor=1) + r = await asyncio.wrap_future(s.get(url+'/csv?sort=')) + r.raise_for_status() + + lines = r.text.split('\n') + assert 'Test Title1' in lines[1] + assert 'Test Title2' in lines[2] + assert 'Test Title3' in lines[3] + assert 'Test Title4' in lines[4]