Added page "{{ theme }} in {{ book }}".
authorMarek Stępniowski <marek@stepniowski.com>
Thu, 16 Oct 2008 10:29:20 +0000 (12:29 +0200)
committerMarek Stępniowski <marek@stepniowski.com>
Thu, 16 Oct 2008 10:29:20 +0000 (12:29 +0200)
apps/catalogue/models.py
apps/catalogue/urls.py
apps/catalogue/views.py
wolnelektury/templates/catalogue/book_detail.html
wolnelektury/templates/catalogue/book_fragments.html [new file with mode: 0644]

index 81965ce..483fdda 100644 (file)
@@ -22,6 +22,7 @@ TAG_CATEGORIES = (
     ('genre', _('genre')),
     ('theme', _('theme')),
     ('set', _('set')),
+    ('book', _('book')),
 )
 
 
@@ -109,7 +110,7 @@ class Book(models.Model):
         if len(self._short_html):
             return mark_safe(self._short_html)
         else:
-            tags = self.tags.filter(~Q(category__in=('set', 'theme')))
+            tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
             tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
 
             formats = []
@@ -188,6 +189,15 @@ class Book(models.Model):
                 tag.category = category
                 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
         
         if hasattr(book_info, 'parts'):
@@ -197,7 +207,14 @@ class Book(models.Model):
                 child_book.parent = book
                 child_book.parent_number = n
                 child_book.save()
-        
+
+        book_descendants = list(book.children.all())
+        while len(book_descendants) > 0:
+            child_book = book_descendants.pop(0)
+            for fragment in child_book.fragments.all():
+                fragment.tags = set(list(fragment.tags) + [book_tag])
+            book_descendants += list(child_book.children.all())
+            
         # Save XML and HTML files
         book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
         
@@ -230,7 +247,7 @@ class Book(models.Model):
                         tag.save()
                     themes.append(tag)
                 new_fragment.save()
-                new_fragment.tags = list(book.tags) + themes
+                new_fragment.tags = set(list(book.tags) + themes + [book_tag])
                 book_themes += themes
             
             book_themes = set(book_themes)
index b24212e..dc8cc9b 100644 (file)
@@ -17,6 +17,8 @@ urlpatterns = patterns('catalogue.views',
     # Public interface. Do not change this URLs.
     url(r'^lektura/(?P<slug>[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'),
     url(r'^lektura/(?P<slug>[a-zA-Z0-9-]+)/$', 'book_detail', name='book_detail'),
+    url(r'^lektura/(?P<book_slug>[a-zA-Z0-9-]+)/motyw/(?P<theme_slug>[a-zA-Z0-9-]+)/$',
+        'book_fragments', name='book_fragments'),
     url(r'^(?P<tags>[a-zA-Z0-9-/]*)/$', 'tagged_object_list', name='tagged_object_list'),
 )
 
index f284e0a..1718d97 100644 (file)
@@ -67,6 +67,9 @@ def tagged_object_list(request, tags=''):
     except models.Tag.DoesNotExist:
         raise Http404
     
+    if len([tag for tag in tags if tag.category == 'book']):
+        raise Http404
+    
     model = models.Book
     shelf = [tag for tag in tags if tag.category == 'set']
     shelf_is_set = (len(tags) == 1 and tags[0].category == 'set')
@@ -76,7 +79,7 @@ def tagged_object_list(request, tags=''):
 
     user_is_owner = (len(shelf) and request.user.is_authenticated() and request.user == shelf[0].user)
     
-    extra_where = 'NOT catalogue_tag.category = "set"'
+    extra_where = 'catalogue_tag.category NOT IN ("set", "book")'
     related_tags = models.Tag.objects.related_for_model(tags, model, counts=True, extra={'where': [extra_where]})
     categories = split_tags(related_tags)
 
