From: richardbarran Date: Mon, 17 May 2010 09:18:12 +0000 (+0000) Subject: Fix design error: the time when message were received was stored as an integer, so... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ecccb7f1e154faf62cf7bfbca8dca4dd624877f3;p=django-jqchat.git Fix design error: the time when message were received was stored as an integer, so when 2 users sent messages at the same time some messages were being lost. git-svn-id: http://django-jqchat.googlecode.com/svn/trunk@16 8369a704-5b4a-11de-992f-fdd7e25b9163 --- diff --git a/jqchat/models.py b/jqchat/models.py index 6ff4b67..bc020ca 100644 --- a/jqchat/models.py +++ b/jqchat/models.py @@ -57,10 +57,10 @@ class Room(models.Model): def save(self, **kw): # If description modified, update the timestamp field. if self._init_description != self.description: - self.description_modified = int(time.time()) + self.description_modified = time.time() # if last_activity is null (i.e. we are creating the room) set it to now. if not self.last_activity: - self.last_activity = int(time.time()) + self.last_activity = time.time() if not self.created: self.created = datetime.datetime.now() super(Room, self).save(**kw) @@ -131,7 +131,7 @@ class Message(models.Model): - a unix timestamp. - a datetime timestamp. The reason: the unix timestamp is higher performance when sending data to the browser (easier - and faster to handle INTs instead of datetimes. The 'created' is used for displaying the date + and faster to handle numbers instead of datetimes. The 'created' is used for displaying the date of messages; I could calculate it from the unix timestamp, but I'm guessing that I will get higher performance by storing it in the database. @@ -141,7 +141,7 @@ class Message(models.Model): room = models.ForeignKey(Room, help_text='This message was posted in a given chat room.') event = models.IntegerField(null=True, blank=True, choices=EVENT_CHOICES, help_text='An action performed in the room, either by a user or by the system (e.g. XYZ leaves room.') text = models.TextField(null=True, blank=True, help_text='A message, either typed in by a user or generated by the system.') - unix_timestamp = models.IntegerField(editable=False, help_text='Unix timestamp when this message was inserted into the database.') + unix_timestamp = models.FloatField(editable=False, help_text='Unix timestamp when this message was inserted into the database.') created = models.DateTimeField(editable=False) def __unicode__(self): @@ -149,7 +149,7 @@ class Message(models.Model): def save(self, **kw): if not self.unix_timestamp: - self.unix_timestamp = int(time.time()) + self.unix_timestamp = time.time() self.created = datetime.datetime.fromtimestamp(self.unix_timestamp) super(Message, self).save(**kw) self.room.last_activity = int(time.time()) diff --git a/jqchat/tests.py b/jqchat/tests.py index 479c8a3..1d54fe6 100644 --- a/jqchat/tests.py +++ b/jqchat/tests.py @@ -71,12 +71,12 @@ class AJAXGetTest(TestCase): # Should have a status of 1 (there are new messages). self.assert_(payload['status'] == 1, payload) - # The server returns the Unix time, this will be an integer > 0. + # The server returns the Unix time, this will be a number > 0. t = payload['time'] try: - t = int(t) + t = float(t) except: - self.assert_(False, 'Time (%s) should be an integer.' % t) + self.assert_(False, 'Time (%s) should be a number.' % t) messages = payload['messages'] @@ -227,6 +227,74 @@ class AJAXPostTest(TestCase): self.assert_('<script>' in messages[-1]['text']) self.assert_('