hackety hack. hackish add-physical-item interface (catalogue search).
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 24 Mar 2009 02:25:29 +0000 (02:25 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Tue, 24 Mar 2009 02:25:29 +0000 (02:25 +0000)
It's primitive. Adding a physical item entails doing a catalogue
search, and picking the desired item from the results. This pushes
parts of the MARC record into Syrup as an indicator of the wanted
item. Resolving bib ID or barcode is out of scope, most likely done in
wetware.

git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@212 6d9bc8c9-1ec2-4278-b937-99fde70a366f

conifer/libsystems/z3950/yaz_search.py
conifer/syrup/urls.py
conifer/syrup/views.py
conifer/templates/components/course.xhtml
conifer/templates/item_add_cat_search.xhtml [new file with mode: 0644]
conifer/templates/item_add_phys.xhtml [new file with mode: 0644]

index 724d235..0b85621 100644 (file)
@@ -9,17 +9,20 @@ import re
 from xml.etree import ElementTree
 import pexpect
 import marctools
+import sys
+
 loc_to_unicode = marctools.locToUTF8().replace
 
 LOG = None              #  for pexpect debugging, try LOG = sys.stderr
 YAZ_CLIENT = 'yaz-client'
-GENERAL_TIMEOUT = 3
+GENERAL_TIMEOUT = 10
 PRESENT_TIMEOUT = 30
 
 
 def search(host, database, query, start=1, limit=None):
 
     server = pexpect.spawn('yaz-client', timeout=GENERAL_TIMEOUT, logfile=LOG)
+    #server.expect('Z>')
     for line in ('open %s' % host, 'base %s' % database, 'format xml'):
         server.sendline(line)
         server.expect('Z>')
index 6499a85..396ca8b 100644 (file)
@@ -9,7 +9,7 @@ GENERIC_REGEX = r'((?P<obj_id>\d+)/)?(?P<action>.+)?$'
 urlpatterns = patterns('conifer.syrup.views',
     (r'^$', 'welcome'),                       
     (r'^course/$', 'my_courses'),
-    (r'^course/new/$', 'add_new_course'),
+    (r'^course/new/$', 'add/$ew_course'),
     (r'^course/new/ajax_title$', 'add_new_course_ajax_title'),
     (r'^course/invitation/$', 'course_invitation'),
     (r'^browse/$', 'browse'),
@@ -38,6 +38,7 @@ urlpatterns = patterns('conifer.syrup.views',
     (ITEM_PREFIX + r'meta$', 'item_metadata'),
     (ITEM_PREFIX + r'edit/$', 'item_edit'),
     (ITEM_PREFIX + r'add/$', 'item_add'), # for adding sub-things
+    (ITEM_PREFIX + r'add/cat_search/$', 'item_add_cat_search'),
     (r'^admin/$', 'admin_index'),
     (r'^admin/terms/' + GENERIC_REGEX, 'admin_terms'),
     (r'^admin/depts/' + GENERIC_REGEX, 'admin_depts'),
index 578ba63..01c3575 100644 (file)
@@ -613,9 +613,13 @@ def item_add(request, course_id, item_id):
     assert item_type, _('No item_type parameter was provided.')
 
     # for the moment, only HEADINGs, URLs and ELECs can be added. fixme.
-    assert item_type in ('HEADING', 'URL', 'ELEC'), \
+    assert item_type in ('HEADING', 'URL', 'ELEC', 'PHYS'), \
         _('Sorry, only HEADINGs, URLs and ELECs can be added right now.')
 
+    if request.method != 'POST' and item_type == 'PHYS':
+        # special handling: send to catalogue search
+        return HttpResponseRedirect('cat_search/')
+
     if request.method != 'POST':
         item = models.Item()    # dummy object
         metadata_formset = metadata_formset_class(queryset=item.metadata_set.all())
@@ -688,6 +692,47 @@ def item_add(request, course_id, item_id):
         else:
             return HttpResponseRedirect(course.course_url())
 
+@instructors_only
+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()
+    pickitem = request.POST.get('pickitem', '').strip()
+    if not pickitem:
+        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)
+    else:
+        #fixme, this block copied from item_add. refactor.
+        parent_item_id = item_id
+        if parent_item_id=='0':
+            parent_item = None
+            course = get_object_or_404(models.Course, pk=course_id)
+        else:
+            parent_item = get_object_or_404(models.Item, pk=parent_item_id, course__id=course_id)
+            assert parent_item.item_type == 'HEADING', _('You can only add items to headings!')
+            course = parent_item.course
+        if not course.can_edit(request.user):
+            return _access_denied(_('You are not an editor.'))
+
+        pickitem = eval(pickitem) # fixme, dangerous. cache result server-side instead, or encrypt it.
+        item = course.item_set.create(parent_heading=parent_item,
+                                      title=pickitem.get('245a', 'Untitled'),
+                                      item_type='PHYS')
+        item.save()
+        # these are a temporary hack, must replace
+        meta = [('245a', 'dc:title'), ('100a', 'dc:creator'), ('260b', 'dc:publisher'),
+                ('dc:260c', 'dc:date'), ('700a', 'dc:contributor')]
+        for marc, dc in meta:
+            value = pickitem.get(marc)
+            if value:
+                md = item.metadata_set.create(item=item, name=dc, value=value)
+        item.save()
+        return HttpResponseRedirect('../../../%d/' % item.id)
+
 metadata_formset_class = modelformset_factory(models.Metadata, 
                                               fields=['name','value'], 
                                               extra=3, can_delete=True)
index 7e5ed18..3aefa4a 100644 (file)
@@ -74,8 +74,8 @@ searchtext = _('search this course...')
     </script>
   </div>
 
-  <div py:def="item_metadata_formset()">
-      <p><a href="javascript:show_metadata();" id="show_metadata">Show more attributes</a></p>
+  <div py:def="item_metadata_formset(show_more=True)">
+      <p py:if="show_more"><a href="javascript:show_metadata();" id="show_metadata">Show more attributes</a></p>
       <div  id="metadatatable">
       ${Markup(metadata_formset.management_form)}
       <table class="pagetable">
diff --git a/conifer/templates/item_add_cat_search.xhtml b/conifer/templates/item_add_cat_search.xhtml
new file mode 100644 (file)
index 0000000..117d7ef
--- /dev/null
@@ -0,0 +1,48 @@
+<?python
+title = _('Add physical item: Catalogue search')
+# I just made up these keys...
+keys = [('245a', 'Title'), ('100a', 'Author'), ('260b', 'Publisher'), ('260c', 'PubDate'), ('700a', 'Editor')]
+?>
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:xi="http://www.w3.org/2001/XInclude"
+      xmlns:py="http://genshi.edgewall.org/">
+<xi:include href="master.xhtml"/>
+<xi:include href="paginate.xhtml"/>
+<head>
+  <title>${title}</title>
+  <style>
+    .lesser { font-size: smaller; color: gray; display: none; }
+  </style>
+  <script type="text/javascript">
+    <!-- !This ought to be in paginate.xhtml, not here. how to do? -->
+    $(function() { $('.pagetable').tablesorter(); });
+  </script>
+</head>
+<body>
+    <h1>${title}</h1>
+    <form method="POST" action=".">
+      <input type="text" name="query" value="${query}" style="font-size: larger;"/>
+      <input type="submit" value="Search"/>
+    </form>
+    <p><a href="javascript:$('.lesser').show(); void(0);">show more detail</a></p>
+    <table py:for="res in results" style="margin: 1em 0; border: black 1px solid;">
+      <tbody>
+      <tr py:for="k, title in keys" py:if="k in res">
+       <th>${title}</th><td>${res[k]}</td>
+      </tr>
+      <tr>
+       <th/><td>
+       <form action="." method="POST">
+         <input type="hidden" name="pickitem" value="${repr(res)}"/>
+         <input type="submit" value="Pick this item"/>
+       </form>
+      </td>
+      </tr>
+      <?python allkeys = res.keys(); allkeys.sort(); ?>
+      <tr py:for="k in allkeys" class="lesser">
+       <th>${k}</th><td>${res[k]}</td>
+      </tr>
+      </tbody>
+    </table>
+</body>
+</html>
diff --git a/conifer/templates/item_add_phys.xhtml b/conifer/templates/item_add_phys.xhtml
new file mode 100644 (file)
index 0000000..460c495
--- /dev/null
@@ -0,0 +1,31 @@
+<?python
+is_edit = bool(item.id)
+title = is_edit and _('Edit a physical-item request') or _('Add a physical-item request')
+course_title = '%s: %s (%s)' % (course.code, course.title, course.term)
+?>
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:xi="http://www.w3.org/2001/XInclude"
+      xmlns:py="http://genshi.edgewall.org/">
+  <xi:include href="master.xhtml"/>
+  <xi:include href="components/course.xhtml"/>
+  <head>
+    <title>${title}</title>
+    <script type="text/javascript">
+      $(function() {$('input[@name="title"]').focus();});
+    </script>
+  </head>
+  <body>
+    ${course_banner(course)}
+    ${nested_title(parent_item)}
+    <h3>${title}</h3>
+
+    <form action=".?item_type=${item_type}" method="POST">
+      ${item_metadata_formset(show_more=False)}
+      <p>
+       <input py:if="not is_edit" type="submit" value="Add heading"/>
+       <input py:if="is_edit" type="submit" value="Update heading"/>
+      </p>
+
+    </form>
+</body>
+</html>