views:

330

answers:

3

Hi,

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,

+2  A: 

Generic relations would be the way to go here. Just make sure to resolve the model yourself instead of joining against the update table.

Ignacio Vazquez-Abrams
Hi Ignacio, what do you mean by "resolve the model yourself"? Thanks for the answer!
Tiago
Use the various `ContentType` methods to get the appropriate model class instead of poking the `GenericForeignKey` field directly.
Ignacio Vazquez-Abrams
Thanks for the clarification, Ignacio!
Tiago
+2  A: 

I can think in two ways:

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

The second thing i can think is 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

diegueus9
Hi diegueus9! Thanks for pointing out the feed framework. I've seen it linked in the docs, but never checked it out.About the Activity model, it seems to be the best way to do indeed.
Tiago
Hi Tiago, my pleasure, if you need more help with ContentType framework or signals don't doubt about contact mw.
diegueus9
+1  A: 

After more googling and one helpful keyword("Activity") that diegueus9 mentioned and that I haven't thought before, I was able to find more relevant material.

First, two blog posts on how to build a tumbleblog using django using the ContentType framework:

After that, another post that gives suggestions on how to reduce the (1 + n) queries problem (which was one of my concerns initially, but I didn't mention to avoid cluttering the question).

And finally an reusable Django app that has some of the feature that I needed and can be useful for further reference:

Tiago