From be7c13e9205777c134b7136e7c469f2657fb62b3 Mon Sep 17 00:00:00 2001 From: rlskoeser Date: Thu, 8 Aug 2013 21:10:22 -0400 Subject: [PATCH] basic COinS citation logic to allow harvesting items via Zotero #131 --- smartstash/core/models.py | 33 +++++++++++++++++- smartstash/core/templates/core/view.html | 4 +++ smartstash/core/tests.py | 44 +++++++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/smartstash/core/models.py b/smartstash/core/models.py index 94ada2f..e0e40e3 100644 --- a/smartstash/core/models.py +++ b/smartstash/core/models.py @@ -1,4 +1,4 @@ - +import urllib class DisplayItem(object): # common item to be used for display @@ -27,6 +27,37 @@ def __init__(self, **kwargs): def __repr__(self): return u'<%s %s>' % (self.title, self.thumbnail or '') + @property + def coins_citation_info(self): + # generate a dictionary with info to build COinS citation + info = { + 'rfr_id': 'info:sid/serendipomatic.org', # referrer id is recommended + 'rft_val_fmt': 'info:ofi/fmt:kev:mtx:dc', # think this basically means we are using Dublin Core metadata + 'rft.identifier': self.url + } + if self.title is not None: + info['rft.title'] = self.title + if self.date is not None: + info['rft.date'] = self.date + + # may not be exact mapping; for books, this is place of publication + if self.location is not None: + info['rft.place'] = self.location + if self.source is not None: + info['rft.source'] = self.source + if self.format is not None: + info['rft.format'] = self.format + + return info + + + @property + def coins_citation(self): + # COinS citation for this item to be embedded in the title attribute of a span + return 'ctx_ver=Z39.88-2004&' + \ + '&'.join(['%s=%s' % (k, urllib.quote(v)) + for k, v in self.coins_citation_info.iteritems()]) + # common result item ''' diff --git a/smartstash/core/templates/core/view.html b/smartstash/core/templates/core/view.html index f76402f..5f8782b 100644 --- a/smartstash/core/templates/core/view.html +++ b/smartstash/core/templates/core/view.html @@ -71,6 +71,10 @@ {% endif %} + {# span with citation information as COinS for zotero harvest #} + + + diff --git a/smartstash/core/tests.py b/smartstash/core/tests.py index 7a15af8..38026d8 100644 --- a/smartstash/core/tests.py +++ b/smartstash/core/tests.py @@ -6,9 +6,51 @@ """ from django.test import TestCase + from smartstash.core.forms import InputForm +from smartstash.core.models import DisplayItem class FormTest(TestCase): + def test_whitespace_validation(self): form = InputForm({'text': " "}) - self.assertFalse(form.is_valid()) \ No newline at end of file + self.assertFalse(form.is_valid()) + + +class DisplayItemTest(TestCase): + + def test_coins_citation_info(self): + # minimal record + item = DisplayItem(title='Hippo', url='http://some.url/to/a/hippo/pic') + + info = item.coins_citation_info + self.assert_('rfr_id' in info, 'referrer id should be set in COinS info') + self.assert_('rft_val_fmt' in info, 'format is specified in COinS info') + self.assertEqual(item.title, info['rft.title']) + self.assertEqual(item.url, info['rft.identifier']) + + for key in ['rft.date', 'rft.place', 'rft.source', 'rft.format']: + self.assert_(key not in info, + 'unavailable data should not be set in COinS info') + + # add all fields to simulate a complete record + item.date = '1887' + item.format = 'Image' + item.source = 'Smithsonian' + item.location = 'USA' + + info = item.coins_citation_info + self.assertEqual(item.date, info['rft.date']) + self.assertEqual(item.format, info['rft.format']) + self.assertEqual(item.source, info['rft.source']) + self.assertEqual(item.location, info['rft.place']) + + def test_coins_citation(self): + # minimal record + item = DisplayItem(title='Hippo', url='http://some.url/to/a/hippo/pic') + + cit = item.coins_citation + # just some basic sanity checks + self.assert_(cit.startswith('ctx_ver=Z39.88-2004')) + self.assert_('rft.title=%s' % item.title in cit) +