I’m trying to design a gmail like thread messaging system on Google App Engine by using Django-nonrel. I’ve watched “Building Scalable, Complex Apps on App Engine” and read the following questions on SO.
App Engine Messaging System with Message Status – Design Pattern
database design in google app engine
I like the idea Kevin pointed at (App Engine Messaging System with Message Status – Design Pattern), so my current models are like these:
class Message(models.Model):
sender = models.ForeignKey(User)
content = models.TextField()
thread_id = models.IntegerField()
class MessageReceivers(models.Model):
message = models.ForeignKey(Message)
users = ListField(models.CharField())
status = models.IntegerField() # 1 unread, 2 read ....
ListField on Django-nonrel is from here:
http://www.allbuttonspressed.com/blog/django/2010/03/Updates-on-djangoappengine
And here is my problem, I don’t know how to achieve to display a count of messages in each thread like gmail. Let me clarify my point.
When a user, Sirius, sends a message to Harry and Ron. Models (simplified for explanation) should be like these:
Message
id = 1
sender = Sirius
thread_id = 1
MessageReceivers
message = 1
users = [Harry, Ron]
Then, Harry replies to Sirius, but for some reason he doesn’t include Ron:
Message
id = 2
sender = Harry
thread_id = 1
MessageReceivers
message = 2
users = [Sirius]
And Sirius replies to Harry too, but he includes Ron again:
Message
id = 3
sender = Sirius
thread_id = 1
MessageReceivers
message = 3
users = [Haary, Ron]
And when Sirius and Harry see their inbox, there is the thread with a count of messages (3). And Ron sees his inbox there is the thread with a count of messages (2). How should I count those numbers? Should I make a new model to keep them?
Thanks in advance,
Yoo
EDIT
As Kevin introduced me to a Thread model and I realized that I don’t need a relation index model for my requirements, so my current models look like the following.
class Message(models.Model):
sender = models.ForeignKey(User)
content = models.TextField()
class Thread(models.Model):
user = models.ForeignKey(User)
messages = ListField(models.CharField()) # array of message id
lastmodified = models.DateTimeField()
# could have a status field for read or unread, not sure for now
I might find difficulties in these models in further development, but I would go with them for the time being 🙂
Try this:
This will give you a dictionary of thread_id’s, with their frequency of occurrence.
For example:
message_threads = [‘a’,’b’,’c’,’a’,’a’,’b’,’a’]
will yield
counted_threads == defaultdict(, {‘a’: 4, ‘c’: 1, ‘b’: 2})