From: Radek Czajka Date: Wed, 7 Dec 2011 16:18:36 +0000 (+0100) Subject: basic multilingual publications support X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/7e6d14043e3e2dce8e3fdcef0b0fc649680b07b3 basic multilingual publications support removed the unused FileRecord model --- diff --git a/apps/api/handlers.py b/apps/api/handlers.py index 8f06deafc..f83a49219 100644 --- a/apps/api/handlers.py +++ b/apps/api/handlers.py @@ -97,11 +97,14 @@ class BookDetailHandler(BaseHandler): 'media', 'url'] + category_singular.keys() @piwik_track - def read(self, request, slug): - """ Returns details of a book, identified by a slug. """ + def read(self, request, book): + """ 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(slug=slug) + return Book.objects.get(**kwargs) except Book.DoesNotExist: return rc.NOT_FOUND @@ -122,7 +125,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.slug]) + return API_BASE + reverse("api_book", args=[book.urlid()]) @classmethod def url(cls, book): @@ -265,11 +268,18 @@ class FragmentDetailHandler(BaseHandler): fields = ['book', 'anchor', 'text', 'url', 'themes'] @piwik_track - def read(self, request, slug, anchor): + def read(self, request, book, 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(book__slug=slug, anchor=anchor) + return Fragment.objects.get(anchor=anchor, **fragment_kwargs) except Fragment.DoesNotExist: return rc.NOT_FOUND @@ -306,7 +316,7 @@ class FragmentsHandler(BaseHandler): def href(cls, fragment): """ Returns URI in the API for the fragment. """ - return API_BASE + reverse("api_fragment", args=[fragment.book.slug, fragment.anchor]) + return API_BASE + reverse("api_fragment", args=[fragment.book.urlid(), fragment.anchor]) @classmethod def url(cls, fragment): diff --git a/apps/api/urls.py b/apps/api/urls.py index 2d92eba46..b6fc7604e 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -4,6 +4,7 @@ from piston.authentication import OAuthAuthentication from piston.resource import Resource from api import handlers +from catalogue.models import Book auth = OAuthAuthentication(realm="Wolne Lektury") @@ -46,10 +47,10 @@ urlpatterns = patterns( # objects details - url(r'^books/(?P[a-z0-9-]+)/$', book_resource, name="api_book"), + url(r'^books/(?P%s)/$' % Book.URLID_RE, book_resource, name="api_book"), url(r'^(?P[a-z0-9-]+)/(?P[a-z0-9-]+)/$', tag_resource, name="api_tag"), - url(r'^books/(?P[a-z0-9-]+)/fragments/(?P[a-z0-9-]+)/$', + url(r'^books/(?P%s)/fragments/(?P[a-z0-9-]+)/$' % Book.URLID_RE, fragment_resource, name="api_fragment"), # books by tags diff --git a/apps/catalogue/migrations/0017_auto__add_field_book_language__del_unique_book_slug__add_unique_book_s.py b/apps/catalogue/migrations/0017_auto__add_field_book_language__del_unique_book_slug__add_unique_book_s.py new file mode 100644 index 000000000..6d1edcfa1 --- /dev/null +++ b/apps/catalogue/migrations/0017_auto__add_field_book_language__del_unique_book_slug__add_unique_book_s.py @@ -0,0 +1,144 @@ +# 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'] + db.delete_unique('catalogue_book', ['slug']) + + # Adding field 'Book.language' + db.add_column('catalogue_book', 'language', self.gf('django.db.models.fields.CharField')(default='pol', max_length=3, db_index=True), keep_default=False) + + # Adding unique constraint on 'Book', fields ['slug', 'language'] + db.create_unique('catalogue_book', ['slug', 'language']) + + + def backwards(self, orm): + + # Removing unique constraint on 'Book', fields ['slug', 'language'] + db.delete_unique('catalogue_book', ['slug', 'language']) + + # Deleting field 'Book.language' + db.delete_column('catalogue_book', 'language') + + # Adding unique constraint on 'Book', fields ['slug'] + db.create_unique('catalogue_book', ['slug']) + + + 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',)", 'unique_together': "[['slug', 'language']]", 'object_name': 'Book'}, + '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'}), + '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', [], {'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.filerecord': { + 'Meta': {'ordering': "('-time', '-slug', '-type')", 'object_name': 'FileRecord'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'sha1': ('django.db.models.fields.CharField', [], {'max_length': '40'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'db_index': 'True'}), + 'time': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.CharField', [], {'max_length': '20', 'db_index': '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'}), + 'main_page': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': '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/0018_auto__del_filerecord.py b/apps/catalogue/migrations/0018_auto__del_filerecord.py new file mode 100644 index 000000000..66a6542a3 --- /dev/null +++ b/apps/catalogue/migrations/0018_auto__del_filerecord.py @@ -0,0 +1,131 @@ +# 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): + + # Deleting model 'FileRecord' + db.delete_table('catalogue_filerecord') + + + def backwards(self, orm): + + # Adding model 'FileRecord' + db.create_table('catalogue_filerecord', ( + ('sha1', self.gf('django.db.models.fields.CharField')(max_length=40)), + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('time', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), + ('type', self.gf('django.db.models.fields.CharField')(max_length=20, db_index=True)), + ('slug', self.gf('django.db.models.fields.SlugField')(max_length=120, db_index=True)), + )) + db.send_create_signal('catalogue', ['FileRecord']) + + + 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',)", 'unique_together': "[['slug', 'language']]", 'object_name': 'Book'}, + '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'}), + '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', [], {'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'}), + 'main_page': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': '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 b82879ba5..2ca78b01e 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -202,7 +202,7 @@ def get_customized_pdf_path(book, customizations): """ customizations.sort() h = hash(tuple(customizations)) - pdf_name = '%s-custom-%s' % (book.slug, h) + pdf_name = '%s-custom-%s' % (book.fileid(), h) pdf_file = models.get_dynamic_path(None, pdf_name, ext='pdf') return pdf_file @@ -211,7 +211,7 @@ def get_existing_customized_pdf(book): """ Returns a list of paths to generated customized pdf of a book """ - pdf_glob = '%s-custom-' % (book.slug,) + pdf_glob = '%s-custom-' % (book.fileid(),) 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)) @@ -250,7 +250,7 @@ class BookMedia(models.Model): super(BookMedia, self).save(*args, **kwargs) # remove the zip package for book with modified media - remove_zip(self.book.slug) + remove_zip(self.book.fileid()) extra_info = self.get_extra_info_value() extra_info.update(self.read_meta()) @@ -319,7 +319,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, unique=True, db_index=True) + 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) created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True) changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True) @@ -339,10 +341,14 @@ 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') @@ -350,6 +356,42 @@ 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 @@ -364,14 +406,18 @@ class Book(models.Model): @permalink def get_absolute_url(self): - return ('catalogue.views.book_detail', [self.slug]) + return ('catalogue.views.book_detail', [self.urlid()]) @property def name(self): return self.title def book_tag_slug(self): - return ('l-' + self.slug)[:120] + stem = 'l-' + self.slug + if self.language != settings.CATALOGUE_DEFAULT_LANGUAGE: + return stem[:116] + ' ' + self.language + else: + return stem[:120] def book_tag(self): slug = self.book_tag_slug() @@ -433,7 +479,7 @@ class Book(models.Model): formats = [] # files generated during publication if self.has_media("html"): - formats.append(u'%s' % (reverse('book_text', kwargs={'slug': self.slug}), _('Read online'))) + formats.append(u'%s' % (reverse('book_text', [self.fileid()]), _('Read online'))) if self.has_media("pdf"): formats.append(u'PDF' % self.get_media('pdf').url) if self.has_media("mobi"): @@ -493,37 +539,36 @@ class Book(models.Model): has_daisy_file.short_description = 'DAISY' has_daisy_file.boolean = True + def wldocument(self, parse_dublincore=True): + from catalogue.utils import ORMDocProvider + from librarian.parser import WLDocument + + return WLDocument.from_file(self.xml_file.path, + provider=ORMDocProvider(self), + parse_dublincore=parse_dublincore) + def build_pdf(self, customizations=None, file_name=None): """ (Re)builds the pdf file. customizations - customizations which are passed to LaTeX class file. file_name - save the pdf file under a different name and DO NOT save it in db. """ - from tempfile import NamedTemporaryFile from os import unlink from django.core.files import File - from librarian import pdf - from catalogue.utils import ORMDocProvider, remove_zip + from catalogue.utils import remove_zip - try: - pdf_file = NamedTemporaryFile(delete=False) - pdf.transform(ORMDocProvider(self), - file_path=str(self.xml_file.path), - output_file=pdf_file, - customizations=customizations - ) - - if file_name is None: - # 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.slug, File(open(pdf_file.name))) - self.pdf_file = current_self.pdf_file - else: - print "safing %s" % file_name - print "to: %s" % DefaultStorage().path(file_name) - DefaultStorage().save(file_name, File(open(pdf_file.name))) - finally: - unlink(pdf_file.name) + pdf = self.wldocument().as_pdf(customizations=customizations) + + if file_name is None: + # 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(), + File(open(pdf.get_filename()))) + self.pdf_file = current_self.pdf_file + else: + print "saving %s" % file_name + print "to: %s" % DefaultStorage().path(file_name) + DefaultStorage().save(file_name, File(open(pdf.get_filename()))) # remove cached downloadables remove_zip(settings.ALL_PDF_ZIP) @@ -534,22 +579,12 @@ class Book(models.Model): """ (Re)builds the MOBI file. """ - from tempfile import NamedTemporaryFile - from os import unlink from django.core.files import File - from librarian import mobi - from catalogue.utils import ORMDocProvider, remove_zip + from catalogue.utils import remove_zip - try: - mobi_file = NamedTemporaryFile(suffix='.mobi', delete=False) - mobi.transform(ORMDocProvider(self), verbose=1, - file_path=str(self.xml_file.path), - output_file=mobi_file.name, - ) + mobi = self.wldocument().as_mobi() - self.mobi_file.save('%s.mobi' % self.slug, File(open(mobi_file.name))) - finally: - unlink(mobi_file.name) + self.mobi_file.save('%s.mobi' % self.fileid(), File(open(mobi.get_filename()))) # remove zip with all mobi files remove_zip(settings.ALL_MOBI_ZIP) @@ -559,21 +594,18 @@ class Book(models.Model): If book has a parent, does nothing. Unless remove_descendants is False, descendants' epubs are removed. """ - from StringIO import StringIO - from hashlib import sha1 - from django.core.files.base import ContentFile - from librarian import epub, NoDublinCore - from catalogue.utils import ORMDocProvider, remove_zip + from django.core.files import File + from catalogue.utils import remove_zip if self.parent: # don't need an epub return - epub_file = StringIO() + epub = self.wldocument().as_epub() + try: - epub.transform(ORMDocProvider(self), self.slug, output_file=epub_file) - self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue())) - FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save() + epub.transform(ORMDocProvider(self), self.fileid(), output_file=epub_file) + self.epub_file.save('%s.epub' % self.fileid(), File(open(epub.get_filename()))) except NoDublinCore: pass @@ -590,19 +622,15 @@ class Book(models.Model): remove_zip(settings.ALL_EPUB_ZIP) def build_txt(self): - from StringIO import StringIO from django.core.files.base import ContentFile - from librarian import text - out = StringIO() - text.transform(open(self.xml_file.path), out) - self.txt_file.save('%s.txt' % self.slug, ContentFile(out.getvalue())) + text = self.wldocument().as_text() + self.txt_file.save('%s.txt' % self.fileid(), ContentFile(text.get_string())) def build_html(self): - from tempfile import NamedTemporaryFile from markupstring import MarkupString - from django.core.files import File + from django.core.files.base import ContentFile from slughifi import slughifi from librarian import html @@ -610,9 +638,10 @@ class Book(models.Model): category__in=('author', 'epoch', 'genre', 'kind'))) book_tag = self.book_tag() - html_file = NamedTemporaryFile() - if html.transform(self.xml_file.path, html_file, parse_dublincore=False): - self.html_file.save('%s.html' % self.slug, File(html_file)) + html_output = self.wldocument(parse_dublincore=False).as_html() + if html_output: + self.html_file.save('%s.html' % self.fileid(), + ContentFile(html_output.get_string())) # get ancestor l-tags for adding to new fragments ancestor_tags = [] @@ -662,7 +691,7 @@ class Book(models.Model): def pretty_file_name(book): return "%s/%s.%s" % ( b.get_extra_info_value()['author'], - b.slug, + b.fileid(), format_) field_name = "%s_file" % format_ @@ -676,7 +705,7 @@ class Book(models.Model): def zip_audiobooks(self): bm = BookMedia.objects.filter(book=self, type='mp3') paths = map(lambda bm: (None, bm.file.path), bm) - result = create_zip.delay(paths, self.slug) + result = create_zip.delay(paths, self.fileid()) return result.wait() @classmethod @@ -706,24 +735,27 @@ class Book(models.Model): children = [] if hasattr(book_info, 'parts'): for part_url in book_info.parts: - base, slug = part_url.rsplit('/', 1) try: - children.append(Book.objects.get(slug=slug)) + children.append(Book.objects.get( + slug=part_url.slug, language=part_url.language)) except Book.DoesNotExist, e: - raise Book.DoesNotExist(_('Book with slug = "%s" does not exist.') % slug) + raise Book.DoesNotExist(_('Book "%s/%s" does not exist.') % + (part_url.slug, part_url.language)) # Read book metadata - book_base, book_slug = book_info.url.rsplit('/', 1) + book_slug = book_info.url.slug + language = book_info.language if re.search(r'[^a-zA-Z0-9-]', book_slug): raise ValueError('Invalid characters in slug') - book, created = Book.objects.get_or_create(slug=book_slug) + book, created = Book.objects.get_or_create(slug=book_slug, language=language) if created: book_shelves = [] else: if not overwrite: - raise Book.AlreadyExists(_('Book %s already exists') % book_slug) + raise Book.AlreadyExists(_('Book %s/%s already exists') % ( + book_slug, language)) # Save shelves for this book book_shelves = list(book.tags.filter(category='set')) @@ -898,7 +930,8 @@ class Book(models.Model): """ books_by_parent = {} - books = cls.objects.all().order_by('parent_number', 'sort_key').only('title', 'parent', 'slug') + books = cls.objects.all().order_by('parent_number', 'sort_key').only( + 'title', 'parent', 'slug', 'language') if filter: books = books.filter(filter).distinct() book_ids = set((book.pk for book in books)) @@ -974,7 +1007,7 @@ class Fragment(models.Model): verbose_name_plural = _('fragments') def get_absolute_url(self): - return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor) + return '%s#m%s' % (self.book.get_html_url(), self.anchor) def reset_short_html(self): if self.id is None: @@ -1001,20 +1034,6 @@ class Fragment(models.Model): return mark_safe(short_html) -class FileRecord(models.Model): - slug = models.SlugField(_('slug'), max_length=120, db_index=True) - type = models.CharField(_('type'), max_length=20, db_index=True) - sha1 = models.CharField(_('sha-1 hash'), max_length=40) - time = models.DateTimeField(_('time'), auto_now_add=True) - - class Meta: - ordering = ('-time','-slug', '-type') - verbose_name = _('file record') - verbose_name_plural = _('file records') - - def __unicode__(self): - return "%s %s.%s" % (self.sha1, self.slug, self.type) - ########### # # SIGNALS diff --git a/apps/catalogue/test_utils.py b/apps/catalogue/test_utils.py index a5f0b4fef..70aae02d4 100644 --- a/apps/catalogue/test_utils.py +++ b/apps/catalogue/test_utils.py @@ -3,6 +3,7 @@ from django.test import TestCase import shutil import tempfile from slughifi import slughifi +from librarian import WLURI class WLTestCase(TestCase): """ @@ -41,12 +42,14 @@ class BookInfoStub(object): return dict((key, unicode(value)) for key, value in self.__dict.items()) -def info_args(title): +def info_args(title, language=None): """ generate some keywords for comfortable BookInfoCreation """ slug = unicode(slughifi(title)) + if language is None: + language = u'pol' return { 'title': unicode(title), - 'slug': slug, - 'url': u"http://wolnelektury.pl/example/%s" % slug, + 'url': WLURI.from_slug_and_lang(slug, language), '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 f4588d169..a97c41711 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -4,6 +4,7 @@ from __future__ import with_statement from django.core.files.base import ContentFile, File from catalogue.test_utils import * from catalogue import models +from librarian import WLURI from nose.tools import raises import tempfile @@ -14,13 +15,14 @@ class BookImportLogicTests(WLTestCase): def setUp(self): WLTestCase.setUp(self) self.book_info = BookInfoStub( - url=u"http://wolnelektury.pl/example/default-book", + url=WLURI.from_slug_and_lang(u"default-book", None), about=u"http://wolnelektury.pl/example/URI/default_book", title=u"Default Book", author=PersonStub(("Jim",), "Lazy"), kind="X-Kind", genre="X-Genre", epoch="X-Epoch", + language=u"pol", ) self.expected_tags = [ @@ -112,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 = "http://wolnelektury.pl/example/default_book" + self.book_info.url = WLURI.from_slug_and_lang(u"default_book", None) BOOK_TEXT = "" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) @@ -242,6 +244,38 @@ class ChildImportTests(WLTestCase): 'wrong related theme list') +class MultilingualBookImportTest(WLTestCase): + def setUp(self): + WLTestCase.setUp(self) + self.pol_info = BookInfoStub( + genre='X-Genre', + epoch='X-Epoch', + kind='X-Kind', + author=PersonStub(("Joe",), "Doe"), + **info_args("A book") + ) + + self.eng_info = BookInfoStub( + genre='X-Genre', + epoch='X-Epoch', + kind='X-Kind', + author=PersonStub(("Joe",), "Doe"), + **info_args("A book", "eng") + ) + + def test_multilingual_import(self): + BOOK_TEXT = """A""" + + book1 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.pol_info) + book2 = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.eng_info) + + self.assertEqual( + set([b.language for b in models.Book.objects.all()]), + set(['pol', 'eng']), + 'Books imported in wrong languages.' + ) + + class BookImportGenerateTest(WLTestCase): def setUp(self): WLTestCase.setUp(self) diff --git a/apps/catalogue/tests/files/fraszki.xml b/apps/catalogue/tests/files/fraszki.xml index edb29abbc..90e7c1245 100755 --- a/apps/catalogue/tests/files/fraszki.xml +++ b/apps/catalogue/tests/files/fraszki.xml @@ -12,7 +12,7 @@ Fraszka -http://wolnelektury.pl/lektura/fraszki +http://wolnelektury.pl/katalog/lektura/fraszki Domena publiczna - Jan Kochanowski zm. 1584 1584 diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 324217a3c..ada2681ea 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -4,19 +4,20 @@ # from django.conf.urls.defaults import * from catalogue.feeds import AudiobookFeed +from catalogue.models import Book urlpatterns = patterns('catalogue.views', url(r'^$', 'main_page', name='main_page'), 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[a-zA-Z0-9-0-]+)/usun$', 'remove_from_shelf', name='remove_from_shelf'), + url(r'^polki/(?P[a-zA-Z0-9-]+)/(?P%s)/usun$' % Book.URLID_RE, '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[a-zA-Z0-9-]+)/polki/', 'book_sets', name='book_shelves'), + url(r'^lektura/(?P%s)/polki/' % Book.URLID_RE, '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'), @@ -26,21 +27,21 @@ urlpatterns = patterns('catalogue.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/audiobook/(?P[a-zA-Z0-9-]+)\.zip', 'download_zip', {'format': 'audiobook'}, 'download_zip_audiobook'), + #url(r'^zip/audiobook/(?P%s)\.zip' % Book.FILEID_RE, 'download_zip', {'format': 'audiobook'}, 'download_zip_audiobook'), # tools url(r'^zegar/$', 'clock', name='clock'), # Public interface. Do not change this URLs. - url(r'^lektura/(?P[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'), - url(r'^lektura/(?P[a-zA-Z0-9-]+)/$', 'book_detail', name='book_detail'), - url(r'^lektura/(?P[a-zA-Z0-9-]+)/motyw/(?P[a-zA-Z0-9-]+)/$', + url(r'^lektura/(?P%s)\.html$' % Book.FILEID_RE, 'book_text', name='book_text'), + 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, '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[a-zA-Z0-9-]+).pdf', 'download_custom_pdf'), + url(r'^custompdf/(?P%s).pdf' % Book.FILEID_RE, 'download_custom_pdf'), ) diff --git a/apps/catalogue/utils.py b/apps/catalogue/utils.py index 3ffe9c0f6..acbd778cd 100644 --- a/apps/catalogue/utils.py +++ b/apps/catalogue/utils.py @@ -66,11 +66,12 @@ class ORMDocProvider(DocProvider): def __init__(self, book): self.book = book - def by_slug(self, slug): - if slug == self.book.slug: - return self.book.xml_file + 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).xml_file + return type(self.book).objects.get( + slug=slug, language=language).xml_file class LockFile(object): diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 2bc1f10aa..0722f39b7 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -204,9 +204,13 @@ def tagged_object_list(request, tags=''): ) -def book_fragments(request, book_slug, theme_slug): - book = get_object_or_404(models.Book, slug=book_slug) - book_tag = get_object_or_404(models.Tag, slug='l-' + book_slug, category='book') +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) + + book_tag = book.book_tag() theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme') fragments = models.Fragment.tagged.with_all([book_tag, theme]) @@ -215,11 +219,14 @@ def book_fragments(request, book_slug, theme_slug): context_instance=RequestContext(request)) -def book_detail(request, slug): +def book_detail(request, book): + kwargs = models.Book.split_urlid(book) + if kwargs is None: + raise Http404 try: - book = models.Book.objects.get(slug=slug) + book = models.Book.objects.get(**kwargs) except models.Book.DoesNotExist: - return pdcounter_views.book_stub_detail(request, slug) + return pdcounter_views.book_stub_detail(request, kwargs['slug']) book_tag = book.book_tag() tags = list(book.tags.filter(~Q(category='set'))) @@ -259,8 +266,12 @@ def book_detail(request, slug): context_instance=RequestContext(request)) -def book_text(request, slug): - book = get_object_or_404(models.Book, slug=slug) +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) + if not book.has_html_file(): raise Http404 book_themes = {} @@ -392,7 +403,7 @@ def books_starting_with(prefix): def find_best_matches(query, user=None): - """ Finds a Book, Tag, BookStub or Author best matching a query. + """ Finds a models.Book, Tag, models.BookStub or Author best matching a query. Returns a with: - zero elements when nothing is found, @@ -494,11 +505,15 @@ def user_shelves(request): context_instance=RequestContext(request)) @cache.never_cache -def book_sets(request, slug): +def book_sets(request, book): if not request.user.is_authenticated(): return HttpResponse(_('

