From 938fc832934e508085dad3645c01060a4dc9f0c9 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 29 Dec 2011 17:23:06 +0100 Subject: [PATCH] change in multilingual --- apps/api/handlers.py | 25 +--- apps/api/urls.py | 4 +- apps/catalogue/import_utils.py | 17 +++ .../management/commands/importbooks.py | 3 +- ...g__add_unique_book_slug__del_unique_boo.py | 137 ++++++++++++++++++ apps/catalogue/migrations/0023_common_slug.py | 123 ++++++++++++++++ apps/catalogue/models.py | 101 ++++--------- apps/catalogue/test_utils.py | 6 +- apps/catalogue/tests/book_import.py | 10 +- apps/catalogue/tests/bookmedia.py | 4 +- apps/catalogue/tests/tags.py | 10 +- apps/catalogue/urls.py | 23 +-- apps/catalogue/utils.py | 21 +-- apps/catalogue/views.py | 62 +++----- .../templates/dictionary/note_list.html | 2 +- apps/lesmianator/urls.py | 2 +- apps/lesmianator/views.py | 7 +- apps/picture/models.py | 8 +- lib/librarian | 2 +- wolnelektury/settings.py | 3 +- .../templates/catalogue/book_detail.html | 81 +---------- .../templates/catalogue/book_sets.html | 2 +- .../templates/catalogue/book_short.html | 2 +- wolnelektury/templates/catalogue/player.html | 4 +- .../catalogue/tagged_object_list.html | 2 +- wolnelektury/templates/lesmianator/poem.html | 4 +- 26 files changed, 393 insertions(+), 272 deletions(-) create mode 100755 apps/catalogue/import_utils.py create mode 100644 apps/catalogue/migrations/0022_auto__add_field_book_common_slug__add_unique_book_slug__del_unique_boo.py create mode 100644 apps/catalogue/migrations/0023_common_slug.py diff --git a/apps/api/handlers.py b/apps/api/handlers.py index d34806f0a..586e921ca 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -99,14 +99,10 @@ class BookDetailHandler(BaseHandler): 'media', 'url'] + category_singular.keys() @piwik_track - def read(self, request, book): + def read(self, request, slug): """ Returns details of a book, identified by a slug and lang. """ - kwargs = Book.split_urlid(book) - if not kwargs: - return rc.NOT_FOUND - try: - return Book.objects.get(**kwargs) + return Book.objects.get(slug=slug) except Book.DoesNotExist: return rc.NOT_FOUND @@ -127,7 +123,7 @@ class AnonymousBooksHandler(AnonymousBaseHandler): @classmethod def href(cls, book): """ Returns an URI for a Book in the API. """ - return API_BASE + reverse("api_book", args=[book.urlid()]) + return API_BASE + reverse("api_book", args=[book.slug]) @classmethod def url(cls, book): @@ -269,18 +265,10 @@ class FragmentDetailHandler(BaseHandler): fields = ['book', 'anchor', 'text', 'url', 'themes'] @piwik_track - def read(self, request, book, anchor): + def read(self, request, slug, anchor): """ Returns details of a fragment, identified by book slug and anchor. """ - kwargs = Book.split_urlid(book) - if not kwargs: - return rc.NOT_FOUND - - fragment_kwargs = {} - for field, value in kwargs.items(): - fragment_kwargs['book__' + field] = value - try: - return Fragment.objects.get(anchor=anchor, **fragment_kwargs) + return Fragment.objects.get(book__slug=slug, anchor=anchor) except Fragment.DoesNotExist: return rc.NOT_FOUND @@ -317,7 +305,8 @@ class FragmentsHandler(BaseHandler): def href(cls, fragment): """ Returns URI in the API for the fragment. """ - return API_BASE + reverse("api_fragment", args=[fragment.book.urlid(), fragment.anchor]) + return API_BASE + reverse("api_fragment", + args=[fragment.book.slug, fragment.anchor]) @classmethod def url(cls, fragment): diff --git a/apps/api/urls.py b/apps/api/urls.py index 8f7bed3fe..fd97d63d9 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -47,10 +47,10 @@ urlpatterns = patterns( # objects details - url(r'^books/(?P%s)/$' % Book.URLID_RE, book_resource, name="api_book"), + url(r'^books/(?P[a-z0-9-]+)/$', book_resource, name="api_book"), url(r'^(?P[a-z0-9-]+)/(?P[a-z0-9-]+)/$', tag_resource, name="api_tag"), - url(r'^books/(?P%s)/fragments/(?P[a-z0-9-]+)/$' % Book.URLID_RE, + url(r'^books/(?P[a-z0-9-]+)/fragments/(?P[a-z0-9-]+)/$', fragment_resource, name="api_fragment"), # books by tags diff --git a/apps/catalogue/import_utils.py b/apps/catalogue/import_utils.py new file mode 100755 index 000000000..bf36ea57f --- /dev/null +++ b/apps/catalogue/import_utils.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from librarian import DocProvider + +class ORMDocProvider(DocProvider): + """Used for getting books' children.""" + + def __init__(self, book): + self.book = book + + def by_slug(self, slug): + if slug == self.book.slug: + return open(self.book.xml_file.path) + else: + return type(self.book).objects.get(slug=slug).xml_file \ No newline at end of file diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index d097ddd1a..995132a82 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -46,11 +46,10 @@ class Command(BaseCommand): build_txt=options.get('build_txt'), build_pdf=options.get('build_pdf'), build_mobi=options.get('build_mobi')) - fileid = book.fileid() for ebook_format in Book.ebook_formats: if os.path.isfile(file_base + '.' + ebook_format): getattr(book, '%s_file' % ebook_format).save( - '%s.%s' % (fileid, ebook_format), + '%s.%s' % (book.slug, ebook_format), File(file(file_base + '.' + ebook_format))) if verbose: print "Importing %s.%s" % (file_base, ebook_format) diff --git a/apps/catalogue/migrations/0022_auto__add_field_book_common_slug__add_unique_book_slug__del_unique_boo.py b/apps/catalogue/migrations/0022_auto__add_field_book_common_slug__add_unique_book_slug__del_unique_boo.py new file mode 100644 index 000000000..75a1c99f4 --- /dev/null +++ b/apps/catalogue/migrations/0022_auto__add_field_book_common_slug__add_unique_book_slug__del_unique_boo.py @@ -0,0 +1,137 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + +class Migration(SchemaMigration): + + def forwards(self, orm): + + # Removing unique constraint on 'Book', fields ['slug', 'language'] + db.delete_unique('catalogue_book', ['slug', 'language']) + + # Adding field 'Book.common_slug' + db.add_column('catalogue_book', 'common_slug', self.gf('django.db.models.fields.SlugField')(default='-', max_length=120, db_index=True), keep_default=False) + + # Adding unique constraint on 'Book', fields ['slug'] + db.create_unique('catalogue_book', ['slug']) + + + def backwards(self, orm): + + # Removing unique constraint on 'Book', fields ['slug'] + db.delete_unique('catalogue_book', ['slug']) + + # Deleting field 'Book.common_slug' + db.delete_column('catalogue_book', 'common_slug') + + # Adding unique constraint on 'Book', fields ['slug', 'language'] + db.create_unique('catalogue_book', ['slug', 'language']) + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'catalogue.book': { + 'Meta': {'ordering': "('sort_key',)", 'object_name': 'Book'}, + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'common_slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + 'cover': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'epub_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'extra_info': ('catalogue.fields.JSONField', [], {'default': "'{}'"}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'html_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'default': "'pol'", 'max_length': '3', 'db_index': 'True'}), + 'mobi_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}), + 'parent_number': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pdf_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'txt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'xml_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}) + }, + 'catalogue.bookmedia': { + 'Meta': {'ordering': "('type', 'name')", 'object_name': 'BookMedia'}, + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'media'", 'to': "orm['catalogue.Book']"}), + 'extra_info': ('catalogue.fields.JSONField', [], {'default': "'{}'"}), + 'file': ('catalogue.fields.OverwritingFileField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'source_sha1': ('django.db.models.fields.CharField', [], {'max_length': '40', 'null': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'uploaded_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}) + }, + 'catalogue.fragment': { + 'Meta': {'ordering': "('book', 'anchor')", 'object_name': 'Fragment'}, + 'anchor': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'fragments'", 'to': "orm['catalogue.Book']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'short_text': ('django.db.models.fields.TextField', [], {}), + 'text': ('django.db.models.fields.TextField', [], {}) + }, + 'catalogue.tag': { + 'Meta': {'ordering': "('sort_key',)", 'unique_together': "(('slug', 'category'),)", 'object_name': 'Tag'}, + 'book_count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'category': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}) + }, + 'catalogue.tagrelation': { + 'Meta': {'unique_together': "(('tag', 'content_type', 'object_id'),)", 'object_name': 'TagRelation', 'db_table': "'catalogue_tag_relation'"}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'items'", 'to': "orm['catalogue.Tag']"}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['catalogue'] diff --git a/apps/catalogue/migrations/0023_common_slug.py b/apps/catalogue/migrations/0023_common_slug.py new file mode 100644 index 000000000..386314915 --- /dev/null +++ b/apps/catalogue/migrations/0023_common_slug.py @@ -0,0 +1,123 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import DataMigration +from django.db import models + +class Migration(DataMigration): + + def forwards(self, orm): + "Write your forwards methods here." + orm.Book.objects.all().update(common_slug=models.F('slug')) + + + def backwards(self, orm): + "Write your backwards methods here." + pass + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'catalogue.book': { + 'Meta': {'ordering': "('sort_key',)", 'object_name': 'Book'}, + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'common_slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + 'cover': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'epub_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'extra_info': ('catalogue.fields.JSONField', [], {'default': "'{}'"}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'html_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'default': "'pol'", 'max_length': '3', 'db_index': 'True'}), + 'mobi_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['catalogue.Book']"}), + 'parent_number': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'pdf_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'txt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'xml_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}) + }, + 'catalogue.bookmedia': { + 'Meta': {'ordering': "('type', 'name')", 'object_name': 'BookMedia'}, + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'media'", 'to': "orm['catalogue.Book']"}), + 'extra_info': ('catalogue.fields.JSONField', [], {'default': "'{}'"}), + 'file': ('catalogue.fields.OverwritingFileField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'source_sha1': ('django.db.models.fields.CharField', [], {'max_length': '40', 'null': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': "'100'"}), + 'uploaded_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}) + }, + 'catalogue.fragment': { + 'Meta': {'ordering': "('book', 'anchor')", 'object_name': 'Fragment'}, + 'anchor': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'book': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'fragments'", 'to': "orm['catalogue.Book']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'short_text': ('django.db.models.fields.TextField', [], {}), + 'text': ('django.db.models.fields.TextField', [], {}) + }, + 'catalogue.tag': { + 'Meta': {'ordering': "('sort_key',)", 'unique_together': "(('slug', 'category'),)", 'object_name': 'Tag'}, + 'book_count': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'category': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'db_index': 'True', 'blank': 'True'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'db_index': 'True', 'blank': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'gazeta_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + 'sort_key': ('django.db.models.fields.CharField', [], {'max_length': '120', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}), + 'wiki_link': ('django.db.models.fields.CharField', [], {'max_length': '240', 'blank': 'True'}) + }, + 'catalogue.tagrelation': { + 'Meta': {'unique_together': "(('tag', 'content_type', 'object_id'),)", 'object_name': 'TagRelation', 'db_table': "'catalogue_tag_relation'"}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'items'", 'to': "orm['catalogue.Tag']"}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['catalogue'] diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 112ec1b99..43a9496d8 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -220,7 +220,7 @@ def get_customized_pdf_path(book, customizations): customizations.sort() h = hash(tuple(customizations)) - pdf_name = '%s-custom-%s' % (book.fileid(), h) + pdf_name = '%s-custom-%s' % (book.slug, h) pdf_file = get_dynamic_path(None, pdf_name, ext='pdf') return pdf_file @@ -230,7 +230,7 @@ def get_existing_customized_pdf(book): """ Returns a list of paths to generated customized pdf of a book """ - pdf_glob = '%s-custom-' % (book.fileid(),) + pdf_glob = '%s-custom-' % (book.slug,) pdf_glob = get_dynamic_path(None, pdf_glob, ext='pdf') pdf_glob = re.sub(r"[.]([a-z0-9]+)$", "*.\\1", pdf_glob) return glob(path.join(settings.MEDIA_ROOT, pdf_glob)) @@ -269,7 +269,7 @@ class BookMedia(models.Model): try: old = BookMedia.objects.get(pk=self.pk) except BookMedia.DoesNotExist, e: - pass + old = None else: # if name changed, change the file name, too if slughifi(self.name) != slughifi(old.name): @@ -278,7 +278,9 @@ class BookMedia(models.Model): super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media - remove_zip(self.book.fileid()) + if old: + remove_zip("%s_%s" % (old.book.slug, old.type)) + remove_zip("%s_%s" % (self.book.slug, self.type)) extra_info = self.get_extra_info_value() extra_info.update(self.read_meta()) @@ -347,7 +349,9 @@ class BookMedia(models.Model): class Book(models.Model): title = models.CharField(_('title'), max_length=120) sort_key = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False) - slug = models.SlugField(_('slug'), max_length=120, db_index=True) + slug = models.SlugField(_('slug'), max_length=120, db_index=True, + unique=True) + common_slug = models.SlugField(_('slug'), max_length=120, db_index=True) language = models.CharField(_('language code'), max_length=3, db_index=True, default=settings.CATALOGUE_DEFAULT_LANGUAGE) description = models.TextField(_('description'), blank=True) @@ -372,14 +376,10 @@ class Book(models.Model): html_built = django.dispatch.Signal() published = django.dispatch.Signal() - URLID_RE = r'[a-z0-9-]+(?:/[a-z]{3})?' - FILEID_RE = r'[a-z0-9-]+(?:_[a-z]{3})?' - class AlreadyExists(Exception): pass class Meta: - unique_together = [['slug', 'language']] ordering = ('sort_key',) verbose_name = _('book') verbose_name_plural = _('books') @@ -387,42 +387,6 @@ class Book(models.Model): def __unicode__(self): return self.title - def urlid(self, sep='/'): - stem = self.slug - if self.language != settings.CATALOGUE_DEFAULT_LANGUAGE: - stem += sep + self.language - return stem - - def fileid(self): - return self.urlid('_') - - @staticmethod - def split_urlid(urlid, sep='/', default_lang=settings.CATALOGUE_DEFAULT_LANGUAGE): - """Splits a URL book id into slug and language code. - - Returns a dictionary usable i.e. for object lookup, or None. - - >>> Book.split_urlid("a-slug/pol", default_lang="eng") - {'slug': 'a-slug', 'language': 'pol'} - >>> Book.split_urlid("a-slug", default_lang="eng") - {'slug': 'a-slug', 'language': 'eng'} - >>> Book.split_urlid("a-slug_pol", "_", default_lang="eng") - {'slug': 'a-slug', 'language': 'pol'} - >>> Book.split_urlid("a-slug/eng", default_lang="eng") - - """ - parts = urlid.rsplit(sep, 1) - if len(parts) == 2: - if parts[1] == default_lang: - return None - return {'slug': parts[0], 'language': parts[1]} - else: - return {'slug': urlid, 'language': default_lang} - - @classmethod - def split_fileid(cls, fileid): - return cls.split_urlid(fileid, '_') - def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs): from sortify import sortify @@ -437,18 +401,14 @@ class Book(models.Model): @permalink def get_absolute_url(self): - return ('catalogue.views.book_detail', [self.urlid()]) + return ('catalogue.views.book_detail', [self.slug]) @property def name(self): return self.title def book_tag_slug(self): - stem = 'l-' + self.slug - if self.language != settings.CATALOGUE_DEFAULT_LANGUAGE: - return stem[:116] + ' ' + self.language - else: - return stem[:120] + return ('l-' + self.slug)[:120] def book_tag(self): slug = self.book_tag_slug() @@ -565,7 +525,7 @@ class Book(models.Model): has_daisy_file.boolean = True def wldocument(self, parse_dublincore=True): - from catalogue.utils import ORMDocProvider + from catalogue.import_utils import ORMDocProvider from librarian.parser import WLDocument return WLDocument.from_file(self.xml_file.path, @@ -601,7 +561,7 @@ class Book(models.Model): # we'd like to be sure not to overwrite changes happening while # (timely) pdf generation is taking place (async celery scenario) current_self = Book.objects.get(id=self.id) - current_self.pdf_file.save('%s.pdf' % self.fileid(), + current_self.pdf_file.save('%s.pdf' % self.slug, File(open(pdf.get_filename()))) self.pdf_file = current_self.pdf_file @@ -624,7 +584,7 @@ class Book(models.Model): mobi = self.wldocument().as_mobi() - self.mobi_file.save('%s.mobi' % self.fileid(), File(open(mobi.get_filename()))) + self.mobi_file.save('%s.mobi' % self.slug, File(open(mobi.get_filename()))) # remove zip with all mobi files remove_zip(settings.ALL_MOBI_ZIP) @@ -636,7 +596,7 @@ class Book(models.Model): epub = self.wldocument().as_epub() - self.epub_file.save('%s.epub' % self.fileid(), + self.epub_file.save('%s.epub' % self.slug, File(open(epub.get_filename()))) # remove zip package with all epub files @@ -646,7 +606,7 @@ class Book(models.Model): from django.core.files.base import ContentFile text = self.wldocument().as_text() - self.txt_file.save('%s.txt' % self.fileid(), ContentFile(text.get_string())) + self.txt_file.save('%s.txt' % self.slug, ContentFile(text.get_string())) def build_html(self): @@ -661,7 +621,7 @@ class Book(models.Model): html_output = self.wldocument(parse_dublincore=False).as_html() if html_output: - self.html_file.save('%s.html' % self.fileid(), + self.html_file.save('%s.html' % self.slug, ContentFile(html_output.get_string())) # get ancestor l-tags for adding to new fragments @@ -712,7 +672,7 @@ class Book(models.Model): def pretty_file_name(book): return "%s/%s.%s" % ( b.get_extra_info_value()['author'], - b.fileid(), + b.slug, format_) field_name = "%s_file" % format_ @@ -726,7 +686,7 @@ class Book(models.Model): def zip_audiobooks(self, format_): bm = BookMedia.objects.filter(book=self, type=format_) paths = map(lambda bm: (None, bm.file.path), bm) - result = create_zip.delay(paths, "%s_%s" % (self.fileid(), format_)) + result = create_zip.delay(paths, "%s_%s" % (self.slug, format_)) return result.wait() @classmethod @@ -756,30 +716,33 @@ class Book(models.Model): if hasattr(book_info, 'parts'): for part_url in book_info.parts: try: - children.append(Book.objects.get( - slug=part_url.slug, language=part_url.language)) + children.append(Book.objects.get(slug=part_url.slug)) except Book.DoesNotExist, e: - raise Book.DoesNotExist(_('Book "%s/%s" does not exist.') % - (part_url.slug, part_url.language)) + raise Book.DoesNotExist(_('Book "%s" does not exist.') % + part_url.slug) # Read book metadata book_slug = book_info.url.slug - language = book_info.language - if re.search(r'[^a-zA-Z0-9-]', book_slug): + if re.search(r'[^a-z0-9-]', book_slug): raise ValueError('Invalid characters in slug') - book, created = Book.objects.get_or_create(slug=book_slug, language=language) + book, created = Book.objects.get_or_create(slug=book_slug) if created: book_shelves = [] else: if not overwrite: - raise Book.AlreadyExists(_('Book %s/%s already exists') % ( - book_slug, language)) + raise Book.AlreadyExists(_('Book %s already exists') % ( + book_slug)) # Save shelves for this book book_shelves = list(book.tags.filter(category='set')) + book.language = book_info.language book.title = book_info.title + if book_info.variant_of: + book.common_slug = book_info.variant_of.slug + else: + book.common_slug = book.slug book.set_extra_info_value(book_info.to_dict()) book.save() @@ -940,7 +903,7 @@ class Book(models.Model): books_by_parent = {} books = cls.objects.all().order_by('parent_number', 'sort_key').only( - 'title', 'parent', 'slug', 'language') + 'title', 'parent', 'slug') if filter: books = books.filter(filter).distinct() book_ids = set((book.pk for book in books)) diff --git a/apps/catalogue/test_utils.py b/apps/catalogue/test_utils.py index 58ef58acd..eeda03f04 100644 --- a/apps/catalogue/test_utils.py +++ b/apps/catalogue/test_utils.py @@ -13,10 +13,12 @@ class WLTestCase(TestCase): self._MEDIA_ROOT, settings.MEDIA_ROOT = settings.MEDIA_ROOT, tempfile.mkdtemp(prefix='djangotest_') settings.NO_BUILD_PDF = settings.NO_BUILD_MOBI = settings.NO_BUILD_EPUB = settings.NO_BUILD_TXT = True settings.CELERY_ALWAYS_EAGER = True + self._CACHE_BACKEND, settings.CACHE_BACKEND = settings.CACHE_BACKEND, 'dummy://' def tearDown(self): shutil.rmtree(settings.MEDIA_ROOT, True) settings.MEDIA_ROOT = self._MEDIA_ROOT + settings.CACHE_BACKEND = self._CACHE_BACKEND class PersonStub(object): @@ -29,7 +31,7 @@ class PersonStub(object): class BookInfoStub(object): - _empty_fields = ['cover_url'] + _empty_fields = ['cover_url', 'variant_of'] # allow single definition for multiple-value fields _salias = { 'authors': 'author', @@ -65,7 +67,7 @@ def info_args(title, language=None): language = u'pol' return { 'title': unicode(title), - 'url': WLURI.from_slug_and_lang(slug, language), + 'url': WLURI.from_slug(slug), 'about': u"http://wolnelektury.pl/example/URI/%s" % slug, 'language': language, } diff --git a/apps/catalogue/tests/book_import.py b/apps/catalogue/tests/book_import.py index a97c41711..3af1bb486 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -15,7 +15,7 @@ class BookImportLogicTests(WLTestCase): def setUp(self): WLTestCase.setUp(self) self.book_info = BookInfoStub( - url=WLURI.from_slug_and_lang(u"default-book", None), + url=WLURI.from_slug(u"default-book"), about=u"http://wolnelektury.pl/example/URI/default_book", title=u"Default Book", author=PersonStub(("Jim",), "Lazy"), @@ -114,7 +114,7 @@ class BookImportLogicTests(WLTestCase): @raises(ValueError) def test_book_with_invalid_slug(self): """ Book with invalid characters in slug shouldn't be imported """ - self.book_info.url = WLURI.from_slug_and_lang(u"default_book", None) + self.book_info.url = WLURI.from_slug(u"default_book") BOOK_TEXT = "" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) @@ -247,12 +247,15 @@ class ChildImportTests(WLTestCase): class MultilingualBookImportTest(WLTestCase): def setUp(self): WLTestCase.setUp(self) + common_uri = WLURI.from_slug('common-slug') + self.pol_info = BookInfoStub( genre='X-Genre', epoch='X-Epoch', kind='X-Kind', author=PersonStub(("Joe",), "Doe"), - **info_args("A book") + variant_of=common_uri, + **info_args(u"Książka") ) self.eng_info = BookInfoStub( @@ -260,6 +263,7 @@ class MultilingualBookImportTest(WLTestCase): epoch='X-Epoch', kind='X-Kind', author=PersonStub(("Joe",), "Doe"), + variant_of=common_uri, **info_args("A book", "eng") ) diff --git a/apps/catalogue/tests/bookmedia.py b/apps/catalogue/tests/bookmedia.py index b6598b33d..5d2ba66c3 100644 --- a/apps/catalogue/tests/bookmedia.py +++ b/apps/catalogue/tests/bookmedia.py @@ -103,8 +103,8 @@ class BookMediaTests(WLTestCase): bm.file.save(None, self.file) bm.save() - zip_url = self.book.zip_audiobooks() - self.assertEqual('zip/'+self.book.slug+'.zip', zip_url) + zip_url = self.book.zip_audiobooks('ogg') + self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url) self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url))) bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title") diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index 7e6e66716..a47e426a5 100644 --- a/apps/catalogue/tests/tags.py +++ b/apps/catalogue/tests/tags.py @@ -3,9 +3,6 @@ from catalogue import models from catalogue.test_utils import * from django.core.files.base import ContentFile -from nose.tools import raises - - class BooksByTagTests(WLTestCase): """ tests the /katalog/category/tag page for found books """ @@ -66,12 +63,13 @@ class BooksByTagTests(WLTestCase): ['Child']) - +from django.test import Client class TagRelatedTagsTests(WLTestCase): """ tests the /katalog/category/tag/ page for related tags """ def setUp(self): WLTestCase.setUp(self) + self.client = Client() author = PersonStub(("Common",), "Man") gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind", @@ -132,14 +130,14 @@ class TagRelatedTagsTests(WLTestCase): def test_related_differ(self): """ related tags shouldn't include filtering tags """ - cats = self.client.get('/katalog/rodzaj/kind/').context['categories'] + response = self.client.get('/katalog/rodzaj/kind/') + cats = response.context['categories'] self.assertFalse('Kind' in [tag.name for tag in cats['kind']], 'filtering tag wrongly included in related') cats = self.client.get('/katalog/motyw/theme/').context['categories'] self.assertFalse('Theme' in [tag.name for tag in cats['theme']], 'filtering theme wrongly included in related') - def test_parent_tag_once(self): """ if parent and descendants have a common tag, count it only once """ diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 241afca3a..e5087b6d0 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -7,22 +7,25 @@ from catalogue.feeds import AudiobookFeed from catalogue.models import Book from picture.models import Picture + +SLUG = r'[a-z0-9-]*' + urlpatterns = patterns('picture.views', # pictures - currently pictures are coupled with catalogue, hence the url is here url(r'^obraz/?$', 'picture_list'), - url(r'^obraz/(?P%s)/?$' % Picture.URLID_RE, 'picture_detail') + url(r'^obraz/(?P%s)/?$' % SLUG, 'picture_detail') ) + \ patterns('catalogue.views', url(r'^$', 'catalogue', name='catalogue'), url(r'^polki/(?P[a-zA-Z0-9-]+)/formaty/$', 'shelf_book_formats', name='shelf_book_formats'), - url(r'^polki/(?P[a-zA-Z0-9-]+)/(?P%s)/usun$' % Book.URLID_RE, 'remove_from_shelf', name='remove_from_shelf'), + url(r'^polki/(?P[a-zA-Z0-9-]+)/(?P%s)/usun$' % SLUG, 'remove_from_shelf', name='remove_from_shelf'), url(r'^polki/$', 'user_shelves', name='user_shelves'), url(r'^polki/(?P[a-zA-Z0-9-]+)/usun/$', 'delete_shelf', name='delete_shelf'), url(r'^polki/(?P[a-zA-Z0-9-]+)\.zip$', 'download_shelf', name='download_shelf'), url(r'^lektury/', 'book_list', name='book_list'), url(r'^audiobooki/$', 'audiobook_list', name='audiobook_list'), url(r'^daisy/$', 'daisy_list', name='daisy_list'), - url(r'^lektura/(?P%s)/polki/' % Book.URLID_RE, 'book_sets', name='book_shelves'), + url(r'^lektura/(?P%s)/polki/' % SLUG, 'book_sets', name='book_shelves'), url(r'^polki/nowa/$', 'new_set', name='new_set'), url(r'^tags/$', 'tags_starting_with', name='hint'), url(r'^jtags/$', 'json_tags_starting_with', name='jhint'), @@ -32,20 +35,20 @@ urlpatterns = patterns('picture.views', url(r'^zip/pdf\.zip$', 'download_zip', {'format': 'pdf', 'slug': None}, 'download_zip_pdf'), url(r'^zip/epub\.zip$', 'download_zip', {'format': 'epub', 'slug': None}, 'download_zip_epub'), url(r'^zip/mobi\.zip$', 'download_zip', {'format': 'mobi', 'slug': None}, 'download_zip_mobi'), - url(r'^zip/mp3/(?P%s)\.zip' % Book.FILEID_RE, 'download_zip', {'format': 'mp3'}, 'download_zip_mp3'), - url(r'^zip/ogg/(?P%s)\.zip' % Book.FILEID_RE, 'download_zip', {'format': 'ogg'}, 'download_zip_ogg'), + url(r'^zip/mp3/(?P%s)\.zip' % SLUG, 'download_zip', {'format': 'mp3'}, 'download_zip_mp3'), + url(r'^zip/ogg/(?P%s)\.zip' % SLUG, 'download_zip', {'format': 'ogg'}, 'download_zip_ogg'), # Public interface. Do not change this URLs. - url(r'^lektura/(?P%s)\.html$' % Book.FILEID_RE, 'book_text', name='book_text'), - url(r'^lektura/(?P%s)/audiobook/$' % Book.URLID_RE, 'player', name='book_player'), - url(r'^lektura/(?P%s)/$' % Book.URLID_RE, 'book_detail', name='book_detail'), - url(r'^lektura/(?P%s)/motyw/(?P[a-zA-Z0-9-]+)/$' % Book.URLID_RE, + url(r'^lektura/(?P%s)\.html$' % SLUG, 'book_text', name='book_text'), + url(r'^lektura/(?P%s)/audiobook/$' % SLUG, 'player', name='book_player'), + url(r'^lektura/(?P%s)/$' % SLUG, 'book_detail', name='book_detail'), + url(r'^lektura/(?P%s)/motyw/(?P[a-zA-Z0-9-]+)/$' % SLUG, 'book_fragments', name='book_fragments'), url(r'^(?P[a-zA-Z0-9-/]*)/$', 'tagged_object_list', name='tagged_object_list'), url(r'^audiobooki/(?Pmp3|ogg|daisy|all).xml$', AudiobookFeed(), name='audiobook_feed'), - url(r'^custompdf/(?P%s).pdf' % Book.FILEID_RE, 'download_custom_pdf'), + url(r'^custompdf/(?P%s).pdf' % SLUG, 'download_custom_pdf'), ) diff --git a/apps/catalogue/utils.py b/apps/catalogue/utils.py index 145511e06..a48ec0370 100644 --- a/apps/catalogue/utils.py +++ b/apps/catalogue/utils.py @@ -20,7 +20,6 @@ from errno import EEXIST, ENOENT from fcntl import flock, LOCK_EX from zipfile import ZipFile -from librarian import DocProvider from reporting.utils import read_chunks from celery.task import task import catalogue.models @@ -60,20 +59,6 @@ class ExistingFile(UploadedFile): pass -class ORMDocProvider(DocProvider): - """Used for getting books' children.""" - - def __init__(self, book): - self.book = book - - def by_slug_and_lang(self, slug, language): - if slug == self.book.slug and language == self.language: - return open(self.book.xml_file.path) - else: - return type(self.book).objects.get( - slug=slug, language=language).xml_file - - class LockFile(object): """ A file lock monitor class; createas an ${objname}.lock @@ -180,7 +165,11 @@ class MultiQuerySet(object): return self.count() def __getitem__(self, item): - indices = (offset, stop, step) = item.indices(self.count()) + try: + indices = (offset, stop, step) = item.indices(self.count()) + except AttributeError: + # it's not a slice - make it one + return self[item : item + 1][0] items = [] total_len = stop - offset for qs in self.querysets: diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 90fe22fd0..3a3283229 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -185,11 +185,8 @@ def tagged_object_list(request, tags=''): context_instance=RequestContext(request)) -def book_fragments(request, book, theme_slug): - kwargs = models.Book.split_urlid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) +def book_fragments(request, slug, theme_slug): + book = get_object_or_404(models.Book, slug=slug) book_tag = book.book_tag() theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme') @@ -199,12 +196,9 @@ def book_fragments(request, book, theme_slug): context_instance=RequestContext(request)) -def book_detail(request, book): - kwargs = models.Book.split_urlid(book) - if kwargs is None: - raise Http404 +def book_detail(request, slug): try: - book = models.Book.objects.get(**kwargs) + book = models.Book.objects.get(slug=slug) except models.Book.DoesNotExist: return pdcounter_views.book_stub_detail(request, kwargs['slug']) @@ -233,11 +227,8 @@ def book_detail(request, book): context_instance=RequestContext(request)) -def player(request, book): - kwargs = models.Book.split_urlid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) +def player(request, slug): + book = get_object_or_404(models.Book, slug=slug) if not book.has_media('mp3'): raise Http404 @@ -274,11 +265,8 @@ def player(request, book): context_instance=RequestContext(request)) -def book_text(request, book): - kwargs = models.Book.split_fileid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) +def book_text(request, slug): + book = get_object_or_404(models.Book, slug=slug) if not book.has_html_file(): raise Http404 @@ -513,14 +501,11 @@ def user_shelves(request): context_instance=RequestContext(request)) @cache.never_cache -def book_sets(request, book): +def book_sets(request, slug): if not request.user.is_authenticated(): return HttpResponse(_('

To maintain your shelves you need to be logged in.

')) - kwargs = models.Book.split_urlid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) + book = get_object_or_404(models.Book, slug=slug) user_sets = models.Tag.objects.filter(category='set', user=request.user) book_sets = book.tags.filter(category='set', user=request.user) @@ -553,11 +538,8 @@ def book_sets(request, book): @login_required @require_POST @cache.never_cache -def remove_from_shelf(request, shelf, book): - kwargs = models.Book.split_urlid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) +def remove_from_shelf(request, shelf, slug): + book = get_object_or_404(models.Book, slug=slug) shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user) @@ -608,11 +590,10 @@ def download_shelf(request, slug): archive = zipfile.ZipFile(temp, 'w') for book in collect_books(models.Book.tagged.with_all(shelf)): - fileid = book.fileid() for ebook_format in models.Book.ebook_formats: if ebook_format in formats and book.has_media(ebook_format): filename = book.get_media(ebook_format).path - archive.write(filename, str('%s.%s' % (fileid, ebook_format))) + archive.write(filename, str('%s.%s' % (book.slug, ebook_format))) archive.close() response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed') @@ -711,25 +692,20 @@ def tag_info(request, id): return HttpResponse(tag.description) -def download_zip(request, format, book=None): - kwargs = models.Book.split_fileid(book) - +def download_zip(request, format, slug=None): url = None if format in models.Book.ebook_formats: url = models.Book.zip_format(format) - elif format in ('mp3', 'ogg') and kwargs is not None: - book = get_object_or_404(models.Book, **kwargs) + elif format in ('mp3', 'ogg') and slug is not None: + book = get_object_or_404(models.Book, slug=slug) url = book.zip_audiobooks(format) else: raise Http404('No format specified for zip package') return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?=')) -def download_custom_pdf(request, book_fileid): - kwargs = models.Book.split_fileid(book_fileid) - if kwargs is None: - raise Http404 - book = get_object_or_404(models.Book, **kwargs) +def download_custom_pdf(request, slug): + book = get_object_or_404(models.Book, slug=slug) if request.method == 'GET': form = forms.CustomPDFForm(request.GET) @@ -740,7 +716,7 @@ def download_custom_pdf(request, book_fileid): if not path.exists(pdf_file): result = async_build_pdf.delay(book.id, cust, pdf_file) result.wait() - return AttachmentHttpResponse(file_name=("%s.pdf" % book_fileid), file_path=pdf_file, mimetype="application/pdf") + return AttachmentHttpResponse(file_name=("%s.pdf" % book.slug), file_path=pdf_file, mimetype="application/pdf") else: raise Http404(_('Incorrect customization options for PDF')) else: diff --git a/apps/dictionary/templates/dictionary/note_list.html b/apps/dictionary/templates/dictionary/note_list.html index cd88c17bc..aad0ddfbe 100755 --- a/apps/dictionary/templates/dictionary/note_list.html +++ b/apps/dictionary/templates/dictionary/note_list.html @@ -42,7 +42,7 @@ {% endfor %} diff --git a/apps/lesmianator/urls.py b/apps/lesmianator/urls.py index eeba6103f..de48644f1 100644 --- a/apps/lesmianator/urls.py +++ b/apps/lesmianator/urls.py @@ -8,7 +8,7 @@ from catalogue.models import Book urlpatterns = patterns('lesmianator.views', url(r'^$', 'main_page', name='lesmianator'), url(r'^wiersz/$', 'new_poem', name='new_poem'), - url(r'^lektura/(?P%s)/$' % Book.URLID_RE, 'poem_from_book', name='poem_from_book'), + url(r'^lektura/(?P[a-z0-9-]+)/$', 'poem_from_book', name='poem_from_book'), url(r'^polka/(?P[a-zA-Z0-9-]+)/$', 'poem_from_set', name='poem_from_set'), url(r'^wiersz/(?P[a-zA-Z0-9-]+)/$', 'get_poem', name='get_poem'), ) diff --git a/apps/lesmianator/views.py b/apps/lesmianator/views.py index 28cb32a87..e86febe91 100644 --- a/apps/lesmianator/views.py +++ b/apps/lesmianator/views.py @@ -33,11 +33,8 @@ def new_poem(request): @cache.never_cache -def poem_from_book(request, book): - kwargs = Book.split_urlid(book) - if kwargs is None: - raise Http404 - book = get_object_or_404(Book, **kwargs) +def poem_from_book(request, slug): + book = get_object_or_404(Book, slug=slug) user = request.user if request.user.is_authenticated() else None text = Poem.write(Continuations.get(book)) p = Poem(slug=get_random_hash(text), text=text, created_by=user) diff --git a/apps/picture/models.py b/apps/picture/models.py index 862c172c2..0217d8bad 100644 --- a/apps/picture/models.py +++ b/apps/picture/models.py @@ -44,9 +44,6 @@ class Picture(models.Model): verbose_name = _('picture') verbose_name_plural = _('pictures') - URLID_RE = r'[a-z0-9-]+' - FILEID_RE = r'[a-z0-9-]+' - def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs): from sortify import sortify @@ -64,10 +61,7 @@ class Picture(models.Model): @permalink def get_absolute_url(self): - return ('picture.views.picture_detail', [self.urlid()]) - - def urlid(self): - return self.slug + return ('picture.views.picture_detail', [self.slug]) @classmethod def from_xml_file(cls, xml_file, image_file=None, overwrite=False): diff --git a/lib/librarian b/lib/librarian index 5fed78856..a34b95aa7 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit 5fed78856949474a36bc5e268517775a9a802e27 +Subproject commit a34b95aa7ba5fd4838541d1cdcd28358fb808062 diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index bc63aa3fa..20a3fc653 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -156,7 +156,8 @@ INSTALLED_APPS = [ ] #CACHE_BACKEND = 'locmem:///?max_entries=3000' -#CACHE_BACKEND = 'memcached://127.0.0.1:11211/' +CACHE_BACKEND = 'memcached://127.0.0.1:11211/' +#CACHE_BACKEND = None CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True # CSS and JavaScript file groups diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index 4b4ee2f18..62b16f242 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -36,11 +36,11 @@

