From 45b36abf753366dd523cae85a8eced7e5c9711e0 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 23 Jan 2012 12:09:31 +0100 Subject: [PATCH] basic fragment choosing --- apps/catalogue/models.py | 15 ++++- apps/catalogue/templatetags/catalogue_tags.py | 39 ++++++++++++- wolnelektury/static/css/book_box.css | 57 +++++++++++-------- .../templates/catalogue/book_wide.html | 10 ++-- .../templates/catalogue/chosen_fragment.html | 11 ++++ .../catalogue/tagged_object_list.html | 9 +-- 6 files changed, 100 insertions(+), 41 deletions(-) create mode 100755 wolnelektury/templates/catalogue/chosen_fragment.html diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index ebecc36b4..9f26ae5b2 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -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)) diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index eeba74ee3..f6347e95d 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -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[^>]*>.*?' % tags_re, re.U) + value = tag_re.sub(u'', value) + return value diff --git a/wolnelektury/static/css/book_box.css b/wolnelektury/static/css/book_box.css index e03326384..7a1a2bcc5 100755 --- a/wolnelektury/static/css/book_box.css +++ b/wolnelektury/static/css/book_box.css @@ -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; +} diff --git a/wolnelektury/templates/catalogue/book_wide.html b/wolnelektury/templates/catalogue/book_wide.html index cd52ff182..da2754e8a 100644 --- a/wolnelektury/templates/catalogue/book_wide.html +++ b/wolnelektury/templates/catalogue/book_wide.html @@ -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 %}
-
- {% block quote %} -
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.
- {% endblock %} -
+
+ {% promo_fragment book %} +

{% trans "See" %}

diff --git a/wolnelektury/templates/catalogue/chosen_fragment.html b/wolnelektury/templates/catalogue/chosen_fragment.html new file mode 100755 index 000000000..a23638e98 --- /dev/null +++ b/wolnelektury/templates/catalogue/chosen_fragment.html @@ -0,0 +1,11 @@ +{% load i18n %} +{% load removewholetags from catalogue_tags %} + +{% if fragment %} + +
+ {{ fragment.text|removewholetags:"a"|truncatewords_html:15|safe }} +
+

Adam Mickiewicz, Dziady część III

+
+{% endif %} diff --git a/wolnelektury/templates/catalogue/tagged_object_list.html b/wolnelektury/templates/catalogue/tagged_object_list.html index 21aef82ce..443276d0a 100644 --- a/wolnelektury/templates/catalogue/tagged_object_list.html +++ b/wolnelektury/templates/catalogue/tagged_object_list.html @@ -59,14 +59,7 @@