From: Radek Czajka Date: Thu, 10 Jun 2010 15:05:11 +0000 (+0200) Subject: tests, epub, tag counters, l-tags X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/1615c0eec40aa73f7662469eaeb082ac14477d11?ds=inline;hp=--cc tests, epub, tag counters, l-tags added field for EPUB added tag, theme counters cache to Book books don't have their own l-tags now, they carry ancestors' instead added more tests some minor changes --- 1615c0eec40aa73f7662469eaeb082ac14477d11 diff --git a/apps/catalogue/admin.py b/apps/catalogue/admin.py index 8a718ffd0..b2744ee70 100644 --- a/apps/catalogue/admin.py +++ b/apps/catalogue/admin.py @@ -21,7 +21,7 @@ class TagAdmin(admin.ModelAdmin): class BookAdmin(TaggableModelAdmin): tag_model = Tag - list_display = ('title', 'slug', 'has_pdf_file', 'has_odt_file', 'has_html_file', 'has_description',) + list_display = ('title', 'slug', 'has_pdf_file', 'has_epub_file', 'has_odt_file', 'has_html_file', 'has_description',) search_fields = ('title',) ordering = ('title',) diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index 076282bf4..1a025ec17 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -71,6 +71,7 @@ FORMATS = ( ('pdf', 'PDF'), ('odt', 'ODT'), ('txt', 'TXT'), + ('epub', 'EPUB'), ) diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index 52aa69feb..c5fbb2e82 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -67,6 +67,10 @@ class Command(BaseCommand): book.pdf_file.save('%s.pdf' % book.slug, File(file(file_base + '.pdf'))) if verbose: print "Importing %s.pdf" % file_base + if os.path.isfile(file_base + '.epub'): + book.epub_file.save('%s.epub' % book.slug, File(file(file_base + '.epub'))) + if verbose: + print "Importing %s.epub" % file_base if os.path.isfile(file_base + '.odt'): book.odt_file.save('%s.odt' % book.slug, File(file(file_base + '.odt'))) if verbose: diff --git a/apps/catalogue/migrations/0006_epub_tag_counters_and_ltags_descendants.py b/apps/catalogue/migrations/0006_epub_tag_counters_and_ltags_descendants.py new file mode 100644 index 000000000..dd2e6a2ff --- /dev/null +++ b/apps/catalogue/migrations/0006_epub_tag_counters_and_ltags_descendants.py @@ -0,0 +1,186 @@ +# encoding: utf-8 +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + +def get_ltag(book, orm): + ltag, created = orm.Tag.objects.get_or_create(slug='l-' + book.slug, category='book') + if created: + ltag.name = book.title + ltag.sort_key = ('l-' + book.slug)[:120] + ltag.save() + return ltag + + +class Migration(SchemaMigration): + + def forwards(self, orm): + """ Add _tag_counter and make sure all books carry their ancestors' l-tags """ + + # Adding fields + db.add_column('catalogue_book', '_tag_counter', self.gf('catalogue.fields.JSONField')(default=''), keep_default=False) + db.add_column('catalogue_book', '_theme_counter', self.gf('catalogue.fields.JSONField')(default=''), keep_default=False) + db.add_column('catalogue_book', 'epub_file', self.gf('django.db.models.fields.files.FileField')(default='', max_length=100, blank=True), keep_default=False) + + def ltag_descendants(book, ltags=None): + if ltags is None: + ltags = [] + for tag in ltags: + orm.TagRelation(object_id=book.pk, tag=tag, content_type=book_ct).save() + print book, tag + ltag = get_ltag(book, orm) + for child in book.children.all(): + ltag_descendants(child, ltags + [ltag]) + + if not db.dry_run: + try: + book_ct = orm['contenttypes.contenttype'].objects.get(app_label='catalogue', model='book') + except: + return + # remove all l-tags on books + orm.TagRelation.objects.filter(content_type=book_ct, tag__category='book').delete() + for book in orm.Book.objects.filter(parent=None): + ltag_descendants(book) + + + def backwards(self, orm): + """ Delete _tag_counter and make sure books carry own l-tag. """ + + # Deleting fields + db.delete_column('catalogue_book', '_tag_counter') + db.delete_column('catalogue_book', '_theme_counter') + db.delete_column('catalogue_book', 'epub_file') + + if not db.dry_run: + try: + book_ct = orm['contenttypes.contenttype'].objects.get(app_label='catalogue', model='book') + except: + return + # remove all l-tags on books + orm.TagRelation.objects.filter(content_type=book_ct, tag__category='book').delete() + for book in orm.Book.objects.filter(parent=None): + orm.TagRelation(object_id=book.pk, tag=get_ltag(book, orm), content_type=book_ct).save() + + + 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']", 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'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']", 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}), + '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']", 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'catalogue.book': { + 'Meta': {'object_name': 'Book'}, + '_short_html': ('django.db.models.fields.TextField', [], {}), + '_short_html_de': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_en': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_es': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_fr': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_lt': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_pl': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_ru': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_uk': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_tag_counter': ('catalogue.fields.JSONField', [], {'default': "''"}), + '_theme_counter': ('catalogue.fields.JSONField', [], {'default': "''"}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': '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', [], {}), + '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'}), + 'mp3_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'odt_file': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'blank': 'True'}), + 'ogg_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'}), + '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.bookstub': { + 'Meta': {'object_name': 'BookStub'}, + 'author': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'pd': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'translator': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'translator_death': ('django.db.models.fields.TextField', [], {'blank': 'True'}) + }, + 'catalogue.fragment': { + 'Meta': {'object_name': 'Fragment'}, + '_short_html': ('django.db.models.fields.TextField', [], {}), + '_short_html_de': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_en': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_es': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_fr': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_lt': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_pl': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_ru': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '_short_html_uk': ('django.db.models.fields.TextField', [], {'null': True, 'blank': True}), + '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': {'object_name': 'Tag'}, + 'book_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'category': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'death': ('django.db.models.fields.IntegerField', [], {'null': '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', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120', 'db_index': 'True'}), + 'sort_key': ('django.db.models.fields.SlugField', [], {'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': {'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 e2b263659..55a2b64b7 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -119,6 +119,7 @@ class Book(models.Model): xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True) html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True) pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True) + epub_file = models.FileField(_('EPUB file'), upload_to=book_upload_path('epub'), blank=True) odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True) txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True) mp3_file = models.FileField(_('MP3 file'), upload_to=book_upload_path('mp3'), blank=True) @@ -129,6 +130,9 @@ class Book(models.Model): objects = models.Manager() tagged = managers.ModelTaggedItemManager(Tag) tags = managers.TagDescriptor(Tag) + + _tag_counter = JSONField(editable=False, default='') + _theme_counter = JSONField(editable=False, default='') class AlreadyExists(Exception): pass @@ -141,7 +145,7 @@ class Book(models.Model): def __unicode__(self): return self.title - def save(self, force_insert=False, force_update=False, reset_short_html=True): + def save(self, force_insert=False, force_update=False, reset_short_html=True, refresh_mp3=True): if reset_short_html: # Reset _short_html during save for key in filter(lambda x: x.startswith('_short_html'), self.__dict__): @@ -149,7 +153,7 @@ class Book(models.Model): book = super(Book, self).save(force_insert, force_update) - if self.mp3_file: + if refresh_mp3 and self.mp3_file: print self.mp3_file, self.mp3_file.path extra_info = self.get_extra_info_value() extra_info.update(self.get_mp3_info()) @@ -165,6 +169,16 @@ class Book(models.Model): @property def name(self): return self.title + + def book_tag(self): + slug = ('l-' + self.slug)[:120] + book_tag, created = Tag.objects.get_or_create(slug=slug) + if created: + book_tag.name = self.title[:50] + book_tag.sort_key = slug + book_tag.category = 'book' + book_tag.save() + return book_tag def short_html(self): key = '_short_html_%s' % get_language() @@ -181,6 +195,8 @@ class Book(models.Model): formats.append(u'%s' % (reverse('book_text', kwargs={'slug': self.slug}), _('Read online'))) if self.pdf_file: formats.append(u'PDF' % self.pdf_file.url) + if self.epub_file: + formats.append(u'EPUB' % self.epub_file.url) if self.odt_file: formats.append(u'ODT' % self.odt_file.url) if self.txt_file: @@ -215,6 +231,11 @@ class Book(models.Model): has_pdf_file.short_description = 'PDF' has_pdf_file.boolean = True + def has_epub_file(self): + return bool(self.epub_file) + has_epub_file.short_description = 'EPUB' + has_epub_file.boolean = True + def has_odt_file(self): return bool(self.odt_file) has_odt_file.short_description = 'ODT' @@ -276,16 +297,10 @@ class Book(models.Model): tag.save() book_tags.append(tag) - book_tag, created = Tag.objects.get_or_create(slug=('l-' + book.slug)[:120]) - if created: - book_tag.name = book.title[:50] - book_tag.sort_key = ('l-' + book.slug)[:120] - book_tag.category = 'book' - book_tag.save() - book_tags.append(book_tag) - book.tags = book_tags + book_tag = book.book_tag() + if hasattr(book_info, 'parts'): for n, part_url in enumerate(book_info.parts): base, slug = part_url.rsplit('/', 1) @@ -300,6 +315,8 @@ class Book(models.Model): book_descendants = list(book.children.all()) while len(book_descendants) > 0: child_book = book_descendants.pop(0) + child_book.tags = list(child_book.tags) + [book_tag] + child_book.save() for fragment in child_book.fragments.all(): fragment.tags = set(list(fragment.tags) + [book_tag]) book_descendants += list(child_book.children.all()) @@ -344,6 +361,42 @@ class Book(models.Model): book.save() return book + + + def refresh_tag_counter(self): + tags = {} + for child in self.children.all().order_by(): + for tag_pk, value in child.tag_counter.iteritems(): + tags[tag_pk] = tags.get(tag_pk, 0) + value + for tag in self.tags.exclude(category__in=('book', 'theme', 'set')).order_by(): + tags[tag.pk] = 1 + self.set__tag_counter_value(tags) + self.save(reset_short_html=False, refresh_mp3=False) + return tags + + @property + def tag_counter(self): + if self._tag_counter == '': + return self.refresh_tag_counter() + return dict((int(k), v) for k, v in self.get__tag_counter_value().iteritems()) + #return self.get__tag_counter_value() + + def refresh_theme_counter(self): + tags = {} + for fragment in Fragment.tagged.with_any([self.book_tag()]).order_by(): + for tag in fragment.tags.filter(category='theme').order_by(): + tags[tag.pk] = tags.get(tag.pk, 0) + 1 + self.set__theme_counter_value(tags) + self.save(reset_short_html=False, refresh_mp3=False) + return tags + + @property + def theme_counter(self): + if self._theme_counter == '': + return self.refresh_theme_counter() + return dict((int(k), v) for k, v in self.get__theme_counter_value().iteritems()) + return self.get__theme_counter_value() + class Fragment(models.Model): diff --git a/apps/catalogue/tests.py b/apps/catalogue/tests.py index 068939806..198aee8a9 100644 --- a/apps/catalogue/tests.py +++ b/apps/catalogue/tests.py @@ -80,9 +80,11 @@ class PersonStub(object): self.first_names = first_names self.last_name = last_name +from slughifi import slughifi + class BookInfoStub(object): - def __init__(self, **kwargs): + def __init__(self, **kwargs): self.__dict = kwargs def __setattr__(self, key, value): @@ -96,6 +98,15 @@ class BookInfoStub(object): def to_dict(self): return dict((key, unicode(value)) for key, value in self.__dict.items()) +def info_args(title): + """ generate some keywords for comfortable BookInfoCreation """ + slug = unicode(slughifi(title)) + return {'title': unicode(title), + 'slug': slug, + 'url': u"http://wolnelektury.pl/example/%s" % slug, + 'about': u"http://wolnelektury.pl/example/URI/%s" % slug, + } + class BookImportLogicTests(TestCase): def setUp(self): @@ -111,13 +122,19 @@ class BookImportLogicTests(TestCase): self.expected_tags = [ ('author', 'jim-lazy'), - ('book', 'l-default_book'), ('genre', 'x-genre'), ('epoch', 'x-epoch'), ('kind', 'x-kind'), ] self.expected_tags.sort() + def tearDown(self): + for book in models.Book.objects.all(): + if book.xml_file: + book.xml_file.delete() + if book.html_file: + book.html_file.delete() + def test_empty_book(self): BOOK_TEXT = "" book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) @@ -140,6 +157,21 @@ class BookImportLogicTests(TestCase): tags.sort() self.assertEqual(tags, self.expected_tags) + + def test_not_quite_empty_book(self): + """ Not empty, but without any real text. + + Should work like any other non-empty book. + """ + + BOOK_TEXT = """ + + Nic + + """ + + book = models.Book.from_text_and_meta(ContentFile(BOOK_TEXT), self.book_info) + self.assertTrue(book.has_html_file()) def test_book_with_fragment(self): BOOK_TEXT = """ @@ -186,53 +218,55 @@ class BookImportLogicTests(TestCase): -class BooksByTagFlat(TestCase): +class BooksByTagTests(TestCase): + """ tests the /katalog/tag page for found books """ + def setUp(self): - self.tag_empty = models.Tag(name='Empty tag', slug='empty', category='author') - self.tag_common = models.Tag(name='Common author', slug='common', category='author') - - self.tag_kind1 = models.Tag(name='Type 1', slug='type1', category='kind') - self.tag_kind2 = models.Tag(name='Type 2', slug='type2', category='kind') - self.tag_kind3 = models.Tag(name='Type 3', slug='type3', category='kind') - for tag in self.tag_empty, self.tag_common, self.tag_kind1, self.tag_kind2, self.tag_kind3: - tag.save() + author = PersonStub(("Common",), "Man") + tags = dict(genre='G', epoch='E', author=author, kind="K") + + # grandchild + kwargs = info_args(u"GChild") + kwargs.update(tags) + gchild_info = BookInfoStub(**kwargs) + # child + kwargs = info_args(u"Child") + kwargs.update(tags) + child_info = BookInfoStub(parts=[gchild_info.url], **kwargs) + # other grandchild + kwargs = info_args(u"Different GChild") + kwargs.update(tags) + diffgchild_info = BookInfoStub(**kwargs) + # other child + kwargs = info_args(u"Different Child") + kwargs.update(tags) + kwargs['kind'] = 'K2' + diffchild_info = BookInfoStub(parts=[diffgchild_info.url], **kwargs) + # parent + kwargs = info_args(u"Parent") + kwargs.update(tags) + parent_info = BookInfoStub(parts=[child_info.url, diffchild_info.url], **kwargs) + + # create the books + book_file = ContentFile('') + for info in gchild_info, child_info, diffgchild_info, diffchild_info, parent_info: + book = models.Book.from_text_and_meta(book_file, info) + + # useful tags + self.author = models.Tag.objects.get(name='Common Man', category='author') + tag_empty = models.Tag(name='Empty tag', slug='empty', category='author') + tag_empty.save() - - self.parent = models.Book(title='Parent', slug='parent') - self.parent.save() - - self.similar_child = models.Book(title='Similar child', - slug='similar_child', - parent=self.parent) - self.similar_child.save() - self.similar_grandchild = models.Book(title='Similar grandchild', - slug='similar_grandchild', - parent=self.similar_child) - self.similar_grandchild.save() - for book in self.parent, self.similar_child, self.similar_grandchild: - book.tags = [self.tag_common, self.tag_kind1] - book.save() - - self.different_child = models.Book(title='Different child', - slug='different_child', - parent=self.parent) - self.different_child.save() - self.different_child.tags = [self.tag_common, self.tag_kind2] - self.different_child.save() - self.different_grandchild = models.Book(title='Different grandchild', - slug='different_grandchild', - parent=self.different_child) - self.different_grandchild.save() - self.different_grandchild.tags = [self.tag_common, self.tag_kind3] - self.different_grandchild.save() - + self.client = Client() + + + def tearDown(self): for book in models.Book.objects.all(): - l_tag = models.Tag(name=book.title, slug='l-'+book.slug, category='book') - l_tag.save() - book.tags = list(book.tags) + [l_tag] - + if book.xml_file: + book.xml_file.delete() + if book.html_file: + book.html_file.delete() - self.client = Client() def test_nonexistent_tag(self): """ Looking for a non-existent tag should yield 404 """ @@ -243,15 +277,136 @@ class BooksByTagFlat(TestCase): self.assertEqual(404, self.client.get('/katalog/parent/').status_code) def test_tag_empty(self): - """ Tag with no books should return no books and no related tags """ + """ Tag with no books should return no books """ context = self.client.get('/katalog/empty/').context self.assertEqual(0, len(context['object_list'])) - self.assertEqual(0, len(context['categories'])) def test_tag_common(self): - """ Filtering by tag should only yield top-level books """ - context = self.client.get('/katalog/%s/' % self.tag_common.slug).context - self.assertEqual(list(context['object_list']), - [self.parent]) + """ Filtering by tag should only yield top-level books. """ + context = self.client.get('/katalog/%s/' % self.author.slug).context + self.assertEqual([book.title for book in context['object_list']], + ['Parent']) + + def test_tag_child(self): + """ Filtering by child's tag should yield the child """ + context = self.client.get('/katalog/k2/').context + self.assertEqual([book.title for book in context['object_list']], + ['Different Child']) + + def test_tag_child_jump(self): + """ Of parent and grandchild, only parent should be returned. """ + context = self.client.get('/katalog/k/').context + self.assertEqual([book.title for book in context['object_list']], + ['Parent']) + +class TagRelatedTagsTests(TestCase): + """ tests the /katalog/tag/ page for related tags """ + + def setUp(self): + author = PersonStub(("Common",), "Man") + + gchild_info = BookInfoStub(author=author, genre="GchildGenre", epoch='Epoch', kind="Kind", + **info_args(u"GChild")) + child1_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind", + parts=[gchild_info.url], + **info_args(u"Child1")) + child2_info = BookInfoStub(author=author, genre="ChildGenre", epoch='Epoch', kind="ChildKind", + **info_args(u"Child2")) + parent_info = BookInfoStub(author=author, genre="Genre", epoch='Epoch', kind="Kind", + parts=[child1_info.url, child2_info.url], + **info_args(u"Parent")) + + for info in gchild_info, child1_info, child2_info, parent_info: + book_text = """ + + Theme, %sTheme + Ala ma kąta + + + """ % info.title.encode('utf-8') + book = models.Book.from_text_and_meta(ContentFile(book_text), info) + book.save() + + tag_empty = models.Tag(name='Empty tag', slug='empty', category='author') + tag_empty.save() + + self.client = Client() + + + def tearDown(self): + for book in models.Book.objects.all(): + if book.xml_file: + book.xml_file.delete() + if book.html_file: + book.html_file.delete() + + + def test_empty(self): + """ empty tag should have no related tags """ + + cats = self.client.get('/katalog/empty/').context['categories'] + self.assertEqual(cats, {}, 'tags related to empty tag') + + + def test_has_related(self): + """ related own and descendants' tags should be generated """ + + cats = self.client.get('/katalog/kind/').context['categories'] + self.assertTrue('Common Man' in [tag.name for tag in cats['author']], + 'missing `author` related tag') + self.assertTrue('Epoch' in [tag.name for tag in cats['epoch']], + 'missing `epoch` related tag') + self.assertTrue("ChildKind" in [tag.name for tag in cats['kind']], + "missing `kind` related tag") + self.assertTrue("Genre" in [tag.name for tag in cats['genre']], + 'missing `genre` related tag') + self.assertTrue("ChildGenre" in [tag.name for tag in cats['genre']], + "missing child's related tag") + self.assertTrue("GchildGenre" in [tag.name for tag in cats['genre']], + "missing grandchild's related tag") + self.assertTrue('Theme' in [tag.name for tag in cats['theme']], + "missing related theme") + self.assertTrue('Child1Theme' in [tag.name for tag in cats['theme']], + "missing child's related theme") + self.assertTrue('GChildTheme' in [tag.name for tag in cats['theme']], + "missing grandchild's related theme") + + + def test_related_differ(self): + """ related tags shouldn't include filtering tags """ + + cats = self.client.get('/katalog/kind/').context['categories'] + self.assertFalse('Kind' in [tag.name for tag in cats['kind']], + 'filtering tag wrongly included in related') + cats = self.client.get('/katalog/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 """ + + cats = self.client.get('/katalog/kind/').context['categories'] + self.assertEqual([(tag.name, tag.count) for tag in cats['epoch']], + [('Epoch', 1)], + 'wrong related tag epoch tag on tag page') + + + def test_siblings_tags_add(self): + """ if children have tags and parent hasn't, count the children """ + + cats = self.client.get('/katalog/epoch/').context['categories'] + self.assertTrue(('ChildKind', 2) in [(tag.name, tag.count) for tag in cats['kind']], + 'wrong related kind tags on tag page') + + def test_themes_add(self): + """ all occurencies of theme should be counted """ + + cats = self.client.get('/katalog/epoch/').context['categories'] + self.assertTrue(('Theme', 4) in [(tag.name, tag.count) for tag in cats['theme']], + 'wrong related theme count') + + + diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 127648b66..4d8ee7e57 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -8,6 +8,8 @@ import sys import pprint import traceback import re +import itertools +from operator import itemgetter from django.conf import settings from django.template import RequestContext @@ -98,7 +100,7 @@ def tagged_object_list(request, tags=''): if shelf_tags: books = models.Book.tagged.with_all(shelf_tags).order_by() - l_tags = [models.Tag.objects.get(slug='l-' + book.slug) for book in books] + l_tags = [book.book_tag() for book in books] fragments = models.Fragment.tagged.with_any(l_tags, fragments) # newtagging goes crazy if we just try: @@ -114,26 +116,29 @@ def tagged_object_list(request, tags=''): objects = fragments else: - books = models.Book.tagged.with_all(tags).order_by() - l_tags = [models.Tag.objects.get(slug='l-' + book.slug) for book in books] - book_keys = [book.pk for book in books] - # newtagging goes crazy if we just try: - #related_tags = models.Tag.objects.usage_for_queryset(books, counts=True, - # extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]}) - if book_keys: - related_tags = models.Book.tags.usage(counts=True, - filters={'pk__in': book_keys}, - extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]}) - categories = split_tags(related_tags) - - fragment_keys = [fragment.pk for fragment in models.Fragment.tagged.with_any(l_tags)] - if fragment_keys: - categories['theme'] = models.Fragment.tags.usage(counts=True, - filters={'pk__in': fragment_keys}, - extra={'where': ["catalogue_tag.category = 'theme'"]}) - - books = books.exclude(parent__in=book_keys) - objects = books + # get relevant books and their tags + objects = models.Book.tagged.with_all(tags).order_by() + l_tags = [book.book_tag() for book in objects] + # eliminate descendants + descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)] + if descendants_keys: + objects = objects.exclude(pk__in=descendants_keys) + + # get related tags from `tag_counter` and `theme_counter` + related_counts = {} + tags_pks = [tag.pk for tag in tags] + for book in objects: + for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()): + if tag_pk in tags_pks: + continue + related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value + related_tags = models.Tag.objects.filter(pk__in=related_counts.keys()) + related_tags = [tag for tag in related_tags if tag not in tags] + for tag in related_tags: + tag.count = related_counts[tag.pk] + + categories = split_tags(related_tags) + del related_tags if not objects: only_author = len(tags) == 1 and tags[0].category == 'author' @@ -174,7 +179,7 @@ def book_detail(request, slug): except models.Book.DoesNotExist: return book_stub_detail(request, slug) - book_tag = get_object_or_404(models.Tag, slug='l-' + slug) + book_tag = book.book_tag() tags = list(book.tags.filter(~Q(category='set'))) categories = split_tags(tags) book_children = book.children.all().order_by('parent_number') @@ -445,7 +450,7 @@ def download_shelf(request, slug): if form.is_valid(): formats = form.cleaned_data['formats'] if len(formats) == 0: - formats = ['pdf', 'odt', 'txt', 'mp3', 'ogg'] + formats = ['pdf', 'epub', 'odt', 'txt', 'mp3', 'ogg'] # Create a ZIP archive temp = tempfile.TemporaryFile() @@ -455,6 +460,9 @@ def download_shelf(request, slug): if 'pdf' in formats and book.pdf_file: filename = book.pdf_file.path archive.write(filename, str('%s.pdf' % book.slug)) + if 'epub' in formats and book.epub_file: + filename = book.epub_file.path + archive.write(filename, str('%s.epub' % book.slug)) if 'odt' in formats and book.odt_file: filename = book.odt_file.path archive.write(filename, str('%s.odt' % book.slug)) @@ -485,11 +493,13 @@ def shelf_book_formats(request, shelf): """ shelf = get_object_or_404(models.Tag, slug=shelf, category='set') - formats = {'pdf': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False} + formats = {'pdf': False, 'epub': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False} for book in collect_books(models.Book.tagged.with_all(shelf)): if book.pdf_file: formats['pdf'] = True + if book.epub_file: + formats['epub'] = True if book.odt_file: formats['odt'] = True if book.txt_file: diff --git a/wolnelektury/templates/catalogue/book_detail.html b/wolnelektury/templates/catalogue/book_detail.html index e85f1938e..7963fe6a9 100644 --- a/wolnelektury/templates/catalogue/book_detail.html +++ b/wolnelektury/templates/catalogue/book_detail.html @@ -33,6 +33,9 @@ {% if book.pdf_file %} {% trans "Download PDF" %} {% endif %} + {% if book.epub_file %} + {% trans "Download EPUB" %} + {% endif %} {% if book.odt_file %} {% trans "Download ODT" %} {% endif %} diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index fd6517c49..fe8abc86c 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -34,6 +34,7 @@

{% trans "Choose books' formats which you want to download:" %}

  • {% trans "for reading" %} {% trans "and printing using" %} Adobe Reader
  • +
  • {% trans "for reading" %} {% trans "and editing using" %} OpenOffice.org
  • {% trans "for reading" %} {% trans "on small displays, for example mobile phones" %}
  • {% trans "for listening" %} {% trans "on favourite MP3 player" %}