Skip to content

Commit

Permalink
Merge e15abec into b7f5c86
Browse files Browse the repository at this point in the history
  • Loading branch information
benwbrum committed May 7, 2019
2 parents b7f5c86 + e15abec commit fb905d4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 18 deletions.
61 changes: 61 additions & 0 deletions apps/iiif/manifests/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django.core.serializers import serialize
from .models import Manifest
from apps.iiif.annotations.models import Annotation
from datetime import datetime
import json
import zipfile
import io

class IiifManifestExport:
@classmethod
def get_zip(self, manifest, version):
zip_subdir = manifest.label
zip_filename = "iiif_export.zip"

# Open BytesIO to grab in-memory ZIP contents
byte_stream = io.BytesIO()

# The zip compressor
zf = zipfile.ZipFile(byte_stream, "w")

# First write basic human-readable metadata
title = manifest.label
now = datetime.utcnow()
readme = "Annotation export from Readux %(version)s\nExported at %(now)s UTC\nVolume: %(title)s\n" % locals()
zf.writestr('README.txt', readme)

# Next write the manifest
zf.writestr('manifest.json',
json.dumps(
json.loads(
serialize(
'manifest',
[manifest],
version=version
)
),
indent=4
)
)

# Then write the annotations
for canvas in manifest.canvas_set.all():
if canvas.annotation_set.count() > 0:
annotation_file = "annotation_list_" + canvas.pid + ".json"
zf.writestr(
annotation_file,
json.dumps(
json.loads(
serialize(
'annotation_list',
[canvas],
version=version
)
),
indent=4
)
)

zf.close() # flush zipfile to byte stream

return byte_stream.getvalue()
1 change: 1 addition & 0 deletions apps/iiif/manifests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

urlpatterns = [
path('iiif/<version>/<pid>/manifest', views.ManifestDetail.as_view(), name="ManifestRender"),
path('iiif/<version>/<pid>/export', views.ManifestExport.as_view(), name="ManifestExport"),
path('col/<collection>/vol/<volume>/citation.ris', views.ManifestRis.as_view(), name='ris' ),
]
19 changes: 19 additions & 0 deletions apps/iiif/manifests/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.http import HttpResponse
from django.views import View
from django.views.generic.base import TemplateView
from django.core.serializers import serialize
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import Manifest
from .export import IiifManifestExport
import json


# TODO Would still be nice to use DRF. Try this?
# https://stackoverflow.com/a/35019122
class ManifestDetail(View):
Expand Down Expand Up @@ -42,3 +45,19 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['volume'] = Manifest.objects.filter(pid=kwargs['volume']).first()
return context


class ManifestExport(View):

def get_queryset(self):
return Manifest.objects.filter(pid=self.kwargs['pid'])

def post(self, request, *args, **kwargs):
# we should probably move this out of the view, into a library
manifest = self.get_queryset()[0]

zip = IiifManifestExport.get_zip(manifest, kwargs['version'])
resp = HttpResponse(zip, content_type = "application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=iiif_export.zip'

return resp
19 changes: 6 additions & 13 deletions apps/iiif/serializers/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,22 @@ def get_dump_object(self, obj):
},
"resource": {
"@type": obj.resource_type,
"format": obj.format,
"chars": obj.content,
"format": "text/html",
"chars": obj.svg,
"language": obj.language
},
"on": [{
"on": {
"full": "%s/iiif/%s/%s/canvas/%s" % (settings.HOSTNAME, self.version, obj.canvas.manifest.pid, obj.canvas.pid),
"@type": "oa:SpecificResource",
"within": {
"@id": "%s/iiif/%s/%s/manifest" % (settings.HOSTNAME, self.version, obj.canvas.manifest.pid),
"@type": "sc:Manifest"
},
"selector": {
"item": {
"@type": "oa:SvgSelector",
"value": obj.svg
},
"@type": "oa:Choice",
"default": {
"@type": "oa:FragmentSelector",
"value": "xywh=%s,%s,%s,%s" % (str(obj.x), str(obj.y), str(obj.w), str(obj.h))
}
"@type": "oa:FragmentSelector",
"value": "xywh=%s,%s,%s,%s" % (str(obj.x), str(obj.y), str(obj.w), str(obj.h))
}
}]
}
}
return data

