From 1cc0e3e5dfe8d6357b71bc5c6cd5c4b32fd2b3a6 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Fri, 14 Jan 2022 15:19:57 +0300 Subject: [PATCH] use user slug as rater in Comment and User ratings --- auth/authorize.py | 1 - migrate.py | 2 +- migration/tables/comments.py | 3 +-- migration/tables/content_items.py | 4 ++-- migration/tables/users.py | 6 +++--- orm/comment.py | 2 +- orm/shout.py | 6 ++---- orm/user.py | 4 ++-- resolvers/zine.py | 13 +++---------- schema.graphql | 9 ++------- 10 files changed, 17 insertions(+), 33 deletions(-) diff --git a/auth/authorize.py b/auth/authorize.py index c9cd573d..5bc8f8a7 100644 --- a/auth/authorize.py +++ b/auth/authorize.py @@ -11,7 +11,6 @@ class TokenStorage: await redis.execute("SET", token_key, "True") if auto_delete: expire_at = (datetime.now() + timedelta(seconds=life_span)).timestamp() - print(expire_at) await redis.execute("EXPIREAT", token_key, int(expire_at)) @staticmethod diff --git a/migrate.py b/migrate.py index d1e1bd3c..0b3eb537 100644 --- a/migrate.py +++ b/migrate.py @@ -61,7 +61,7 @@ def users(users_by_oid, users_by_slug, users_data): del user['username'] del user['email'] users_by_slug[user['slug']] = user # public - id_map[user['old_id']] = user['id'] + id_map[user['old_id']] = user['slug'] counter += 1 for entry in users_data: migrateUser_2stage(entry, id_map) diff --git a/migration/tables/comments.py b/migration/tables/comments.py index 4ae00422..21a9982a 100644 --- a/migration/tables/comments.py +++ b/migration/tables/comments.py @@ -71,14 +71,13 @@ def migrate(entry, shouts_by_oid): if rater and comment: comment_rating_dict = { 'value': comment_rating_old['value'], - 'createdBy': rater.id, + 'createdBy': rater.slug, 'comment_id': comment.id } cts = comment_rating_old.get('createdAt') if cts: comment_rating_dict['createdAt'] = date_parse(cts) try: comment_rating = CommentRating.create(**comment_rating_dict) - # comment_rating_dict['id'] = comment_rating.id comment_dict['ratings'].append(comment_rating_dict) except Exception as e: print(comment_rating_dict) diff --git a/migration/tables/content_items.py b/migration/tables/content_items.py index 16147fd6..f024fe2b 100644 --- a/migration/tables/content_items.py +++ b/migration/tables/content_items.py @@ -217,11 +217,11 @@ def migrate(entry, users_by_oid, topics_by_oid): if rater: shout_rating_dict = { 'value': shout_rating_old['value'], - 'rater': rater.id, + 'rater': rater.slug, 'shout': s.slug } cts = shout_rating_old.get('createdAt') - if cts: shout_rating_dict['rater_id'] = date_parse(cts) + if cts: shout_rating_dict['ts'] = date_parse(cts) try: shout_rating = ShoutRating.create(**shout_rating_dict) except sqlalchemy.exc.IntegrityError: pass shout_dict['ratings'].append(shout_rating_dict) diff --git a/migration/tables/users.py b/migration/tables/users.py index 0d368e9d..fd0f8cac 100644 --- a/migration/tables/users.py +++ b/migration/tables/users.py @@ -91,13 +91,13 @@ def migrate(entry): def migrate_2stage(entry, id_map): for rating_entry in entry.get('ratings',[]): rater_old_id = rating_entry['createdBy'] - rater_id = id_map.get(rater_old_id) - if not rater_id: + rater_slug = id_map.get(rater_old_id) + if not rater_slug: continue old_id = entry['_id'] user_rating_dict = { 'value': rating_entry['value'], - 'rater': rater_id, + 'rater': rater_slug, 'user': id_map.get(old_id) } with local_session() as session: diff --git a/orm/comment.py b/orm/comment.py index 56a7b4a1..a36d24e3 100644 --- a/orm/comment.py +++ b/orm/comment.py @@ -11,7 +11,7 @@ class CommentRating(Base): id = None comment_id = Column(ForeignKey('comment.id'), primary_key = True) - createdBy = Column(ForeignKey('user.id'), primary_key = True) + createdBy = Column(ForeignKey('user.slug'), primary_key = True) createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp") value = Column(Integer) diff --git a/orm/shout.py b/orm/shout.py index 92267877..be28bb9d 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -36,7 +36,7 @@ class ShoutRating(Base): __tablename__ = "shout_rating" id = None - rater = Column(ForeignKey('user.id'), primary_key = True) + rater = Column(ForeignKey('user.slug'), primary_key = True) shout = Column(ForeignKey('shout.slug'), primary_key = True) ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp") value = Column(Integer) @@ -49,9 +49,7 @@ class ShoutRatingStorage: @staticmethod def init(session): - #TODO use user slug as rater - ShoutRatingStorage.ratings = session.query(ShoutRating.shout, ShoutRating.value, User.slug.label("rater")).\ - join(User).all() + ShoutRatingStorage.ratings = session.query(ShoutRating).all() @staticmethod async def get_total_rating(shout_slug): diff --git a/orm/user.py b/orm/user.py index 3a4cfcb3..47f2c9a2 100644 --- a/orm/user.py +++ b/orm/user.py @@ -22,8 +22,8 @@ class UserRating(Base): __tablename__ = "user_rating" id = None - rater = Column(ForeignKey('user.id'), primary_key = True) - user = Column(ForeignKey('user.id'), primary_key = True) + rater = Column(ForeignKey('user.slug'), primary_key = True) + user = Column(ForeignKey('user.slug'), primary_key = True) value = Column(Integer) class UserRole(Base): diff --git a/resolvers/zine.py b/resolvers/zine.py index b67e2927..32d5fa86 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -298,29 +298,22 @@ async def update_shout(_, info, input): async def rate_shout(_, info, slug, value): auth = info.context["request"].auth user = info.context["request"].user - user_id = user.id with local_session() as session: rating = session.query(ShoutRating).\ - filter(and_(ShoutRating.rater == user_id, ShoutRating.shout == slug)).first() + filter(and_(ShoutRating.rater == user.slug, ShoutRating.shout == slug)).first() if rating: rating.value = value; rating.ts = datetime.now() session.commit() else: rating = ShoutRating.create( - rater = user_id, + rater = user.slug, shout = slug, value = value ) - rating_dict = { - "shout" : shout, - "value" : value, - "rater" : user.slug - } - - await ShoutRatingStorage.update_rating(rating_dict) + await ShoutRatingStorage.update_rating(rating) return {"error" : ""} diff --git a/schema.graphql b/schema.graphql index ec826dc8..ed9e478c 100644 --- a/schema.graphql +++ b/schema.graphql @@ -211,11 +211,6 @@ type Role { } type Rating { - rater: Int! - value: Int! -} - -type ShoutRating { rater: String! value: Int! } @@ -293,7 +288,7 @@ type Comment { type CommentRating { id: Int! comment_id: Int! - createdBy: Int! + createdBy: String! createdAt: DateTime! value: Int! } @@ -304,7 +299,7 @@ type Shout { body: String! createdAt: DateTime! authors: [User!]! - ratings: [ShoutRating] + ratings: [Rating] visibleFor: [User] community: Int cover: String