zcash-grant-system/backend/grant/comment/models.py

53 lines
1.5 KiB
Python
Raw Normal View History

2018-09-10 09:55:26 -07:00
import datetime
from grant.extensions import ma, db
from grant.utils.misc import dt_to_unix
class Comment(db.Model):
__tablename__ = "comment"
id = db.Column(db.Integer(), primary_key=True)
date_created = db.Column(db.DateTime)
content = db.Column(db.Text, nullable=False)
parent_comment_id = db.Column(db.Integer, db.ForeignKey("comment.id"), nullable=True)
2018-09-10 09:55:26 -07:00
proposal_id = db.Column(db.Integer, db.ForeignKey("proposal.id"), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
2018-09-10 09:55:26 -07:00
author = db.relationship("User", back_populates="comments")
replies = db.relationship("Comment")
def __init__(self, proposal_id, user_id, parent_comment_id, content):
2018-09-10 09:55:26 -07:00
self.proposal_id = proposal_id
self.user_id = user_id
self.parent_comment_id = parent_comment_id
2018-09-10 09:55:26 -07:00
self.content = content
self.date_created = datetime.datetime.now()
class CommentSchema(ma.Schema):
class Meta:
model = Comment
# Fields to expose
fields = (
"id",
"proposal_id",
"author",
2018-09-10 09:55:26 -07:00
"content",
"parent_comment_id",
2018-09-10 09:55:26 -07:00
"date_created",
"replies"
2018-09-10 09:55:26 -07:00
)
date_created = ma.Method("get_date_created")
author = ma.Nested("UserSchema", exclude=["email_address"])
replies = ma.Nested("CommentSchema", many=True)
2018-09-10 09:55:26 -07:00
def get_date_created(self, obj):
return dt_to_unix(obj.date_created)
comment_schema = CommentSchema()
comments_schema = CommentSchema(many=True)