{% endif %}
-

{% trans "Put a book" %} {% trans "on the shelf!" %}

+

{% trans "Put a book" %} {% trans "on the shelf!" %}

{% if book.has_html_file %} - {% trans "Read online" %} + {% trans "Read online" %} {% endif %}
{% if book.pdf_file %} @@ -60,83 +60,12 @@
{% trans "Dowload customized PDF" %}. {% endif %}
- {% if book.has_mp3_file or book.has_ogg_file or book.has_daisy_file %} -

- {% trans "Audiobooks" %}: - - {% if book.has_mp3_file %}MP3{% endif %} - {% if book.has_ogg_file %}Ogg Vorbis{% endif %} - {% if book.has_daisy_file %}DAISY{% endif %} - -

-
- Speaker icon - {% if book.has_mp3_file %} -
    - {% for media in book.get_mp3 %} -
  • - {{ media.name }}
    - {% trans "Artist" %}: {{ media.get_extra_info_value.artist_name }}
    - {% trans "Director"%}: {{ media.get_extra_info_value.director_name }}
    - - - - - - -
  • - {% endfor %} -
- {% endif %} - - {% if book.has_ogg_file %} - - {% endif %} - {% if book.has_daisy_file %} - - {% endif %} -
- {% if projects|length > 1 %} -

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

