summaryrefslogtreecommitdiff
path: root/notification/feeds.py
blob: 7c1f46d0427a31ca476e2e050522deb47426e2ea (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
import datetime

from django.core.urlresolvers import reverse
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.template.defaultfilters import linebreaks, escape, striptags
from django.utils.translation import ugettext_lazy as _

from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from notification.models import Notice
from notification.atomformat import Feed


ITEMS_PER_FEED = getattr(settings, "ITEMS_PER_FEED", 20)
DEFAULT_HTTP_PROTOCOL = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")


class BaseNoticeFeed(Feed):
    
    def item_id(self, notification):
        return "%s://%s%s" % (
            DEFAULT_HTTP_PROTOCOL,
            Site.objects.get_current().domain,
            notification.get_absolute_url(),
        )
    
    def item_title(self, notification):
        return striptags(notification.message)
    
    def item_updated(self, notification):
        return notification.added
    
    def item_published(self, notification):
        return notification.added
    
    def item_content(self, notification):
        return {"type": "html"}, linebreaks(escape(notification.message))
    
    def item_links(self, notification):
        return [{"href": self.item_id(notification)}]
    
    def item_authors(self, notification):
        return [{"name": notification.recipient.username}]


class NoticeUserFeed(BaseNoticeFeed):
    
    def get_object(self, params):
        return get_object_or_404(User, username=params[0].lower())
    
    def feed_id(self, user):
        return "%s://%s%s" % (
            DEFAULT_HTTP_PROTOCOL,
            Site.objects.get_current().domain,
            reverse("notification_feed_for_user"),
        )
    
    def feed_title(self, user):
        return _("Notices Feed")
    
    def feed_updated(self, user):
        qs = Notice.objects.filter(recipient=user)
        # We return an arbitrary date if there are no results, because there
        # must be a feed_updated field as per the Atom specifications, however
        # there is no real data to go by, and an arbitrary date can be static.
        if qs.count() == 0:
            return datetime.datetime(year=2008, month=7, day=1)
        return qs.latest("added").added
    
    def feed_links(self, user):
        complete_url = "%s://%s%s" % (
            DEFAULT_HTTP_PROTOCOL,
            Site.objects.get_current().domain,
            reverse("notification_notices"),
        )
        return ({"href": complete_url},)
    
    def items(self, user):
        return Notice.objects.notices_for(user).order_by("-added")[:ITEMS_PER_FEED]