Use long timestamps as id

svn path=/plone.app.discussion/trunk/; revision=26964
This commit is contained in:
Lennart Regebro 2009-05-16 11:21:23 +00:00
parent 51b076935e
commit a2542ea5a8
3 changed files with 20 additions and 9 deletions

View File

@ -1,5 +1,6 @@
"""The default comment class and factory.
"""
import time
from datetime import datetime
from zope.interface import implements
from zope.component.factory import Factory
@ -42,8 +43,9 @@ class Comment(Explicit, Traversable, RoleManager, Owned):
author_name = None
author_email = None
def __init__(self, id=0, conversation=None, **kw):
self.comment_id = id
def __init__(self, conversation=None, **kw):
self.comment_id = long(time.time() * 1e6)
self.__parent__ = conversation
self.creation_date = self.modification_date = datetime.now()

View File

@ -20,6 +20,13 @@ from zope.annotation.interfaces import IAnnotatable
from BTrees.OIBTree import OIBTree
from BTrees.IOBTree import IOBTree
from BTrees.IIBTree import IIBTree, IISet
try:
from BTrees.LOBTree import LOBTree
from BTrees.LLBTree import LLBTree # TODO: Does this even exist?
except ImportError:
from BTrees.OOBTree import OOBTree as LOBTree
from BTrees.OOBTree import OOSet as LLSet
from Acquisition import Explicit
@ -42,10 +49,10 @@ class Conversation(Persistent, Explicit):
self._last_comment_date = None
# id -> comment - find comment by id
self._comments = IOBTree()
self._comments = LOBTree()
# id -> IISet (children) - find all children for a given comment. 0 signifies root.
self._children = IOBTree()
self._children = LOBTree()
def getId(self):
"""
@ -64,7 +71,6 @@ class Conversation(Persistent, Explicit):
@property
def last_comment_date(self):
# TODO
return self._last_comment_date
@property
@ -76,10 +82,13 @@ class Conversation(Persistent, Explicit):
return self._comments.values()
def getThreads(self, start=0, size=None, root=None, depth=None):
# TODO:
return self._comments.values()
def addComment(self, comment):
id = len(self._comments) + 1
id = comment.comment_id
if id in self._comments:
id = max(self._comments.keys()) +1
self._comments[id] = comment
comment.comment_id = id
@ -92,7 +101,7 @@ class Conversation(Persistent, Explicit):
reply_to = comment.in_reply_to
if not reply_to in self._children:
self._children[reply_to] = IISet()
self._children[reply_to] = LLSet()
self._children[reply_to].insert(id)
# Dict API

View File

@ -37,7 +37,7 @@ class ConversationTest(TestCase):
conversation.addComment(comment)
# Check that the conversation methods return the correct data
self.assertEquals(comment.id, '1')
self.assert_(isinstance(comment.comment_id, long))
self.assertEquals(len(conversation.getComments()), 1)
self.assertEquals(len(conversation.getThreads()), 1)
self.assertEquals(conversation.total_comments, 1)