urma/app/models.py
2023-01-02 14:48:22 +00:00

122 lines
3.8 KiB
Python

#!/usr/bin/python3
import os.path
from dbconfig import Session, scoped_session
from sqlalchemy import (
Boolean,
Column,
DateTime,
ForeignKey,
Integer,
String,
)
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import (
declarative_base,
relationship,
)
from config import Config
from log import log
Base = declarative_base()
# Database classes
class Accounts(Base):
__tablename__ = 'accounts'
id = Column(Integer, primary_key=True, autoincrement=True)
account_id = Column(Integer, index=True, nullable=False)
username = Column(String(256), index=True, nullable=False)
acct = Column(String(256), index=False, nullable=False)
display_name = Column(String(256), index=False, nullable=False)
bot = Column(Boolean, index=False, nullable=False, default=False)
url = Column(String(256), index=False)
followed = Column(Boolean, index=False, nullable=False, default=False)
posts = relationship("Posts", back_populates="account")
def __repr__(self) -> str:
return (
f"<Accounts(id={self.id}, url={self.content[:60]}, "
f"followed={self.followed}>"
)
class Attachments(Base):
__tablename__ = 'attachments'
id = Column(Integer, primary_key=True, autoincrement=True)
media_id = Column(Integer, index=True, nullable=False)
media_type = Column(String(256), index=False)
url = Column(String(256), index=False)
preview_url = Column(String(256), index=False)
description = Column(String(2048), index=False)
posts = relationship("Posts", back_populates="media_attachments")
def __repr__(self) -> str:
return (
f"<Attachments(id={self.id}, url={self.url}, "
f"content={self.content[:60]}, followed={self.followed}>"
)
class Hashtags(Base):
__tablename__ = 'hashtags'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(256), index=True, nullable=False)
url = Column(String(256), index=False)
posts = relationship("Posts", secondary="post_tags", backref="hashtags")
followed = Column(Boolean, index=False, nullable=False, default=False)
posttags = relationship("PostTags", back_populates="hashtag")
posts = association_proxy("posttags", "post")
def __repr__(self) -> str:
return (
f"<Hashtags(id={self.id}, name={self.name}, "
f"url={self.url}>, followed={self.followed}>"
)
class Posts(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, autoincrement=True)
post_id = Column(Integer, index=True, nullable=False)
created_at = Column(DateTime, index=True, default=None)
uri = Column(String(256), index=False)
url = Column(String(256), index=False)
content = Column(String(2048), index=False, default="")
account_id = Column(Integer, ForeignKey('accounts.id'), nullable=True)
account = relationship("Accounts", back_populates="posts")
parent_id = Column(Integer, ForeignKey("posts.id"))
reblog = relationship("Posts")
media_attachments_id = Column(Integer, ForeignKey('attachments.id'),
nullable=True)
media_attachments = relationship("Attachments", back_populates="posts")
posttags = relationship("PostTags", back_populates="post")
rating = Column(Integer, index=True, default=None)
def __repr__(self) -> str:
return f"<Posts(id={self.id}, content={self.content[:60]}>"
class PostTags(Base):
__tablename__ = 'post_tags'
id = Column(Integer, primary_key=True, autoincrement=True)
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False)
post = relationship(Posts, back_populates="posttags")
hashtag_id = Column(Integer, ForeignKey('hashtags.id'), nullable=False)
hashtag = relationship("Hashtags", back_populates="posttags")