From f030289137a031a2e78f9aadb9ce075d8de76649 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 12 Aug 2011 14:23:41 +0200 Subject: [PATCH] Display audiobook project and sponsors info from metadata, Keep BookMedia filenames tied to media names (keep filename when updating the file, update on name change), which means overwriting the replaced files, Split book files into dirs by type, savemedia: update name on media replace, don't need to slugify though (BookMedia does it anyway) --- .../management/commands/savemedia.py | 8 +++--- apps/catalogue/models.py | 28 ++++++++++++++++--- apps/catalogue/views.py | 27 +++++++++++++++++- .../templates/catalogue/book_detail.html | 26 +++++++++++++++-- .../templates/catalogue/counters.html | 12 +++++++- 5 files changed, 89 insertions(+), 12 deletions(-) diff --git a/apps/catalogue/management/commands/savemedia.py b/apps/catalogue/management/commands/savemedia.py index fdc25cb5a..49f1d484e 100755 --- a/apps/catalogue/management/commands/savemedia.py +++ b/apps/catalogue/management/commands/savemedia.py @@ -6,7 +6,6 @@ import os.path from django.core.management.base import BaseCommand from django.core.files import File -from slughifi import slughifi from catalogue.models import Book, BookMedia from catalogue.utils import ExistingFile @@ -40,11 +39,12 @@ class Command(BaseCommand): try: assert source_sha1 bm = book.media.get(type=ext, source_sha1=source_sha1) - print "Replacing media: %s (%s)" % (bm.name, ext) + print "Replacing media: %s (%s)" % (bm.name.encode('utf-8'), ext) except (AssertionError, BookMedia.DoesNotExist): - bm = BookMedia(book=book, type=ext, name=name) + bm = BookMedia(book=book, type=ext) print "Creating new media" - bm.file.save(slughifi(name), ExistingFile(path)) + bm.name = name + bm.file.save(None, ExistingFile(path)) bm.save() transaction.commit() transaction.leave_transaction_management() diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 84bcfd342..6291d97e8 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -18,6 +18,7 @@ from django.conf import settings from newtagging.models import TagBase, tags_updated from newtagging import managers from catalogue.fields import JSONField +from catalogue.utils import ExistingFile from librarian import dcparser, html, epub, NoDublinCore import mutagen @@ -176,7 +177,7 @@ def book_upload_path(ext=None, maxlen=100): name = slughifi(filename.split(".")[0]) else: name = slughifi(media.name) - return 'lektura/%s.%s' % (name[:maxlen-len('lektura/.%s' % ext)-4], ext) + return 'book/%s/%s.%s' % (ext, name[:maxlen-len('book/%s/.%s' % (ext, ext))-4], ext) return get_dynamic_path @@ -198,6 +199,18 @@ class BookMedia(models.Model): verbose_name_plural = _('book media') def save(self, *args, **kwargs): + try: + b = BookMedia.objects.get(pk=self.pk) + except BookMedia.DoesNotExist, e: + pass + else: + # if file is replaced, delete the old one + if self.file.path != b.file.path: + b.file.delete(save=False) + # if name changed, change the file name, too + elif self.name != b.name: + self.file.save(None, ExistingFile(self.file.path)) + super(BookMedia, self).save(*args, **kwargs) extra_info = self.get_extra_info_value() extra_info.update(self.read_meta()) @@ -210,12 +223,16 @@ class BookMedia(models.Model): Reads some metadata from the audiobook. """ - artist_name = director_name = '' + artist_name = director_name = project = funded_by = '' if self.type == 'mp3': try: audio = id3.ID3(self.file.path) artist_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE1')) director_name = ', '.join(', '.join(tag.text) for tag in audio.getall('TPE3')) + project = ", ".join([t.data for t in audio.getall('PRIV') + if t.owner=='wolnelektury.pl?project']) + funded_by = ", ".join([t.data for t in audio.getall('PRIV') + if t.owner=='wolnelektury.pl?funded_by']) except: pass elif self.type == 'ogg': @@ -223,11 +240,14 @@ class BookMedia(models.Model): audio = mutagen.File(self.file.path) artist_name = ', '.join(audio.get('artist', [])) director_name = ', '.join(audio.get('conductor', [])) + project = ", ".join(audio.get('project', [])) + funded_by = ", ".join(audio.get('funded_by', [])) except: pass else: return {} - return {'artist_name': artist_name, 'director_name': director_name} + return {'artist_name': artist_name, 'director_name': director_name, + 'project': project, 'funded_by': funded_by} @staticmethod def read_source_sha1(filepath, filetype): @@ -239,7 +259,7 @@ class BookMedia(models.Model): try: audio = id3.ID3(filepath) return [t.data for t in audio.getall('PRIV') - if t.owner=='http://wolnelektury.pl?flac_sha1'][0] + if t.owner=='wolnelektury.pl?flac_sha1'][0] except: return None elif filetype == 'ogg': diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index ac778d4a7..c5eb0c7cc 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -129,6 +129,8 @@ def daisy_list(request): def counters(request): + form = forms.SearchForm() + books = models.Book.objects.count() books_nonempty = models.Book.objects.exclude(html_file='').count() books_empty = models.Book.objects.filter(html_file='').count() @@ -139,7 +141,18 @@ def counters(request): annotate(count=Count('type')).\ order_by('type') for mt in media_types: - mt['size'] = sum(b.file.size for b in models.BookMedia.objects.filter(type=mt['type'])) + size = 0 + deprecated = missing_project = 0 + for b in models.BookMedia.objects.filter(type=mt['type']): + size += b.file.size + if b.type in ('mp3', 'ogg'): + if not b.source_sha1: + deprecated += 1 + if not 'project' in b.get_extra_info_value(): + missing_project += 1 + mt['size'] = size + mt['deprecated'] = deprecated + mt['missing_project'] = missing_project return render_to_response('catalogue/counters.html', locals(), context_instance=RequestContext(request)) @@ -294,6 +307,18 @@ def book_detail(request, slug): extra_info = book.get_extra_info_value() + projects = set() + for m in book.media.filter(type='mp3'): + # ogg files are always from the same project + meta = m.get_extra_info_value() + project = meta.get('project') + if not project: + # temporary fallback + project = u'CzytamySłuchając' + + projects.add((project, meta.get('funded_by'))) + projects = sorted(projects) + form = forms.SearchForm() return render_to_response('catalogue/book_detail.html', locals(), context_instance=RequestContext(request)) diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index 54548007e..e6cc0f46c 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -103,8 +103,30 @@ {% endif %} -

