Skip to content

Commit

Permalink
Internal report fixes (#20)
Browse files Browse the repository at this point in the history
* fix csv output if a field is missing from a pub

* make abstract hidden by default

* fix flake8

* fix tests
  • Loading branch information
dsschult authored Jan 18, 2024
1 parent b023f5f commit 3fc7555
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 50 deletions.
13 changes: 13 additions & 0 deletions pubs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@
'ara': 'ARA',
'wipac': 'WIPAC',
}

FIELDS = [
'_id',
'title',
'authors',
'type',
'citation',
'date',
'abstract',
'downloads',
'projects',
'sites',
]
2 changes: 1 addition & 1 deletion pubs/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging

from wipac_dev_tools import from_environment
from wipac_dev_tools import from_environment

from .server import create_server

Expand Down
10 changes: 6 additions & 4 deletions pubs/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from bson.objectid import ObjectId

from . import __version__ as version
from . import PUBLICATION_TYPES, PROJECTS, SITES
from . import PUBLICATION_TYPES, PROJECTS, SITES, FIELDS
from .utils import create_indexes, date_format, add_pub, edit_pub, try_import_file

logger = logging.getLogger('server')
Expand Down Expand Up @@ -182,12 +182,14 @@ async def get(self):
pubs = await self.get_pubs()

