93b39cef3a4c0f2f213e89afd4986370841676b5
[prawokultury.git] / migdal / feeds.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.contrib.syndication.views import Feed
6 from django.core.urlresolvers import reverse
7 from django.shortcuts import get_object_or_404
8 from django.utils.translation import ugettext as _
9 from migdal import api
10 from migdal.models import Category
11 from migdal.settings import TYPES_DICT
12
13
14 class EntriesFeed(Feed):
15     def get_object(self, request, type_db=None, category_slug=None):
16         lang = request.LANGUAGE_CODE
17         if category_slug:
18             category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
19         else:
20             category = None
21         if type_db:
22             entry_type = TYPES_DICT[type_db]
23         return {'entry_type': entry_type, 'category': category}
24
25     def title(self, obj):
26         t = "Prawo kultury, " + _("latest") + " "
27         if obj['entry_type']:
28             t += unicode(obj['entry_type'].slug)
29         else:
30             t += _("entries")
31         if obj['category']:
32             t += " " + _("in category") + " " + obj['category'].title
33         return t
34
35     def link(self, obj):
36         if obj['category']:
37             return reverse('migdal_category', args=[obj['category'].slug])
38         if obj['entry_type']:
39             return reverse('migdal_entry_list_%s' % obj['entry_type'].db)
40         return reverse('migdal_main')
41
42     def items(self, obj):
43         return api.entry_list(**obj)
44
45     def item_title(self, item):
46         return item.title
47
48     def item_description(self, item):
49         return item.lead