core/orm/community.py

39 lines
1.4 KiB
Python
Raw Normal View History

from sqlalchemy import Column, DateTime, ForeignKey, String, func
2023-10-30 21:00:55 +00:00
2023-10-26 21:07:35 +00:00
from base.orm import Base, local_session
2023-10-26 17:56:42 +00:00
2022-09-03 10:50:14 +00:00
class CommunityFollower(Base):
2022-09-03 10:50:14 +00:00
__tablename__ = "community_followers"
2023-10-30 21:00:55 +00:00
id = None
follower: Column = Column(ForeignKey("user.id"), primary_key=True)
community: Column = Column(ForeignKey("community.id"), primary_key=True)
joinedAt = Column(
DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at"
)
2022-11-24 08:27:01 +00:00
# role = Column(ForeignKey(Role.id), nullable=False, comment="Role for member")
2021-08-26 21:14:20 +00:00
class Community(Base):
2022-09-03 10:50:14 +00:00
__tablename__ = "community"
name = Column(String, nullable=False, comment="Name")
slug = Column(String, nullable=False, unique=True, comment="Slug")
desc = Column(String, nullable=False, default="")
pic = Column(String, nullable=False, default="")
createdAt = Column(
DateTime(timezone=True), nullable=False, server_default=func.now(), comment="Created at"
)
2022-09-03 10:50:14 +00:00
@staticmethod
def init_table():
with local_session() as session:
2023-10-30 21:00:55 +00:00
d = session.query(Community).filter(Community.slug == "discours").first()
2022-11-19 11:35:34 +00:00
if not d:
2022-11-24 08:27:01 +00:00
d = Community.create(name="Дискурс", slug="discours")
2022-11-19 11:35:34 +00:00
session.add(d)
session.commit()
Community.default_community = d
2023-10-30 21:00:55 +00:00
print("[orm] default community id: %s" % d.id)