Merge branch 'stub-marc' into marc_package

pull/1/head
eric 2014-10-17 17:12:36 -04:00
commit 7bfd807a74
7 changed files with 157 additions and 53 deletions

View File

@ -5,8 +5,8 @@ Consider it a catalogolem: http://commons.wikimedia.org/wiki/File:Arcimboldo_Lib
Use the MARCXML file for the non-unglued edition from Library of Congress.
"""
import logging
import pymarc
import logging
from copy import deepcopy
from datetime import datetime
from StringIO import StringIO
@ -18,6 +18,10 @@ from django.core.urlresolvers import reverse
import regluit.core.cc as cc
from regluit.core import models
def makestub(edition):
return makemarc(None, edition)
def makemarc(marcfile, edition):
logger = logging.getLogger(__name__)
@ -26,28 +30,34 @@ def makemarc(marcfile, edition):
ebf = None
except IndexError:
license = None
ebf = edition.ebook_files.all()[0]
try:
ebf = edition.ebook_files.all()[0]
except IndexError:
# no record if no ebooks
return None
logger.info("Making MARC records for edition %s " % edition)
record = pymarc.parse_xml_to_array(marcfile)[0]
# save lccn for later (if there is one) before deleting it
print_lccn = None
for lccn in record.get_fields('010'):
for validlccn in lccn.get_subfields('a'):
print_lccn = validlccn
if marcfile:
record = pymarc.parse_xml_to_array(marcfile)[0]
for lccn in record.get_fields('010'):
for validlccn in lccn.get_subfields('a'):
print_lccn = validlccn
fields_to_delete = []
fields_to_delete += record.get_fields('001')
fields_to_delete += record.get_fields('003')
fields_to_delete += record.get_fields('005')
fields_to_delete += record.get_fields('006')
fields_to_delete += record.get_fields('007')
fields_to_delete += record.get_fields('010')
fields_to_delete += record.get_fields('040')
for field in fields_to_delete:
record.remove_field(field)
else:
record = pymarc.Record()
fields_to_delete = []
fields_to_delete += record.get_fields('001')
fields_to_delete += record.get_fields('003')
fields_to_delete += record.get_fields('005')
fields_to_delete += record.get_fields('006')
fields_to_delete += record.get_fields('007')
fields_to_delete += record.get_fields('010')
fields_to_delete += record.get_fields('040')
for field in fields_to_delete:
record.remove_field(field)
# create accession number and write 001 field
# (control field syntax is special)
@ -81,10 +91,19 @@ def makemarc(marcfile, edition):
)
record.add_ordered_field(field007)
field008 = record.get_fields('008')[0]
record.remove_field(field008)
old_field_value = field008.value()
new_field_value = old_field_value[:23] + 'o' + old_field_value[24:]
try:
field008 = record.get_fields('008')[0]
record.remove_field(field008)
old_field_value = field008.value()
new_field_value = old_field_value[:23] + 'o' + old_field_value[24:]
except IndexError:
# fun fun fun
new_field_value= now.strftime('%y%m%d')+'s'
if len(edition.publication_date)>3:
new_field_value += edition.publication_date[0:4]
else:
new_field_value += '||||'
new_field_value += '||||xx |||||o|||||||||||eng||'
field008 = pymarc.Field(tag='008', data=new_field_value)
record.add_ordered_field(field008)
@ -124,25 +143,77 @@ def makemarc(marcfile, edition):
except:
pass # if no 082 field, don't need to change indicator
# author name
try:
field100 = record.get_fields('100')[0]
except IndexError:
num_auths = edition.authors.count()
if num_auths:
field100 = pymarc.Field(
tag='100',
indicators = ['1', ' '],
subfields = [
'a', edition.authors.all()[0].last_name_first,
]
)
record.add_ordered_field(field100)
if num_auths > 1:
for auth in edition.authors.all()[1:]:
field = pymarc.Field(
tag='700',
indicators = ['1', ' '],
subfields = [
'a', auth.last_name_first,
'e', 'joint author.',
]
)
record.add_ordered_field(field)
# add subfield to 245 indicating format
field245 = record.get_fields('245')[0]
field245.add_subfield('h', '[electronic resource]')
try:
field245 = record.get_fields('245')[0]
except IndexError:
field245 = pymarc.Field(
tag='245',
indicators = ['1', '0'],
subfields = [
'a', edition.title,
]
)
record.add_ordered_field(field245)
field245.add_subfield('a', '[electronic resource]')
# publisher, date
try:
field260 = record.get_fields('260')[0]
except IndexError:
field260 = pymarc.Field(
tag='260',
indicators = [' ', ' '],
subfields = [
'b', edition.publisher_name.name,
'c', unicode(edition.publication_date),
]
)
record.add_ordered_field(field260)
# modify 300 field (physical description)
field300 = record.get_fields('300')[0]
subfield_a = field300.get_subfields('a')[0]
if (
subfield_a[-2:] == ' ;' or
subfield_a[-2:] == ' :' or
subfield_a[-2:] == ' +'
):
subfield_a = subfield_a[:-2]
new300a = '1 online resource (' + subfield_a + ')'
if field300.get_subfields('b'):
new300a += ' :'
field300.delete_subfield('a')
field300.add_subfield('a', new300a)
field300.delete_subfield('c')
try:
field300 = record.get_fields('300')[0]
subfield_a = field300.get_subfields('a')[0]
if (
subfield_a[-2:] == ' ;' or
subfield_a[-2:] == ' :' or
subfield_a[-2:] == ' +'
):
subfield_a = subfield_a[:-2]
new300a = '1 online resource (' + subfield_a + ')'
if field300.get_subfields('b'):
new300a += ' :'
field300.delete_subfield('a')
field300.add_subfield('a', new300a)
field300.delete_subfield('c')
except:
pass
if license:
# add 536 field (funding information)
@ -196,6 +267,8 @@ def makemarc(marcfile, edition):
if print_isbn:
subfields.extend(['z', print_isbn])
elif isbn:
subfields.extend(['z', isbn])
if print_lccn:
subfields.extend(['w', '(DLC) ' + print_lccn, ])
if oclcnum:
@ -291,3 +364,4 @@ def makemarc(marcfile, edition):
writer.write(record)
mrc_file.close()
return marc_record.pk

View File

@ -1520,6 +1520,22 @@ class Author(models.Model):
def __unicode__(self):
return self.name
@property
def last_name_first(self):
names = self.name.rsplit()
if len(names) == 0:
return ''
elif len(names) == 1:
return names[0]
elif len(names) == 2:
return names[1] + ", " + names[0]
else:
reversed_name= names[-1]+","
for name in names[0:-1]:
reversed_name+=" "
reversed_name+=name
return reversed_name
class Subject(models.Model):
created = models.DateTimeField(auto_now_add=True)

View File

@ -43,9 +43,15 @@
See <a href="https://encrypted.google.com/books?id={{ edition.googlebooks_id }}">this edition on Google Books</a><br />
{% endif %}
{% endif %}
{% if 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 />
{% endfor %}
{% else %}
{% if edition.ebooks.all %}
Download {{record.link_target}} MARC record for this edition: (<a href="{% url marc_concatenate %}?edition_{{ edition.id }}=on&amp;format=xml">XML</a>) (<a href="{% url marc_concatenate %}?edition_{{ edition.id }}=on&amp;format=mrc">mrc</a>)<br />
{% endif %}
{% endif %}
{% if user.is_staff %}{% if edition.ebooks.0 or edition.ebook_files.0 %}
<a href="{% url MARCUngluify %}?edition={{ edition.id }}">Upload</a> a MARC record for this edition. <br />
{% endif %} {% endif %}

View File

@ -47,7 +47,7 @@ urlpatterns = patterns(
url(r"^landing/$", "home", {'landing': True}, name="landing"),
url(r"^next/$", "next", name="next"),
url(r"^supporter/(?P<supporter_username>[^/]+)/$", "supporter", {'template_name': 'supporter.html'}, name="supporter"),
url(r"^supporter/(?P<userlist>[^/]+)/marc/$", "marc", name="user_marc"),
url(r"^supporter/(?P<userlist>[^/]+)/marc/$", "marc_admin", name="user_marc"),
url(r"^library/(?P<library_name>[^/]+)/$", "library", name="library"),
url(r"^accounts/manage/$", login_required(ManageAccount.as_view()), name="manage_account"),
url(r"^search/$", "search", name="search"),
@ -150,7 +150,7 @@ urlpatterns = patterns(
url(r"^accounts/edit/kindle_config/$", "kindle_config", name="kindle_config"),
url(r"^accounts/edit/kindle_config/(?P<work_id>\d+)/$", "kindle_config", name="kindle_config_download"),
url(r"^send_to_kindle/(?P<work_id>\d+)/(?P<javascript>\d)/$", "send_to_kindle", name="send_to_kindle"),
url(r"^marc/$", "marc", name="marc"),
url(r"^marc/$", "marc_admin", name="marc"),
url(r"^marc/ungluify/$", staff_member_required(MARCUngluifyView.as_view()), name="MARCUngluify"),
url(r"^marc/concatenate/$", "marc_concatenate", name="marc_concatenate"),
url(r"^accounts/edit/marc_config/$", login_required(MARCConfigView.as_view()), name="marc_config"),

View File

@ -72,7 +72,7 @@ from regluit.core import (
librarything,
userlists,
goodreads,
ungluify_record
marc
)
import regluit.core.cc as cc
from regluit.core.bookloader import merge_works, detach_edition
@ -3091,7 +3091,7 @@ def send_to_kindle(request, work_id, javascript='0'):
return local_response(request, javascript, context, 2)
def marc(request, userlist=None):
def marc_admin(request, userlist=None):
link_target = 'UNGLUE'
libpref = {'marc_link_target': settings.MARC_CHOICES[1]}
try:
@ -3134,7 +3134,7 @@ class MARCUngluifyView(FormView):
edition = form.cleaned_data['edition']
try:
ungluify_record.makemarc(
marc.makemarc(
marcfile=self.request.FILES['file'],
edition=edition
)
@ -3204,7 +3204,15 @@ def marc_concatenate(request):
if format == 'xml':
outfile.write(preamble)
for record in selected_records:
record_id = long(record[7:])
if record.startswith('edition_'):
record_id = long(record[8:])
try:
edition = models.Edition.objects.get(pk=record_id)
except:
continue
record_id = marc.makestub(edition)
else:
record_id = long(record[7:])
if format == 'xml':
record_url = models.MARCRecord.objects.get(
pk=record_id

View File

@ -49,7 +49,7 @@ paramiko==1.7.7.2
postmonkey==1.0a4
pyasn1==0.1.4
pycrypto==2.6
pymarc==2.8.8
pymarc==3.0.2
python-dateutil==2.1
python-openid==2.2.5
pytz==2012d
@ -58,7 +58,7 @@ redis==2.6.2
reportlab==3.1.8
requests==2.4.3
selenium==2.43.0
six==1.7.3
six==1.8.0
ssh==1.7.14
stevedore==0.4
stripe==1.9.1

View File

@ -384,7 +384,7 @@ CONTENT_TYPES = {
'mobi': 'application/x-mobipocket-ebook'
}
# if you add more of these, make sure core/ungluify_record.py can deal
# if you add more of these, make sure core/marc.py can deal
MARC_CHOICES = (
('DIRECT', 'Raw link'),
('UNGLUE', 'Unglue.it link'),