diff --git a/core/ungluify_record.py b/core/marc.py similarity index 70% rename from core/ungluify_record.py rename to core/marc.py index 64d0d74d..db199168 100644 --- a/core/ungluify_record.py +++ b/core/marc.py @@ -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,13 +91,22 @@ 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) - + record.add_ordered_field(field008) + # add IBSN for ebook where applicable; relegate print ISBN to $z isbn = '' try: @@ -123,26 +142,78 @@ def makemarc(marcfile, edition): record.add_ordered_field(field082_new) 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: @@ -290,4 +363,5 @@ def makemarc(marcfile, edition): writer = pymarc.MARCWriter(mrc_file) writer.write(record) mrc_file.close() - + + return marc_record.pk diff --git a/core/models.py b/core/models.py index c2470786..cdfa1527 100755 --- a/core/models.py +++ b/core/models.py @@ -1519,7 +1519,23 @@ 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) diff --git a/frontend/templates/edition_display.html b/frontend/templates/edition_display.html index 947377a8..6a1b0d23 100644 --- a/frontend/templates/edition_display.html +++ b/frontend/templates/edition_display.html @@ -42,10 +42,16 @@ {% if edition.googlebooks_id %} See this edition on Google Books
{% endif %} - {% endif %} + {% endif %} + {% if edition.MARCrecords.all %} {% for record in edition.MARCrecords.all %} Download {{record.link_target}} MARC record for this edition: (XML) (mrc)
{% endfor %} + {% else %} + {% if edition.ebooks.all %} + Download {{record.link_target}} MARC record for this edition: (XML) (mrc)
+ {% endif %} + {% endif %} {% if user.is_staff %}{% if edition.ebooks.0 or edition.ebook_files.0 %} Upload a MARC record for this edition.
{% endif %} {% endif %} diff --git a/frontend/urls.py b/frontend/urls.py index 41a2105b..70b8ba74 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -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", {'template_name': 'supporter.html'}, name="supporter"), - url(r"^supporter/(?P[^/]+)/marc/$", "marc", name="user_marc"), + url(r"^supporter/(?P[^/]+)/marc/$", "marc_admin", name="user_marc"), url(r"^library/(?P[^/]+)/$", "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\d+)/$", "kindle_config", name="kindle_config_download"), url(r"^send_to_kindle/(?P\d+)/(?P\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"), diff --git a/frontend/views.py b/frontend/views.py index ae55d7f0..94632625 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -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 diff --git a/requirements_versioned.pip b/requirements_versioned.pip index 52605e34..ac38c29c 100644 --- a/requirements_versioned.pip +++ b/requirements_versioned.pip @@ -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 diff --git a/settings/common.py b/settings/common.py index 25140592..094689a2 100644 --- a/settings/common.py +++ b/settings/common.py @@ -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'),