~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wiki/feeds.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
2
 
from django.utils.feedgenerator import Atom1Feed
3
 
from django.contrib.contenttypes.models import ContentType
4
 
from django.core.exceptions import ObjectDoesNotExist
5
 
from django.core.urlresolvers import reverse
6
 
from django.utils.translation import ugettext_lazy as _
7
 
from django.shortcuts import get_object_or_404, render_to_response
8
 
from django.template import Context, Template
9
 
from django.template.loader import get_template
 
1
from django.contrib.syndication.views import Feed, FeedDoesNotExist
10
2
from wiki.models import ChangeSet, Article
11
 
from wiki.utils import get_ct
12
3
from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
13
4
 
14
 
ALL_ARTICLES = Article.objects.all()
15
 
ALL_CHANGES = ChangeSet.objects.all()
 
5
# Validated through http://validator.w3.org/feed/
 
6
 
16
7
 
17
8
class RssHistoryFeed(Feed):
18
9
 
19
10
    feed_type = Rss201rev2Feed
20
11
    title = 'History for all articles'
21
 
    link = '/wiki/feeds/rss'
22
12
    description = 'Recent changes in wiki'
23
 
 
24
 
    def __init__(self, request,
25
 
                 group_slug=None, group_slug_field=None, group_qs=None, 
26
 
                 article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES, 
27
 
                 extra_context=None, 
28
 
                 title_template = u'feeds/history_title.html', 
29
 
                 description_template = u'feeds/history_description.html', 
30
 
                 *args, **kw):
31
 
 
32
 
        if  group_slug is not None:
33
 
            group = get_object_or_404(group_qs, 
34
 
                                      **{group_slug_field : group_slug})
35
 
            self.changes_qs = changes_qs.filter(article__content_type=get_ct(group), 
36
 
                                                article__object_id=group.id)
37
 
        else:
38
 
            self.changes_qs = changes_qs
39
 
 
40
 
        self.title_template = title_template
41
 
        self.description_template = description_template
42
 
        super(RssHistoryFeed, self).__init__('', request)
 
13
    link = '/wiki/feeds/rss/'
 
14
    title_template = 'wiki/feeds/history_title.html'
 
15
    description_template = 'wiki/feeds/history_description.html'
43
16
 
44
17
    def items(self):
45
 
        return self.changes_qs.order_by('-modified')[:30]
46
 
        
 
18
        return ChangeSet.objects.order_by('-modified')[:30]
 
19
 
47
20
    def item_pubdate(self, item):
48
 
        """
49
 
        Return the item's pubdate. It's this modified date
 
21
        """Return the item's pubdate.
 
22
    
 
23
        It's this modified date
 
24
    
50
25
        """
51
26
        return item.modified
52
27
 
53
 
    def item_author_name(self, item):
54
 
        """
55
 
        Takes the object returned by get_object and returns the feeds's
56
 
        auhor's name as a Python string
57
 
        """
58
 
        if item.is_anonymous_change():
59
 
            return _("Anonymous")
60
 
        return item.editor.username
 
28
# Validated through http://validator.w3.org/feed/
61
29
 
62
30
 
63
31
class AtomHistoryFeed(RssHistoryFeed):
64
32
 
65
33
    feed_type = Atom1Feed
66
 
    feed_subtitle = 'Recent changes in wiki'
67
 
    link = '/wiki/feeds/atom'
68
 
 
69
 
    def item_id(self, item):
70
 
        return "%s" % item.id
71
 
 
72
 
    def item_title(self, item):
73
 
        c = Context({'obj' : item})
74
 
        return self.title_template.render(c)
75
 
 
76
 
    def item_updated(self, item):
 
34
    subtitle = 'Recent changes in wiki'
 
35
    link = '/wiki/feeds/atom/'
 
36
 
 
37
    def item_updateddate(self, item):
77
38
        return item.modified
78
39
 
79
 
    def item_authors(self, item):
80
 
        if item.is_anonymous_change():
81
 
            return [{'name' : _('Anonimous')},]
82
 
        return [{'name' : item.editor.username},]
83
 
 
84
 
    def item_links(self, item):
85
 
        return [{'href': item.get_absolute_url()}, ]
86
 
 
87
 
    def item_content(self, item):
88
 
        c = Context({'obj' : item,})
89
 
        return ({'type': 'html'}, self.description_template.render(c))
90
 
 
91
 
    def item_author_name(self, item):
92
 
        """
93
 
        Takes the object returned by get_object and returns the feeds's
94
 
        auhor's name as a Python string
