return all rating for shout; unify rating fields

This commit is contained in:
knst-kotov
2021-12-14 19:37:49 +03:00
parent 64a728de41
commit 4f76f85bed
5 changed files with 23 additions and 12 deletions

View File

@@ -52,11 +52,17 @@ class ShoutRatingStorage:
ShoutRatingStorage.ratings = session.query(ShoutRating).all()
@staticmethod
async def get_rating(shout_slug):
async def get_total_rating(shout_slug):
async with ShoutRatingStorage.lock:
shout_ratings = list(filter(lambda x: x.shout == shout_slug, ShoutRatingStorage.ratings))
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
@staticmethod
async def get_ratings(shout_slug):
async with ShoutRatingStorage.lock:
shout_ratings = list(filter(lambda x: x.shout == shout_slug, ShoutRatingStorage.ratings))
return shout_ratings
@staticmethod
async def update_rating(new_rating):
async with ShoutRatingStorage.lock:

View File

@@ -22,8 +22,8 @@ class UserRating(Base):
__tablename__ = "user_rating"
id = None
rater_id = Column(ForeignKey('user.id'), primary_key = True)
user_id = Column(ForeignKey('user.id'), primary_key = True)
rater = Column(ForeignKey('user.id'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True)
value = Column(Integer)
class UserRole(Base):
@@ -51,7 +51,7 @@ class User(Base):
links: JSONType = Column(JSONType, nullable=True, comment="Links")
oauth: str = Column(String, nullable=True)
notifications = relationship(lambda: UserNotifications)
ratings = relationship(UserRating, foreign_keys=UserRating.user_id)
ratings = relationship(UserRating, foreign_keys=UserRating.user)
roles = relationship(lambda: Role, secondary=UserRole.__tablename__)
old_id: str = Column(String, nullable = True)