From 5341bb80a5ebe2a65026eec3ccfd0b8793c719a6 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Tue, 11 Jan 2022 16:33:25 +0300 Subject: [PATCH] user slug as rater in shout ratings --- orm/shout.py | 4 +++- resolvers/zine.py | 11 +++++++++-- schema.graphql | 7 ++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/orm/shout.py b/orm/shout.py index a13a49fd..92267877 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -49,7 +49,9 @@ class ShoutRatingStorage: @staticmethod def init(session): - ShoutRatingStorage.ratings = session.query(ShoutRating).all() + #TODO use user slug as rater + ShoutRatingStorage.ratings = session.query(ShoutRating.shout, ShoutRating.value, User.slug.label("rater")).\ + join(User).all() @staticmethod async def get_total_rating(shout_slug): diff --git a/resolvers/zine.py b/resolvers/zine.py index 1b81f983..b67e2927 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -297,7 +297,8 @@ async def update_shout(_, info, input): @login_required async def rate_shout(_, info, slug, value): auth = info.context["request"].auth - user_id = auth.user_id + user = info.context["request"].user + user_id = user.id with local_session() as session: rating = session.query(ShoutRating).\ @@ -313,7 +314,13 @@ async def rate_shout(_, info, slug, value): value = value ) - await ShoutRatingStorage.update_rating(rating) + rating_dict = { + "shout" : shout, + "value" : value, + "rater" : user.slug + } + + await ShoutRatingStorage.update_rating(rating_dict) return {"error" : ""} diff --git a/schema.graphql b/schema.graphql index 67f66278..fadf2f37 100644 --- a/schema.graphql +++ b/schema.graphql @@ -218,6 +218,11 @@ type Rating { value: Int! } +type ShoutRating { + rater: String! + value: Int! +} + type Notification { kind: String! # unique primary key template: String! @@ -302,7 +307,7 @@ type Shout { body: String! createdAt: DateTime! authors: [User!]! - ratings: [Rating] + ratings: [ShoutRating] visibleFor: [User] community: Int cover: String