Expand Down
1 change: 1 addition & 0 deletions apps/readux/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
# url for page altered to prevent conflict with Wagtail
# TODO: find another way to resolve this conflict
path('col/<collection>/vol/<volume>/page/<page>', views.PageDetail.as_view(), name='page' ),
path('col/<collection>/vol/<volume>/export', views.ExportOptions.as_view(), name='export' ),
]
8 changes: 8 additions & 0 deletions apps/readux/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ def get_context_data(self, **kwargs):
context['page'] = Canvas.objects.filter(pid=kwargs['page']).first()
context['volume'] = Manifest.objects.filter(pid=kwargs['volume']).first()
return context

class ExportOptions(TemplateView):
template_name = "export.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['volume'] = Manifest.objects.filter(pid=kwargs['volume']).first()
return context
39 changes: 39 additions & 0 deletions apps/templates/export.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "base.html" %}
{% load static i18n %}
{% block extra_css %}
<link href="{% static 'mirador/css/mirador-combined.css' %}" rel="stylesheet">
{% endblock extra_css %}

{% block content %}

<h1> NEW EXPORT PAGE</h1>
<h1 class='uk-text-truncate'>
{{volume.label}}
</h1>
{% endblock content %}
{% block viewer %}
<div class='uk-flex-center' uk-grid>
<div class='uk-width-3-4'>
<div class='uk-padding'>
<b>Author:</b> {{ volume.author }}
<br />
<b>Publication Date:</b> {{ volume.published_date }}
<br />{% for col in volume.collections.all %}
<b>Collection:</b> <a class="nav-link" href="{% url 'collection' col.pid %}">{{ col.label }}</a>{% endfor %}
<br />
<b>Publisher:</b> {{ volume.published_city }} : {{ volume.publisher }}
</div>
<div class='uk-padding'>
<h3>IIIF Export</h3>
<p>Export volume and annotations as a IIIF bundle for preservation. (Note this is different than the IIIF Manifest URL for reuse.)</p>
<form action="{% url 'ManifestExport' 'v2' volume.pid %}" class="button_to" method="POST"><div><input type="submit" value="Export IIIF Bundle" title="Export volume and annotations as a IIIF bundle for preservation."/>{% csrf_token %}</div></form>
</div>

<!-- start ocr el -->
<div id='ocr' style='opacity: 0;'>

</div>
<!-- end ocr el -->
</div>
</div>
{% endblock viewer %}
13 changes: 8 additions & 5 deletions apps/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ <h1 class='uk-text-truncate'>
<br />
<b>Export Citation:</b> <a href="https://{{ request.META.HTTP_HOST }}{% url 'ris' col.pid volume.pid %}" target="_blank">Click to download RIS file</a>
<br />
<br />
<b>Stable volume URL:</b> <a href="https://{{ request.META.HTTP_HOST }}{% url 'volume' col.pid volume.pid %}" target="_blank">https://{{ request.META.HTTP_HOST }}{% url 'volume' col.pid volume.pid %}</a>
<br />
<b>Canvas URL:</b> <span class="link" id="myLink"></span>
<br />
<b>Stable page URL:</b> <span class="link" id="mySpan"></span><a class="link" id="mySpan"></a>
<br/>


<b>Stable page URL:</b> <span class="link" id="mySpan"></span>
<br />
<form action="{% url 'export' col.pid volume.pid %}" class="button_to" method="GET"><div><input type="submit" value="Export" title="Export in a variety of formats"/>{% csrf_token %}</div></form>
<!--
<p id="test"></p>
<a class="facebook" href='http://www.facebook.com/sharer.php?s=100&p[url]=http://{{ request.META.HTTP_HOST }}{{ request.path }}&p[images][0]{{ page.IIIF_IMAGE_SERVER_BASE }}{{ page.pid }}/full/600,/0/default.jpg' target="_blank">
<img src="https://www.cabq.gov/culturalservices/biopark/images/share-on-facebook.png/@@images/image.png" title="Facebook" alt="Facebook" ></a>
-->
{% endfor %}
</div>

Expand Down

0 comments on commit fb905d4

Please sign in to comment.