95
 
        """
96
 
        if item.is_anonymous_change():
97
 
            return _("Anonymous")
98
 
        return item.editor.username
 
40
# Validated through http://validator.w3.org/feed/
99
41
 
100
42
 
101
43
class RssArticleHistoryFeed(Feed):
102
 
 
103
44
    feed_type = Rss201rev2Feed
104
 
    def __init__(self, title, request, 
105
 
                group_slug=None, group_slug_field=None, group_qs=None,
106
 
                article_qs=ALL_ARTICLES, changes_qs=ALL_CHANGES,
107
 
                extra_context=None,
108
 
                title_template = u'feeds/history_title.html',
109
 
                description_template = u'feeds/history_description.html',
110
 
                *args, **kw):
111
 
 
112
 
        if  group_slug is not None:
113
 
            group = get_object_or_404(group_qs,
114
 
                                      **{group_slug_field : group_slug})
115
 
            self.article_qs = article_qs.filter(content_type=get_ct(group),
116
 
                                           object_id=group.id)
117
 
        else:
118
 
            self.article_qs = article_qs
119
 
 
120
 
        self.title_template = title_template
121
 
        self.description_template = description_template
122
 
        super(RssArticleHistoryFeed, self).__init__(title, request)
123
 
 
124
 
    def get_object(self, bits):
125
 
        return self.article_qs.get(title = bits[0])
126
 
 
127
 
    def title(self, obj):
128
 
        return "History for: %s " % obj.title
129
 
 
130
 
    def link(self, obj):
131
 
        if not obj:
 
45
    title_template = 'wiki/feeds/history_title.html'
 
46
    description_template = 'wiki/feeds/history_description.html'
 
47
 
 
48
    def get_object(self, request, *args, **kwargs):
 
49
        return Article.objects.get(title=kwargs['title'])
 
50
 
 
51
    def title(self, item):
 
52
        return 'History for: %s ' % item.title
 
53
 
 
54
    def link(self, item):
 
55
        if not item:
132
56
            raise FeedDoesNotExist
133
 
        return obj.get_absolute_url()
134
 
 
135
 
    def description(self, obj):
136
 
        return "Recent changes in %s" % obj.title
137
 
 
138
 
    def items(self, obj):
139
 
        return ChangeSet.objects.filter(article__id__exact=obj.id).order_by('-modified')[:30]
 
57
        return item.get_absolute_url()
 
58
 
 
59
    def description(self, item):
 
60
        return 'Recent changes in %s' % item.title
 
61
 
 
62
    def items(self, item):
 
63
        return ChangeSet.objects.filter(article__id__exact=item.id).order_by('-modified')[:30]
140
64
 
141
65
    def item_pubdate(self, item):
142
 
        """
143
 
        Returns the modified date
144
 
        """
 
66
        """Returns the modified date."""
145
67
        return item.modified
146
68
 
 
69
# Validated through http://validator.w3.org/feed/
 
70
 
147
71
 
148
72
class AtomArticleHistoryFeed(RssArticleHistoryFeed):
149
73
    feed_type = Atom1Feed
150
74
 
151
 
    def get_object(self, bits):
152
 
        # We work around a bug here which is likely in atomformat.py; 
153
 
        # when the Article doesn't exist this throws an Exception. We
154
 
        # will care for this by first checking for the Article
155
 
        get_object_or_404(Article,title=bits[0])
156
 
        
157
 
        return self.article_qs.get(title = bits[0])
158
 
 
159
 
 
160
 
    def feed_title(self, obj):
161
 
        return "History for: %s " % obj.title
162
 
 
163
 
    def feed_subtitle(self, obj):
164
 
        return "Recent changes in %s" % obj.title
165
 
 
166
 
    def feed_id(self):
167
 
        return "feed_id"
168
 
 
169
 
    def item_id(self, item):
170
 
        return "%s" % item.id
171
 
 
172
 
    def item_title(self, item):
173
 
        c = Context({'obj' : item})
174
 
        return self.title_template.render(c)
175
 
 
176
 
    def item_updated(self, item):
 
75
    def subtitle(self, item):
 
76
        return 'Recent changes in %s' % item.title
 
77
 
 
78
    def item_updateddate(self, item):
177
79
        return item.modified
178
 
 
179
 
    def item_authors(self, item):
180
 
        if item.is_anonymous_change():
181
 
            return [{'name' : _('Anonimous')},]
182
 
        return [{'name' : item.editor.username},]
183
 
 
184
 
    def item_links(self, item):
185
 
        return [{'href': item.get_absolute_url()},]
186
 
 
187
 
    def item_content(self, item):
188
 
        c = Context({'obj' : item, })
189
 
        return ({'type': 'html'}, self.description_template.render(c))