def bib_to_ourl(request): """ Converts bibjson to openurl. """ log.debug('\n\n\nstarting bib_to_ourl()...') start = datetime.datetime.now() bibjson = request.GET.get('bibjson', None) if not bibjson: return HttpResponseBadRequest( '400 / Bad Request -- no `bibjson` parameter') bibjson = urllib.parse.unquote(bibjson) bib = json.loads(bibjson) ourl = openurl_from_bib(bib) log.debug('ourl, ```%s```' % ourl) rtrn_dct = { 'query': { 'date_time': str(start), 'url': '{schm}://{hst}{uri}'.format(schm=request.scheme, hst=request.META['HTTP_HOST'], uri=request.META.get( 'REQUEST_URI', request.META['PATH_INFO'])) }, 'response': { 'openurl': ourl, 'elapsed_time': str(datetime.datetime.now() - start), 'decoded_bibjson': bibjson } } jsn = json.dumps(rtrn_dct, sort_keys=True, indent=2) return HttpResponse(jsn, content_type='application/javascript; charset=utf-8')
def test_to_openurl_pmid(self): #Round trip the query q = 'rft_val_fmt=info:ofi/fmt:kev:mtx:journal&rfr_id=info:sid/pss.sagepub.com&rft.spage=569&rft.issue=4&rft.epage=582&rft.aulast=Nolen-Hoeksema&ctx_tim=2010-11-27T19:38:39.6-08:00&url_ctx_fmt=info:ofi/fmt:kev:mtx:ctx&rft.volume=100&url_ver=Z39.88-2004&rft.stitle=J%20Abnorm%20Psychol&rft.auinit1=S.&rft.atitle=Responses%20to%20depression%20and%20their%20effects%20on%20the%20duration%20of%20depressive%20episodes.&ctx_ver=Z39.88-2004&rft_id=info:pmid/1757671&rft.jtitle=Journal%20of%20abnormal%20psychology&rft.genre=article' bib = bib_from_openurl(q) #pprint(bib) ourl = openurl_from_bib(bib) bib2 = bib_from_openurl(ourl) #pprint(bib2) self.assertEqual(bib['journal']['shortcode'], bib2['journal']['shortcode'])
def test_to_openurl_article(self): q = 'issn=1175-5652&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rfr_id=info%3Asid%2Ffirstsearch.oclc.org%3AMEDLINE&req_dat=<sessionid>0<%2Fsessionid>&pid=<accession+number>678061209<%2Faccession+number><fssessid>0<%2Ffssessid>&rft.date=2010&volume=8&date=2010&rft.volume=8&rfe_dat=<accessionnumber>678061209<%2Faccessionnumber>&url_ver=Z39.88-2004&atitle=The+missing+technology%3A+an+international+comparison+of+human+capital+investment+in+healthcare.&genre=article&epage=71&spage=361&id=doi%3A&rft.spage=361&rft.sici=1175-5652%282010%298%3A6<361%3ATMTAIC>2.0.TX%3B2-O&aulast=Frogner&rft.issue=6&rft.epage=71&rft.jtitle=Applied+health+economics+and+health+policy&rft.aulast=Frogner&title=Applied+health+economics+and+health+policy&rft.aufirst=BK&rft_id=urn%3AISSN%3A1175-5652&sici=1175-5652%282010%298%3A6<361%3ATMTAIC>2.0.TX%3B2-O&sid=FirstSearch%3AMEDLINE&rft.atitle=The+missing+technology%3A+an+international+comparison+of+human+capital+investment+in+healthcare.&issue=6&rft.issn=1175-5652&rft.genre=article&aufirst=BK' bib = bib_from_openurl(q) #Round trip the query ourl = openurl_from_bib(bib) bib2 = bib_from_openurl(ourl) self.assertEqual(bib['type'], bib2['type']) self.assertEqual(bib['title'], bib2['title']) self.assertEqual(bib['journal']['name'], bib2['journal']['name']) self.assertEqual(bib['year'], bib2['year'])
def test_dissertation(self): request = { 'ctx_enc': ['info:ofi/enc:UTF-8'], 'ctx_ver': ['Z39.88-2004'], 'rft.au': ['Mangla, Akshay'], 'rft.aufirst': ['Akshay'], 'rft.aulast': ['Mangla'], 'rft.date': ['2013-01-01'], 'rft.genre': ['dissertations & theses'], 'rft.title': [ 'Rights for the Voiceless: The State, Civil Society and Primary Education in Rural India' ], 'rft_id': ['info:doi/'], 'rft_val_fmt': ['info:ofi/fmt:kev:mtx:dissertation'] } b = bib_from_qdict(request) ourl = openurl_from_bib(b) parsed_ourl = cgi.parse_qs(ourl) self.assertTrue('dissertation' in parsed_ourl.get('rft.genre')) self.assertTrue( 'Rights for the Voiceless' in parsed_ourl.get('rft.title')[0]) self.assertTrue('Mangla, Akshay') in parsed_ourl.get('rft.au') self.assertTrue('2013' in parsed_ourl.get('rft.date'))
def test_missing_title(self): #Mock a sample request dict coming from Django. request_dict = { 'rft.pub': ['Triple Canopy'], 'rft_val_fmt': ['info:ofi/fmt:kev:mtx:book'], 'rfr_id': ['info:sid/libx:brown'], 'rft.au': ['Coleman, Gabriella'], 'rft.aulast': ['Coleman'], 'rft.aufirst': ['Gabriella'], 'rft_id': ['http://canopycanopycanopy.com/15/our_weirdness_is_free'], 'rft.btitle': [ 'Our Weirdness Is Free: The logic of Anonymous \u2014 online army, agent of chaos, and seeker of justice' ], 'url_ver': ['Z39.88-2004'], 'rft.atitle': [''], 'rft.genre': ['bookitem'] } b = bib_from_qdict(request_dict) ourl = openurl_from_bib(b) parsed_ourl = cgi.parse_qs(ourl) self.assertTrue('bookitem' in parsed_ourl.get('rft.genre')) self.assertTrue('Coleman, Gabriella' in parsed_ourl.get('rft.au'))
def test_symbols_in_title(self): q = u"rft.title=Elective delivery at 34⁰(/)⁷ to 36⁶(/)⁷ weeks' gestation and its impact on neonatal outcomes in women with stable mild gestational hypertension&pmid=20934682&genre=journal" #Just round trip to see if we raise encoding errors. bib = bib_from_openurl(q) openurl = openurl_from_bib(bib) bib2 = bib_from_openurl(openurl)
def test_book_chapter(self): q = 'sid=info:sid/sersol:RefinerQuery&genre=bookitem&isbn=9781402032899&&title=The+roots+of+educational+change&atitle=Finding+Keys+to+School+Change%3A+A+40-Year+Odyssey&volume=&part=&issue=&date=2005&spage=25&epage=57&aulast=Miles&aufirst=Matthew' b = bib_from_openurl(q) ourl = openurl_from_bib(b) qdict = cgi.parse_qs(ourl) self.assertTrue('bookitem' in qdict.get('rft.genre'))