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")
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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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):

View File

@ -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):

View File

@ -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" : ""}

View File

@ -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