class Tag(db.Model): """ORM class used for modelling tags. Inherit for SQLAlchemy.Model. Create class variables which the ORM uses as table columns. --- Class variables --------------- id : SQLALchemy.Column integer, primary key content: SQLALchemy.Column string, mandatory post_id: SQLALchemy.Column integer, foreign key, points to Post.id Methods ------- __repr__(self): str string representation of a Tag """ id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(20), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) def __repr__(self): return f"Tag {self.id} for post {self.post_id}\n"
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), unique=True, nullable=False) body = db.Column(db.Text, nullable=False) body_html = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) category_id = db.Column(db.Integer, db.ForeignKey("categories.id")) comments = db.relationship('Comment', backref='post', lazy='dynamic') def __repr__(self): return "Category : {}, Title {}".format(self.category_id, self.title) @staticmethod def on_changed_body(target, value, oldvalue, initiator): md = Markdown(renderer=HighLightRenderer(), extensions=['fenced-code']) target.body_html = md(value)
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) body_html = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) disabled = db.Column(db.Boolean) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) post_id = db.Column(db.Integer, db.ForeignKey('posts.id')) @staticmethod def on_changed_body(target, value, oldvalue, initiator): allowed_tags = [ 'a', 'abbr', 'acronym', 'b', 'code', 'em', 'i', 'strong' ] target.body_html = bleach.linkify( bleach.clean(markdown(value, output_format='html', extensions=["fenced_code"]), tags=allowed_tags, strip=True))
class Comment(db.Model): """ORM class used for modelling comments. Inherit for SQLAlchemy.Model. Create class variables which the ORM uses as table columns. --- Class variables --------------- id : SQLALchemy.Column integer, primary key date_posted: SQLALchemy.Column datetime, mandatory, indexed, defaults to utcnow content: SQLALchemy.Column text, mandatory user_id: SQLALchemy.Column integer, foreign key, points to User.id post_id: SQLALchemy.Column integer, foreign key, points to Post.id Methods ------- __repr__(self): str string representation of a Comment """ id = db.Column(db.Integer, primary_key=True) date_posted = db.Column(db.DateTime, nullable=False, index=True, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) def __repr__(self): return f"Comment {self.id} by user {self.user_id} for "\ f"post {self.post_id}\n"
class Post(SearchableMixin, db.Model): """ORM class used for modelling posts and their behavior. Inherit for SQLAlchemy.Model and SearchableMixin. Create class variables which the ORM uses as table columns. --- Class variables --------------- __searchable__ : list[str] names of columns which should be full-text searched id : SQLALchemy.Column integer, primary key title: SQLALchemy.Column string, mandatory, indexed date_posted: SQLALchemy.Column datetime, mandatory, indexed, defaults to utcnow content: SQLALchemy.Column text, mandatory user_id: SQLALchemy.Column integer, foreign key, points to User.id tags: SQLAlchemy.relationship every tag record has a parent post, delete on cascade comments: SQLAlchemy.relationship every comment record has a parent post, delete on cascade Methods ------- __repr__(self): str string representation of a Post instance """ __searchable__ = ['content'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120), index=True, nullable=False) date_posted = db.Column(db.DateTime, nullable=False, index=True, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # 'user' above is in lowercase because it references the table name comments = db.relationship('Comment', cascade='all,delete', backref='parent_post', lazy=True) tags = db.relationship('Tag', cascade='all,delete', backref='parent_post', lazy=True) def __repr__(self): return f"Blog post: {self.title}, \nPosted on: {self.date_posted}\n"
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean, default=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') name = db.Column(db.String(64)) location = db.Column(db.String(64)) about_me = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.utcnow) last_seen = db.Column(db.DateTime(), default=datetime.utcnow) posts = db.relationship('Post', backref='author', lazy='dynamic') comments = db.relationship('Comment', backref='author', lazy='dynamic') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == current_app.config['ADMIN']: self.role = Role.query.filter_by(name='Administrator').first() if self.role is None: self.role = Role.query.filter_by(default=True).first() def __repr__(self): return 'User {}'.format(self.username) @property def password(self): raise AttributeError('Password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_confirmation_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm': self.id}).decode('utf-8') def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) except: return False if data.get('confirm') != self.id: return False self.confirmed = True db.session.add(self) return True def generate_reset_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'reset': self.id}).decode('utf-8') @staticmethod def reset_password(token, new_password): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) except: return False user = User.query.get(data.get('reset')) if user is None: return False user.password = new_password db.session.add(user) return True def generate_email_change_token(self, new_email, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({ 'change_email': self.id, 'new_email': new_email }).decode('utf-8') def change_email(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) except: return False if data.get('change_email') != self.id: return False new_email = data.get('new_email') if new_email is None: return False if self.query.filter_by(email=new_email).first() is not None: return False self.email = new_email db.session.add(self) return True def can(self, perm): return self.role is not None and self.role.has_permission(perm) def ping(self): self.last_seen = datetime.utcnow() db.session.add(self) def is_administrator(self): return self.can(Permission.ADMIN)