summaryrefslogtreecommitdiffstats
path: root/feeds.py
blob: dd57b5a9cf328dc6ca156c1cba373ffae10e63f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from datetime import datetime, time
from pytz import utc

from django.core.exceptions import ObjectDoesNotExist
from django.contrib.admin.models import ADDITION, DELETION
from django.contrib.sites.models import Site
from django.contrib.syndication.views import Feed
from django.db import connection
from django.db.models import Q
from django.urls import reverse
from django.utils.feedgenerator import Rss201rev2Feed
from django.views.decorators.http import condition

from main.models import Arch, Repo, Package
from news.models import News
from packages.models import Update
from releng.models import Release
from planet.models import FeedItem


class BatchWritesWrapper(object):
    def __init__(self, outfile):
        self.outfile = outfile
        self.buf = []

    def write(self, s):
        buf = self.buf
        buf.append(s)
        if len(buf) >= 40:
            self.outfile.write(b''.join(buf))
            self.buf = []

    def flush(self):
        self.outfile.write(b''.join(self.buf))
        self.outfile.flush()


class FasterRssFeed(Rss201rev2Feed):
    def write(self, outfile, encoding):
        '''
        Batch the underlying 'write' calls on the outfile because Python's
        default saxutils XmlGenerator is a POS that insists on unbuffered
        write/flush calls. This sucks when it is making 1-byte calls to write
        '>' closing tags and over 1600 write calls in our package feed.
        '''
        wrapper = BatchWritesWrapper(outfile)
        super(FasterRssFeed, self).write(wrapper, encoding)
        wrapper.flush()


def package_last_modified(request, *args, **kwargs):
    try:
        return Package.objects.latest('last_update').last_update
    except ObjectDoesNotExist:
        return


class PackageFeed(Feed):
    feed_type = FasterRssFeed

    link = '/packages/'

    def __call__(self, request, *args, **kwargs):
        wrapper = condition(last_modified_func=package_last_modified)
        return wrapper(super(PackageFeed, self).__call__)(request, *args, **kwargs)

    __name__ = 'package_feed'

    def get_object(self, request, arch='', repo=''):
        obj = dict()
        qs = Package.objects.normal().order_by('-last_update')

        if arch != '':
            # feed for a single arch, also include 'any' packages everywhere
            a = Arch.objects.get(name=arch)
            qs = qs.filter(Q(arch=a) | Q(arch__agnostic=True))
            obj['arch'] = a
        if repo != '':
            if repo == 'stable-repos':
                # feed for a single arch AND all stable repos
                r = Repo.objects.filter(testing=False, staging=False)
                qs = qs.filter(repo__in=r)
                obj['repos'] = r
                setattr(obj['repos'], 'name', 'all stable repositories')
            elif repo == 'testing-repos':
                # feed for a single arch AND all testing repos
                r = Repo.objects.filter(testing=True, staging=False)
                qs = qs.filter(repo__in=r)
                obj['repos'] = r
                setattr(obj['repos'], 'name', 'all testing repositories')
            else:
                # feed for a single arch AND repo
                r = Repo.objects.get(name__iexact=repo)
                qs = qs.filter(repo=r)
                obj['repo'] = r
        else:
            qs = qs.filter(repo__staging=False)
        obj['qs'] = qs[:50]
        return obj

    def title(self, obj):
        s = 'Arch Linux: Recent package updates'
        fields = dict(
            arch=obj['arch'].name if 'arch' in obj else None,
            repo='[%s]' % obj['repo'].name.lower() if 'repo' in obj else None,
            repos=obj['repos'].name if 'repos' in obj else None,)

        if fields['arch']:
            if fields['repo']:
                s += ' (%(arch)s in %(repo)s)'
            elif fields['repos']:
                s += ' (%(arch)s in %(repos)s)'
            else:
                s += ' (%(arch)s)'

        elif fields['repo']:
            s += ' in %(repo)s'

        elif fields['repos']:
            s += ' in %(repos)s'

        return s % fields

    def description(self, obj):
        s = 'Recently updated packages'

        fields = dict(
            arch=obj['arch'].name if 'arch' in obj else None,
            repo='[%s]' % obj['repo'].name.lower() if 'repo' in obj else None,
            repos=', '.join(['[%s]' % r.name.lower() for r in obj['repos'].all()]) if 'repos' in obj else None,)

        if fields['arch']:
            s += ' for the \'%(arch)s\' architecture'
            if not obj['arch'].agnostic:
                s += ' (including \'any\' packages)'
        if fields['repo']:
            s += ' in the Arch Linux %(repo)s repository'
        elif fields['repos']:
            s += ' in the %(repos)s repositories'
        else:
            s += ' in the Arch Linux package repositories'
        s += '.'
        return s % fields

    subtitle = description

    def items(self, obj):
        return obj['qs']

    item_guid_is_permalink = False

    def item_guid(self, item):
        # http://diveintomark.org/archives/2004/05/28/howto-atom-id
        date = item.last_update
        return 'tag:%s,%s:%s%s' % (Site.objects.get_current().domain,
                                   date.strftime('%Y-%m-%d'), item.get_absolute_url(),
                                   date.strftime('%Y%m%d%H%M'))

    def item_pubdate(self, item):
        return item.last_update

    def item_title(self, item):
        return '%s %s %s' % (item.pkgname, item.full_version, item.arch.name)

    def item_description(self, item):
        return item.pkgdesc

    def item_categories(self, item):
        return (item.repo.name, item.arch.name)


