I want to implement something like facebook’s unread items/notifications. I have the following model currently:
class UnreadItems(models.Model):
unread = models.BooleanField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
user = models.ForeignKey(User,null=True)
content_object = generic.GenericForeignKey('content_type','object_id')
class Meta:
db_table = 'unread_items'
Whenever someone creates a conversation/message or whatever to someone else, I created a new entry with the recipient as the target. However, creating unread is easy, but how should I do it in a managable fashion such that items will be marked as read whenever that resource is requested/viewed??
One option is to use Signals for this.
Create a custom signal [set the providing argurments to be the Item accessed]
Create a signal handler that registers to that signal. In that signal hander to access the UnreadItem corresponding to that item.
When you access the Item that is linked with the UnreadItem (in the appropriate view method), you trigger the signal.
This scheme would let you isolate your logic for handling the unread aspect of it, in a central signal handler.
I presume that the reason you chose this design, is because you have multiple Item types that can be considered Unread. With this scheme, you won’t have to spread the logic of handling the Unread everywhere. All the view needs to know is to trigger the Signal.