diff --git a/orm/base.py b/orm/base.py index 4df84b58..0904d1f6 100644 --- a/orm/base.py +++ b/orm/base.py @@ -7,17 +7,7 @@ from sqlalchemy.sql.schema import Table 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, - #pool_size=10, - #max_overflow=2, - #pool_recycle=300, - pool_pre_ping=True, - #pool_use_lifo=True - future=True - ) +engine = create_engine(DB_URL, convert_unicode=True, echo=False) T = TypeVar("T") diff --git a/orm/shout.py b/orm/shout.py index afe5d4d5..329c9691 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -35,7 +35,7 @@ class ShoutRating(Base): id = None rater_id = Column(ForeignKey('user.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) class ShoutRatingStorage: @@ -48,8 +48,8 @@ class ShoutRatingStorage: return reduce((lambda x, y: x + y.value), shout_ratings, 0) def update_rating(self, new_rating): - rating = next(x for x in self.ratings \ - if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id) + rating = next((x for x in self.ratings \ + if x.rater_id == new_rating.rater_id and x.shout_id == new_rating.shout_id), None) if rating: rating.value = new_rating.value rating.ts = new_rating.ts diff --git a/resolvers/zine.py b/resolvers/zine.py index e0c12ef3..a10b2397 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -12,7 +12,7 @@ import asyncio from datetime import datetime, timedelta from pathlib import Path -from sqlalchemy import select, func, desc +from sqlalchemy import select, func, desc, and_ from sqlalchemy.orm import selectinload class GitTask: @@ -260,13 +260,39 @@ async def update_shout(_, info, id, input): "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") 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: shout = session.query(Shout).\ - options(selectinload(Shout.authors)).\ - options(selectinload(Shout.comments)).\ - options(selectinload(Shout.topics)).\ + options(select_options).\ filter(Shout.slug == slug).first() shout.rating = rating_storage.get_rating(shout.id) shout.views = view_storage.get_view(shout.id) diff --git a/schema.graphql b/schema.graphql index f7445f06..f69cedcc 100644 --- a/schema.graphql +++ b/schema.graphql @@ -68,7 +68,7 @@ type Mutation { createShout(input: ShoutInput!): ShoutResult! updateShout(id: Int!, input: ShoutInput!): ShoutResult! deleteShout(id: Int!): Result! - rateShout(id: Int!, value: Int!): Result! + rateShout(shout_id: Int!, value: Int!): Result! # user profile # rateUser(value: Int!): Result!