ShoutRatingStorage refactor
This commit is contained in:
parent
c32fc43538
commit
0a9949caa3
|
@ -16,5 +16,5 @@ Operation.init_table()
|
||||||
Resource.init_table()
|
Resource.init_table()
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
rating_storage = ShoutRatingStorage(session)
|
ShoutRatingStorage.init(session)
|
||||||
ShoutViewStorage.init(session)
|
ShoutViewStorage.init(session)
|
||||||
|
|
33
orm/shout.py
33
orm/shout.py
|
@ -43,21 +43,30 @@ class ShoutRating(Base):
|
||||||
|
|
||||||
class ShoutRatingStorage:
|
class ShoutRatingStorage:
|
||||||
|
|
||||||
def __init__(self, session):
|
ratings = []
|
||||||
self.ratings = session.query(ShoutRating).all()
|
|
||||||
|
|
||||||
def get_rating(self, shout_id):
|
lock = asyncio.Lock()
|
||||||
shout_ratings = list(filter(lambda x: x.shout_id == shout_id, self.ratings))
|
|
||||||
|
@staticmethod
|
||||||
|
def init(session):
|
||||||
|
ShoutRatingStorage.ratings = session.query(ShoutRating).all()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get_rating(shout_id):
|
||||||
|
async with ShoutRatingStorage.lock:
|
||||||
|
shout_ratings = list(filter(lambda x: x.shout_id == shout_id, ShoutRatingStorage.ratings))
|
||||||
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
|
return reduce((lambda x, y: x + y.value), shout_ratings, 0)
|
||||||
|
|
||||||
def update_rating(self, new_rating):
|
@staticmethod
|
||||||
rating = next((x for x in self.ratings \
|
async def update_rating(new_rating):
|
||||||
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None)
|
async with ShoutRatingStorage.lock:
|
||||||
if rating:
|
rating = next((x for x in ShoutRatingStorage.ratings \
|
||||||
rating.value = new_rating.value
|
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None)
|
||||||
rating.ts = new_rating.ts
|
if rating:
|
||||||
else:
|
rating.value = new_rating.value
|
||||||
self.ratings.append(new_rating)
|
rating.ts = new_rating.ts
|
||||||
|
else:
|
||||||
|
ShoutRatingStorage.ratings.append(new_rating)
|
||||||
|
|
||||||
|
|
||||||
class ShoutViewByDay(Base):
|
class ShoutViewByDay(Base):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\
|
from orm import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay, User, Community, Resource,\
|
||||||
rating_storage, ShoutViewStorage
|
ShoutRatingStorage, ShoutViewStorage
|
||||||
from orm.base import local_session
|
from orm.base import local_session
|
||||||
|
|
||||||
from resolvers.base import mutation, query
|
from resolvers.base import mutation, query
|
||||||
|
@ -280,7 +280,7 @@ async def rate_shout(_, info, shout_id, value):
|
||||||
value = value
|
value = value
|
||||||
)
|
)
|
||||||
|
|
||||||
rating_storage.update_rating(rating)
|
await ShoutRatingStorage.update_rating(rating)
|
||||||
|
|
||||||
return {"error" : ""}
|
return {"error" : ""}
|
||||||
|
|
||||||
|
@ -299,6 +299,6 @@ async def get_shout_by_slug(_, info, slug):
|
||||||
shout = session.query(Shout).\
|
shout = session.query(Shout).\
|
||||||
options(select_options).\
|
options(select_options).\
|
||||||
filter(Shout.slug == slug).first()
|
filter(Shout.slug == slug).first()
|
||||||
shout.rating = rating_storage.get_rating(shout.id)
|
shout.rating = await ShoutRatingStorage.get_rating(shout.id)
|
||||||
shout.views = await ShoutViewStorage.get_view(shout.id)
|
shout.views = await ShoutViewStorage.get_view(shout.id)
|
||||||
return shout
|
return shout
|
||||||
|
|
Loading…
Reference in New Issue
Block a user