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>')
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'),
(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'),
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())
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)
</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">
--- /dev/null
+<?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>
--- /dev/null
+<?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>