Skip to content

Instantly share code, notes, and snippets.

@jpic
Created September 19, 2011 16:05
Show Gist options
  • Select an option

  • Save jpic/1226841 to your computer and use it in GitHub Desktop.

Select an option

Save jpic/1226841 to your computer and use it in GitHub Desktop.
# context processor
def acknowledge(request):
if not request.user.is_authenticated():
return {}
if 'acknowledge' in request.GET.keys():
b = RedisBackend()
b.acknowledge(request.user, request.GET['acknowledge'])
return {}
# facebook.com/notifications
def notification_list(request, queue,
template_name='scoobet/notification_list.html', extra_context=None):
b = RedisBackend()
notification_list = b.get_notifications(request.user, clear_undelivered=True, queues=[queue], group_by=['day'])
# commented in favor of group_by['day']
#notification_list = sorted(notification_list, key=lambda n: n['timestamp'])
context = {
'notification_list': notification_list,
'today': datetime.date.today()
}
context.update(extra_context or {})
return shortcuts.render(request, template_name, context)
# javascript live updater
def user_stream_json(request):
if not request.user.is_authenticated():
return http.HttpResponseForbidden()
b = RedisBackend()
# javascript wants group by queue, not by state
context = b.get_notifications(request.user, clear_undelivered=True, group_by=['queue'], minimal_unserialisation=True, limit=7, annotate_count=True)
for g in context.keys():
#for v in context[g]:
# not serializable, commented if favor of minimal_unserialisation
#del v['datetime']
# oldest first, i might not need that anymore
context[g].reverse()
return http.HttpResponse(simplejson.dumps(context))
def auto_subscribe(sender, **kwargs):
comment = kwargs.pop('comment')
Subscription.objects.subscribe(comment.user,comment.content_object)
comment_was_posted.connect(auto_subscribe, sender=Comment)
def emit_new_comment(comment):
Subscription.objects.emit(
u'%(actor)s commented on %(target)s',
subscribers_of=comment.content_object,
dont_send_to=[comment.user],
context={
'comment': comment,
},
actor=comment.user,
queue='chat',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment