Permalink
Browse files

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: https://django-notification.googlecode.com/svn/trunk@88 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information...
1 parent 451205c commit 959697bbb6fc43a8ce100b1b941c912ac7a6b21b @jezdez jezdez committed Jul 24, 2008
Showing with 100 additions and 3 deletions.
  1. +71 −0 notification/feeds.py
  2. +4 −0 notification/models.py
  3. +3 −1 notification/urls.py
  4. +22 −2 notification/views.py
View
@@ -0,0 +1,71 @@
+from atomformat import Feed
+from datetime import datetime
+
+from django.core.urlresolvers import reverse
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.contrib.auth.models import User
+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 notification.models import Notice
+
+ITEMS_PER_FEED = getattr(settings, 'ITEMS_PER_FEED', 20)
+
+class BaseNoticeFeed(Feed):
+ def item_id(self, notification):
+ return "http://%s%s" % (
+ 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.user.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 "http://%s%s" % (
+ 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(user=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(year=2008, month=7, day=1)
+ return qs.latest('added').added
+
+ def feed_links(self, user):
+ complete_url = "http://%s%s" % (
+ 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]
View
@@ -140,6 +140,10 @@ class Meta:
ordering = ["-added"]
verbose_name = _("notice")
verbose_name_plural = _("notices")
+
+ @models.permalink
+ def get_absolute_url(self):
+ return ("notification_notice", [str(self.pk)])
def create_notice_type(label, display, description, default=2):
"""
View
@@ -2,9 +2,11 @@
# @@@ from atom import Feed
-from notification.views import notices, mark_all_seen
+from notification.views import notices, mark_all_seen, feed_for_user, single
urlpatterns = patterns('',
url(r'^$', notices, name="notification_notices"),
+ url(r'^(\d+)/$', single, name="notification_notice"),
+ url(r'^feed/$', feed_for_user, name="notification_feed_for_user"),
url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),
)
View
@@ -1,9 +1,20 @@
-from django.shortcuts import render_to_response
-from django.http import HttpResponseRedirect
+from django.core.urlresolvers import reverse
+from django.shortcuts import render_to_response, get_object_or_404
+from django.http import HttpResponseRedirect, Http404
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
+from django.contrib.syndication.views import feed
from notification.models import *
+from notification.decorators import basic_auth_required, simple_basic_auth_callback
+from notification.feeds import NoticeUserFeed
+
+@basic_auth_required(realm='Notices Feed', callback_func=simple_basic_auth_callback)
+def feed_for_user(request):
+ url = "feed/%s" % request.user.username
+ return feed(request, url, {
+ "feed": NoticeUserFeed,
+ })
@login_required
def notices(request):
@@ -35,6 +46,15 @@ def notices(request):
"notice_settings": notice_settings,
}, context_instance=RequestContext(request))
+@login_required
+def single(request, id):
+ notice = get_object_or_404(Notice, id=id)
+ if request.user == notice.user:
+ return render_to_response("notification/single.html", {
+ "notice": notice,
+ }, context_instance=RequestContext(request))
+ raise Http404
+
@login_required
def archive(request, noticeid=None, next_page=None):
if noticeid:

0 comments on commit 959697b

Please sign in to comment.