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

@ -97,8 +97,8 @@ def migrate_2stage(entry, id_map):
old_id = entry['_id']
user_rating_dict = {
'value': rating_entry['value'],
'rater_id': rater_id,
'user_id': id_map.get(old_id)
'rater': rater_id,
'user': id_map.get(old_id)
}
with local_session() as session:
try:

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)

View File

@ -87,7 +87,7 @@ class ShoutsCache:
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = await ShoutRatingStorage.get_rating(shout.slug)
shout.ratings = await ShoutRatingStorage.get_ratings(shout.slug)
shout.views = await ShoutViewStorage.get_view(shout.slug)
shouts.append(shout)
async with ShoutsCache.lock:
@ -107,7 +107,7 @@ class ShoutsCache:
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = row.rating
shout.ratings = await ShoutRatingStorage.get_ratings(shout.slug)
shout.views = await ShoutViewStorage.get_view(shout.slug)
shouts.append(shout)
async with ShoutsCache.lock:
@ -127,7 +127,7 @@ class ShoutsCache:
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = row.rating
shout.ratings = await ShoutRatingStorage.get_ratings(shout.slug)
shout.views = await ShoutViewStorage.get_view(shout.slug)
shouts.append(shout)
async with ShoutsCache.lock:
@ -147,7 +147,7 @@ class ShoutsCache:
shouts = []
for row in session.execute(stmt):
shout = row.Shout
shout.rating = await ShoutRatingStorage.get_rating(shout.slug)
shout.ratings = await ShoutRatingStorage.get_ratings(shout.slug)
shout.views = row.views
shouts.append(shout)
async with ShoutsCache.lock:
@ -362,7 +362,12 @@ async def get_shout_by_slug(_, info, slug):
shout = session.query(Shout).\
options(select_options).\
filter(Shout.slug == slug).first()
shout.rating = await ShoutRatingStorage.get_rating(slug)
if not shout:
print("shout not exist")
return {} #TODO return error field
shout.ratings = await ShoutRatingStorage.get_ratings(slug)
shout.views = await ShoutViewStorage.get_view(slug)
return shout

View File

@ -216,7 +216,7 @@ type Role {
}
type Rating {
rater_id: Int!
rater: Int!
value: Int!
}