Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added get_absolute_url for Notice and a single view that checks for t…

…hat owncer of the notice before displaying.

Added feed_for_user view that wraps Django's syndication framework. In conjunction with the basic_auth_decorator this enables the user to subscribe to his notifications with /notices/feed/.


git-svn-id: http://django-notification.googlecode.com/svn/trunk@88 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
commit 6f5dec0ddb5f02f46bffe39e3b9121701b4206d4 1 parent c473276
Jannis Leidel jezdez authored
71 notification/feeds.py
... ... @@ -0,0 +1,71 @@
  1 +from atomformat import Feed
  2 +from datetime import datetime
  3 +
  4 +from django.core.urlresolvers import reverse
  5 +from django.conf import settings
  6 +from django.contrib.sites.models import Site
  7 +from django.contrib.auth.models import User
  8 +from django.shortcuts import get_object_or_404
  9 +from django.template.defaultfilters import linebreaks, escape, striptags
  10 +from django.utils.translation import ugettext_lazy as _
  11 +
  12 +from notification.models import Notice
  13 +
  14 +ITEMS_PER_FEED = getattr(settings, 'ITEMS_PER_FEED', 20)
  15 +
  16 +class BaseNoticeFeed(Feed):
  17 + def item_id(self, notification):
  18 + return "http://%s%s" % (
  19 + Site.objects.get_current().domain,
  20 + notification.get_absolute_url(),
  21 + )
  22 +
  23 + def item_title(self, notification):
  24 + return striptags(notification.message)
  25 +
  26 + def item_updated(self, notification):
  27 + return notification.added
  28 +
  29 + def item_published(self, notification):
  30 + return notification.added
  31 +
  32 + def item_content(self, notification):
  33 + return {"type" : "html", }, linebreaks(escape(notification.message))
  34 +
  35 + def item_links(self, notification):
  36 + return [{"href" : self.item_id(notification)}]
  37 +
  38 + def item_authors(self, notification):
  39 + return [{"name" : notification.user.username}]
  40 +
  41 +class NoticeUserFeed(BaseNoticeFeed):
  42 + def get_object(self, params):
  43 + return get_object_or_404(User, username=params[0].lower())
  44 +
  45 + def feed_id(self, user):
  46 + return "http://%s%s" % (
  47 + Site.objects.get_current().domain,
  48 + reverse('notification_feed_for_user'),
  49 + )
  50 +
  51 + def feed_title(self, user):
  52 + return _('Notices Feed')
  53 +
  54 + def feed_updated(self, user):
  55 + qs = Notice.objects.filter(user=user)
  56 + # We return an arbitrary date if there are no results, because there
  57 + # must be a feed_updated field as per the Atom specifications, however
  58 + # there is no real data to go by, and an arbitrary date can be static.
  59 + if qs.count() == 0:
  60 + return datetime(year=2008, month=7, day=1)
  61 + return qs.latest('added').added
  62 +
  63 + def feed_links(self, user):
  64 + complete_url = "http://%s%s" % (
  65 + Site.objects.get_current().domain,
  66 + reverse('notification_notices'),
  67 + )
  68 + return ({'href': complete_url},)
  69 +
  70 + def items(self, user):
  71 + return Notice.objects.notices_for(user).order_by("-added")[:ITEMS_PER_FEED]
4 notification/models.py
@@ -140,6 +140,10 @@ class Meta:
140 140 ordering = ["-added"]
141 141 verbose_name = _("notice")
142 142 verbose_name_plural = _("notices")
  143 +
  144 + @models.permalink
  145 + def get_absolute_url(self):
  146 + return ("notification_notice", [str(self.pk)])
143 147
144 148 def create_notice_type(label, display, description, default=2):
145 149 """
4 notification/urls.py
@@ -2,9 +2,11 @@
2 2
3 3 # @@@ from atom import Feed
4 4
5   -from notification.views import notices, mark_all_seen
  5 +from notification.views import notices, mark_all_seen, feed_for_user, single
6 6
7 7 urlpatterns = patterns('',
8 8 url(r'^$', notices, name="notification_notices"),
  9 + url(r'^(\d+)/$', single, name="notification_notice"),
  10 + url(r'^feed/$', feed_for_user, name="notification_feed_for_user"),
9 11 url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),
10 12 )
24 notification/views.py
... ... @@ -1,9 +1,20 @@
1   -from django.shortcuts import render_to_response
2   -from django.http import HttpResponseRedirect
  1 +from django.core.urlresolvers import reverse
  2 +from django.shortcuts import render_to_response, get_object_or_404
  3 +from django.http import HttpResponseRedirect, Http404
3 4 from django.template import RequestContext
4 5 from django.contrib.auth.decorators import login_required
  6 +from django.contrib.syndication.views import feed
5 7
6 8 from notification.models import *
  9 +from notification.decorators import basic_auth_required, simple_basic_auth_callback
  10 +from notification.feeds import NoticeUserFeed
  11 +
  12 +@basic_auth_required(realm='Notices Feed', callback_func=simple_basic_auth_callback)
  13 +def feed_for_user(request):
  14 + url = "feed/%s" % request.user.username
  15 + return feed(request, url, {
  16 + "feed": NoticeUserFeed,
  17 + })
7 18
8 19 @login_required
9 20 def notices(request):
@@ -36,6 +47,15 @@ def notices(request):
36 47 }, context_instance=RequestContext(request))
37 48
38 49 @login_required
  50 +def single(request, id):
  51 + notice = get_object_or_404(Notice, id=id)
  52 + if request.user == notice.user:
  53 + return render_to_response("notification/single.html", {
  54 + "notice": notice,
  55 + }, context_instance=RequestContext(request))
  56 + raise Http404
  57 +
  58 +@login_required
39 59 def archive(request, noticeid=None, next_page=None):
40 60 if noticeid:
41 61 try:

0 comments on commit 6f5dec0

Please sign in to comment.
Something went wrong with that request. Please try again.