core/orm/community.py

36 lines
1.2 KiB
Python
Raw Normal View History

2021-08-26 21:14:20 +00:00
from datetime import datetime
from sqlalchemy import Column, String, ForeignKey, DateTime
2022-08-11 05:53:14 +00:00
from base.orm import Base, local_session
2021-08-26 21:14:20 +00:00
class CommunityFollower(Base):
__tablename__ = 'community_followers'
id = None
follower = Column(ForeignKey('user.slug'), primary_key = True)
community = Column(ForeignKey('community.slug'), primary_key = True)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
2021-08-26 21:14:20 +00:00
class Community(Base):
__tablename__ = 'community'
2021-08-26 21:14:20 +00:00
name: str = Column(String, nullable=False, comment="Name")
2022-07-14 13:41:53 +00:00
slug: str = Column(String, nullable = False, unique=True, comment="Slug")
2021-08-28 10:13:50 +00:00
desc: str = Column(String, nullable=False, default='')
pic: str = Column(String, nullable=False, default='')
2021-08-26 21:14:20 +00:00
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
createdBy: str = Column(ForeignKey("user.slug"), nullable=False, comment="Author")
@staticmethod
def init_table():
with local_session() as session:
2021-12-16 19:16:59 +00:00
default = session.query(Community).filter(Community.slug == "discours").first()
2021-12-10 13:52:55 +00:00
if not default:
default = Community.create(
2021-12-16 11:34:16 +00:00
name = "Дискурс",
slug = "discours",
2022-07-14 13:41:53 +00:00
createdBy = "discours"
2021-12-10 13:52:55 +00:00
)
Community.default_community = default