From 9b5a254f1dd3ec092513ecfefb4244cc6667d908 Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 23 Sep 2013 12:39:47 -0400 Subject: [PATCH] Adapt marc record machinery to B2U titles --- core/models.py | 11 ++- core/ungluify_record.py | 152 ++++++++++++++++++----------------- frontend/templates/marc.html | 4 +- frontend/templates/work.html | 4 +- frontend/views.py | 8 +- settings/common.py | 5 ++ 6 files changed, 100 insertions(+), 84 deletions(-) diff --git a/core/models.py b/core/models.py index 01153e79..1e99757e 100755 --- a/core/models.py +++ b/core/models.py @@ -1346,7 +1346,7 @@ class Libpref(models.Model): marc_link_target = models.CharField( max_length=6, default = 'UNGLUE', - choices = settings.MARC_CHOICES, + choices = settings.MARC_PREF_OPTIONS, verbose_name="MARC record link targets" ) @@ -1524,8 +1524,13 @@ class MARCRecord(models.Model): def _record(self, filetype): test = '' if '/unglue.it' in settings.BASE_URL else '_test' - via = '_via_unglueit.' if self.link_target!='DIRECT' else '_unglued.' - return 'marc' + test + '/' + self.accession + via + filetype + if self.link_target == 'DIRECT': + fn = '_unglued.' + elif self.link_target == 'UNGLUE': + fn = '_via_unglueit.' + else: + fn = '_ungluing.' + return 'marc' + test + '/' + self.accession + fn + filetype # this was causing a circular import problem and we do not seem to be using # anything from regluit.core.signals after this line diff --git a/core/ungluify_record.py b/core/ungluify_record.py index 2c144ad8..b177121f 100644 --- a/core/ungluify_record.py +++ b/core/ungluify_record.py @@ -18,15 +18,16 @@ from django.core.urlresolvers import reverse from regluit.core import models def makemarc(marcfile, edition): - """ - fyi if we're going to suck down LOC records directly: - parse_xml_to_array takes a file, so we need to faff about with file writes - would be nice to have a suitable z39.50 - can use LCCN to grab record with urllib, but file writes are inconsistent - """ - license = edition.ebooks.all()[0].rights logger = logging.getLogger(__name__) - logger.info("Making MARC records for edition %s and license %s" % (edition, license)) + + try: + license = edition.ebooks.all()[0].rights + ebf = None + except IndexError: + license = None + ebf = edition.ebook_files.all()[0] + + logger.info("Making MARC records for edition %s " % edition) record = pymarc.parse_xml_to_array(marcfile)[0] @@ -46,7 +47,10 @@ def makemarc(marcfile, edition): # create accession number and write 001 field # (control field syntax is special) - (marc_record, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='DIRECT') + if ebf: + (marc_record, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='B2U') + else: + (marc_record, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='UNGLUE') field001 = pymarc.Field(tag='001', data=marc_record.accession) record.add_ordered_field(field001) @@ -135,34 +139,35 @@ def makemarc(marcfile, edition): field300.delete_subfield('a') field300.add_subfield('a', new300a) field300.delete_subfield('c') - - # add 536 field (funding information) - if edition.unglued: - funding_info = 'The book is available as a free download thanks to the generous support of interested readers and organizations, who made donations using the crowd-funding website Unglue.it.' - else: - if edition.ebooks.all()[0].rights in ['CC BY', 'CC BY-NC-SA', 'CC BY-NC-ND', 'CC BY-NC', 'CC BY-ND', 'CC BY-SA']: - funding_info = 'The book is available as a free download thanks to a Creative Commons license.' - else: - funding_info = 'The book is available as a free download because it is in the Public Domain.' - field536 = pymarc.Field( - tag='536', - indicators = [' ', ' '], - subfields = [ - 'a', funding_info, - ] - ) - record.add_ordered_field(field536) - # add 540 field (terms governing use) - field540 = pymarc.Field( - tag='540', - indicators = [' ', ' '], - subfields = [ - 'a', dict(settings.CHOICES)[license], - 'u', dict(settings.GRANTS)[license], - ] - ) - record.add_ordered_field(field540) + if license: + # add 536 field (funding information) + if edition.unglued: + funding_info = 'The book is available as a free download thanks to the generous support of interested readers and organizations, who made donations using the crowd-funding website Unglue.it.' + else: + if edition.ebooks.all()[0].rights in ['CC BY', 'CC BY-NC-SA', 'CC BY-NC-ND', 'CC BY-NC', 'CC BY-ND', 'CC BY-SA']: + funding_info = 'The book is available as a free download thanks to a Creative Commons license.' + else: + funding_info = 'The book is available as a free download because it is in the Public Domain.' + field536 = pymarc.Field( + tag='536', + indicators = [' ', ' '], + subfields = [ + 'a', funding_info, + ] + ) + record.add_ordered_field(field536) + + # add 540 field (terms governing use) + field540 = pymarc.Field( + tag='540', + indicators = [' ', ' '], + subfields = [ + 'a', dict(settings.CHOICES)[license], + 'u', dict(settings.GRANTS)[license], + ] + ) + record.add_ordered_field(field540) # add 588 field (source of description) - credit where credit is due field588 = pymarc.Field( @@ -220,25 +225,25 @@ def makemarc(marcfile, edition): # add 856 fields with links for each available file # doing this out of order as it's the only thing that differs # between direct-link and via-unglue.it versions - # need deepcopy() because omg referential transparency! - record_via_unglueit = deepcopy(record) + if not ebf: + # need deepcopy() because omg referential transparency! + record_direct = deepcopy(record) # 2 records for unglued stuff - content_types = settings.CONTENT_TYPES - for format_tuple in settings.FORMATS: - format = format_tuple[0] - ebooks = edition.ebooks.filter(format=format) - if ebooks: - for book in ebooks: - field856 = pymarc.Field( - tag='856', - indicators = ['4', '0'], - subfields = [ - '3', format + ' version', - 'q', content_types[format], - 'u', book.url, - ] - ) - record.add_ordered_field(field856) + for format_tuple in settings.FORMATS: + format = format_tuple[0] + ebooks = edition.ebooks.filter(format=format) + if ebooks: + for book in ebooks: + field856 = pymarc.Field( + tag='856', + indicators = ['4', '0'], + subfields = [ + '3', format + ' version', + 'q', settings.CONTENT_TYPES[format], + 'u', book.url, + ] + ) + record_direct.add_ordered_field(field856) unglued_url = settings.BASE_URL_SECURE + reverse('download', args=[edition.work.id]) field856_via = pymarc.Field( @@ -248,34 +253,35 @@ def makemarc(marcfile, edition): 'u', unglued_url, ] ) - record_via_unglueit.add_ordered_field(field856_via) + record.add_ordered_field(field856_via) - # this via_unglueit record needs its own accession number - field001 = record_via_unglueit.get_fields('001')[0] - record_via_unglueit.remove_field(field001) - (marc_record_via, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='UNGLUE') - field001 = pymarc.Field(tag='001', data=marc_record_via.accession) - record_via_unglueit.add_ordered_field(field001) + if not ebf: + # this via_unglueit record needs its own accession number + field001 = record_direct.get_fields('001')[0] + record_direct.remove_field(field001) + (marc_record_direct, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='DIRECT') + field001 = pymarc.Field(tag='001', data=marc_record_direct.accession) + marc_record_direct.add_ordered_field(field001) + + # write the unglued MARCxml records + xmlrecord = pymarc.record_to_xml(record_direct) + xml_file = default_storage.open(marc_record_direct.xml_record, 'w') + xml_file.write(xmlrecord) + xml_file.close() + + # write the unglued .mrc records, then save to s3 + mrc_file = default_storage.open(marc_record_direct.mrc_record, 'w') + writer = pymarc.MARCWriter(mrc_file) + writer.write(record_direct) + mrc_file.close() - # write the unglued MARCxml records xmlrecord = pymarc.record_to_xml(record) xml_file = default_storage.open(marc_record.xml_record, 'w') xml_file.write(xmlrecord) xml_file.close() - - xmlrecord = pymarc.record_to_xml(record_via_unglueit) - xml_file = default_storage.open(marc_record_via.xml_record, 'w') - xml_file.write(xmlrecord) - xml_file.close() - # write the unglued .mrc records, then save to s3 mrc_file = default_storage.open(marc_record.mrc_record, 'w') writer = pymarc.MARCWriter(mrc_file) writer.write(record) mrc_file.close() - mrc_file = default_storage.open(marc_record_via.mrc_record, 'w') - writer = pymarc.MARCWriter(mrc_file) - writer.write(record_via_unglueit) - mrc_file.close() - diff --git a/frontend/templates/marc.html b/frontend/templates/marc.html index 747bacb6..a02ad3d3 100644 --- a/frontend/templates/marc.html +++ b/frontend/templates/marc.html @@ -97,8 +97,8 @@ ul.local li {

