basic fragment choosing
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 23 Jan 2012 11:09:31 +0000 (12:09 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 23 Jan 2012 11:09:50 +0000 (12:09 +0100)
apps/catalogue/models.py
apps/catalogue/templatetags/catalogue_tags.py
wolnelektury/static/css/book_box.css
wolnelektury/templates/catalogue/book_wide.html
wolnelektury/templates/catalogue/chosen_fragment.html [new file with mode: 0755]
wolnelektury/templates/catalogue/tagged_object_list.html

index ebecc36..9f26ae5 100644 (file)
@@ -610,8 +610,9 @@ class Book(models.Model):
 
                 text = fragment.to_string()
                 short_text = ''
-                if (len(MarkupString(text)) > 240):
-                    short_text = unicode(MarkupString(text)[:160])
+                markup = MarkupString(text)
+                if (len(markup) > 240):
+                    short_text = unicode(markup[:160])
                 new_fragment = Fragment.objects.create(anchor=fragment.id, book=self,
                     text=text, short_text=short_text)
 
@@ -935,6 +936,16 @@ class Book(models.Model):
         audiences = sorted(set([self._audiences_pl[a] for a in audiences]))
         return [a[1] for a in audiences]
 
+    def choose_fragment(self):
+        tag = self.book_tag()
+        fragments = Fragment.tagged.with_any([tag])
+        if fragments.exists():
+            return fragments.order_by('?')[0]
+        elif self.parent:
+            return self.parent.choose_fragment()
+        else:
+            return None
+
 
 def _has_factory(ftype):
     has = lambda self: bool(getattr(self, "%s_file" % ftype))
index eeba74e..f6347e9 100644 (file)
@@ -2,8 +2,9 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-import feedparser
 import datetime
+import feedparser
+import re
 
 from django import template
 from django.template import Node, Variable
@@ -13,9 +14,11 @@ from django.core.urlresolvers import reverse
 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
 from django.db.models import Q
 from django.conf import settings
+from django.template.defaultfilters import stringfilter
 from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
+from catalogue.models import Book, Fragment
 
 register = template.Library()
 
@@ -291,6 +294,7 @@ def book_wide(context, book):
         'formats': formats,
         'extra_info': book.get_extra_info_value(),
         'request': context.get('request'),
+        'fragment': book.choose_fragment(),
     }
 
 
@@ -316,4 +320,35 @@ def work_list(context, object_list):
     request = context.get('request')
     if object_list:
         object_type = type(object_list[0]).__name__
-    return locals()
\ No newline at end of file
+    return locals()
+
+
+@register.inclusion_tag('catalogue/chosen_fragment.html')
+def promo_fragment(arg=None):
+    if arg is None:
+        fragments = Fragment.objects.all().order_by('?')
+        fragment = fragments[0] if fragments.exists() else None
+    elif isinstance(arg, Book):
+        fragment = arg.choose_fragment()
+    else:
+        fragments = Fragment.tagged.with_all(arg).order_by('?')
+        fragment = fragments[0] if fragments.exists() else None
+
+    return {
+        'fragment': fragment,
+    }
+
+
+@register.filter
+@stringfilter
+def removewholetags(value, tags):
+    """Removes a space separated list of [X]HTML tags from the output.
+
+    FIXME: It makes the assumption the removed tags aren't nested.
+
+    """
+    tags = [re.escape(tag) for tag in tags.split()]
+    tags_re = u'(%s)' % u'|'.join(tags)
+    tag_re = re.compile(ur'<%s[^>]*>.*?</\s*\1\s*>' % tags_re, re.U)
+    value = tag_re.sub(u'', value)
+    return value
index e033263..7a1a2bc 100755 (executable)
@@ -239,28 +239,6 @@ ul.book-box-tools {
     width: 41.5em;
 }
 
-.book-wide-box blockquote.cite-body {
-    /* @ 18pt */
-    width: 100%; /*23.055em;*/
-    height: 7.222em;
-    background-color: #f7f7f7;
-    margin: 0;
-    position: relative;
-    top: -0.444em;
-    right: -0.555em;
-    vertical-align: center;
-}
-
-.book-wide-box blockquote div.cite-text {
-    padding: 0.888em;
-}
-
-.book-wide-box blockquote p.cite-more {
-    display: inline;
-    font-size: 0.611em;
-    float: right;
-}
-
 ul.inline-items, ul.inline-items li {
     margin: 0;
     padding: 0;
@@ -315,6 +293,7 @@ ul.inline-items li {
 
 .unlike .if-like {
     display: none;
+}
 
 .snippets {
     width: 44em;
@@ -326,6 +305,38 @@ ul.inline-items li {
     margin: 1.083em 0em;
 }
 
-.snipptes .anchor {
+.snippets .anchor {
     display: none;
 }
+
+
+.cite blockquote p {
+    margin: 0;
+}
+
+.cite blockquote {
+    padding: 0;
+    margin: 0;
+}
+
+.book-wide-box .cite {
+    /* @ 18pt */
+    width: 100%; /*23.055em;*/
+    height: 7.222em;
+    background-color: #f7f7f7;
+    margin: 0;
+    position: relative;
+    top: -0.444em;
+    right: -0.555em;
+    vertical-align: center;
+}
+
+.abook-wide-box blockquote div.cite-text {
+    padding: 0.888em;
+}
+
+.abook-wide-box blockquote p.cite-more {
+    display: inline;
+    font-size: 0.611em;
+    float: right;
+}
index cd52ff1..da2754e 100644 (file)
@@ -1,16 +1,14 @@
 {% extends "catalogue/book_short.html" %}
 {% load i18n %}
+{% load promo_fragment from catalogue_tags %}
 
 {% block box-class %}book-wide-box{% endblock %}
 
 {% block right-column %}
 <div class="right-column">
-  <blockquote id="quote" class="cite-body">
-    {% block quote %}
-    <div>Ten, który walczy z potworami powinien zadbać, by sam nie stał się potworem. 
-    Gdy długo spoglądamy w otchłań, otchłań spogląda również w nas.</div>
-    {% endblock %}
-  </blockquote>
+    <div class="quote">
+  {% promo_fragment book %}
+  </div>
 
   <div class="other-tools">
     <h2 class="mono">{% trans "See" %}</h2>
diff --git a/wolnelektury/templates/catalogue/chosen_fragment.html b/wolnelektury/templates/catalogue/chosen_fragment.html
new file mode 100755 (executable)
index 0000000..a23638e
--- /dev/null
@@ -0,0 +1,11 @@
+{% load i18n %}
+{% load removewholetags from catalogue_tags %}
+
+{% if fragment %}
+<a href="{{ fragment.get_absolute_url }}" class="cite">
+    <blockquote class="cite-body">
+        {{ fragment.text|removewholetags:"a"|truncatewords_html:15|safe }}
+    </blockquote>
+    <p class="mono">Adam Mickiewicz, Dziady część III</p>
+</a>
+{% endif %}
index 21aef82..443276d 100644 (file)
 
 
     <div class="right-column">
-        <a href="" class="cite">
-            <div class="cite-body">
-            Dobranoc, obróć jeszcze raz na mnie oczęta,<br/>
-            (…) Daj mi pierś ucałować<br/>
-            Dobranoc, zapięta.
-            </div>
-            <p class="mono">Adam Mickiewicz, Dziady część III</p>
-        </a>
+        {% promo_fragment tags %}
 
         <div class="see-also">
             <h2 class='mono'>{% trans "See also" %}:</h2>