from app import db from datetime import datetime class Post(db.Model): """Contents created and published by Users.""" __tablename__ = 'post' # Unique Post ID for the whole site id = db.Column(db.Integer, primary_key=True) # Post type (polymorphic discriminator) type = db.Column(db.String(20)) # Creation and edition date date_created = db.Column(db.DateTime) date_modified = db.Column(db.DateTime) # Post author author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) author = db.relationship('User', backref="posts", foreign_keys=author_id) # TODO: Post attachments? __mapper_args__ = { 'polymorphic_identity': __tablename__, 'polymorphic_on': type } def __init__(self, author): """ Create a new Post. Arguments: author -- post author (User) """ self.author = author self.date_created = datetime.now() self.date_modified = self.date_created def touch(self): """Touch a Post when it is edited.""" self.date_modified = datetime.now() def change_ownership(self, new_author): """ Change ownership of a Post. This is a privileged operation! Arguments: new_author -- new post author (User) """ self.author = new_author def __repr__(self): return f''