The options are:

You can change your preferences here.

diff --git a/frontend/templates/work.html b/frontend/templates/work.html index f53ba7e7..e2a40f90 100644 --- a/frontend/templates/work.html +++ b/frontend/templates/work.html @@ -405,9 +405,9 @@ {% for record in edition.MARCrecords.all %} Download {{record.link_target}} MARC record for this edition: (XML) (mrc)
{% endfor %} - {% if user.is_staff and edition.ebooks.count %} + {% if user.is_staff %}{% if edition.ebooks.count or edition.ebook_files.count %} Upload a MARC record for this edition.
- {% endif %} + {% endif %} {% endif %} diff --git a/frontend/views.py b/frontend/views.py index 18aae6cd..9058c463 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -2803,13 +2803,15 @@ def marc(request, userlist=None): except: if not request.user.is_anonymous(): libpref = models.Libpref(user=request.user) + unwanted = 'UNGLUE' if link_target == 'DIRECT' else 'DIRECT' if userlist: records = [] user = get_object_or_404(User,username=userlist) for work in user.wishlist.works.all(): records.extend(models.MARCRecord.objects.filter(edition__work=work,link_target=link_target)) + records.extend(models.MARCRecord.objects.filter(edition__work=work,link_target='B2U')) else: - records = models.MARCRecord.objects.filter(link_target=link_target) + records = models.MARCRecord.objects.exclude(link_target=unwanted) return render( request, 'marc.html', @@ -2827,7 +2829,7 @@ class MARCUngluifyView(FormView): edition = self.request.GET.get('edition',None) if models.Edition.objects.filter(id=edition).count(): edition = models.Edition.objects.filter(id=edition)[0] - if edition.ebooks.count(): + if edition.ebooks.count() or edition.ebook_files.count(): return {'edition':edition.id} return {} @@ -2910,12 +2912,10 @@ def marc_concatenate(request): record_url = models.MARCRecord.objects.get( pk=record_id ).xml_record - logger.info(record_url) elif format == 'mrc': record_url = models.MARCRecord.objects.get( pk=record_id ).mrc_record - logger.info(record_url) try: record_file = default_storage.open(record_url).read() outfile.write(record_file) diff --git a/settings/common.py b/settings/common.py index d66224e8..5c9a6fcc 100644 --- a/settings/common.py +++ b/settings/common.py @@ -407,6 +407,11 @@ CONTENT_TYPES = { MARC_CHOICES = ( ('DIRECT', 'Raw link'), ('UNGLUE', 'Unglue.it link'), + ('B2U', 'Library link'), +) +MARC_PREF_OPTIONS =( + ('DIRECT', 'Raw link'), + ('UNGLUE', 'Unglue.it link'), ) BOOXTREAM_API_KEY = '7ynRCsx4q21zEY67it7yk8u5rc6EXY'