Search code examples
djangodjango-modelsstatusfeedactivity-streams

Django-way for building a "News Feed" / "Status update" / "Activity Stream"


I'd like to create a reusable Django app that handles status updates of the Users. Much like facebook's "news feed".

Use cases includes, for example:

  • A Professor can create an Assignment due to an specific date and every student can see on the news feed that the assignment was created, with a short description, the date that it's due and a link to see the full description.
  • He also can upload a new PDF that he finds interesting for his students. On the news feed, the info regarding this should be displayed, eg, the description of the pdf, an link to download and a link to preview it.
  • A link to a YouTube video can be posted and on the News Feed is displayed an small thumbnail and, with a click, the video is embbeded using javascript and the user can watch it right away.

One concern is how to handle different kinds of Updates and display the correct "html snippet" for it. The other, which is more important, is how to design the Models of this "Django way".

About the former, I could think of two ways of doing it:

  1. Using Model inheritance;
  2. Using Generic relations.

I searched before posting here, but I didn't find anything. I checked Pinax to see if they had it implemented, but they don't. So, I'm here looking for more suggestions on how to handle this in a nice and non-hacky way.

Thanks in advance,


Solution

  • I can think in two ways:

    First, maybe you could make feeds for your models Assigments, PdfFiles, and Youtube link, and use the library feedparser to embed it in your news views, this is the easy way because you can define in the templates, the code for each kind of new activity.

    The second thing I can think of is to make a class Activity:

    class Activity(models.Model):
        date = models.DateTimeField(auto_now_add = True)
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey('content_type', 'object_id')
    

    And through the signals make a new instance of Activity every time you have a new assigment or pdf upload or youtube link, and for each class make a method like render_to_html, in this way in your view, you can make a for over Activities and call the method render_to_html