def removal_last_modified(request, *args, **kwargs):
    try:
        return Update.objects.latest('created').created
    except ObjectDoesNotExist:
        return


class PackageUpdatesFeed(Feed):
    feed_type = FasterRssFeed
    link = '/packages/'

    def __call__(self, request, *args, **kwargs):
        wrapper = condition(last_modified_func=removal_last_modified)
        return wrapper(super(PackageUpdatesFeed, self).__call__)(request, *args, **kwargs)

    __name__ = 'packages_updates_feed'

    def get_object(self, request, operation='', arch='', repo=''):
        obj = dict()

        if 'added' in request.path:
            flag = ADDITION
            obj['action'] = 'added'
        elif 'removed' in request.path:
            flag = DELETION
            obj['action'] = 'removed'

        qs = Update.objects.filter(action_flag=flag).order_by('-created')

        if arch != '':
            # feed for a single arch, also include 'any' packages everywhere
            a = Arch.objects.get(name=arch)
            qs = qs.filter(Q(arch=a) | Q(arch__agnostic=True))
            obj['arch'] = a
        if repo != '':
            if repo == 'stable-repos':
                # feed for a single arch AND all stable repos
                r = Repo.objects.filter(testing=False, staging=False)
                qs = qs.filter(repo__in=r)
                obj['repos'] = r
                setattr(obj['repos'], 'name', 'all stable repositories')
            elif repo == 'testing-repos':
                # feed for a single arch AND all testing repos
                r = Repo.objects.filter(testing=True, staging=False)
                qs = qs.filter(repo__in=r)
                obj['repos'] = r
                setattr(obj['repos'], 'name', 'all testing repositories')
            else:
                # feed for a single arch AND repo
                r = Repo.objects.get(name__iexact=repo)
                qs = qs.filter(repo=r)
                obj['repo'] = r
        else:
            qs = qs.filter(repo__staging=False)

        obj['qs'] = qs[:50]
        return obj

    def title(self, obj):
        s = 'Arch Linux: Recently %(action)s packages' % obj

        fields = dict(
            arch=obj['arch'].name if 'arch' in obj else None,
            repo='[%s]' % obj['repo'].name.lower() if 'repo' in obj else None,
            repos=obj['repos'].name if 'repos' in obj else None,)

        if fields['arch']:
            if fields['repo']:
                s += ' (%(arch)s in %(repo)s)'
            elif fields['repos']:
                s += ' (%(arch)s in %(repos)s)'
            else:
                s += ' (%(arch)s)'

        elif fields['repo']:
            s += ' in %(repo)s'

        elif fields['repos']:
            s += ' in %(repos)s'

        return s % fields

    def description(self, obj):
        s = 'Recently %(action)s packages' % obj

        fields = dict(
            arch=obj['arch'].name if 'arch' in obj else None,
            repo='[%s]' % obj['repo'].name.lower() if 'repo' in obj else None,
            repos=', '.join(['[%s]' % r.name.lower() for r in obj['repos'].all()]) if 'repos' in obj else None,)

        if fields['arch']:
            s += ' for the \'%(arch)s\' architecture'
            if not obj['arch'].agnostic:
                s += ' (including \'any\' packages)'
        if fields['repo']:
            s += ' in the Arch Linux %(repo)s repository'
        elif fields['repos']:
            s += ' in the %(repos)s repositories'
        else:
            s += ' in the Arch Linux package repositories'
        s += '.'
        return s % fields

    subtitle = description

    def items(self, obj):
        return obj['qs']

    item_guid_is_permalink = False

    def item_guid(self, item):
        # http://diveintomark.org/archives/2004/05/28/howto-atom-id
        date = item.created
        return 'tag:%s,%s:%s%s' % (Site.objects.get_current().domain,
                                   date.strftime('%Y-%m-%d'), item.get_absolute_url(),
                                   date.strftime('%Y%m%d%H%M'))

    def item_pubdate(self, item):
        return item.created

    def item_title(self, item):
        return '%s %s' % (item.pkgname, item.arch.name)

    def item_description(self, item):
        return item.pkgname

    def item_categories(self, item):
        return (item.repo.name, item.arch.name)


