core/orm/shout.py

104 lines
3.5 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
2021-09-05 08:56:15 +00:00
from orm import Permission, User, Topic
from orm.comment import Comment
from orm.base import Base
2021-09-24 14:39:37 +00:00
from functools import reduce
2021-08-28 10:13:50 +00:00
class ShoutAuthor(Base):
__tablename__ = "shout_author"
id = None
shout = Column(ForeignKey('shout.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True)
2021-09-05 08:56:15 +00:00
2021-09-05 07:16:28 +00:00
class ShoutViewer(Base):
2021-09-05 08:56:15 +00:00
__tablename__ = "shout_viewer"
id = None
shout = Column(ForeignKey('shout.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True)
2021-08-20 08:08:32 +00:00
2021-08-28 15:12:13 +00:00
class ShoutTopic(Base):
__tablename__ = 'shout_topic'
id = None
shout = Column(ForeignKey('shout.id'), primary_key = True)
topic = Column(ForeignKey('topic.id'), primary_key = True)
2021-08-30 07:41:59 +00:00
class ShoutRating(Base):
2021-08-31 15:15:27 +00:00
__tablename__ = "shout_rating"
2021-08-25 21:20:53 +00:00
id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True)
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
2021-08-31 15:15:27 +00:00
ts: str = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
value = Column(Integer)
2021-09-24 14:39:37 +00:00
class ShoutRatingStorage:
def __init__(self, session):
self.ratings = session.query(ShoutRating).all()
def get_rating(self, shout_id):
shout_ratings = list(filter(lambda x: x.shout_id == shout_id, self.ratings))
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
def update_rating(self, new_rating):
rating = next(x for x in self.ratings \
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id)
if rating:
rating.value = new_rating.value
rating.ts = new_rating.ts
else:
self.ratings.append(new_rating)
2021-08-31 15:15:27 +00:00
class ShoutViewByDay(Base):
__tablename__ = "shout_view_by_day"
id = None
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
day: str = Column(DateTime, primary_key = True, default = datetime.now)
2021-08-25 21:20:53 +00:00
value = Column(Integer)
2021-09-24 14:39:37 +00:00
class ShoutViewStorage:
def __init__(self, session):
self.views = session.query(ShoutViewByDay).all()
def get_view(self, shout_id):
shout_views = list(filter(lambda x: x.shout_id == shout_id, self.views))
return reduce((lambda x, y: x + y.value), shout_views, 0)
def add_view(self, view):
self.views.append(view)
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-26 21:14:20 +00:00
community: int = Column(Integer, ForeignKey("community.id"), nullable=True, comment="Community")
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)
2021-09-05 07:16:28 +00:00
publishedBy: bool = Column(ForeignKey("user.id"), nullable=True)
2021-08-20 08:08:32 +00:00
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-09-05 07:16:28 +00:00
comments = relationship(Comment)
2021-08-20 08:08:32 +00:00
layout: str = Column(String, nullable = True)
2021-08-28 10:13:50 +00:00
authors = relationship(lambda: User, secondary=ShoutAuthor.__tablename__) # NOTE: multiple authors
2021-08-28 15:12:13 +00:00
topics = relationship(lambda: Topic, secondary=ShoutTopic.__tablename__)
2021-09-05 07:16:28 +00:00
visibleFor = relationship(lambda: User, secondary=ShoutViewer.__tablename__)
2021-08-25 21:20:53 +00:00
old_id: str = Column(String, nullable = True)