resolvers, orm, migration, schema fixes
This commit is contained in:
@@ -1,52 +1,10 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import Column, String, ForeignKey, DateTime
|
||||
from base.orm import Base, local_session
|
||||
import enum
|
||||
from base.orm import Base
|
||||
from sqlalchemy import Enum
|
||||
from services.stat.reacted import ReactedStorage, ReactionKind
|
||||
from services.stat.viewed import ViewedStorage
|
||||
|
||||
class ReactionKind(enum.Enum):
|
||||
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
|
||||
|
||||
def kind_to_rate(kind) -> int:
|
||||
if kind in [
|
||||
ReactionKind.AGREE,
|
||||
ReactionKind.LIKE,
|
||||
ReactionKind.PROOF,
|
||||
ReactionKind.ACCEPT
|
||||
]: return 1
|
||||
elif kind in [
|
||||
ReactionKind.DISAGREE,
|
||||
ReactionKind.DISLIKE,
|
||||
ReactionKind.DISPROOF,
|
||||
ReactionKind.REJECT
|
||||
]: return -1
|
||||
else: return 0
|
||||
|
||||
def get_bookmarked(reactions):
|
||||
c = 0
|
||||
for r in reactions:
|
||||
c += 1 if r.kind in [ ReactionKind.QUOTE, ReactionKind.ASK] else 0
|
||||
return c
|
||||
|
||||
def get_rating(reactions):
|
||||
rating = 0
|
||||
for r in reactions:
|
||||
rating += kind_to_rate(r.kind)
|
||||
return rating
|
||||
|
||||
class Reaction(Base):
|
||||
__tablename__ = 'reaction'
|
||||
body: str = Column(String, nullable=True, comment="Reaction Body")
|
||||
@@ -64,15 +22,10 @@ class Reaction(Base):
|
||||
|
||||
@property
|
||||
async def stat(self):
|
||||
reacted = []
|
||||
try:
|
||||
with local_session() as session:
|
||||
reacted = session.query(Reaction).filter(Reaction.replyTo == self.id).all()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
rrr = await ReactedStorage.get_reaction(self.id)
|
||||
print(rrr[0])
|
||||
return {
|
||||
"viewed": await ViewedStorage.get_reaction(self.id),
|
||||
"reacted": reacted.count(),
|
||||
"rating": get_rating(reacted),
|
||||
"bookmarked": get_bookmarked(reacted)
|
||||
}
|
||||
"reacted": len(rrr),
|
||||
"rating": await ReactedStorage.get_reaction_rating(self.id)
|
||||
}
|
||||
|
20
orm/shout.py
20
orm/shout.py
@@ -3,10 +3,10 @@ from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
from orm.user import User
|
||||
from orm.topic import Topic, ShoutTopic
|
||||
from orm.reaction import Reaction, get_bookmarked
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from orm.reaction import Reaction
|
||||
from services.stat.reacted import ReactedStorage, ReactionKind
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from base.orm import Base, local_session
|
||||
from base.orm import Base
|
||||
|
||||
|
||||
class ShoutReactionsFollower(Base):
|
||||
@@ -63,15 +63,9 @@ class Shout(Base):
|
||||
|
||||
@property
|
||||
async def stat(self):
|
||||
reacted = []
|
||||
try:
|
||||
with local_session() as session:
|
||||
reacted = session.query(Reaction).where(Reaction.shout == self.slug).all()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
rrr = await ReactedStorage.get_shout(self.slug)
|
||||
return {
|
||||
"viewed": await ViewedStorage.get_shout(self.slug),
|
||||
"reacted": await ReactedStorage.get_shout(self.slug),
|
||||
"rating": await ReactedStorage.get_rating(self.slug),
|
||||
"bookmarked": get_bookmarked(reacted)
|
||||
}
|
||||
"reacted": len(rrr),
|
||||
"rating": await ReactedStorage.get_rating(self.slug)
|
||||
}
|
||||
|
Reference in New Issue
Block a user