add rateShout
This commit is contained in:
parent
9a5ec80b12
commit
f23d375b4d
12
orm/base.py
12
orm/base.py
|
@ -7,17 +7,7 @@ from sqlalchemy.sql.schema import Table
|
||||||
|
|
||||||
from settings import DB_URL
|
from settings import DB_URL
|
||||||
|
|
||||||
# engine = create_engine(DB_URL, convert_unicode=True, echo=False)
|
engine = create_engine(DB_URL, convert_unicode=True, echo=False)
|
||||||
engine = create_engine(DB_URL,
|
|
||||||
convert_unicode=True,
|
|
||||||
echo=False,
|
|
||||||
#pool_size=10,
|
|
||||||
#max_overflow=2,
|
|
||||||
#pool_recycle=300,
|
|
||||||
pool_pre_ping=True,
|
|
||||||
#pool_use_lifo=True
|
|
||||||
future=True
|
|
||||||
)
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ class ShoutRating(Base):
|
||||||
id = None
|
id = None
|
||||||
rater_id = Column(ForeignKey('user.id'), primary_key = True)
|
rater_id = Column(ForeignKey('user.id'), primary_key = True)
|
||||||
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
shout_id = Column(ForeignKey('shout.id'), primary_key = True)
|
||||||
ts: str = 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)
|
||||||
|
|
||||||
class ShoutRatingStorage:
|
class ShoutRatingStorage:
|
||||||
|
@ -48,8 +48,8 @@ class ShoutRatingStorage:
|
||||||
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):
|
def update_rating(self, new_rating):
|
||||||
rating = next(x for x in self.ratings \
|
rating = next((x for x in self.ratings \
|
||||||
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id)
|
if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None)
|
||||||
if rating:
|
if rating:
|
||||||
rating.value = new_rating.value
|
rating.value = new_rating.value
|
||||||
rating.ts = new_rating.ts
|
rating.ts = new_rating.ts
|
||||||
|
|
|
@ -12,7 +12,7 @@ import asyncio
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from sqlalchemy import select, func, desc
|
from sqlalchemy import select, func, desc, and_
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
class GitTask:
|
class GitTask:
|
||||||
|
@ -260,13 +260,39 @@ async def update_shout(_, info, id, input):
|
||||||
"shout" : shout
|
"shout" : shout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@mutation.field("rateShout")
|
||||||
|
@login_required
|
||||||
|
async def rate_shout(_, info, shout_id, value):
|
||||||
|
auth = info.context["request"].auth
|
||||||
|
user_id = auth.user_id
|
||||||
|
|
||||||
|
with local_session() as session:
|
||||||
|
rating = session.query(ShoutRating).\
|
||||||
|
filter(and_(ShoutRating.rater_id == user_id, ShoutRating.shout_id == shout_id)).first()
|
||||||
|
if rating:
|
||||||
|
rating.value = value;
|
||||||
|
rating.ts = datetime.now()
|
||||||
|
session.commit()
|
||||||
|
else:
|
||||||
|
rating = ShoutRating.create(
|
||||||
|
rater_id = user_id,
|
||||||
|
shout_id = shout_id,
|
||||||
|
value = value
|
||||||
|
)
|
||||||
|
|
||||||
|
rating_storage.update_rating(rating)
|
||||||
|
|
||||||
|
return {"error" : ""}
|
||||||
|
|
||||||
@query.field("getShoutBySlug")
|
@query.field("getShoutBySlug")
|
||||||
async def get_shout_by_slug(_, info, slug):
|
async def get_shout_by_slug(_, info, slug):
|
||||||
|
slug_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
|
||||||
|
slug_fields = set(["authors", "comments", "topics"]).intersection(slug_fields)
|
||||||
|
select_options = [selectinload(getattr(Shout, field)) for field in slug_fields]
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shout = session.query(Shout).\
|
shout = session.query(Shout).\
|
||||||
options(selectinload(Shout.authors)).\
|
options(select_options).\
|
||||||
options(selectinload(Shout.comments)).\
|
|
||||||
options(selectinload(Shout.topics)).\
|
|
||||||
filter(Shout.slug == slug).first()
|
filter(Shout.slug == slug).first()
|
||||||
shout.rating = rating_storage.get_rating(shout.id)
|
shout.rating = rating_storage.get_rating(shout.id)
|
||||||
shout.views = view_storage.get_view(shout.id)
|
shout.views = view_storage.get_view(shout.id)
|
||||||
|
|
|
@ -68,7 +68,7 @@ type Mutation {
|
||||||
createShout(input: ShoutInput!): ShoutResult!
|
createShout(input: ShoutInput!): ShoutResult!
|
||||||
updateShout(id: Int!, input: ShoutInput!): ShoutResult!
|
updateShout(id: Int!, input: ShoutInput!): ShoutResult!
|
||||||
deleteShout(id: Int!): Result!
|
deleteShout(id: Int!): Result!
|
||||||
rateShout(id: Int!, value: Int!): Result!
|
rateShout(shout_id: Int!, value: Int!): Result!
|
||||||
|
|
||||||
# user profile
|
# user profile
|
||||||
# rateUser(value: Int!): Result!
|
# rateUser(value: Int!): Result!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user