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( marc_link_target = models.CharField(
max_length=6, max_length=6,
default = 'UNGLUE', default = 'UNGLUE',
choices = settings.MARC_CHOICES, choices = settings.MARC_PREF_OPTIONS,
verbose_name="MARC record link targets" verbose_name="MARC record link targets"
) )
@ -1524,8 +1524,13 @@ class MARCRecord(models.Model):
def _record(self, filetype): def _record(self, filetype):
test = '' if '/unglue.it' in settings.BASE_URL else '_test' test = '' if '/unglue.it' in settings.BASE_URL else '_test'
via = '_via_unglueit.' if self.link_target!='DIRECT' else '_unglued.' if self.link_target == 'DIRECT':
return 'marc' + test + '/' + self.accession + via + filetype 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 # this was causing a circular import problem and we do not seem to be using
# anything from regluit.core.signals after this line # 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 from regluit.core import models
def makemarc(marcfile, edition): 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 = 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] record = pymarc.parse_xml_to_array(marcfile)[0]
@ -46,7 +47,10 @@ def makemarc(marcfile, edition):
# create accession number and write 001 field # create accession number and write 001 field
# (control field syntax is special) # (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) field001 = pymarc.Field(tag='001', data=marc_record.accession)
record.add_ordered_field(field001) record.add_ordered_field(field001)
@ -136,6 +140,7 @@ def makemarc(marcfile, edition):
field300.add_subfield('a', new300a) field300.add_subfield('a', new300a)
field300.delete_subfield('c') field300.delete_subfield('c')
if license:
# add 536 field (funding information) # add 536 field (funding information)
if edition.unglued: 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.' 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.'
@ -220,10 +225,10 @@ def makemarc(marcfile, edition):
# add 856 fields with links for each available file # add 856 fields with links for each available file
# doing this out of order as it's the only thing that differs # doing this out of order as it's the only thing that differs
# between direct-link and via-unglue.it versions # between direct-link and via-unglue.it versions
if not ebf:
# need deepcopy() because omg referential transparency! # need deepcopy() because omg referential transparency!
record_via_unglueit = deepcopy(record) record_direct = deepcopy(record) # 2 records for unglued stuff
content_types = settings.CONTENT_TYPES
for format_tuple in settings.FORMATS: for format_tuple in settings.FORMATS:
format = format_tuple[0] format = format_tuple[0]
ebooks = edition.ebooks.filter(format=format) ebooks = edition.ebooks.filter(format=format)
@ -234,11 +239,11 @@ def makemarc(marcfile, edition):
indicators = ['4', '0'], indicators = ['4', '0'],
subfields = [ subfields = [
'3', format + ' version', '3', format + ' version',
'q', content_types[format], 'q', settings.CONTENT_TYPES[format],
'u', book.url, 'u', book.url,
] ]
) )
record.add_ordered_field(field856) record_direct.add_ordered_field(field856)
unglued_url = settings.BASE_URL_SECURE + reverse('download', args=[edition.work.id]) unglued_url = settings.BASE_URL_SECURE + reverse('download', args=[edition.work.id])
field856_via = pymarc.Field( field856_via = pymarc.Field(
@ -248,34 +253,35 @@ def makemarc(marcfile, edition):
'u', unglued_url, 'u', unglued_url,
] ]
) )
record_via_unglueit.add_ordered_field(field856_via) record.add_ordered_field(field856_via)
if not ebf:
# this via_unglueit record needs its own accession number # this via_unglueit record needs its own accession number
field001 = record_via_unglueit.get_fields('001')[0] field001 = record_direct.get_fields('001')[0]
record_via_unglueit.remove_field(field001) record_direct.remove_field(field001)
(marc_record_via, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='UNGLUE') (marc_record_direct, created) = models.MARCRecord.objects.get_or_create(edition=edition,link_target='DIRECT')
field001 = pymarc.Field(tag='001', data=marc_record_via.accession) field001 = pymarc.Field(tag='001', data=marc_record_direct.accession)
record_via_unglueit.add_ordered_field(field001) marc_record_direct.add_ordered_field(field001)
# write the unglued MARCxml records # 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()
xmlrecord = pymarc.record_to_xml(record) xmlrecord = pymarc.record_to_xml(record)
xml_file = default_storage.open(marc_record.xml_record, 'w') xml_file = default_storage.open(marc_record.xml_record, 'w')
xml_file.write(xmlrecord) xml_file.write(xmlrecord)
xml_file.close() 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') mrc_file = default_storage.open(marc_record.mrc_record, 'w')
writer = pymarc.MARCWriter(mrc_file) writer = pymarc.MARCWriter(mrc_file)
writer.write(record) writer.write(record)
mrc_file.close() 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> <p>The options are:</p>
<ul class="local"> <ul class="local">
<li> Raw link. MARC records link direct to provider (one click, no help) </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, context sensitive help for getting files onto user's device)</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> </ul>
<p>You can <a href="{% url marc_config %}">change your preferences here</a>.</p> <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 %} {% 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 /> 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 %} {% 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 /> <a href="{% url MARCUngluify %}?edition={{ edition.id }}">Upload</a> a MARC record for this edition. <br />
{% endif %} {% endif %} {% endif %}
</div> </div>
</div> </div>

View File

@ -2803,13 +2803,15 @@ def marc(request, userlist=None):
except: except:
if not request.user.is_anonymous(): if not request.user.is_anonymous():
libpref = models.Libpref(user=request.user) libpref = models.Libpref(user=request.user)
unwanted = 'UNGLUE' if link_target == 'DIRECT' else 'DIRECT'
if userlist: if userlist:
records = [] records = []
user = get_object_or_404(User,username=userlist) user = get_object_or_404(User,username=userlist)
for work in user.wishlist.works.all(): 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=link_target))
records.extend(models.MARCRecord.objects.filter(edition__work=work,link_target='B2U'))
else: else:
records = models.MARCRecord.objects.filter(link_target=link_target) records = models.MARCRecord.objects.exclude(link_target=unwanted)
return render( return render(
request, request,
'marc.html', 'marc.html',
@ -2827,7 +2829,7 @@ class MARCUngluifyView(FormView):
edition = self.request.GET.get('edition',None) edition = self.request.GET.get('edition',None)
if models.Edition.objects.filter(id=edition).count(): if models.Edition.objects.filter(id=edition).count():
edition = models.Edition.objects.filter(id=edition)[0] 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 {'edition':edition.id}
return {} return {}
@ -2910,12 +2912,10 @@ def marc_concatenate(request):
record_url = models.MARCRecord.objects.get( record_url = models.MARCRecord.objects.get(
pk=record_id pk=record_id
).xml_record ).xml_record
logger.info(record_url)
elif format == 'mrc': elif format == 'mrc':
record_url = models.MARCRecord.objects.get( record_url = models.MARCRecord.objects.get(
pk=record_id pk=record_id
).mrc_record ).mrc_record
logger.info(record_url)
try: try:
record_file = default_storage.open(record_url).read() record_file = default_storage.open(record_url).read()
outfile.write(record_file) outfile.write(record_file)

View File

@ -407,6 +407,11 @@ CONTENT_TYPES = {
MARC_CHOICES = ( MARC_CHOICES = (
('DIRECT', 'Raw link'), ('DIRECT', 'Raw link'),
('UNGLUE', 'Unglue.it link'), ('UNGLUE', 'Unglue.it link'),
('B2U', 'Library link'),
)
MARC_PREF_OPTIONS =(
('DIRECT', 'Raw link'),
('UNGLUE', 'Unglue.it link'),
) )
BOOXTREAM_API_KEY = '7ynRCsx4q21zEY67it7yk8u5rc6EXY' BOOXTREAM_API_KEY = '7ynRCsx4q21zEY67it7yk8u5rc6EXY'