diff --git a/migration/tables/users.py b/migration/tables/users.py index eec4b1c6..0d368e9d 100644 --- a/migration/tables/users.py +++ b/migration/tables/users.py @@ -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: diff --git a/orm/shout.py b/orm/shout.py index 0b84d787..6ee56656 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -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: diff --git a/orm/user.py b/orm/user.py index 7cde7e3b..3a4cfcb3 100644 --- a/orm/user.py +++ b/orm/user.py @@ -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) diff --git a/resolvers/zine.py b/resolvers/zine.py index 6f597b39..ad7de6d6 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -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 diff --git a/schema.graphql b/schema.graphql index 631d014c..0ae46e77 100644 --- a/schema.graphql +++ b/schema.graphql @@ -216,7 +216,7 @@ type Role { } type Rating { - rater_id: Int! + rater: Int! value: Int! }