class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) content = db.Column(db.Text) status = db.Column(db.Integer, default=1) updated = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) created = db.Column(db.DateTime, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False) parent_comm = db.Column(db.Integer, db.ForeignKey('comments.id'), nullable=True) getSubComments = db.relationship('Comment', remote_side='Comment.id', backref=db.backref('getParentComment', lazy=True)) def __repr__(self): return f"<Comment (id={self.id})>"
class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), unique=True, nullable=False) slug = db.Column(db.String(180), unique=True, nullable=False) parent_cat = db.Column(db.Integer, db.ForeignKey('categories.id')) getSubCategories = db.relationship('Category', remote_side='Category.id', backref=db.backref('getParentCategory', lazy=True)) def __repr__(self): return f"<Category (id={self.id}, name={self.name})>"
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(180), nullable=False) slug = db.Column(db.String(180), unique=True, nullable=False) content = db.Column(db.Text) status = db.Column(db.Integer, default=1) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) updated = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) created = db.Column(db.DateTime, default=datetime.utcnow) comments = db.relationship('Comment', backref=db.backref('getPost', lazy=True)) categories = db.relationship('Category', secondary=Post_category, lazy='subquery', backref=db.backref('getPosts', lazy=True)) def __repr__(self): return f"<Post (id={self.id})>"
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) fname = db.Column(db.String(25)) lname = db.Column(db.String(25)) username = db.Column(db.String(25), unique=False, nullable=False) email = db.Column(db.String(180), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) token = db.Column(db.String(40), unique=True, nullable=False) status = db.Column(db.Integer, default=1) profile_img = db.Column(db.String(40)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=False) updated = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) created = db.Column(db.DateTime, default=datetime.utcnow) posts = db.relationship('Post', backref='getUser', lazy=True) comments = db.relationship('Comment', backref=db.backref('getUser', lazy=True)) def getProfileImg(self): default_profile = 'default.png' #profileURI = os.path.join(current_app.static_url_path, 'imgs/profiles', self.profile_img if self.profile_img else default_profile) profileURI = url_for('static', filename=f'imgs/profiles/{(self.profile_img if self.profile_img else default_profile)}') return profileURI def getBadge(self): colors = {'author': 'aqua', 'owner': 'green', 'admin': 'yellow'} color = None try: color = colors[self.role.name] except: pass return Markup(f'<span class="badge bg-{color}">{self.role.name}</span>') def __repr__(self): return f"<User (id={self.id})>"
def getBadge(self): colors = {'author': 'aqua', 'owner': 'green', 'admin': 'yellow'} color = None try: color = colors[self.role.name] except: pass return Markup(f'<span class="badge bg-{color}">{self.role.name}</span>') def __repr__(self): return f"<User (id={self.id})>" Post_category = db.Table('post_category', db.Column('post_id', db.Integer, db.ForeignKey('posts.id'), primary_key=True,), db.Column('category_id', db.Integer, db.ForeignKey('categories.id'), primary_key=True) ) class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(180), nullable=False) slug = db.Column(db.String(180), unique=True, nullable=False) content = db.Column(db.Text) status = db.Column(db.Integer, default=1) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) updated = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) created = db.Column(db.DateTime, default=datetime.utcnow)