core/orm/shout.py

53 lines
2.0 KiB
Python
Raw Normal View History

from typing import List
from datetime import datetime
2021-08-20 08:08:32 +00:00
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship
from orm import Permission, User, Topic
from orm.base import Base
2021-08-20 08:08:32 +00:00
ShoutAuthors = Table('shout_authors',
Base.metadata,
2021-08-25 21:20:53 +00:00
Column('shout', Integer, ForeignKey('shout.id')),
2021-08-20 08:08:32 +00:00
Column('user_id', Integer, ForeignKey('user.id'))
)
ShoutTopics = Table('shout_topics',
Base.metadata,
2021-08-25 21:20:53 +00:00
Column('shout', Integer, ForeignKey('shout.id')),
Column('topic', Integer, ForeignKey('topic.id'))
2021-08-20 08:08:32 +00:00
)
2021-08-25 21:20:53 +00:00
class ShoutRatings(Base):
__tablename__ = "user_ratings"
id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True)
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
value = Column(Integer)
class Shout(Base):
2021-08-07 16:14:20 +00:00
__tablename__ = 'shout'
2021-08-25 21:20:53 +00:00
# NOTE: automatic ID here
slug: str = Column(String, nullable=False, unique=True)
2021-08-19 15:33:39 +00:00
org_id: int = Column(Integer, ForeignKey("organization.id"), nullable=False, comment="Organization")
2021-08-07 16:14:20 +00:00
body: str = Column(String, nullable=False, comment="Body")
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
updatedAt: str = Column(DateTime, nullable=True, comment="Updated at")
2021-08-25 21:20:53 +00:00
replyTo: int = Column(ForeignKey("shout.id"), nullable=True)
versionOf: int = Column(ForeignKey("shout.id"), nullable=True)
2021-08-08 12:23:12 +00:00
tags: str = Column(String, nullable=True)
views: int = Column(Integer, default=0)
2021-08-20 08:08:32 +00:00
published: bool = Column(Boolean, default=False)
publishedAt: str = Column(DateTime, nullable=True)
cover: str = Column(String, nullable = True)
2021-08-25 21:20:53 +00:00
title: str = Column(String, nullable = True)
subtitle: str = Column(String, nullable = True)
2021-08-20 08:08:32 +00:00
layout: str = Column(String, nullable = True)
authors = relationship(lambda: User, secondary=ShoutAuthors) # NOTE: multiple authors
topics = relationship(lambda: Topic, secondary=ShoutTopics)
2021-08-25 21:20:53 +00:00
rating: int = Column(Integer, nullable=True, comment="Rating")
ratings = relationship(ShoutRatings, foreign_keys=ShoutRatings.shout_id)
old_id: str = Column(String, nullable = True)