More tolerance for bad url metadata.
[wolnelektury.git] / apps / catalogue / templatetags / catalogue_tags.py
index 6ff60a3..fd8c8c4 100644 (file)
@@ -5,6 +5,7 @@
 import datetime
 import feedparser
 from random import randint
+from urlparse import urlparse
 
 from django.conf import settings
 from django import template
@@ -15,7 +16,7 @@ from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
 from django.utils.translation import ugettext as _
 
 from catalogue.utils import related_tag_name as _related_tag_name
-from catalogue.models import Book, BookMedia, Fragment, Tag
+from catalogue.models import Book, BookMedia, Fragment, Tag, Source
 from catalogue.constants import LICENSES
 
 register = template.Library()
@@ -326,6 +327,7 @@ def book_wide(context, book):
     book_themes = book.related_themes()
     extra_info = book.extra_info
     hide_about = extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl')
+    stage_note, stage_note_url = book.stage_note()
 
     return {
         'book': book,
@@ -336,17 +338,23 @@ def book_wide(context, book):
         'themes': book_themes,
         'request': context.get('request'),
         'show_lang': book.language_code() != settings.LANGUAGE_CODE,
+        'stage_note': stage_note,
+        'stage_note_url': stage_note_url,
     }
 
 
 @register.inclusion_tag('catalogue/book_short.html', takes_context=True)
 def book_short(context, book):
+    stage_note, stage_note_url = book.stage_note()
+
     return {
         'book': book,
         'main_link': book.get_absolute_url(),
         'related': book.related_info(),
         'request': context.get('request'),
         'show_lang': book.language_code() != settings.LANGUAGE_CODE,
+        'stage_note': stage_note,
+        'stage_note_url': stage_note_url,
     }
 
 
@@ -365,9 +373,6 @@ def book_mini(book, with_link=True):
 @register.inclusion_tag('catalogue/work-list.html', takes_context=True)
 def work_list(context, object_list):
     request = context.get('request')
-    for obj in object_list:
-        obj.object_type = type(obj).__name__
-
     return locals()
 
 
@@ -472,7 +477,18 @@ def related_tag_name(tag, lang=None):
     return _related_tag_name(tag, lang)
 
 
-@register.simple_tag
+@register.filter
 def class_name(obj):
     return obj.__class__.__name__
 
+
+@register.simple_tag
+def source_name(url):
+    url = url.lstrip()
+    netloc = urlparse(url).netloc
+    if not netloc:
+        netloc = urlparse('http://' + url).netloc
+    if not netloc:
+        return ''
+    source, created = Source.objects.get_or_create(netloc=netloc)
+    return source.name or netloc