Added document extractor
This commit is contained in:
@@ -19,7 +19,7 @@ import re
|
||||
from interface.views import EnsembleMixin, ProjectMixin, AuthorizedResourceMixin
|
||||
from interface.models import Project
|
||||
from library.models import Collection, Work, Document, Section
|
||||
from library.music_tags import MUSIC_TAGS, MUSIC_TAG_BY_NAME
|
||||
from library.music_tags import MUSIC_TAGS, MUSIC_TAG_BY_NAME, MusicTag
|
||||
from library import forms, models
|
||||
from library.pdf_utils import extract_pages, extract_and_concat
|
||||
|
||||
@@ -308,6 +308,38 @@ class WorkPartSetView(CollectionMixin, DetailView):
|
||||
works = works.filter(collection__allowed_ensembles__ensemble=self.request.ensemble_id)
|
||||
return works
|
||||
|
||||
class WorkDownloadView(CollectionMixin, SingleObjectMixin, View):
|
||||
model = models.Work
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
|
||||
tags = request.GET.getlist('tag')
|
||||
|
||||
if not tags:
|
||||
raise Http404("No tags given")
|
||||
|
||||
sections = list(self.object.tagged_sections(*tags))
|
||||
print(sections)
|
||||
|
||||
if len(sections) == 0:
|
||||
raise Http404("No matching sections")
|
||||
|
||||
if len(sections) == 1 and sections[0].start == 0:
|
||||
# bypass extraction and redirect to the url
|
||||
logger.debug("Redirecting to url")
|
||||
return redirect(sections[0].upload.url)
|
||||
|
||||
result = extract_and_concat([ (s.upload.path, s.upload.name, s.start, s.end, 1) for s in sections ])
|
||||
|
||||
tag_names = " - ".join([ str(MusicTag.from_tag(tag)) for tag in tags ])
|
||||
|
||||
download_name = f'{self.object.name} - {tag_names}.pdf'
|
||||
|
||||
response = FileResponse(result, content_type="application/pdf")
|
||||
response['Content-Disposition'] = f'inline; filename="{download_name}"'
|
||||
return response
|
||||
|
||||
class WorkAddDocumentView(CollectionMixin, CreateView):
|
||||
template_name = "interface/default_form.html"
|
||||
model = Document
|
||||
|
||||
@@ -35,6 +35,7 @@ class WorkExportView(EnsembleMixin, WorkMixin, View):
|
||||
from interface.views import AuthorizedResourceMixin
|
||||
|
||||
from rest_framework import routers, serializers, viewsets
|
||||
from rest_framework.exceptions import APIException
|
||||
|
||||
from library.models import Collection, Work, Document, Section, WorkMeta
|
||||
|
||||
@@ -72,14 +73,12 @@ class SectionSerializer(serializers.ModelSerializer):
|
||||
|
||||
def to_internal_value(self, data):
|
||||
tag, start, end = data.split(":")
|
||||
try:
|
||||
start = int(start)
|
||||
except:
|
||||
start = 0
|
||||
try:
|
||||
end = int(end)
|
||||
except:
|
||||
end = 0
|
||||
start = int(start)
|
||||
end = int(end)
|
||||
if start < 1:
|
||||
start = None
|
||||
if end < 1:
|
||||
end = None
|
||||
return super().to_internal_value({'tag': tag, 'start': start, 'end': end})
|
||||
|
||||
class DocumentSerializer(serializers.ModelSerializer):
|
||||
@@ -139,7 +138,9 @@ class WorkSerializer(serializers.ModelSerializer):
|
||||
filename = os.path.basename(url.path)
|
||||
|
||||
r = requests.get(d['upload'], stream=True)
|
||||
f = TemporaryUploadedFile(filename, r.headers['content-type'], r.headers['content-length'], r.encoding)
|
||||
if r.status_code != 200:
|
||||
raise APIException("Failed to download file")
|
||||
f = TemporaryUploadedFile(filename, r.headers['content-type'], r.headers.get('content-length'), r.encoding)
|
||||
shutil.copyfileobj(r.raw, f.file)
|
||||
r.close()
|
||||
d['upload'] = f
|
||||
|
||||
Reference in New Issue
Block a user