f = StringIO()
writer = csv.DictWriter(f, fieldnames=list(pubs['publications'][0].keys()))
writer = csv.DictWriter(f, fieldnames=FIELDS)
writer.writeheader()
for p in pubs['publications']:
data = {}
for k in p:
if isinstance(p[k], list):
for k in FIELDS:
if k not in p:
data[k] = ''
elif isinstance(p[k], list):
data[k] = ','.join(p[k])
else:
data[k] = p[k]
Expand Down
8 changes: 8 additions & 0 deletions pubs/static/external.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ article.publication {
content: "and";
padding: 0 .4rem;
}
.publication .abstract_div button {
line-height: .9rem;
}
.publication .abstract {
padding: .5rem;
margin: .5rem;
border: 1px solid #ccc;
}
.publication .downloads .download:not(:first-child)::before {
content: "|";
padding: 0 .4rem;
Expand Down
15 changes: 12 additions & 3 deletions pubs/static/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ async function Pubs(id, baseurl = 'https://publications.icecube.aq', filters = {
let pubsCount = await pubs_count_fut;

Vue.component('pub', {
data: function() {
return {
show_abstract: false
}
},
props: {
title: String,
authors: String,
Expand Down Expand Up @@ -182,15 +187,19 @@ async function Pubs(id, baseurl = 'https://publications.icecube.aq', filters = {
<div><span class="type">({{ type }})</span>
<span class="citation">{{ citation }}</span>
<span v-if="show_date" class="date">{{ day_month_year }}</span></div>
<div class="abstract_div" v-if="abstract">Abstract: <span class="abstract">{{ abstract }}</span></div>
<div>
<span class="downloads" v-if="downloads">Download:
<span class="downloads" v-if="downloads.length">Download:
<span class="download" v-for="link in downloads"><a :href="link" target="_blank">{{ getDomain(link) }}</a></span>
</span>
<span class="projects" v-if="projects && !filters.hide_projects">Project:
<span class="projects" v-if="projects.length && !filters.hide_projects">Project:
<span class="project" v-for="project in projects">{{ project_labels[project] }}</span>
</span>
</div>
<div class="abstract_div" v-if="abstract">Abstract:
<button v-if="show_abstract" v-on:click="show_abstract = false">Hide</button>
<button v-else v-on:click="show_abstract = true">Show</button>
<div class="abstract" v-if="show_abstract">{{ abstract }}</div>
</div>
</article>`
});

Expand Down
6 changes: 3 additions & 3 deletions pubs/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ <h2>Selected Publications:</h2>
<div><span class="type">({{ PUBLICATION_TYPES[pub['type']] }})</span>
<span class="citation">{{ pub['citation'] }}</span>
<span class="date">{{ date_format(pub['date']) }}</span></div>
{% if pub.get('abstract', '') %}
<div class="abstract_div">Abstract: <span class="abstract">{{ pub['abstract'] }}</span></div>
{% end %}
<div>
{% if pub['downloads'] %}
<span class="downloads">Download:
Expand All @@ -58,6 +55,9 @@ <h2>Selected Publications:</h2>
{% end %}
</span>
</div>
{% if pub.get('abstract', '') %}
<div class="abstract_div">Abstract: <div class="abstract">{{ pub['abstract'] }}</div></div>
{% end %}
</article>
{% end %}
</div>
Expand Down
36 changes: 18 additions & 18 deletions pubs/templates/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ <h2>Existing Publications:</h2>
<div><span class="type">({{ PUBLICATION_TYPES[pub['type']] }})</span>
<span class="citation">{{ pub['citation'] }}</span>
<span class="date">{{ date_format(pub['date']) }}</span></div>
{% if pub.get('abstract', '') %}
<div class="abstract_div">Abstract: <span class="abstract">{{ pub['abstract'] }}</span></div>
{% end %}
<div>
{% if pub['downloads'] %}
<span class="downloads">Download:
Expand All @@ -121,21 +118,24 @@ <h2>Existing Publications:</h2>
{% for site in pub['sites'] %}<span class="site">{{ SITES[site] }}</span>{% end %}
</span>
{% end %}
<div class="actions">
<button class="edit">Edit</button>
<form class="delete" action="{{ request.path }}" method="post">
{% module xsrf_form_html() %}
{% if search %}<input type="hidden" name="search" value="{{ search }}" />{% end %}
{% if start_date %}<input type="hidden" name="start_date" value="{{ start_date }}" />{% end %}
{% if end_date %}<input type="hidden" name="end_date" value="{{ end_date }}" />{% end %}
{% for t in type %}<input type="hidden" name="type" value="{{ t }}" />{% end %}
{% for p in projects %}<input type="hidden" name="projects" value="{{ p }}" />{% end %}
{% for s in sites %}<input type="hidden" name="sites" value="{{ s }}" />{% end %}
<input type="hidden" name="pub_id" value="{{ pub['_id'] }}" />
<input type="hidden" name="action" value="delete" />
<input type="submit" value="Delete" />
</form>
</div>
</div>
{% if pub.get('abstract', '') %}
<div class="abstract_div">Abstract: <div class="abstract">{{ pub['abstract'] }}</div></div>
{% end %}
<div class="actions">
<button class="edit">Edit</button>
<form class="delete" action="{{ request.path }}" method="post">
{% module xsrf_form_html() %}
{% if search %}<input type="hidden" name="search" value="{{ search }}" />{% end %}
{% if start_date %}<input type="hidden" name="start_date" value="{{ start_date }}" />{% end %}
{% if end_date %}<input type="hidden" name="end_date" value="{{ end_date }}" />{% end %}
{% for t in type %}<input type="hidden" name="type" value="{{ t }}" />{% end %}
{% for p in projects %}<input type="hidden" name="projects" value="{{ p }}" />{% end %}
{% for s in sites %}<input type="hidden" name="sites" value="{{ s }}" />{% end %}
<input type="hidden" name="pub_id" value="{{ pub['_id'] }}" />
<input type="hidden" name="action" value="delete" />
<input type="submit" value="Delete" />
</form>
</div>
</article>
{% end %}
Expand Down
18 changes: 9 additions & 9 deletions pubs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,38 @@ async def add_pub(db, title, authors, pub_type, abstract, citation, date, downlo
async def edit_pub(db, mongo_id, title=None, authors=None, pub_type=None, abstract=None, citation=None, date=None, downloads=None, projects=None, sites=None):
match = {'_id': ObjectId(mongo_id)}
update = {}
if title:
if title is not None:
assert isinstance(title, str)
update['title'] = title
if authors:
if authors is not None:
assert isinstance(authors, list)
for a in authors:
assert isinstance(a, str)
update['authors'] = authors
if pub_type:
if pub_type is not None:
assert pub_type in PUBLICATION_TYPES
update['type'] = pub_type
if abstract:
if abstract is not None:
assert isinstance(abstract, str)
update['abstract'] = abstract
if citation:
if citation is not None:
assert isinstance(citation, str)
update['citation'] = citation
if date:
if date is not None:
assert isinstance(date, str)
date_format(date)
update['date'] = date
if downloads:
if downloads is not None:
assert isinstance(downloads, list)
for d in downloads:
assert isinstance(d, str)
update['downloads'] = downloads
if projects:
if projects is not None:
assert isinstance(projects, list)
for p in projects:
assert p in PROJECTS
update['projects'] = projects
if sites:
if sites is not None:
assert isinstance(sites, list)
for s in sites:
assert s in SITES
Expand Down
26 changes: 26 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,29 @@ async def test_authors(server):

pubs = await get_pubs(url, params={'authors': 'auth4'})
assert len(pubs) == 0


@pytest.mark.asyncio
async def test_csv(server):
db, url = server

await add_pub(db, title='Test Title1', authors=['auth1'], abstract='',
pub_type="journal", citation="TestJournal", date=nowstr(),
downloads=[], projects=['icecube'])

await add_pub(db, title='Test Title2', authors=['auth2'], abstract='',
pub_type="proceeding", citation="TestJournal", date=nowstr(),
downloads=[], projects=['icecube'])

await add_pub(db, title='Test Title3', authors=['auth1', 'auth3'], abstract='',
pub_type="thesis", citation="TestJournal", date=nowstr(),
downloads=[], projects=['icecube'])

await add_pub(db, title='Test Title4', authors=['auth1', 'auth4'], abstract='the abstract',
pub_type="internal", citation="TestReport", date=nowstr(),
downloads=[], projects=['icecube'])

s = AsyncSession(retries=0, backoff_factor=1)
r = await asyncio.wrap_future(s.get(url+'/csv'))
r.raise_for_status()

24 changes: 12 additions & 12 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,30 @@ async def test_add_pub_err(mocker, title, authors, pub_type, abstract, citation,
@pytest.mark.parametrize('abstract', ['This is an abstract', '', None])
@pytest.mark.parametrize('citation', ['citation', None])
@pytest.mark.parametrize('date', ['2020-11-03T00:00:00', None])
@pytest.mark.parametrize('downloads', [['down1', 'down2'], None])
@pytest.mark.parametrize('projects', [['icecube','hawc'], None])
@pytest.mark.parametrize('sites', [['icecube', 'wipac'], None])
@pytest.mark.parametrize('downloads', [['down1', 'down2'], [], None])
@pytest.mark.parametrize('projects', [['icecube','hawc'], [], None])
@pytest.mark.parametrize('sites', [['icecube', 'wipac'], [], None])
@pytest.mark.asyncio
async def test_edit_pub(mocker, title, authors, pub_type, abstract, citation, date, downloads, projects, sites):
mongo_id = ObjectId()
args = {}
if title:
if title is not None:
args['title'] = title
if authors:
if authors is not None:
args['authors'] = authors
if pub_type:
if pub_type is not None:
args['type'] = pub_type
if abstract:
if abstract is not None:
args['abstract'] = abstract
if citation:
if citation is not None:
args['citation'] = citation
if date:
if date is not None:
args['date'] = date
if downloads:
if downloads is not None:
args['downloads'] = downloads
if projects:
if projects is not None:
args['projects'] = projects
if sites:
if sites is not None:
args['sites'] = sites

db = mocker.AsyncMock()
Expand Down

0 comments on commit 3fc7555

Please sign in to comment.