Skip to content

Commit

Permalink
Merge pull request #98 from ehenneken/service_update
Browse files Browse the repository at this point in the history
service uses new schema
  • Loading branch information
ehenneken committed Feb 18, 2021
2 parents 812bddf + d6ff93b commit 116f5a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pip-log.txt
include
lib
man
venv
venv*

# Mac specific files
.DS_Store
Expand Down
78 changes: 25 additions & 53 deletions graphics_service/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,47 @@
import simplejson as json
import random
from flask import current_app
#from models import AlchemyEncoder, GraphicsModel
from .models import get_graphics_record
#from sqlalchemy.orm.exc import NoResultFound

thumb_link = '<a href="%s" target="_new" border=0>' + \
'<img src="%s" width="100px"></a>'
graph_link = '<a href="graphics" border=0><img src="%s"></a>'
ADSASS_img = '<img src="%s">'
ADSASS_thmb_img = '<img src="%s" width="100px">'
ADSASS_thmb_link = '<a href="graphics" border=0>%s</a>'
ADS_base_url = 'http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query'
ADS_image_url = ADS_base_url + \
'?bibcode=%s&db_key=AST&page_ind=%s&data_type=GIF&type=SCREEN_VIEW'


def get_graphics(bibcode):
# Query graphics database with bibcode supplied
results = get_graphics_record(bibcode)

if results and 'figures' in results:
if len(results['figures']) == 0:
# There are cases where an entry exists, but the 'figures'
# list is empty. Report this as if no data exists.
return {'Error': 'Unable to get results!', 'Error Info': 'No figure data for %s' % bibcode}
eprint = results.get('eprint')
if results and 'thumbnails' in results:
if len(results['thumbnails']) == 0:
# No thumbnails = nothing to display
return {'Error': 'Unable to get results!', 'Error Info': 'No thumbnail data for %s' % bibcode}
output = {}
source = results.get('source', 'NA')
results['ADSlink'] = []
if not eprint:
results['figures'] = [a for a in results['figures'] if a['figure_label'] != None]
display_figure = random.choice(results['figures'])
results['pick'] = ''
results['number'] = 0
output['bibcode'] = results['bibcode']
output['number'] = len(results['thumbnails'])
output['pick'] = graph_link % random.choice(results['thumbnails'])
if not output['pick'].find('http') >-1:
return {'Error': 'Unable to get results!', 'Error Info': 'Failed to get thumbnail for display image for %s' % output['bibcode']}
# Create this convoluted construct for backwards compatibility
output['figures'] = []
for t in results['thumbnails']:
fig_data = {
'figure_label':'',
'figure_caption':'',
'figure_type':'',
'images':[{'thumbnail':t}]
}
output['figures'].append(fig_data)
if source in current_app.config.get('GRAPHICS_EXTSOURCES'):
# Non-AAS journals link to IOPscience, rather than AIE
if source == "IOP" and bibcode[4:9] not in ['ApJ..','ApJS.','AJ...']:
source = "IOPscience"
header = current_app.config.get('GRAPHICS_HEADER').get(source,'')
results['header'] = header
try:
display_image = random.choice(display_figure['images'])
thumb_url = display_image['thumbnail']
results['pick'] = graph_link % thumb_url
except:
return {'Error': 'Unable to get results!', 'Error Info': 'Failed to get thumbnail for display image for %s' % bibcode}
for figure in results['figures']:
images = figure.get('images', [])
results['number'] += len(images)
for image in images:
thumb_url = image['thumbnail']
highr_url = image['highres']
output['header'] = header
elif source.upper() == 'ARXIV' \
and current_app.config.get('GRAPHICS_INCLUDE_ARXIV'):
results['header'] = 'Images extracted from the arXiv e-print'
try:
display_image = random.choice(display_figure['images'])
thumb_url = display_image['highres']
results['pick'] = graph_link % thumb_url
except:
return {'Error': 'Unable to get results!', 'Error Info': 'Failed to get thumbnail for display image for %s' % bibcode}
for figure in results['figures']:
images = figure.get('images', [])
results['number'] += len(images)
for image in images:
thumb_url = image['thumbnail']
highr_url = image['highres']
output['header'] = 'Images extracted from the arXiv e-print'
elif source.upper() == 'TEST':
results['pick'] = display_figure
output['pick'] = random.choice(results['thumbnails'])
else:
results = {'Error': 'Unable to get results!', 'Error Info': 'Unknown data source %s' % source}
return results
output = {'Error': 'Unable to get results!', 'Error Info': 'Unknown data source %s' % source}
return output

