From b1eaf9c7bca47b059a38242bf341836dc1d7d53d Mon Sep 17 00:00:00 2001 From: Tris Forster Date: Thu, 2 Mar 2023 10:05:26 +1100 Subject: [PATCH] Added document extractor --- app/library/models.py | 17 +++++++--- app/library/pdf_utils.py | 4 +-- .../templates/library/document_annotate.html | 2 +- .../templates/library/work_detail.html | 21 +++++++----- app/library/tests.py | 10 +++--- app/library/urls.py | 1 + app/library/views/__init__.py | 34 ++++++++++++++++++- app/library/views/api.py | 19 ++++++----- 8 files changed, 77 insertions(+), 31 deletions(-) diff --git a/app/library/models.py b/app/library/models.py index 63b68f0..77cb216 100644 --- a/app/library/models.py +++ b/app/library/models.py @@ -202,13 +202,14 @@ class Work(models.Model): def folder(self): return f"{slugify(self.composer)}/{slugify(self.name)}-{self.pk:04d}" - def extract(self, *tags): - + def tagged_sections(self, *tags): qs = self.docs.filter(sections__tag__in=tags) qs = qs.annotate(Count('sections'), end=Min('sections__end'), start=Max('sections__start')) \ .filter(sections__count=len(tags)) - - return list(qs.values_list('upload', 'start', 'end')) + return qs + + def list_sections(self, *tags): + return list(self.tagged_sections(*tags).values_list('upload', 'start', 'end')) @property def digital_parts(self): @@ -276,6 +277,14 @@ class Work(models.Model): assigned = set(self.assigned_instruments()) return [ x for x in self.orchestration.as_list() if not x[0] in assigned ] + def music_tags(self): + tags = dict(self.orchestration.as_list()) + for section in Section.objects.filter(doc__work_id=self.pk): + tags.setdefault(section.tag, section.name) + + return tags.items() + + def __str__(self): return f"{self.name} ({self.composer})" diff --git a/app/library/pdf_utils.py b/app/library/pdf_utils.py index c0e5b67..a9eec64 100644 --- a/app/library/pdf_utils.py +++ b/app/library/pdf_utils.py @@ -24,12 +24,12 @@ def extract_and_concat(items): if count == 0: continue - if start is None: + if not start: sections.append(source) else: - if end is None: + if not end: end = start dest = os.path.join(d.name, f'section_{i}.pdf') diff --git a/app/library/templates/library/document_annotate.html b/app/library/templates/library/document_annotate.html index 2cf3fba..78bd13b 100644 --- a/app/library/templates/library/document_annotate.html +++ b/app/library/templates/library/document_annotate.html @@ -75,7 +75,7 @@