use user slug as rater in Comment and User ratings

This commit is contained in:
knst-kotov 2022-01-14 15:19:57 +03:00
parent 65fa744ea5
commit 1cc0e3e5df
10 changed files with 17 additions and 33 deletions

View File

@ -11,7 +11,6 @@ class TokenStorage:
await redis.execute("SET", token_key, "True") await redis.execute("SET", token_key, "True")
if auto_delete: if auto_delete:
expire_at = (datetime.now() + timedelta(seconds=life_span)).timestamp() expire_at = (datetime.now() + timedelta(seconds=life_span)).timestamp()
print(expire_at)
await redis.execute("EXPIREAT", token_key, int(expire_at)) await redis.execute("EXPIREAT", token_key, int(expire_at))
@staticmethod @staticmethod

View File

@ -61,7 +61,7 @@ def users(users_by_oid, users_by_slug, users_data):
del user['username'] del user['username']
del user['email'] del user['email']
users_by_slug[user['slug']] = user # public users_by_slug[user['slug']] = user # public
id_map[user['old_id']] = user['id'] id_map[user['old_id']] = user['slug']
counter += 1 counter += 1
for entry in users_data: for entry in users_data:
migrateUser_2stage(entry, id_map) migrateUser_2stage(entry, id_map)

View File

@ -71,14 +71,13 @@ def migrate(entry, shouts_by_oid):
if rater and comment: if rater and comment:
comment_rating_dict = { comment_rating_dict = {
'value': comment_rating_old['value'], 'value': comment_rating_old['value'],
'createdBy': rater.id, 'createdBy': rater.slug,
'comment_id': comment.id 'comment_id': comment.id
} }
cts = comment_rating_old.get('createdAt') cts = comment_rating_old.get('createdAt')
if cts: comment_rating_dict['createdAt'] = date_parse(cts) if cts: comment_rating_dict['createdAt'] = date_parse(cts)
try: try:
comment_rating = CommentRating.create(**comment_rating_dict) comment_rating = CommentRating.create(**comment_rating_dict)
# comment_rating_dict['id'] = comment_rating.id
comment_dict['ratings'].append(comment_rating_dict) comment_dict['ratings'].append(comment_rating_dict)
except Exception as e: except Exception as e:
print(comment_rating_dict) print(comment_rating_dict)

View File

@ -217,11 +217,11 @@ def migrate(entry, users_by_oid, topics_by_oid):
if rater: if rater:
shout_rating_dict = { shout_rating_dict = {
'value': shout_rating_old['value'], 'value': shout_rating_old['value'],
'rater': rater.id, 'rater': rater.slug,
'shout': s.slug 'shout': s.slug
} }
cts = shout_rating_old.get('createdAt') 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) try: shout_rating = ShoutRating.create(**shout_rating_dict)
except sqlalchemy.exc.IntegrityError: pass except sqlalchemy.exc.IntegrityError: pass
shout_dict['ratings'].append(shout_rating_dict) shout_dict['ratings'].append(shout_rating_dict)

View File

@ -91,13 +91,13 @@ def migrate(entry):
def migrate_2stage(entry, id_map): def migrate_2stage(entry, id_map):
for rating_entry in entry.get('ratings',[]): for rating_entry in entry.get('ratings',[]):
rater_old_id = rating_entry['createdBy'] rater_old_id = rating_entry['createdBy']
rater_id = id_map.get(rater_old_id) rater_slug = id_map.get(rater_old_id)
if not rater_id: if not rater_slug:
continue continue
old_id = entry['_id'] old_id = entry['_id']
user_rating_dict = { user_rating_dict = {
'value': rating_entry['value'], 'value': rating_entry['value'],
'rater': rater_id, 'rater': rater_slug,
'user': id_map.get(old_id) 'user': id_map.get(old_id)
} }
with local_session() as session: with local_session() as session:

View File

@ -11,7 +11,7 @@ class CommentRating(Base):
id = None id = None
comment_id = Column(ForeignKey('comment.id'), primary_key = True) 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") createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
value = Column(Integer) value = Column(Integer)

View File

@ -36,7 +36,7 @@ class ShoutRating(Base):
__tablename__ = "shout_rating" __tablename__ = "shout_rating"
id = None 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) shout = Column(ForeignKey('shout.slug'), primary_key = True)
ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp") ts = Column(DateTime, nullable=False, default = datetime.now, comment="Timestamp")
value = Column(Integer) value = Column(Integer)
@ -49,9 +49,7 @@ class ShoutRatingStorage:
@staticmethod @staticmethod
def init(session): def init(session):
#TODO use user slug as rater ShoutRatingStorage.ratings = session.query(ShoutRating).all()
ShoutRatingStorage.ratings = session.query(ShoutRating.shout, ShoutRating.value, User.slug.label("rater")).\
join(User).all()
@staticmethod @staticmethod
async def get_total_rating(shout_slug): async def get_total_rating(shout_slug):

View File

@ -22,8 +22,8 @@ class UserRating(Base):
__tablename__ = "user_rating" __tablename__ = "user_rating"
id = None id = None
rater = Column(ForeignKey('user.id'), primary_key = True) rater = Column(ForeignKey('user.slug'), primary_key = True)
user = Column(ForeignKey('user.id'), primary_key = True) user = Column(ForeignKey('user.slug'), primary_key = True)
value = Column(Integer) value = Column(Integer)
class UserRole(Base): class UserRole(Base):

View File

@ -298,29 +298,22 @@ async def update_shout(_, info, input):
async def rate_shout(_, info, slug, value): async def rate_shout(_, info, slug, value):
auth = info.context["request"].auth auth = info.context["request"].auth
user = info.context["request"].user user = info.context["request"].user
user_id = user.id
with local_session() as session: with local_session() as session:
rating = session.query(ShoutRating).\ 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: if rating:
rating.value = value; rating.value = value;
rating.ts = datetime.now() rating.ts = datetime.now()
session.commit() session.commit()
else: else:
rating = ShoutRating.create( rating = ShoutRating.create(
rater = user_id, rater = user.slug,
shout = slug, shout = slug,
value = value value = value
) )
rating_dict = { await ShoutRatingStorage.update_rating(rating)
"shout" : shout,
"value" : value,
"rater" : user.slug
}
await ShoutRatingStorage.update_rating(rating_dict)
return {"error" : ""} return {"error" : ""}

View File

@ -211,11 +211,6 @@ type Role {
} }
type Rating { type Rating {
rater: Int!
value: Int!
}
type ShoutRating {
rater: String! rater: String!
value: Int! value: Int!
} }
@ -293,7 +288,7 @@ type Comment {
type CommentRating { type CommentRating {
id: Int! id: Int!
comment_id: Int! comment_id: Int!
createdBy: Int! createdBy: String!
createdAt: DateTime! createdAt: DateTime!
value: Int! value: Int!
} }
@ -304,7 +299,7 @@ type Shout {
body: String! body: String!
createdAt: DateTime! createdAt: DateTime!
authors: [User!]! authors: [User!]!
ratings: [ShoutRating] ratings: [Rating]
visibleFor: [User] visibleFor: [User]
community: Int community: Int
cover: String cover: String