@@ -104,6 +104,7 @@ class User(UserMixin, db.Model):
104104 backref = db .backref ('followed' , lazy = 'joined' ),
105105 lazy = 'dynamic' ,
106106 cascade = 'all, delete-orphan' )
107+ comments = db .relationship ('Comment' , backref = 'author' , lazy = 'dynamic' )
107108
108109 @staticmethod
109110 def add_self_follows ():
@@ -264,6 +265,7 @@ class Post(db.Model):
264265 body_html = db .Column (db .Text )
265266 timestamp = db .Column (db .DateTime , index = True , default = datetime .utcnow )
266267 author_id = db .Column (db .Integer , db .ForeignKey ('users.id' ))
268+ comments = db .relationship ('Comment' , backref = 'post' , lazy = 'dynamic' )
267269
268270 @staticmethod
269271 def on_changed_body (target , value , oldvalue , initiator ):
@@ -275,3 +277,24 @@ def on_changed_body(target, value, oldvalue, initiator):
275277 tags = allowed_tags , strip = True ))
276278
277279db .event .listen (Post .body , 'set' , Post .on_changed_body )
280+
281+
282+ class Comment (db .Model ):
283+ __tablename__ = 'comments'
284+ id = db .Column (db .Integer , primary_key = True )
285+ body = db .Column (db .Text )
286+ body_html = db .Column (db .Text )
287+ timestamp = db .Column (db .DateTime , index = True , default = datetime .utcnow )
288+ disabled = db .Column (db .Boolean )
289+ author_id = db .Column (db .Integer , db .ForeignKey ('users.id' ))
290+ post_id = db .Column (db .Integer , db .ForeignKey ('posts.id' ))
291+
292+ @staticmethod
293+ def on_changed_body (target , value , oldvalue , initiator ):
294+ allowed_tags = ['a' , 'abbr' , 'acronym' , 'b' , 'code' , 'em' , 'i' ,
295+ 'strong' ]
296+ target .body_html = bleach .linkify (bleach .clean (
297+ markdown (value , output_format = 'html' ),
298+ tags = allowed_tags , strip = True ))
299+
300+ db .event .listen (Comment .body , 'set' , Comment .on_changed_body )
0 commit comments