@@ -93,11 +96,25 @@ 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)
+    theme = get_object_or_404(models.Tag, slug=theme_slug)
+    fragments = models.Fragment.tagged.with_all([book_tag, theme])
+    
+    form = forms.SearchForm()
+    return render_to_response('catalogue/book_fragments.html', locals(),
+        context_instance=RequestContext(request))
+
+
 def book_detail(request, slug):
     book = get_object_or_404(models.Book, slug=slug)
+    book_tag = get_object_or_404(models.Tag, slug = 'l-' + slug)
     tags = list(book.tags.filter(~Q(category='set')))
     categories = split_tags(tags)
     book_children = book.children.all().order_by('parent_number')
+    extra_where = 'catalogue_tag.category = "theme"'
+    book_themes = models.Tag.objects.related_for_model(book_tag, models.Fragment, counts=True, extra={'where': [extra_where]})
     
     form = forms.SearchForm()
     return render_to_response('catalogue/book_detail.html', locals(),
@@ -124,9 +141,9 @@ def _tags_starting_with(prefix, user):
     books = models.Book.objects.filter(title__icontains=prefix)
     tags = models.Tag.objects.filter(name__icontains=prefix)
     if user.is_authenticated():
-        tags = tags.filter(~Q(category='set') | Q(user=user))
+        tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
     else:
-        tags = tags.filter(~Q(category='set'))
+        tags = tags.filter(~Q(category='book') & ~Q(category='set'))
 
     return list(books) + list(tags)
         
index 028ac62..9866053 100644 (file)
             </ul>
         </div>
         <div id="themes-list">
-            {% if categories.theme %}
-                <h2>Motywy w utworze</h2>
-                <ul>
-                {% for theme in categories.theme %}
-                    <li><a href="{{ theme.get_absolute_url }}">{{ theme }}</a></li>
-                {% endfor %}
-                </ul>
-            {% endif %}
+            <h2>Motywy w utworze</h2>
+            <ul>
+            {% for theme in book_themes %}
+                <li><a href="{% url book_fragments book.slug,theme.slug %}">{{ theme }} ({{ theme.count }})</a></li>
+            {% endfor %}
+            </ul>
         </div>
         <div class="clearboth"></div>
     </div>
diff --git a/wolnelektury/templates/catalogue/book_fragments.html b/wolnelektury/templates/catalogue/book_fragments.html
new file mode 100644 (file)
index 0000000..4f1c0eb
--- /dev/null
@@ -0,0 +1,37 @@
+{% extends "base.html" %}
+{% load catalogue_tags pagination_tags %}
+
+{% block title %}Motyw {{ theme }} w utworze {{ book }} w WolneLektury.pl{% endblock %}
+
+{% block bodyid %}tagged-object-list{% endblock %}
+
+{% block body %}
+    <h1>Motyw {{ theme }} w utworze {{ book }}</h1>
+    <form action="{% url search %}" method="get" accept-charset="utf-8" id="search-form">
+        <p>{{ form.q }} <input type="submit" value="Szukaj" /> <strong>lub</strong> <a href="{{ book.get_absolute_url }}">wróć do strony utworu</a></p>
+    </form>
+
+    {% autopaginate fragments 10 %}
+    <div id="books-list">
+        <ol>
+        {% for fragment in fragments %}
+            <li>{{ fragment.short_html }}</li>
+        {% endfor %}
+        </ol>
+        {% paginate %}
+    </div>
+    <div id="tags-list">
+        <div id="categories-list">
+            Zobacz opis <a href="{{ book.get_absolute_url }}">utworu {{ book }}</a>
+        </div>
+        <div id="themes-list">
+        </div>
+        <div class="clearboth"></div>
+    </div>
+    <div id="set-window">
+        <div class="header"><a href="#" class="jqmClose">Zamknij</a></div>
+        <div class="target">
+            <p><img src="/media/img/indicator.gif" alt="*"/> Ładowanie</p>
+        </div>
+    </div>
+{% endblock %}
\ No newline at end of file