David Cramer's Blog

New Pluggable: Django-object-view-tracking

I posted another one of my small pluggable apps up on Google code the other day. It’s called django-object-view-tracking and is most suited for tracking things such as if a user has viewed a thread in a forum.

The API is fairly simple, but may need some improvements based on feedback:

1
2
3
4
5
6
7
8
9
def view_thread_list(request):
    threads = Thread.objects.all()
    tracking = ObjectTracker.objects.get_for_request(request, Thread)

    # This isn't the *best* approach to checking if it's been viewed, but it works
    for thread in threads:
        thread.has_viewed = tracking.has_viewed(thread)

    return render(...)

It was written fairly DRY and I didn’t do up any tests for it, so I’m crossing my fingers there’s no issues :)