stats refactored
This commit is contained in:
@@ -6,6 +6,7 @@ from orm.reaction import Reaction
|
||||
from orm.shout import Shout
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from orm.user import User, UserRating
|
||||
from orm.viewed import ViewedByDay
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
@@ -19,6 +20,7 @@ __all__ = [
|
||||
"Notification",
|
||||
"Reaction",
|
||||
"UserRating",
|
||||
"ViewedByDay"
|
||||
]
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
@@ -27,3 +29,5 @@ Resource.init_table()
|
||||
User.init_table()
|
||||
Community.init_table()
|
||||
Role.init_table()
|
||||
|
||||
# NOTE: keep orm module isolated
|
||||
|
@@ -2,10 +2,25 @@ from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, String, ForeignKey, DateTime
|
||||
from sqlalchemy import Enum
|
||||
from enum import Enum as Enumeration
|
||||
|
||||
from base.orm import Base
|
||||
from services.stat.reacted import ReactedStorage, ReactionKind
|
||||
from services.stat.viewed import ViewedStorage
|
||||
|
||||
|
||||
class ReactionKind(Enumeration):
|
||||
AGREE = 1 # +1
|
||||
DISAGREE = 2 # -1
|
||||
PROOF = 3 # +1
|
||||
DISPROOF = 4 # -1
|
||||
ASK = 5 # +0 bookmark
|
||||
PROPOSE = 6 # +0
|
||||
QUOTE = 7 # +0 bookmark
|
||||
COMMENT = 8 # +0
|
||||
ACCEPT = 9 # +1
|
||||
REJECT = 0 # -1
|
||||
LIKE = 11 # +1
|
||||
DISLIKE = 12 # -1
|
||||
# TYPE = <reaction index> # rating diff
|
||||
|
||||
|
||||
class Reaction(Base):
|
||||
@@ -26,12 +41,3 @@ class Reaction(Base):
|
||||
range = Column(String, nullable=True, comment="Range in format <start index>:<end>")
|
||||
kind = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
||||
oid = Column(String, nullable=True, comment="Old ID")
|
||||
|
||||
@property
|
||||
async def stat(self):
|
||||
return {
|
||||
"viewed": await ViewedStorage.get_reaction(self.id),
|
||||
"reacted": len(await ReactedStorage.get_reaction(self.id)),
|
||||
"rating": await ReactedStorage.get_reaction_rating(self.id),
|
||||
"commented": len(await ReactedStorage.get_reaction_comments(self.id)),
|
||||
}
|
||||
|
22
orm/shout.py
22
orm/shout.py
@@ -5,10 +5,16 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
from base.orm import Base
|
||||
from orm.reaction import Reaction
|
||||
from orm.topic import Topic, ShoutTopic
|
||||
from orm.topic import Topic
|
||||
from orm.user import User
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.viewed import ViewedStorage
|
||||
|
||||
|
||||
class ShoutTopic(Base):
|
||||
__tablename__ = "shout_topic"
|
||||
|
||||
id = None # type: ignore
|
||||
shout = Column(ForeignKey("shout.slug"), primary_key=True)
|
||||
topic = Column(ForeignKey("topic.slug"), primary_key=True)
|
||||
|
||||
|
||||
class ShoutReactionsFollower(Base):
|
||||
@@ -62,17 +68,9 @@ class Shout(Base):
|
||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
||||
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
|
||||
publishedAt = Column(DateTime, nullable=True)
|
||||
deletedAt = Column(DateTime, nullable=True)
|
||||
|
||||
versionOf = Column(ForeignKey("shout.slug"), nullable=True)
|
||||
draft = Column(Boolean, default=False)
|
||||
lang = Column(String, default='ru')
|
||||
oid = Column(String, nullable=True)
|
||||
|
||||
@property
|
||||
async def stat(self):
|
||||
return {
|
||||
"viewed": await ViewedStorage.get_shout(self.slug),
|
||||
"reacted": len(await ReactedStorage.get_shout(self.slug)),
|
||||
"commented": len(await ReactedStorage.get_comments(self.slug)),
|
||||
"rating": await ReactedStorage.get_rating(self.slug),
|
||||
}
|
||||
|
@@ -5,14 +5,6 @@ from sqlalchemy import Column, Boolean, String, ForeignKey, DateTime, JSON as JS
|
||||
from base.orm import Base
|
||||
|
||||
|
||||
class ShoutTopic(Base):
|
||||
__tablename__ = "shout_topic"
|
||||
|
||||
id = None # type: ignore
|
||||
shout = Column(ForeignKey("shout.slug"), primary_key=True)
|
||||
topic = Column(ForeignKey("topic.slug"), primary_key=True)
|
||||
|
||||
|
||||
class TopicFollower(Base):
|
||||
__tablename__ = "topic_followers"
|
||||
|
||||
|
28
orm/user.py
28
orm/user.py
@@ -82,17 +82,25 @@ class User(Base):
|
||||
@staticmethod
|
||||
def init_table():
|
||||
with local_session() as session:
|
||||
default = session.query(User).filter(User.slug == "discours").first()
|
||||
default = session.query(User).filter(User.slug == "anonymous").first()
|
||||
if not default:
|
||||
default = User.create(
|
||||
id=0,
|
||||
email="welcome@discours.io",
|
||||
username="welcome@discours.io",
|
||||
name="Дискурс",
|
||||
slug="discours",
|
||||
userpic="https://discours.io/images/logo-mini.svg",
|
||||
)
|
||||
|
||||
defaul_dict = {
|
||||
"email": "noreply@discours.io",
|
||||
"username": "noreply@discours.io",
|
||||
"name": "Аноним",
|
||||
"slug": "anonymous",
|
||||
}
|
||||
default = User.create(**defaul_dict)
|
||||
session.add(default)
|
||||
discours_dict = {
|
||||
"email": "welcome@discours.io",
|
||||
"username": "welcome@discours.io",
|
||||
"name": "Дискурс",
|
||||
"slug": "discours",
|
||||
}
|
||||
discours = User.create(**discours_dict)
|
||||
session.add(discours)
|
||||
session.commit()
|
||||
User.default_user = default
|
||||
|
||||
async def get_permission(self):
|
||||
|
12
orm/viewed.py
Normal file
12
orm/viewed.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, ForeignKey, Integer
|
||||
from base.orm import Base
|
||||
|
||||
|
||||
class ViewedByDay(Base):
|
||||
__tablename__ = "viewed_by_day"
|
||||
|
||||
id = None
|
||||
shout = Column(ForeignKey("shout.slug"), primary_key=True)
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
value = Column(Integer)
|
Reference in New Issue
Block a user