From 8a369952d4b5e3f4bc0e0c4dc586c4e4b8f314fd Mon Sep 17 00:00:00 2001 From: gfawcett Date: Sat, 4 Apr 2009 17:23:57 +0000 Subject: [PATCH] added lib_integration.cat_search as proper hook for z3950. Evergreen URLs are valid search queries. It's a hack, but if you paste an Evergreen "title details" URL into the catalogue search interface, it will fetch the correct MARCXML record and display the item. Currently hard-coded to the Conifer catalogue, will fix that. git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@265 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/TODO | 2 ++ conifer/custom/lib_integration.py | 13 +++++++++++++ conifer/libsystems/evergreen/item_status.py | 18 ++++++++++++++++-- conifer/libsystems/z3950/marcxml.py | 3 +++ conifer/syrup/views.py | 18 +++++++++++------- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/conifer/TODO b/conifer/TODO index 4028fcd..eeba77e 100644 --- a/conifer/TODO +++ b/conifer/TODO @@ -15,6 +15,8 @@ IMPORTANT: * testing on broken browsers (you know who you are) +* the code is littered with 'dwarf' refrences. Factor out into config. + MAYBE: * Generating barcodes in emails, printable screens? (3 of 9 enough?) diff --git a/conifer/custom/lib_integration.py b/conifer/custom/lib_integration.py index 2aa0732..c976183 100644 --- a/conifer/custom/lib_integration.py +++ b/conifer/custom/lib_integration.py @@ -37,6 +37,8 @@ from django.conf import settings from conifer.libsystems.evergreen import item_status as I from conifer.libsystems.sip.sipclient import SIP +from conifer.libsystems.z3950 import yaz_search +from conifer.libsystems.z3950.marcxml import marcxml_to_dictionary @SIP @@ -67,3 +69,14 @@ def barcode_to_copy(barcode): @caching('bimx', timeout=3600) def bib_id_to_marcxml(bib_id): return I.bib_id_to_marcxml(bib_id) + + +def cat_search(query, start=1, limit=10): + # this is a total hack for conifer. If the query is a Conifer + # title-detail URL, then return just that one item. + if query.startswith('http://dwarf'): + results = [marcxml_to_dictionary(I.url_to_marcxml(query))] + else: + cat_host, cat_db = ('dwarf.cs.uoguelph.ca:2210', 'conifer') + results = yaz_search.search(cat_host, cat_db, query, start, limit) + return results diff --git a/conifer/libsystems/evergreen/item_status.py b/conifer/libsystems/evergreen/item_status.py index 026607f..9f471a1 100644 --- a/conifer/libsystems/evergreen/item_status.py +++ b/conifer/libsystems/evergreen/item_status.py @@ -1,4 +1,6 @@ from support import ER, E1 +import re +import urllib2 def barcode_to_bib_id(barcode): bib_id = (E1('open-ils.search.bib_id.by_barcode', barcode)) @@ -10,6 +12,18 @@ def barcode_to_bib_id(barcode): def bib_id_to_marcxml(bib_id): return E1('open-ils.supercat.record.marcxml.retrieve', bib_id) +def url_to_marcxml(url): + # this is a hack. Given a opac Title Details url, return marcxml. + if url.startswith('http://dwarf.cs.uoguelph.ca'): + m = re.match(r'.*r=(\d+).*', url) + item_id = m and m.group(1) or None + if item_id: + marc_url = ("http://dwarf.cs.uoguelph.ca/opac/extras" + "/supercat/retrieve/marcxml/record/" + item_id) + xml = urllib2.urlopen(marc_url).read() + return xml + if __name__ == '__main__': - from pprint import pprint - print bib_id_to_marcxml(barcode_to_bib_id(31862016799294)) +# from pprint import pprint +# print bib_id_to_marcxml(barcode_to_bib_id(31862016799294)) + print url_to_marcxml('http://dwarf.cs.uoguelph.ca/opac/en-US/skin/default/xml/rdetail.xml?r=1082665&t=dylan%20thomas%20ralph&tp=keyword&d=0&hc=14&rt=keyword') diff --git a/conifer/libsystems/z3950/marcxml.py b/conifer/libsystems/z3950/marcxml.py index 6f54d74..d1f2dff 100644 --- a/conifer/libsystems/z3950/marcxml.py +++ b/conifer/libsystems/z3950/marcxml.py @@ -5,6 +5,9 @@ loc_to_unicode = marctools.locToUTF8().replace def marcxml_to_dictionary(rec): tree = ElementTree.fromstring(rec) + if tree.tag == '{http://www.loc.gov/MARC21/slim}collection': + # thenwe only look at the first record. + tree = tree.find('{http://www.loc.gov/MARC21/slim}record') dct = {} for df in tree.findall('{http://www.loc.gov/MARC21/slim}datafield'): t = df.attrib['tag'] diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index 3e17a5e..0e8c247 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -729,16 +729,18 @@ def item_add(request, course_id, item_id): def item_add_cat_search(request, course_id, item_id): if request.method != 'POST': return g.render('item_add_cat_search.xhtml', results=[], query='@and dylan thomas') - query = request.POST.get('query','').strip() + + # POST handler + query = request.POST.get('query','').strip() _pickitem = request.POST.get('pickitem', '').strip() if not _pickitem: + # process the query. assert query, 'must provide a query.' - from conifer.libsystems.z3950 import yaz_search - host, db, query = ('dwarf.cs.uoguelph.ca:2210', 'conifer', query) - #host, db = ('z3950.loc.gov:7090', 'VOYAGER') - results = yaz_search.search(host, db, query) - return g.render('item_add_cat_search.xhtml', results=results, query=query) + results = lib_integration.cat_search(query) + return g.render('item_add_cat_search.xhtml', + results=results, query=query) else: + # User has selected an item; add it to course site. #fixme, this block copied from item_add. refactor. parent_item_id = item_id if parent_item_id=='0': @@ -767,11 +769,13 @@ def item_add_cat_search(request, course_id, item_id): item.save() return HttpResponseRedirect('../../../%d/' % item.id) +#------------------------------------------------------------ + +#this is used in item_edit. metadata_formset_class = modelformset_factory(models.Metadata, fields=['name','value'], extra=3, can_delete=True) - @instructors_only def item_edit(request, course_id, item_id): course = get_object_or_404(models.Course, pk=course_id) -- 2.11.0