-
    - {% for cs, fb in projects %} -
  • - {% if fb %} - {% blocktrans %}{{ cs }}, funded by {{ fb }}{% endblocktrans %} - {% else %} - {{ cs }} - {% endif %} -
  • - {% endfor %} -
- {% else %} -

- {% with projects.0.0 as cs %} - {% with projects.0.1 as fb %} - {% 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 %} - {% endwith %} -

- {% endif %} - {% endif %}
@@ -199,13 +128,13 @@ {% endif %}

{% trans "View XML source" %}

-

Miksuj ten utwór

+

Miksuj ten utwór

{% trans "Work's themes " %}

diff --git a/wolnelektury/templates/catalogue/book_sets.html b/wolnelektury/templates/catalogue/book_sets.html index f151650d1..1eee61d21 100644 --- a/wolnelektury/templates/catalogue/book_sets.html +++ b/wolnelektury/templates/catalogue/book_sets.html @@ -9,7 +9,7 @@ {% if not user.tag_set.count %}

{% trans "You do not have any shelves. You can create one below, if you want to."%}

{% else %} -
+
  1. {{ form.set_ids }}
  2. diff --git a/wolnelektury/templates/catalogue/book_short.html b/wolnelektury/templates/catalogue/book_short.html index 5557126da..9800a0cae 100644 --- a/wolnelektury/templates/catalogue/book_short.html +++ b/wolnelektury/templates/catalogue/book_short.html @@ -49,7 +49,7 @@
    • {% if book.html_file %} - {% trans "Read online" %} + {% trans "Read online" %} {% endif %}
    • diff --git a/wolnelektury/templates/catalogue/player.html b/wolnelektury/templates/catalogue/player.html index 95fcb5e62..414bf0eeb 100755 --- a/wolnelektury/templates/catalogue/player.html +++ b/wolnelektury/templates/catalogue/player.html @@ -77,8 +77,8 @@

      {% trans "Download as" %}: - MP3{% if have_oggs %}, - Ogg Vorbis{% endif %}. + MP3{% if have_oggs %}, + Ogg Vorbis{% endif %}.

      diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index fce00dcbd..7c33a3740 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -134,7 +134,7 @@ {% for book in object_list %}
    • {% if user_is_owner %} - {% trans "Delete" %} + {% trans "Delete" %} {% endif %} {{ book.short_html }}
    • {% endfor %} diff --git a/wolnelektury/templates/lesmianator/poem.html b/wolnelektury/templates/lesmianator/poem.html index 23e943759..03d1e9d03 100644 --- a/wolnelektury/templates/lesmianator/poem.html +++ b/wolnelektury/templates/lesmianator/poem.html @@ -15,7 +15,7 @@ {# shelf or global mixing #} Twórzże się jeszcze raz! {% else %}{% if book %} - Twórzże się jeszcze raz! + Twórzże się jeszcze raz! {% endif %}{% endif %} Wolne Lektury przepuszczone przez mikser. @@ -40,7 +40,7 @@ {% if book %}

      Tekst powstał przez zmiksowanie utworu {{ book.title }}.
      - Zmiksuj go ponownie + Zmiksuj go ponownie albo zobacz, co jeszcze możesz zamieszać.

      {% else %}{% if books %}

      Tekst powstał przez zmiksowanie utworów:

      -- 2.20.1