{% blocktrans with 'CzytamySłuchając' as cs %}Audiobooks were prepared as a part of the {{ cs }} project.{% endblocktrans %} -

+ {% if projects|length > 1 %} +

{% trans "Audiobooks were prepared as a part of the projects:" %}

+ + {% else %} +

+ {% with cs=projects.0.0 fb=projects.0.1 %} + {% if fb %} + {% blocktrans %}Audiobooks were prepared as a part of the {{ cs }} project funded by {{ fb }}.{% endblocktrans %} + {% else %} + {% blocktrans %}Audiobooks were prepared as a part of the {{ cs }} project.{% endblocktrans %} + {% endif %} + {% endwith %} +

+ {% endif %} {% endif %} diff --git a/wolnelektury/templates/catalogue/counters.html b/wolnelektury/templates/catalogue/counters.html index 572583c65..9262a4841 100755 --- a/wolnelektury/templates/catalogue/counters.html +++ b/wolnelektury/templates/catalogue/counters.html @@ -7,14 +7,24 @@ {% block body %}

Liczniki

+
+

{{ form.q }} {% trans "or" %} {% trans "return to main page" %}

+
+ + + {% for mt in media_types %} - + + + + + {% endfor %}
Utwory
Wszystkie utwory:{{ books }}
Utwory z własną treścią:{{ books_nonempty }}
Utwory bez własnej treści:{{ books_empty }}
Niezależne książki:{{ books_root }}
MediaLiczbaRozmiarDo wymiany
Media – {{ mt.type }}:{{ mt.count }}, {{ mt.size|filesizeformat }}
{{ mt.type }}:{{ mt.count }}{{ mt.size|filesizeformat }}{{ mt.deprecated }}
-- 2.20.1