Adapt marc record machinery to B2U titles

pull/1/head
eric 2013-09-23 12:39:47 -04:00
parent be711e142d
commit 9b5a254f1d
6 changed files with 100 additions and 84 deletions

View File

@ -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

View File

@ -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()

View File

@ -97,8 +97,8 @@ ul.local li {
<p>The options are:</p>
<ul class="local">
<li> Raw link. MARC records link direct to provider (one click, no help) </li>
<li> Unglue.it link. MARC records link through Unglue.it download page (extra click, context sensitive help for getting files onto user's device)</li>
<li> Raw link if available. MARC records link direct to provider (one click, no help, not available for "Buy-to-unglue" titles) </li>
<li> Unglue.it link. MARC records link through Unglue.it download page (extra click, library user authentication, context sensitive help for getting files onto user's device)</li>
</ul>
<p>You can <a href="{% url marc_config %}">change your preferences here</a>.</p>

View File

@ -405,9 +405,9 @@
{% for record in edition.MARCrecords.all %}
Download {{record.link_target}} MARC record for this edition: (<a href="{% url marc_concatenate %}?record_{{ record.id }}=on&amp;format=xml">XML</a>) (<a href="{% url marc_concatenate %}?record_{{ record.id }}=on&amp;format=mrc">mrc</a>)<br />
{% endfor %}
{% if user.is_staff and edition.ebooks.count %}
{% if user.is_staff %}{% if edition.ebooks.count or edition.ebook_files.count %}
<a href="{% url MARCUngluify %}?edition={{ edition.id }}">Upload</a> a MARC record for this edition. <br />
{% endif %}
{% endif %} {% endif %}
</div>
</div>

View File

@ -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)

View File

@ -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'