To maintain your shelves you need to be logged in.

')) - book = get_object_or_404(models.Book, slug=slug) + kwargs = models.Book.split_urlid(book) + if kwargs is None: + raise Http404 + book = get_object_or_404(models.Book, **kwargs) + user_sets = models.Tag.objects.filter(category='set', user=request.user) book_sets = book.tags.filter(category='set', user=request.user) @@ -533,7 +548,11 @@ def book_sets(request, slug): @require_POST @cache.never_cache def remove_from_shelf(request, shelf, book): - book = get_object_or_404(models.Book, slug=book) + kwargs = models.Book.split_urlid(book) + if kwargs is None: + raise Http404 + book = get_object_or_404(models.Book, **kwargs) + shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user) if shelf in book.tags: @@ -586,15 +605,16 @@ def download_shelf(request, slug): already = set() for book in collect_books(models.Book.tagged.with_all(shelf)): + fileid = book.fileid() if 'pdf' in formats and book.pdf_file: filename = book.pdf_file.path - archive.write(filename, str('%s.pdf' % book.slug)) + archive.write(filename, str('%s.pdf' % fileid)) if 'mobi' in formats and book.mobi_file: filename = book.mobi_file.path - archive.write(filename, str('%s.mobi' % book.slug)) + archive.write(filename, str('%s.mobi' % fileid)) if book.root_ancestor not in already and 'epub' in formats and book.root_ancestor.epub_file: filename = book.root_ancestor.epub_file.path - archive.write(filename, str('%s.epub' % book.root_ancestor.slug)) + archive.write(filename, str('%s.epub' % book.root_ancestor.fileid())) already.add(book.root_ancestor) if 'odt' in formats and book.has_media("odt"): for file in book.get_media("odt"): @@ -602,7 +622,7 @@ def download_shelf(request, slug): archive.write(filename, str('%s.odt' % slughifi(file.name))) if 'txt' in formats and book.txt_file: filename = book.txt_file.path - archive.write(filename, str('%s.txt' % book.slug)) + archive.write(filename, str('%s.txt' % fileid)) archive.close() response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed') @@ -754,20 +774,26 @@ def tag_info(request, id): return HttpResponse(tag.description) -def download_zip(request, format, slug): +def download_zip(request, format, book=None): + kwargs = models.Book.split_fileid(book) + url = None if format in ('pdf', 'epub', 'mobi'): url = models.Book.zip_format(format) - elif format == 'audiobook' and slug is not None: - book = models.Book.objects.get(slug=slug) + elif format == 'audiobook' and kwargs is not None: + book = get_object_or_404(models.Book, **kwargs) url = book.zip_audiobooks() else: raise Http404('No format specified for zip package') return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?=')) -def download_custom_pdf(request, slug): - book = models.Book.objects.get(slug=slug) +def download_custom_pdf(request, book_fileid): + kwargs = models.Book.split_urlid(book) + if kwargs is None: + raise Http404 + book = get_object_or_404(models.Book, **kwargs) + if request.method == 'GET': form = forms.CustomPDFForm(request.GET) if form.is_valid(): @@ -777,7 +803,7 @@ def download_custom_pdf(request, slug): 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.slug), file_path=pdf_file, mimetype="application/pdf") + return AttachmentHttpResponse(file_name=("%s.pdf" % book_fileid), 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 e0b10f3c2..fc4ad66b3 100755 --- a/apps/dictionary/templates/dictionary/note_list.html +++ b/apps/dictionary/templates/dictionary/note_list.html @@ -44,7 +44,7 @@ {% endfor %} diff --git a/apps/lesmianator/urls.py b/apps/lesmianator/urls.py index e7bbb48d4..eeba6103f 100644 --- a/apps/lesmianator/urls.py +++ b/apps/lesmianator/urls.py @@ -3,11 +3,12 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls.defaults import * +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[a-zA-Z0-9-]+)/$', 'poem_from_book', name='poem_from_book'), + url(r'^lektura/(?P%s)/$' % Book.URLID_RE, '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 56acb5763..cebcf8bae 100644 --- a/apps/lesmianator/views.py +++ b/apps/lesmianator/views.py @@ -1,5 +1,6 @@ # Create your views here. +from django.http import Http404 from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.contrib.auth.decorators import login_required @@ -34,8 +35,11 @@ def new_poem(request): @cache.never_cache -def poem_from_book(request, slug): - book = get_object_or_404(Book, slug=slug) +def poem_from_book(request, book): + kwargs = Book.split_urlid(book) + if kwargs is None: + raise Http404 + book = get_object_or_404(Book, **kwargs) 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/lib/librarian b/lib/librarian index d7ba2c607..4077ffb3c 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit d7ba2c607dacf7a6136b83a1588b5adf2278ad46 +Subproject commit 4077ffb3cbd868df95239898563508b64e6d6ecf diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 45dfe3709..5b5d4fe0b 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -234,6 +234,8 @@ ALL_EPUB_ZIP = 'wolnelektury_pl_epub' ALL_PDF_ZIP = 'wolnelektury_pl_pdf' ALL_MOBI_ZIP = 'wolnelektury_pl_mobi' +CATALOGUE_DEFAULT_LANGUAGE = 'pol' + PAGINATION_INVALID_PAGE_RAISES_404 = True import djcelery diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index 5708b914e..2d83c733b 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -39,11 +39,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 %} @@ -66,7 +66,7 @@
{% trans "Dowload customized PDF" %}. {% endif %}

{% trans "Work's themes " %}

diff --git a/wolnelektury/templates/catalogue/book_sets.html b/wolnelektury/templates/catalogue/book_sets.html index 1eee61d21..f151650d1 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 8b36718e0..aced8bf48 100644 --- a/wolnelektury/templates/catalogue/book_short.html +++ b/wolnelektury/templates/catalogue/book_short.html @@ -1,7 +1,7 @@ {% load i18n %}
    {% if book.children.all|length %}
    diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index fec910530..f845c624f 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -93,7 +93,7 @@ {% for book in object_list %}
  3. {% if user_is_owner %} - {% trans "Delete" %} + {% trans "Delete" %} {% endif %} {{ book.short_html }}
  4. {% endfor %} diff --git a/wolnelektury/templates/lesmianator/poem.html b/wolnelektury/templates/lesmianator/poem.html index 03d1e9d03..23e943759 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: