Skip to content

Commit

Permalink
Adds an index field to the results['query']['limits'] data structure.
Browse files Browse the repository at this point in the history
This allows webview to properly reference auxiliary author data
when performing a special case search.
  • Loading branch information
rich-hart authored and mmulich committed Feb 4, 2015
1 parent 8e192f1 commit ab08d0a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cnxarchive/sql/get-users-by-ids.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ FROM (SELECT username AS id,
NULL AS website,
NULL AS othername
FROM users AS u
WHERE u.username = ANY ( %s )) AS user_row
WHERE u.username = %s ) AS user_row
74 changes: 41 additions & 33 deletions cnxarchive/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,51 +1247,59 @@ def test_author_special_case_search(self):
# Build the request
import string
environ = self._make_environ()
sub = 'subject:"LADY GAGA AND THE SOCIOLOGY OF FAME"'
sub = 'subject:"Arguing with Judge Judy: Popular ‘Logic’ on TV Judge Shows"'
auth0 = 'authorID:cnxcap'
auth1 = 'authorID:OpenStaxCollege'
environ['QUERY_STRING'] = 'q=' + string.join([sub, auth0, auth1], ' ')
auth2 = 'authorID:DrBunsenHoneydew'
fields = [sub, auth0, auth1, auth2]
environ['QUERY_STRING'] = 'q=' + string.join(fields, ' ')

from ..views import search
results = search(environ, self._start_response)[0]
status = self.captured_response['status']
headers = self.captured_response['headers']

self.assertEqual(status, '200 OK')
self.assertEqual(headers[0], ('Content-type', 'application/json'))
results = json.loads(results)

self.assertEqual(results['results']['total'], 0)

expected = [
[
{
u'website': None,
u'surname': u'Physics',
u'suffix': None,
u'firstname': u'College',
u'title': None,
u'othername': None,
u'emails': [u'info@openstaxcollege.org'],
u'fullname': u'OSC Physics Maintainer',
u'id': u'cnxcap'
}
],
[
{
u'website': None,
u'surname': None,
u'suffix': None,
u'firstname': u'OpenStax College',
u'title': None,
u'othername': None,
u'emails': [u'info@openstaxcollege.org'],
u'fullname': u'OpenStax College',
u'id': u'OpenStaxCollege'
}
{u'website': None,
u'surname': u'Physics',
u'suffix': None,
u'firstname': u'College',
u'title': None,
u'othername': None,
u'emails': [u'info@openstaxcollege.org'],
u'fullname': u'OSC Physics Maintainer',
u'id': u'cnxcap',
},
{u'website': None,
u'surname': None,
u'suffix': None,
u'firstname': u'OpenStax College',
u'title': None,
u'othername': None,
u'emails': [u'info@openstaxcollege.org'],
u'fullname': u'OpenStax College',
u'id': u'OpenStaxCollege',
},
{u'fullname': None,
u'id': u'DrBunsenHoneydew',
},
]
]

self.assertEqual(results['results']['auxiliary']['authors'], expected)
auxiliary_authors = results['results']['auxiliary']['authors']

self.assertEqual(auxiliary_authors, expected)

# check to see if auxilary authors list is referenced
# by the correct indexs in results['query']['limits']
# list
for limit in results['query']['limits']:
if limit['tag'] == 'authorID':
self.assertIn('index', limit.keys())
idx = limit['index']
aux_info = expected[idx]
self.assertEqual(limit['value'], aux_info['id'])

def test_search_only_subject(self):
# From the Content page, we have a list of subjects (tags),
Expand Down
47 changes: 28 additions & 19 deletions cnxarchive/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,6 @@ def search(environ, start_response):
# Add the supplemental result information.
results['results']['auxiliary'] = db_results.auxiliary

status = '200 OK'
headers = [('Content-type', 'application/json')]
start_response(status, headers)

# In the case where a search is performed with an authorId
# has a filter, it is possible for the database to return
# no results even if the author exists in the database.
Expand All @@ -567,21 +563,34 @@ def search(environ, start_response):
# The author information is then used to update the
# results returned by the first database query.
if len(db_results) <= 0:
authors_list = []
keyword_list = query.filters
for key, value in keyword_list:
if key == 'authorID':
authors_list.append(value)
if authors_list:
settings = get_settings()
connection = settings[config.CONNECTION_STRING]
with psycopg2.connect(connection) as db_connection:
with db_connection.cursor() as cursor:
arguments = (authors_list, )
statement = SQL['get-users-by-ids']
cursor.execute(statement, arguments)
authors_db_results = cursor.fetchall()
results['results']['auxiliary']['authors'] = authors_db_results
authors_results = []
limits = results['query']['limits']
index = 0
settings = get_settings()
connection = settings[config.CONNECTION_STRING]
statement = SQL['get-users-by-ids']
with psycopg2.connect(connection) as db_connection:
with db_connection.cursor() as cursor:
for idx, limit in enumerate(limits):
if limit['tag'] == 'authorID':
author = limit['value']
arguments = (author,)
cursor.execute(statement, arguments)
author_db_result = cursor.fetchall()
if author_db_result:
author_db_result = author_db_result[0][0]
else:
author_db_result = {'id': author, 'fullname': None}
authors_results.append(author_db_result)
limit['index'] = index
index = index + 1
limits[idx] = limit
results['query']['limits'] = limits
results['results']['auxiliary']['authors'] = authors_results

status = '200 OK'
headers = [('Content-type', 'application/json')]
start_response(status, headers)

return [json.dumps(results)]

Expand Down

0 comments on commit ab08d0a

Please sign in to comment.