Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorted csv option #22

Merged
merged 8 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pubs/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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'])
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion pubs/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,32 @@ <h2>Selected Publications:</h2>
</span>
</div>
{% if pub.get('abstract', '') %}
<div class="abstract_div">Abstract: <div class="abstract">{{ pub['abstract'] }}</div></div>
<div class="abstract_div">Abstract:
<button>Show</button>
<div class="abstract">{{ pub['abstract'] }}</div>
</div>
{% end %}
</article>
{% end %}
</div>
{% end %}

{% block scripts %}
<script src="https://code.jquery.com/jquery-3.7.1.slim.min.js" integrity="sha256-kmHvs0B+OpCW5GVHUNjv9rOmY0IvSIRcf7zGUDTDQM8=" crossorigin="anonymous"></script>
<script>
$( document ).ready(function() {
$(".abstract_div").each(function(i){
let ab = $(this).find('.abstract');
ab.hide();
$(this).find('button').on('click', function(){
ab.toggle();
if ($(this).text() == 'Show') {
$(this).text('Hide');
} else {
$(this).text('Show');
}
});
});
});
</script>
{% end %}
63 changes: 62 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()



@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]
Loading