From: artunit Date: Tue, 17 Mar 2009 03:31:19 +0000 (+0000) Subject: adding some z39.50 searching support X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=83428f07ed70724fed6e4e39d30a4565b75109cd;p=Syrup.git adding some z39.50 searching support git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@191 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- diff --git a/conifer/syrup/admin.py b/conifer/syrup/admin.py index 2ccaf53..449c21b 100644 --- a/conifer/syrup/admin.py +++ b/conifer/syrup/admin.py @@ -23,5 +23,5 @@ from conifer.syrup.models import * # admin.site.register(value) for m in [LibraryUnit, ServiceDesk, Member, Department, Course, Term, UserProfile, NewsItem, - Item]: + Item, Target]: admin.site.register(m) diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index f8fc6f0..1a63b62 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -391,6 +391,11 @@ class Item(m.Model): # Physical Item properties + ''' + want to add enumeration and chronology info + ''' + # enumeration = m.CharField(max_length=255, blank=True, null=True) + # chronology = m.CharField(max_length=255, blank=True, null=True) call_number = m.CharField(max_length=30, blank=True, null=True) # long enough? barcode = m.CharField(max_length=30, blank=True, null=True) # long enough? @@ -506,3 +511,17 @@ class NewsItem(m.Model): return Markup(self.body) elif self.encoding == 'markdown': return Markup(do_markdown(self.body)) + +#---------------------------------------------------------------------- +# Z39.50 Support + +class Target(m.Model): + name = m.CharField(max_length=100) + host = m.CharField(max_length=50) + db = m.CharField(max_length=50) + port = m.IntegerField(default=210) + syntax = m.CharField(max_length=10,default='USMARC') + active = m.BooleanField(default=True) + + def __unicode__(self): + return self.name diff --git a/conifer/syrup/urls.py b/conifer/syrup/urls.py index 8c4a1c8..c3ff691 100644 --- a/conifer/syrup/urls.py +++ b/conifer/syrup/urls.py @@ -18,6 +18,7 @@ urlpatterns = patterns('conifer.syrup.views', (r'^z3950test/$', 'z3950_test'), (r'^opencourse/$', 'open_courses'), (r'^search/$', 'search'), + (r'^zsearch/$', 'zsearch'), (r'^instructors/$', 'instructors'), (r'^departments/$', 'departments'), (r'^course/(?P\d+)/$', 'course_detail'), @@ -39,6 +40,7 @@ urlpatterns = patterns('conifer.syrup.views', (r'^admin/items/' + GENERIC_REGEX, 'admin_items'), (r'^admin/depts/' + GENERIC_REGEX, 'admin_depts'), (r'^admin/news/' + GENERIC_REGEX, 'admin_news'), + (r'^admin/targets/' + GENERIC_REGEX, 'admin_targets'), # (r'^admin/terms/(?P\d+)/$', 'admin_term_edit'), # (r'^admin/terms/(?P\d+)/delete$', 'admin_term_delete'), # (r'^admin/terms/$', 'admin_term'), diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index a66efed..e392d9c 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -60,7 +60,7 @@ try: # for Z39.50 support, not sure whether this is the way to go yet but # as generic as it gets - from PyZ3950 import zoom + from PyZ3950 import zoom, zmarc except: warnings.warn('Could not load Z39.50 support.') @@ -210,10 +210,11 @@ def user_prefs(request): def z3950_test(request): conn = zoom.Connection ('z3950.loc.gov', 7090) + #conn = zoom.Connection ('webvoy.uwindsor.ca', 9000) conn.databaseName = 'VOYAGER' conn.preferredRecordSyntax = 'USMARC' query = zoom.Query ('CCL', 'ti="1066 and all that"') - # print("connecting...") + print("connecting...") res = conn.search (query) collector = [] for r in res: @@ -804,6 +805,64 @@ def search(request, in_course=None): return g.render('search_results.xhtml', **locals()) +#----------------------------------------------------------------------------- +# Z39.50 support + +def zsearch(request): + ''' + ''' + if request.method == 'GET': + targets_list = models.Target.objects.filter(active=True).order_by('name') + targets_len = len(targets_list) + return g.render('zsearch.xhtml', **locals()) + else: + start = int(request.POST.get('start', 1)) + count = int(request.POST.get('count', 5)) + search_target= models.Target.objects.get(name=request.POST['target']) + conn = zoom.Connection (search_target.host, search_target.port) + conn.databaseName = search_target.db + conn.preferredRecordSyntax = search_target.syntax + # query = zoom.Query ('CCL', '%s="%s"' % ('ti','1066 and all that')) + query = zoom.Query ('CCL', '%s="%s"' % ('ti',request.POST['ztitle'])) + # print("connecting...") + res = conn.search (query) + collector = [] + + for r in res[start: start + count]: + if r.syntax <> 'USMARC': + collector.append ((None, 'Unsupported syntax: ' + r.syntax, None)) + else: + raw = r.data + + # Convert to MARC + marcdata = zmarc.MARC(raw) + # print marcdata + + # Convert to MARCXML + marcxml = marcdata.toMARCXML() + print marcxml + + # How to Remove non-ascii characters (in case this is a problem) + marcxmlascii = unicode(marcxml, 'ascii', 'ignore').encode('ascii') + + bibid = marcdata.fields[1][0] + title = " ".join ([v[1] for v in marcdata.fields [245][0][2]]) + + # And then Amara XML tools would allow using xpath + ''' + title = "" + doc = binderytools.bind_string(marcxml) + t = doc.xml_xpath("//datafield[@tag='245']/subfield[@code='a']") + if len(title)>0: + title = t[0].xml_text_content() + ''' + + collector.append ((bibid, title)) + conn.close () + # print("done searching...") + + return g.render('zsearch_results.xhtml', **locals()) + #----------------------------------------------------------------------------- # Administrative options @@ -883,6 +942,21 @@ class ItemForm(ModelForm): clean_author = strip_and_nonblank('author') admin_items = generic_handler(ItemForm, decorator=admin_only) + +class TargetForm(ModelForm): + class Meta: + model = models.Target + + class Index: + title = _('Targets') + all = models.Target.objects.order_by('name').all + cols = ['name', 'host'] + links = [0,1] + + clean_name = strip_and_nonblank('name') + clean_host = strip_and_nonblank('host') + +admin_targets = generic_handler(TargetForm, decorator=admin_only) ### diff --git a/conifer/templates/admin/index.xhtml b/conifer/templates/admin/index.xhtml index cf14e53..4e6f178 100644 --- a/conifer/templates/admin/index.xhtml +++ b/conifer/templates/admin/index.xhtml @@ -16,6 +16,7 @@ title = _('Administrative Options')
  • Courses
  • Items
  • News Items
  • +
  • Z39.50 Targets
  • diff --git a/conifer/templates/zsearch.xhtml b/conifer/templates/zsearch.xhtml new file mode 100644 index 0000000..370ea81 --- /dev/null +++ b/conifer/templates/zsearch.xhtml @@ -0,0 +1,23 @@ + + + + + ${title} + + +

    ${title}

    +
    + + Title: +

    +
    +
    + + diff --git a/conifer/templates/zsearch_results.xhtml b/conifer/templates/zsearch_results.xhtml new file mode 100644 index 0000000..05aac02 --- /dev/null +++ b/conifer/templates/zsearch_results.xhtml @@ -0,0 +1,28 @@ + + + + + + ${title} + + + +

    ${title}

    + + + + + + +
    ${item[0]}${item[1]}
    + + +