1 # -*- coding: utf-8 -*-
2 from django import template
3 from django.template import Node, Variable
4 from django.utils.encoding import smart_str
5 from django.core.urlresolvers import reverse
8 register = template.Library()
21 return '%s%s' % (text[0].upper(), text[1:])
27 def title_from_tags(tags):
31 result[tag.category] = tag
34 class Flection(object):
35 def get_case(self, name, flection):
39 self = split_tags(tags)
43 # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka
44 # jest wybrana przez użytkownika
45 if 'epoch' in self and len(self) == 1:
46 text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik')
49 # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane
50 # są tylko rodzaj literacki i autor
51 if 'kind' in self and 'author' in self and len(self) == 2:
52 text = u'%s w twórczości %s' % (unicode(self['kind']),
53 flection.get_case(unicode(self['author']), u'dopełniacz'))
56 # Przypadki ogólniejsze
58 title += u'Motyw %s' % unicode(self['theme'])
62 title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik')
64 title += unicode(self['genre'])
66 if 'kind' in self or 'author' in self or 'epoch' in self:
67 if 'genre' in self or 'theme' in self:
69 title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik')
71 title += u' w twórczości '
73 title += u'%s ' % unicode(self.get('kind', u'twórczość'))
76 title += flection.get_case(unicode(self['author']), u'dopełniacz')
78 title += flection.get_case(unicode(self['epoch']), u'dopełniacz')
80 return capfirst(title)
84 def catalogue_url(parser, token):
85 bits = token.split_contents()
92 tags_to_remove.append(bit[1:])
94 tags_to_add.append(bit)
96 return CatalogueURLNode(tags_to_add, tags_to_remove)
99 class CatalogueURLNode(Node):
100 def __init__(self, tags_to_add, tags_to_remove):
101 self.tags_to_add = [Variable(tag) for tag in tags_to_add]
102 self.tags_to_remove = [Variable(tag) for tag in tags_to_remove]
104 def render(self, context):
108 for tag_variable in self.tags_to_add:
109 tag = tag_variable.resolve(context)
110 if isinstance(tag, (list, dict)):
111 tags_to_add += [t for t in tag]
113 tags_to_add.append(tag)
115 for tag_variable in self.tags_to_remove:
116 tag = tag_variable.resolve(context)
118 tags_to_remove += [t for t in tag]
120 tags_to_remove.append(tag)
122 tag_slugs = [tag.slug for tag in tags_to_add]
123 for tag in tags_to_remove:
125 tag_slugs.remove(tag.slug)
129 if len(tag_slugs) > 0:
130 return reverse('tagged_book_list', kwargs={'tags': '/'.join(tag_slugs)})
132 return reverse('main_page')
135 @register.inclusion_tag('catalogue/latest_blog_posts.html')
136 def latest_blog_posts(feed_url, posts_to_show=5):
140 feed = feedparser.parse(feed_url)
142 for i in range(posts_to_show):
143 pub_date = feed['entries'][i].updated_parsed
144 published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
146 'title': feed['entries'][i].title,
147 'summary': feed['entries'][i].summary,
148 'link': feed['entries'][i].link,
151 return {'posts': posts}