Hello folks,
I develop web application like Reddit basing on Pyramid, SQLAlchemy (MySQL)
and Mako templating system. I faced with building comment tree problem
(nested tree).
In SQLAlchemy I have database model with self-referential table Comments.
Objects with parent_id=None will be first-level comments, their leafs will
have appropriate parent_id field.
class Comments(Base):
"""This table represents list of added comments"""
__tablename__ = 'comments'
id = Column(INTEGER(10), primary_key=True, index=True)
article_id = Column(INTEGER(10, unsigned=True), ForeignKey(Articles.id),
nullable=False)
author_id = Column(INTEGER(10, unsigned=True), ForeignKey(Users.userid),
nullable=False)
content = Column(TEXT(), nullable=False)
added_dt = Column(DATETIME(timezone=True), nullable=False, default=
datetime.now())
parent_id = Column(INTEGER(10, unsigned=True), ForeignKey('comments.id'
), nullable=True)
author = relationship(Users, single_parent=True, lazy='subquery')
children = relationship("Comments",
# cascade deletions
cascade="all, delete-orphan",
# many to one + adjacency list - remote_side
# is required to reference the 'remote'
# column in the join condition.
backref=backref("parent", remote_side=id),
# children will be represented as a dictionary
# on the "name" attribute.
collection_class=attribute_mapped_collection('id'),
)
In assumption I will have multiple nested trees with comments, for example:
- C1
- C1.1
- C1.2
- C.1.2.1
- C.1.2.2
- C.1.2.3
- C.1.2.3.1
- C.1.2.3.2
- C.1.2.4
- C.1.3
- C.1.4
- C.1.4.1
- C.1.4.1.1
- C.1.4.1.2
- C.1.5
- C2
- C3
- C.3.1
- C.3.2
- C.3.2.1
- C.3.2.1.1
Building relation in database is not a problem - id-parent_id relation is
OK.
In view function I get list of comments for given article, everything is
clear. So I have list of comments of root level (without parent_id) for
given article. But how to recursively get all leafs for each of them?
In model I have *children relation:*
children = relationship("Comments",
# cascade deletions
cascade="all, delete-orphan",
# many to one + adjacency list - remote_side
# is required to reference the 'remote'
# column in the join condition.
backref=backref("parent", remote_side=id),
# children will be represented as a dictionary
# on the "name" attribute.
collection_class=attribute_mapped_collection('id'),
)
It generates dictionary structure mapped by id (of course with SQLAlchemy
objects for comments, dictionary structures written to show idea).
root_comment_1 = {
id: 1,
parent_id: 0,
content: 'abcdef',
children: {
2: {
id: 2,
parent_id: 1,
content: 'reply for abcdef',
children: {
3: {
id: 3,
parent_id: 2,
content: 'reply for reply for abcdef',
children: {}
}
}
},
4: {
id: 4,
parent_id: 1,
content: 'Second reply for abcdef',
children: {
5: {
id: 5,
parent_id: 4,
content: 'reply for second reply for abcdef',
children: {}
}
}
}
}
}
Main question is: *how to render such nested tree in Mako? *I need to do it
like is done on Reddit.com.
I suppose best way would be use some kind of recursion.
Thanks in advance
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.