model upgrade

This commit is contained in:
2021-08-20 11:08:32 +03:00
parent 94518adcc5
commit ee3b186ba1
8 changed files with 85 additions and 38 deletions

View File

@@ -1,24 +1,37 @@
from typing import List
from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from orm import Permission
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
ShoutAuthors = Table('shout_authors',
Base.metadata,
Column('shout', String, ForeignKey('shout.slug')),
Column('user_id', Integer, ForeignKey('user.id'))
)
ShoutTopics = Table('shout_topics',
Base.metadata,
Column('shout', String, ForeignKey('shout.slug')),
Column('topic', String, ForeignKey('topic.slug'))
)
class Shout(Base):
__tablename__ = 'shout'
slug: str = Column(String, primary_key=True)
org_id: int = Column(Integer, ForeignKey("organization.id"), nullable=False, comment="Organization")
author_id: int = Column(Integer, ForeignKey("user.id"), nullable = False, comment="Author")
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")
replyTo: str = Column(ForeignKey("shout.slug"), nullable=True)
versionOf: str = Column(ForeignKey("shout.slug"), nullable=True)
tags: str = Column(String, nullable=True)
topics: str = Column(String, nullable=True)
views: int = Column(Integer, default=0)
# TODO: add all the fields
published: bool = Column(Boolean, default=False)
publishedAt: str = Column(DateTime, nullable=True)
cover: str = Column(String, nullable = True)
layout: str = Column(String, nullable = True)
authors = relationship(lambda: User, secondary=ShoutAuthors) # NOTE: multiple authors
topics = relationship(lambda: Topic, secondary=ShoutTopics)