def news_last_modified(request, *args, **kwargs):
    cursor = connection.cursor()
    cursor.execute("SELECT MAX(last_modified) FROM news")
    return cursor.fetchone()[0]


class NewsFeed(Feed):
    feed_type = FasterRssFeed

    title = 'Arch Linux: Recent news updates'
    link = '/news/'
    description = 'The latest and greatest news from the Arch Linux distribution.'
    subtitle = description

    def __call__(self, request, *args, **kwargs):
        wrapper = condition(last_modified_func=news_last_modified)
        return wrapper(super(NewsFeed, self).__call__)(request, *args, **kwargs)

    __name__ = 'news_feed'

    def items(self):
        return News.objects.select_related('author').order_by('-postdate', '-id')[:10]

    item_guid_is_permalink = False

    def item_guid(self, item):
        return item.guid

    def item_pubdate(self, item):
        return item.postdate

    def item_updateddate(self, item):
        return item.last_modified

    def item_author_name(self, item):
        return item.author.get_full_name()

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.html()


class ReleaseFeed(Feed):
    feed_type = FasterRssFeed

    title = 'Arch Linux: Releases'
    link = '/download/'
    description = 'Release ISOs'
    subtitle = description

    __name__ = 'release_feed'

    def items(self):
        return Release.objects.filter(available=True)[:10]

    def item_title(self, item):
        return item.version

    def item_description(self, item):
        return item.info_html()

    def item_pubdate(self, item):
        return datetime.combine(item.release_date, time()).replace(tzinfo=utc)

    def item_updateddate(self, item):
        return item.last_modified

    item_guid_is_permalink = False

    def item_guid(self, item):
        # http://diveintomark.org/archives/2004/05/28/howto-atom-id
        date = item.release_date
        return 'tag:%s,%s:%s' % (Site.objects.get_current().domain,
                                 date.strftime('%Y-%m-%d'), item.get_absolute_url())

    def item_enclosure_url(self, item):
        domain = Site.objects.get_current().domain
        proto = 'https'
        # Use archweb internal link, as the rsync job might not have been
        # running and RSS torrent clients do not retry failed urls often.
        return "%s://%s/%s" % (proto, domain, reverse('releng-release-torrent', args=[item.version]))

    def item_enclosure_length(self, item):
        if item.torrent_data:
            torrent = item.torrent()
            return torrent['file_length'] or ""
        return ""

    item_enclosure_mime_type = 'application/x-bittorrent'


class PlanetFeed(Feed):
    feed_type = FasterRssFeed

    title = 'Planet Arch Linux'
    link = '/planet/'
    description = 'Planet Arch Linux is a window into the world, work and lives of Arch Linux hackers and developers'
    subtitle = description

    def __call__(self, request, *args, **kwargs):
        wrapper = condition(last_modified_func=planet_last_modified)
        return wrapper(super(PlanetFeed, self).__call__)(request, *args, **kwargs)

    __name__ = 'planet_feed'

    def items(self):
        return FeedItem.objects.filter().order_by('-publishdate')

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.summary

    def item_pubdate(self, item):
        return datetime.combine(item.publishdate, time()).replace(tzinfo=utc)

    def item_updateddate(self, item):
        return item.publishdate

    item_guid_is_permalink = False

    def item_guid(self, item):
        # http://diveintomark.org/archives/2004/05/28/howto-atom-id
        date = item.publishdate
        return 'tag:%s,%s:%s' % (Site.objects.get_current().domain,
                                 date.strftime('%Y-%m-%d'), item.url)


def planet_last_modified(request, *args, **kwargs):
    try:
        return FeedItem.objects.latest().publishdate
    except FeedItem.DoesNotExist:
        pass

# vim: set ts=4 sw=4 et: