Fix deletion of comments with votes.

This commit is contained in:
Georg Brandl
2010-11-21 20:02:23 +01:00
parent fc9549767c
commit 9295e3ce1a
2 changed files with 20 additions and 17 deletions

View File

@@ -98,6 +98,22 @@ class Node(Base):
self.source = source
class CommentVote(Base):
"""A vote a user has made on a Comment."""
__tablename__ = db_prefix + 'commentvote'
username = Column(String(64), primary_key=True)
comment_id = Column(Integer, ForeignKey(db_prefix + 'comments.id'),
primary_key=True)
# -1 if downvoted, +1 if upvoted, 0 if voted then unvoted.
value = Column(Integer, nullable=False)
def __init__(self, comment_id, username, value):
self.comment_id = comment_id
self.username = username
self.value = value
class Comment(Base):
"""An individual Comment being stored."""
__tablename__ = db_prefix + 'comments'
@@ -115,6 +131,9 @@ class Comment(Base):
node_id = Column(String, ForeignKey(db_prefix + 'nodes.id'))
node = relation(Node, backref="comments")
votes = relation(CommentVote, backref="comment",
cascade="all, delete-orphan")
def __init__(self, text, displayed, username, rating, time,
proposal, proposal_diff):
self.text = text
@@ -187,20 +206,3 @@ class Comment(Base):
dt = (days, 'day')
return '%s %s ago' % dt if dt[0] == 1 else '%s %ss ago' % dt
class CommentVote(Base):
"""A vote a user has made on a Comment."""
__tablename__ = db_prefix + 'commentvote'
username = Column(String(64), primary_key=True)
comment_id = Column(Integer, ForeignKey(db_prefix + 'comments.id'),
primary_key=True)
comment = relation(Comment, backref="votes")
# -1 if downvoted, +1 if upvoted, 0 if voted then unvoted.
value = Column(Integer, nullable=False)
def __init__(self, comment_id, username, value):
self.comment_id = comment_id
self.username = username
self.value = value

View File

@@ -81,6 +81,7 @@ class SQLAlchemyStorage(StorageBackend):
comment.set_path(node_id, parent_id)
session.commit()
d = comment.serializable()
d['document'] = comment.node.document
session.close()
return d