viewed fixed

This commit is contained in:
tonyrewin 2022-11-23 14:30:44 +03:00
parent 2cb152bdb1
commit c6128ac641
6 changed files with 24 additions and 22 deletions

View File

@ -176,7 +176,7 @@ async def login(_, info, email: str, password: str = "", lang: str = "ru"):
}
except InvalidPassword:
print(f"[auth] {email}: invalid password")
raise InvalidPassword("invalid passoword") # contains webserver status
raise InvalidPassword("invalid password") # contains webserver status
# return {"error": "invalid password"}

View File

@ -31,6 +31,7 @@ def apply_filters(q, filters, user=None):
q = q.filter(Shout.createdAt > before)
return q
@query.field("loadShout")
async def load_shout(_, info, slug):
with local_session() as session:

View File

@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from sqlalchemy import and_, asc, desc, select, text, func, column
from sqlalchemy import and_, asc, desc, select, text, func
from sqlalchemy.orm import aliased
from auth.authenticate import login_required
@ -23,12 +23,10 @@ async def get_reaction_stat(reaction_id):
def reactions_follow(user: User, slug: str, auto=False):
with local_session() as session:
following = (
session.query(ShoutReactionsFollower)
.where(and_(
session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.follower == user.slug,
ShoutReactionsFollower.shout == slug
))
.first()
)).first()
)
if not following:
following = ShoutReactionsFollower.create(
@ -43,12 +41,10 @@ def reactions_follow(user: User, slug: str, auto=False):
def reactions_unfollow(user, slug):
with local_session() as session:
following = (
session.query(ShoutReactionsFollower)
.where(and_(
session.query(ShoutReactionsFollower).where(and_(
ShoutReactionsFollower.follower == user.slug,
ShoutReactionsFollower.shout == slug
))
.first()
)).first()
)
if following:
session.delete(following)
@ -199,6 +195,7 @@ async def delete_reaction(_, info, rid):
session.commit()
return {}
def map_result_item(result_item):
reaction = result_item[0]
user = result_item[1]

View File

@ -23,10 +23,12 @@ class UserStorage:
async def get_user(id):
with local_session() as session:
user = (
session.query(User)
.options(selectinload(User.roles), selectinload(User.ratings))
.filter(User.id == id)
.one()
session.query(User).options(
selectinload(User.roles),
selectinload(User.ratings)
).filter(
User.id == id
).one()
)
return user

View File

@ -35,7 +35,7 @@ class ReactedStorage:
@staticmethod
async def get_shout_stat(slug):
viewed = await ViewedStorage.get_shout(slug)
viewed = int(await ViewedStorage.get_shout(slug))
print(viewed)
return {
"viewed": viewed,

View File

@ -3,7 +3,7 @@ from datetime import timedelta, timezone, datetime
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from base.orm import local_session
from sqlalchemy import func, select
from sqlalchemy import func
from orm.shout import ShoutTopic
from orm.viewed import ViewedEntry
from ssl import create_default_context
@ -119,12 +119,14 @@ class ViewedStorage:
if not shout_views:
shout_views = 0
with local_session() as session:
shout_views_q = select(func.sum(ViewedEntry.amount)).where(
try:
shout_views = session.query(func.sum(ViewedEntry.amount)).where(
ViewedEntry.shout == shout_slug
)
shout_views = session.execute(shout_views_q)
).all()[0][0]
self.by_shouts[shout_slug] = shout_views
self.update_topics(session, shout_slug)
except Exception as e:
raise e
return shout_views