def make_illiad_url(bibjson): """ Create an Illiad request URL from bibsjon. Requires adding a couple of Illiad specific fields. """ import urllib import urlparse from app_settings import ILLIAD_URL from bibjsontools import to_openurl base = ILLIAD_URL #Send to validate_bib to add default values missing fields. bib = illiad_validate(bibjson) #Holder for values to add to the raw openurl extra = {} #Get OCLC or pubmed IDS identifiers = bibjson.get('identifier', []) for idt in identifiers: if idt['type'] == 'pmid': extra['Notes'] = "PMID: %s.\r via easyAccess" % idt['id'] elif idt['type'] == 'oclc': extra['ESPNumber'] = idt['id'] if bib.get('_valid') is not True: if extra.get('Notes') is None: extra['Notes'] = '' extra['Notes'] += "\rNot enough data provided by original request." ourl = to_openurl(bib) for k,v in extra.iteritems(): ourl += "&%s=%s" % (urllib.quote_plus(k), urllib.quote_plus(v)) illiad = base % ourl return illiad
def get_context_data(self, **kwargs): """ Handle the request and return the 'resolver' to the user. """ from bibjsontools import to_openurl, from_openurl from utils import merge_bibjson context = super(ResolveView, self).get_context_data(**kwargs) #Check to see if a resource is set for this view. #This will happen when a permalink is being requested. this_resource = getattr(self, 'resource', None) if self.sersol_key != SERSOL_KEY: context['customer'] = self.sersol_key query = self.request.META.get('QUERY_STRING', None) if (not query) and (not this_resource): self.template_name = 'resolver/index.html' return context if this_resource: citation = this_resource.bib openurl = to_openurl(citation) else: #Check to see if caching is activated. if app_settings.CACHE is True: cached_sersol = cache.get(query) else: cached_sersol = None if cached_sersol: data = cached_sersol else: resp = sersol.get(query, key=self.sersol_key, timeout=10) try: data = resp.json() except sersol.Link360Exception, e: logger.error("%s -- %s" % (query, e)) return HttpResponseServerError(e) cache.set(query, data, 300) #Use the first bib only now. this_bib = data.get('records', [])[0] this_bib['_library'] = data.get('metadata', {}).get('library') orig_bib = from_openurl(query) citation = merge_bibjson(orig_bib, this_bib) #generate a new openurl based on merged bibjson objects openurl = to_openurl(citation)
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 = from_openurl(q) #pprint(bib) ourl = to_openurl(bib) bib2 = 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 = from_openurl(q) #Round trip the query ourl = to_openurl(bib) bib2 = 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_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 = from_openurl(q) #pprint(bib) ourl = to_openurl(bib) #print ourl bib2 = from_openurl(ourl) #pprint(bib2) self.assertEqual(bib['journal']['shortcode'], bib2['journal']['shortcode'])
def make_illiad_url( self, initial_querystring, permalink ): """ Manages steps of constructing illiad url for possible use in article-requesting. Called by FinditResolver.update_session() """ bib_dct = bibjsontools.from_openurl( initial_querystring ) ill_bib_dct = self.validator.add_required_kvs( bib_dct ) log.debug( 'validator call complete' ) extra_dct = self.check_identifiers( ill_bib_dct ) log.debug( 'check_identifiers call complete' ) extra_dct = self.check_validity( ill_bib_dct, extra_dct ) log.debug( 'check_validity call complete' ) extra_dct['Notes'] = self.update_note( extra_dct.get('Notes', ''), '`shortlink: <{}>`'.format(permalink) ) openurl = bibjsontools.to_openurl( ill_bib_dct ) for k,v in extra_dct.iteritems(): openurl += '&%s=%s' % ( urllib.quote_plus(k), urllib.quote_plus(v) ) illiad_url = app_settings.ILLIAD_URL_ROOT % openurl # ILLIAD_URL_ROOT is like `http...OpenURL?%s log.debug( 'illiad_url, ```%s```' % illiad_url ) return illiad_url
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 = from_dict(request_dict) ourl = to_openurl(b) parsed_ourl = parse_qs(ourl) self.assertTrue('bookitem' in parsed_ourl.get('rft.genre')) self.assertTrue('Coleman, Gabriella' in parsed_ourl.get('rft.au'))
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 = from_dict(request) ourl = to_openurl(b) parsed_ourl = 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_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 = from_dict(request) ourl = to_openurl(b) parsed_ourl = 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 = from_dict(request_dict) ourl = to_openurl(b) parsed_ourl = parse_qs(ourl) self.assertTrue('bookitem' in parsed_ourl.get('rft.genre')) self.assertTrue('Coleman, Gabriella' in parsed_ourl.get('rft.au'))
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 = from_openurl(q) ourl = to_openurl(b) qdict = parse_qs(ourl) self.assertTrue('bookitem' in qdict.get('rft.genre'))
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 = from_openurl(q) openurl = to_openurl(bib) bib2 = from_openurl(openurl)
def to_bibjson(pmid): d = BibJSON(pmid).parse() d['_openurl'] = to_openurl(d) return d