return results
21 changes: 10 additions & 11 deletions graphics_service/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
import mock
from datetime import datetime

def get_testdata(figures = [], source='TEST'):
def get_testdata(figures = [], thumbnails=[], source='TEST'):
thumbs = [f['images'][0].get('thumbnail','') for f in figures]
g = GraphicsModel(
bibcode='9999BBBBBVVVVQPPPPI',
doi='DOI',
source=source,
eprint=False,
figures=figures,
thumbnails=thumbs,
modtime=datetime.now()
)
results = json.loads(json.dumps(g, cls=AlchemyEncoder))
return results

class TestExpectedResults(TestCase):

figure_data = [{"images": [{"image_id": "fg1", "format": "gif",
"thumbnail": "fg1_thumb_url", "highres":
"fg1_highres_url"}],
"figure_caption": "Figure 1",
"figure_label": "Figure 1",
"figure_id": "fg1"}]
figure_data = [{'images': [{'thumbnail': 'http://fg1_thumb_url'}],
'figure_caption': '',
'figure_label': '',
'figure_type': u''}]

figure_data_no_thumb = [{"images": [{"image_id": "fg1", "format": "gif"}],
"figure_caption": "Figure 1",
Expand Down Expand Up @@ -74,7 +74,6 @@ def test_query(self, mock_execute_SQL_query):
self.assertTrue(r.status_code == 200)
self.assertTrue(r.json.get('figures') == self.figure_data)
self.assertTrue(r.json.get('bibcode') == '9999BBBBBVVVVQPPPPI')
self.assertTrue(r.json.get('pick')['figure_label'] == 'Figure 1')

@mock.patch('graphics_service.models.execute_SQL_query', return_value=get_testdata(figures=figure_data_no_thumb, source='IOP'))
def test_query_no_thumbnail(self, mock_execute_SQL_query):
Expand All @@ -83,7 +82,7 @@ def test_query_no_thumbnail(self, mock_execute_SQL_query):
url = url_for('graphics', bibcode='9999ApJ..VVVVQPPPPI')
r = self.client.get(url)
self.assertTrue(r.status_code == 200)
expected = {u'Error Info': u'Failed to get thumbnail for display image for 9999ApJ..VVVVQPPPPI', u'Error': u'Unable to get results!'}
expected = {u'Error Info': u'Failed to get thumbnail for display image for 9999BBBBBVVVVQPPPPI', u'Error': u'Unable to get results!'}
self.assertTrue(r.json == expected)

@mock.patch('graphics_service.models.execute_SQL_query', return_value=get_testdata(figures=figure_data_no_thumb, source='ARXIV'))
Expand All @@ -93,7 +92,7 @@ def test_query_no_thumbnail_2(self, mock_execute_SQL_query):
url = url_for('graphics', bibcode='9999ApJ..VVVVQPPPPI')
r = self.client.get(url)
self.assertTrue(r.status_code == 200)
expected = {u'Error Info': u'Failed to get thumbnail for display image for 9999ApJ..VVVVQPPPPI', u'Error': u'Unable to get results!'}
expected = {u'Error Info': u'Failed to get thumbnail for display image for 9999BBBBBVVVVQPPPPI', u'Error': u'Unable to get results!'}
self.assertTrue(r.json == expected)

@mock.patch('graphics_service.models.execute_SQL_query')
Expand Down Expand Up @@ -136,7 +135,7 @@ def test_query_no_data(self, mock_execute_SQL_query):
url = url_for('graphics', bibcode='foo')
r = self.client.get(url)
self.assertTrue(r.status_code == 200)
expected = {u'Error Info': u'No figure data for foo', u'Error': u'Unable to get results!'}
expected = {u'Error Info': u'No thumbnail data for foo', u'Error': u'Unable to get results!'}
self.assertTrue(r.json == expected)

@mock.patch('graphics_service.models.execute_SQL_query', return_value=get_testdata(figures=figure_data, source='IOP'))
Expand Down

0 comments on commit 116f5a9

Please sign in to comment.