Merge branch 'master' of http://github.com/fnp/wolnelektury
[wolnelektury.git] / apps / catalogue / templatetags / catalogue_tags.py
index 51ee863..504ee69 100644 (file)
@@ -1,10 +1,17 @@
 # -*- coding: utf-8 -*-
+# 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
+
 from django import template
 from django.template import Node, Variable
 from django.utils.encoding import smart_str
 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
 
 
 register = template.Library()
@@ -37,6 +44,23 @@ def capfirst(text):
         return ''
 
 
+
+def simple_title(tags):
+    mapping = {
+        'author': u'Autor',
+        'theme': u'motyw',
+        'epoch': u'epoka',
+        'genre': u'gatunek',
+        'kind': u'rodzaj',
+        'set': u'półka',
+    }
+    
+    title = []
+    for tag in tags:
+        title.append("%s: %s" % (mapping[tag.category], tag.name))
+    return capfirst(', '.join(title))
+
+
 @register.simple_tag
 def title_from_tags(tags):
     def split_tags(tags):
@@ -45,6 +69,9 @@ def title_from_tags(tags):
             result[tag.category] = tag
         return result
     
+    # TODO: Remove this after adding flection mechanism
+    return simple_title(tags)
+    
     class Flection(object):
         def get_case(self, name, flection):
             return name
@@ -112,7 +139,11 @@ def authentication_form():
 def breadcrumbs(tags, search_form=True):
     from catalogue.forms import SearchForm
     context = {'tag_list': tags}
-    if search_form:
+    try:
+        max_tag_list = settings.MAX_TAG_LIST
+    except AttributeError:
+        max_tag_list = -1
+    if search_form and (max_tag_list == -1 or len(tags) < max_tag_list):
         context['search_form'] = SearchForm(tags=tags)
     return context
 
@@ -156,10 +187,10 @@ class CatalogueURLNode(Node):
             else:
                 tags_to_remove.append(tag)
             
-        tag_slugs = [tag.slug for tag in tags_to_add]
+        tag_slugs = [tag.url_chunk for tag in tags_to_add]
         for tag in tags_to_remove:
             try:
-                tag_slugs.remove(tag.slug)
+                tag_slugs.remove(tag.url_chunk)
             except KeyError:
                 pass
         
@@ -170,22 +201,22 @@ class CatalogueURLNode(Node):
 
 
 @register.inclusion_tag('catalogue/latest_blog_posts.html')
-def latest_blog_posts(feed_url, posts_to_show=5):
-    import feedparser
-    import datetime
-    
-    feed = feedparser.parse(feed_url)
-    posts = []
-    for i in range(posts_to_show):
-        pub_date = feed['entries'][i].updated_parsed
-        published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
-        posts.append({
-            'title': feed['entries'][i].title,
-            'summary': feed['entries'][i].summary,
-            'link': feed['entries'][i].link,
-            'date': published,
-            })
-    return {'posts': posts}
+def latest_blog_posts(feed_url, posts_to_show=5):    
+    try:
+        feed = feedparser.parse(str(feed_url))
+        posts = []
+        for i in range(posts_to_show):
+            pub_date = feed['entries'][i].updated_parsed
+            published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
+            posts.append({
+                'title': feed['entries'][i].title,
+                'summary': feed['entries'][i].summary,
+                'link': feed['entries'][i].link,
+                'date': published,
+                })
+        return {'posts': posts}
+    except:
+        return {'posts': []}
 
 
 @register.inclusion_tag('catalogue/tag_list.html')
@@ -196,3 +227,19 @@ def tag_list(tags, choices=None):
         one_tag = tags[0]
     return locals()
 
+
+@register.inclusion_tag('catalogue/folded_tag_list.html')
+def folded_tag_list(tags, choices=None):
+    if choices is None:
+        choices = []
+    some_tags_hidden = False
+    tag_count = len(tags)
+    
+    if tag_count == 1:
+        one_tag = tags[0]
+    else:
+        shown_tags = [tag for tag in tags if tag.main_page]
+        if tag_count > len(shown_tags):
+            some_tags_hidden = True
+    return locals()
+