core/orm/community.py

40 lines
1.4 KiB
Python
Raw Normal View History

2021-08-26 21:14:20 +00:00
from datetime import datetime
2022-11-24 08:27:01 +00:00
from sqlalchemy import Column, String, ForeignKey, DateTime
2023-10-23 14:47:11 +00:00
from sqlalchemy.orm import relationship
2023-10-23 14:51:13 +00:00
from services.db import Base, local_session
2023-10-23 14:47:11 +00:00
from orm.author import Author
2021-08-26 21:14:20 +00:00
2023-10-23 14:47:11 +00:00
class CommunityAuthor(Base):
__tablename__ = "community_author"
2022-09-03 10:50:14 +00:00
id = None # type: ignore
2023-10-23 14:47:11 +00:00
follower = Column(ForeignKey("author.id"), primary_key=True)
community = Column(ForeignKey("community.id"), primary_key=True)
2023-10-23 14:47:11 +00:00
joinedAt = Column(DateTime, nullable=False, default=datetime.now)
2023-10-25 16:55:30 +00:00
role = Column(String, nullable=False)
2021-08-26 21:14:20 +00:00
class Community(Base):
2022-09-03 10:50:14 +00:00
__tablename__ = "community"
2023-10-23 14:47:11 +00:00
name = Column(String, nullable=False)
slug = Column(String, nullable=False, unique=True)
2022-09-03 10:50:14 +00:00
desc = Column(String, nullable=False, default="")
pic = Column(String, nullable=False, default="")
2023-10-23 14:47:11 +00:00
createdAt = Column(DateTime, nullable=False, default=datetime.now)
authors = relationship(lambda: Author, secondary=CommunityAuthor.__tablename__, nullable=True)
2022-09-03 10:50:14 +00:00
@staticmethod
def init_table():
with local_session() as session:
2023-10-23 14:47:11 +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-23 14:47:11 +00:00
print('[orm] default community id: %s' % d.id)