now users can select the MARC records they want and get as single download

pull/1/head
Andromeda Yelton 2013-07-24 14:52:01 -04:00
parent 6528848b95
commit da08afd0b8
4 changed files with 138 additions and 27 deletions

View File

@ -2,6 +2,19 @@
{% block title %} MARC records{% endblock %}
{% block extra_js %}
{{ block.super }}
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j('#record_form').submit(function() {
$j('#submit').hide();
$j('#unsubmit').show();
});
});
</script>
{% endblock %}
{% block extra_extra_head %}
<style type="text/css">
.marc {
@ -21,6 +34,14 @@ ul.local li {
list-style-type: disc;
list-style-position: inside;
}
/* need to do a hide/show instead of a .replaceWith() on the submit button
because the image takes long enough to load that it's preempted by the
redirect for quick downloads, and users never see it
*/
#unsubmit {
display: none;
}
</style>
{% endblock %}
@ -32,32 +53,53 @@ ul.local li {
<p>Go ahead: add unglued ebooks to your library catalog!</p>
<p>Your preference for the links in the 856 field is:</p>
<ul class="local">
<li>{{ request.user.profile.get_marc_format_display }}</li>
</ul>
<p>The options are:</p>
<ul class="local">
{% for choice in MARC_CHOICES %}
<li>{{ choice.1 }}</li>
{% endfor %}
</ul>
<p>You can <a href="{% url marc_config %}">change your preferences here</a>.</p>
{% if request.user.is_staff %}
<p>Hi, {{ request.user.username }}. Unglue.it staffers can also <a href="{% url MARCUngluify %}">add new records</a>.</p>
{% endif %}
{% if messages %}
<ul class="errors">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div class="marc">
{% for record in records %}
<div>
<a class="fakeinput" href="{{ record.xml_record }}">Download MARCXML</a>
<a class="fakeinput" href="{{ record.mrc_record }}">Download .mrc</a>
<a href="{% url work record.edition.work.id %}" class="title clearfix">{{ record.edition.work.title }}</a>
</div>
<br />
{% endfor %}
<form method="POST" id="record_form" action="{% url marc_concatenate %}">
{% csrf_token %}
Record format:
<select name="format">
<option value="xml">xml</option>
<option value="mrc">mrc</option>
</select>
<br /><br />
{% for record in records %}
<input type="checkbox" name="record_{{ record.id }}" id="record_{{ record.id }}"/>
<label for="record_{{ record.id }}">
Record for <a href="{% url work record.edition.work.id %}" class="title clearfix">{{ record.edition.work.title }}{% if record.edition.isbn_13 %} (ISBN {{ record.edition.isbn_13 }}){% endif %}</a>
</label>
<br/>
{% endfor %}
<input type="submit" name="submit" value="Download" id="submit">
<img id="unsubmit" src="/static/images/loading.gif">
</form>
</div>
<hr>
<div>
<p>Your preference for the links in the 856 field is:</p>
<ul class="local">
<li>{{ request.user.profile.get_marc_format_display }}</li>
</ul>
<p>The options are:</p>
<ul class="local">
{% for choice in MARC_CHOICES %}
<li>{{ choice.1 }}</li>
{% endfor %}
</ul>
<p>You can <a href="{% url marc_config %}">change your preferences here</a>.</p>
</div>
{% endblock %}

View File

@ -14,7 +14,7 @@
{% block ce_content %}
{% if messages %}
<ul class="messages">
<ul class="errors">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}

View File

@ -1,5 +1,6 @@
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.views.generic import ListView, DetailView
@ -134,7 +135,8 @@ urlpatterns = patterns(
url(r"^send_to_kindle/(?P<kindle_ebook_id>\d+)/(?P<javascript>\d)/$", "send_to_kindle", name="send_to_kindle"),
url(r"^send_to_kindle/result/(?P<message>\d)/$", "send_to_kindle_graceful", name="send_to_kindle_graceful"),
url(r"^marc/$", "marc", name="marc"),
url(r"^marc/ungluify/$", login_required(MARCUngluifyView.as_view()), name="MARCUngluify"),
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

@ -33,6 +33,7 @@ from django.contrib.comments import Comment
from django.contrib.sites.models import Site
from django.core import signing
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.files.storage import default_storage
from django.core.files.temp import NamedTemporaryFile
from django.core.mail import EmailMessage
from django.core.urlresolvers import reverse, reverse_lazy
@ -2610,10 +2611,12 @@ def send_to_kindle_graceful(request, message):
)
def marc(request):
marc_format = request.user.profile.marc_format
logger.info(marc_format)
try:
marc_format = request.user.profile.marc_format
except AttributeError:
# set default for anonymous users
marc_format = 'DIRECT'
records = models.MARCRecord.objects.filter(marc_format=marc_format)
logger.info(records)
return render(
request,
'marc.html',
@ -2664,4 +2667,68 @@ class MARCConfigView(FormView):
return HttpResponseRedirect(reverse('marc_config', args=[]))
else:
return super(MARCConfigView, self).form_valid(form)
@require_POST
def marc_concatenate(request):
"""
options for future work...
cache files: keep records of each combo created, check before building anew?
dispatch as background process, email when done?
if not caching, delete files on s3 after a while?
make the control flow suck less during file write
"""
format = request.POST['format']
# extract the user-selected records from the POST QueryDict
selected_records = list(
k for k in request.POST if request.POST[k] == u'on'
)
if not selected_records:
messages.error(
request,
"Please select at least one record to download."
)
return HttpResponseRedirect(reverse('marc', args=[]))
# keep test files from stepping on production files
if '/unglue.it' in settings.BASE_URL:
directory = 'marc/'
else:
directory = 'marc_test/'
# write the concatenated file
datestamp = now().strftime('%Y%m%d%H%M%S')
filename = directory + datestamp + '.' + format
with default_storage.open(filename, 'w') as outfile:
if format == 'xml':
string = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
'<collection '
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
'xmlns="http://www.loc.gov/MARC21/slim" '
'xsi:schemaLocation="http://www.loc.gov/MARC21/slim '
'http://www.loc.gov/standards/marcxml/schema/'
'MARC21slim.xsd">')
outfile.write(string)
for record in selected_records:
record_id = long(record[7:])
if format == 'xml':
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 = urllib.urlopen(record_url).read()
outfile.write(record_file)
except IOError:
logger.warning('MARC record with id number %s has a problem with its URL' % record_id)
if format == 'xml':
string = ('</collection>')
outfile.write(string)
download_url = default_storage.url(filename)
return HttpResponseRedirect(download_url)