class TagModel(db.Model): __tablename__ = "blog_tag" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False, unique=True) @classmethod def find_by_id(cls, _id: int) -> Union["TagModel", None]: return cls.query.filter_by(id=_id).first() @classmethod def find_by_name(cls, name: str) -> Union["TagModel", None]: return cls.query.filter_by(name=name).first() @classmethod def get_or_create(cls, name: str) -> "TagModel": tag = cls.find_by_name(name) if not tag: tag = cls(name=name) tag.save_to_db() return tag def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class ArticleModel(db.Model): __tablename__ = "blog_article" id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey("blog_user.id")) title = db.Column(db.String(200), nullable=False, unique=True) content = db.Column(JSONField(enforce_string=True, enforce_unicode=False), nullable=False) description = db.Column(db.String, nullable=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) published_date = db.Column(db.DateTime) image_url = db.Column(db.String) author = db.relationship("UserModel") likes = db.relationship("UserModel", secondary=article_likes) tags = db.relationship("TagModel", secondary=article_tags) @property def slug(self) -> str: return slugify(self.title) @classmethod def find_by_id(cls, _id: int) -> "ArticleModel": return cls.query.filter_by(id=_id).first() @classmethod def find_all(cls, published: bool = True) -> List["ArticleModel"]: if published: return cls.query.filter(cls.published_date != None) return cls.query.filter(cls.published_date == None) @classmethod def find_by_slug(cls, slug: str) -> "ArticleModel": unslugged_title = " ".join(slug.split("-")) return cls.query.filter(cls.title.ilike(unslugged_title)).first() @classmethod def find_by_title(cls, title: str) -> "ArticleModel": return cls.query.filter_by(title=title).first() def publish(self) -> None: self.published_date = datetime.utcnow() self.save_to_db() def unpublish(self) -> None: self.published_date = None self.save_to_db() def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class SnippetModel(db.Model): __tablename__ = "blog_snippet" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False, unique=True) description = db.Column(db.String, nullable=False) code = db.Column(db.String, nullable=False) language = db.Column(db.String, nullable=False) # kiedyś zrobić ForeignKey do blog_user author = db.Column(db.String, nullable=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) published_date = db.Column(db.DateTime) # likes = db.relationship("UserModel", secondary=snippet_likes) <-- dodać jak już będzie opcja logowania się tags = db.relationship("TagModel", secondary=snippet_tags) @classmethod def find_by_id(cls, _id: int) -> Union["SnippetModel", None]: return cls.query.filter_by(id=_id).first() @classmethod def find_all(cls, published: bool = True) -> List["SnippetModel"]: if published: return cls.query.filter(cls.published_date != None) return cls.query.filter(cls.published_date == None) @classmethod def find_by_slug(cls, slug: str) -> Union["SnippetModel", None]: unslugged_title = " ".join(slug.split("-")) return cls.query.filter(cls.title.ilike(unslugged_title)).first() @classmethod def find_by_title(cls, title: str) -> Union["SnippetModel", None]: return cls.query.filter_by(title=title).first() def approve(self) -> None: self.published_date = datetime.utcnow() self.save_to_db() def revoke_approval(self) -> None: self.published_date = None self.save_to_db() def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class UserModel(db.Model): __tablename__ = "blog_user" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(180), nullable=False) email = db.Column(db.String(40), nullable=False, unique=True) created_date = db.Column(db.DateTime, default=datetime.now) active = db.Column(db.Boolean, default=False) is_staff = db.Column(db.Boolean, default=False) is_superuser = db.Column(db.Boolean, default=False) # path to image file avatar_name = db.Column(db.String) @classmethod def find_all(cls) -> List["UserModel"]: return cls.query.all() @classmethod def find_by_id(cls, _id: int) -> "UserModel": return cls.query.filter_by(id=_id).first() @classmethod def find_by_username(cls, username: str) -> "UserModel": return cls.query.filter_by(username=username).first() @classmethod def find_by_email(cls, email: str) -> "UserModel": return cls.query.filter_by(email=email).first() def set_password(self, password: str) -> None: self.password = bcrypt.hashpw( password.encode("utf-8"), bcrypt.gensalt()) def check_password(self, password: str) -> bool: return bcrypt.checkpw(password.encode("utf-8"), self.password.encode("utf-8")) def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class ViewExample(db.Model, CRUDMixin): __tablename__ = "view_example" __table_args__ = {"info": dict(is_view=True)} # 不会被创建成表 id = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(50)) title = db.Column(db.String(100)) is_albums = db.Column(db.Integer) is_attach = db.Column(db.Integer) is_spec = db.Column(db.Integer) sort_id = db.Column(db.Integer) def __str__(self): return 'id : %s' % self.id
class Example(db.Model, CRUDMixin): __tablename__ = "example" __table_args__ = ({'comment': '案例表'}) id = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(50), comment="名称") title = db.Column(db.String(100), comment="标题") is_albums = db.Column(db.SMALLINT, comment="是否相册") is_attach = db.Column(db.SMALLINT, comment="是否附件") is_spec = db.Column(db.SMALLINT, comment="是否") sort_id = db.Column(db.SMALLINT, comment="排序") def __str__(self): return self.name
from datetime import datetime from typing import List, Union from settings.db import db snippet_tags = db.Table('blog_snippet_tag', db.Column('tag_id', db.Integer, db.ForeignKey('blog_tag.id')), db.Column('snippet_id', db.Integer, db.ForeignKey('blog_snippet.id')) ) class SnippetModel(db.Model): __tablename__ = "blog_snippet" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False, unique=True) description = db.Column(db.String, nullable=False) code = db.Column(db.String, nullable=False) language = db.Column(db.String, nullable=False) # kiedyś zrobić ForeignKey do blog_user author = db.Column(db.String, nullable=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) published_date = db.Column(db.DateTime) # likes = db.relationship("UserModel", secondary=snippet_likes) <-- dodać jak już będzie opcja logowania się tags = db.relationship("TagModel", secondary=snippet_tags) @classmethod
from datetime import datetime from typing import List from sqlalchemy_jsonfield import JSONField from slugify import slugify from settings.db import db article_likes = db.Table( 'likes_in_article', db.Column('user_id', db.Integer, db.ForeignKey('blog_user.id')), db.Column('article_id', db.Integer, db.ForeignKey('blog_article.id'))) article_tags = db.Table( 'blog_article_tag', db.Column('tag_id', db.Integer, db.ForeignKey('blog_tag.id')), db.Column('article_id', db.Integer, db.ForeignKey('blog_article.id'))) class ArticleModel(db.Model): __tablename__ = "blog_article" id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey("blog_user.id")) title = db.Column(db.String(200), nullable=False, unique=True) content = db.Column(JSONField(enforce_string=True, enforce_unicode=False), nullable=False) description = db.Column(db.String, nullable=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) published_date = db.Column(db.DateTime) image_url = db.Column(db.String) author = db.relationship("UserModel")