typechecker-column-fix

This commit is contained in:
Untone 2024-03-12 10:52:32 +03:00
parent 04f7231fe9
commit 91ffcb85df
3 changed files with 20 additions and 21 deletions

View File

@ -2,7 +2,7 @@ import asyncio
import json import json
import time import time
from sqlalchemy import select, or_, and_, text, desc from sqlalchemy import select, or_, and_, text, desc, literal
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
from sqlalchemy_searchable import search from sqlalchemy_searchable import search
@ -52,7 +52,7 @@ async def get_author(_, _info, slug='', author_id=None):
if author_id: if author_id:
cache = await redis.execute('GET', f'id:{author_id}:author') cache = await redis.execute('GET', f'id:{author_id}:author')
logger.debug(f'result from cache: {cache}') logger.debug(f'result from cache: {cache}')
q = select(Author).where(Author.id == author_id) q = select(Author).where(Author.id == literal(author_id))
author_dict = None author_dict = None
if cache: if cache:
author_dict = json.loads(cache) author_dict = json.loads(cache)
@ -257,7 +257,6 @@ async def get_author_followers(_, _info, slug: str):
) )
if author_id: if author_id:
cached = await redis.execute('GET', f'author:{author_id}:followers') cached = await redis.execute('GET', f'author:{author_id}:followers')
results = []
if not cached: if not cached:
author_follower_alias = aliased(AuthorFollower, name='af') author_follower_alias = aliased(AuthorFollower, name='af')
q = select(Author).join( q = select(Author).join(
@ -287,6 +286,6 @@ async def get_author_followers(_, _info, slug: str):
@query.field('search_authors') @query.field('search_authors')
def search_authors(_, info, what: str): def search_authors(_, _info, what: str):
q = search(select(Author), what) q = search(select(Author), what)
return get_with_stat(q) return get_with_stat(q)

View File

@ -2,7 +2,7 @@ import json
import time import time
from typing import List from typing import List
from sqlalchemy import select, or_ from sqlalchemy import select, or_, column
from sqlalchemy.sql import and_ from sqlalchemy.sql import and_
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
@ -34,7 +34,7 @@ async def follow(_, info, what, slug):
user_id = info.context.get('user_id') user_id = info.context.get('user_id')
if not user_id: if not user_id:
return {"error": "unauthorized"} return {"error": "unauthorized"}
[follower] = get_with_stat(select(Author).select_from(Author).filter(Author.user == user_id)) [follower] = get_with_stat(select(Author).select_from(Author).filter(column(Author.user) == user_id))
if not follower: if not follower:
return {"error": "cant find follower"} return {"error": "cant find follower"}
@ -42,7 +42,7 @@ async def follow(_, info, what, slug):
error = author_follow(follower.id, slug) error = author_follow(follower.id, slug)
if not error: if not error:
logger.debug(f'@{follower.slug} followed @{slug}') logger.debug(f'@{follower.slug} followed @{slug}')
[author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug)) [author] = get_with_stat(select(Author).select_from(Author).where(column(Author.slug) == slug))
if not author: if not author:
return {"error": "author is not found"} return {"error": "author is not found"}
follows = await update_follows_for_author(follower, 'author', author.dict(), True) follows = await update_follows_for_author(follower, 'author', author.dict(), True)
@ -52,7 +52,7 @@ async def follow(_, info, what, slug):
elif what == 'TOPIC': elif what == 'TOPIC':
error = topic_follow(follower.id, slug) error = topic_follow(follower.id, slug)
if not error: if not error:
[topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) [topic] = get_with_stat(select(Topic).where(column(Topic.slug) == slug))
if not topic: if not topic:
return {"error": "topic is not found"} return {"error": "topic is not found"}
follows = await update_follows_for_author(follower, 'topic', topic.dict(), True) follows = await update_follows_for_author(follower, 'topic', topic.dict(), True)
@ -63,7 +63,7 @@ async def follow(_, info, what, slug):
elif what == 'SHOUT': elif what == 'SHOUT':
error = reactions_follow(follower.id, slug) error = reactions_follow(follower.id, slug)
if not error: if not error:
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) [shout] = local_session().execute(select(Shout).where(column(Shout.slug) == slug))
if not shout: if not shout:
return {"error": "cant find shout"} return {"error": "cant find shout"}
follows = await update_follows_for_author(follower, 'shout', shout.dict(), True) follows = await update_follows_for_author(follower, 'shout', shout.dict(), True)
@ -79,7 +79,7 @@ async def unfollow(_, info, what, slug):
user_id = info.context.get('user_id') user_id = info.context.get('user_id')
if not user_id: if not user_id:
return {"error": "unauthorized"} return {"error": "unauthorized"}
follower_query = select(Author).filter(Author.user == user_id) follower_query = select(Author).filter(column(Author.user) == user_id)
[follower] = get_with_stat(follower_query) [follower] = get_with_stat(follower_query)
if not follower: if not follower:
return {"error": "follower profile is not found"} return {"error": "follower profile is not found"}
@ -88,7 +88,7 @@ async def unfollow(_, info, what, slug):
error = author_unfollow(follower.id, slug) error = author_unfollow(follower.id, slug)
if not error: if not error:
logger.info(f'@{follower.slug} unfollowing @{slug}') logger.info(f'@{follower.slug} unfollowing @{slug}')
[author] = get_with_stat(select(Author).where(Author.slug == slug)) [author] = get_with_stat(select(Author).where(column(Author.slug) == slug))
if not author: if not author:
return {"error": "cant find author"} return {"error": "cant find author"}
_followers = await update_followers_for_author(follower, author, False) _followers = await update_followers_for_author(follower, author, False)
@ -99,7 +99,7 @@ async def unfollow(_, info, what, slug):
error = topic_unfollow(follower.id, slug) error = topic_unfollow(follower.id, slug)
if not error: if not error:
logger.info(f'@{follower.slug} unfollowing §{slug}') logger.info(f'@{follower.slug} unfollowing §{slug}')
[topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) [topic] = get_with_stat(select(Topic).where(column(Topic.slug) == slug))
if not topic: if not topic:
return {"error": "cant find topic"} return {"error": "cant find topic"}
follows = await update_follows_for_author(follower, 'topic', topic.dict(), False) follows = await update_follows_for_author(follower, 'topic', topic.dict(), False)
@ -111,7 +111,7 @@ async def unfollow(_, info, what, slug):
error = reactions_unfollow(follower.id, slug) error = reactions_unfollow(follower.id, slug)
if not error: if not error:
logger.info(f'@{follower.slug} unfollowing §{slug}') logger.info(f'@{follower.slug} unfollowing §{slug}')
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) [shout] = local_session().execute(select(Shout).where(column(Shout.slug) == slug))
if not shout: if not shout:
return {"error": "cant find shout"} return {"error": "cant find shout"}
if not error: if not error:
@ -276,8 +276,8 @@ def author_unfollow(follower_id, slug):
def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]: def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
q = select(Author) q = select(Author)
q = ( q = (
q.join(TopicFollower, TopicFollower.follower == Author.id) q.join(TopicFollower, column(TopicFollower.follower) == Author.id)
.join(Topic, Topic.id == TopicFollower.topic) .join(Topic, column(Topic.id) == column(TopicFollower.topic))
.filter(or_(Topic.slug == slug, Topic.id == topic_id)) .filter(or_(Topic.slug == slug, Topic.id == topic_id))
) )
return get_with_stat(q) return get_with_stat(q)

View File

@ -98,7 +98,7 @@ async def update_followers_for_author(
@event.listens_for(Shout, 'after_insert') @event.listens_for(Shout, 'after_insert')
@event.listens_for(Shout, 'after_update') @event.listens_for(Shout, 'after_update')
def after_shouts_update(mapper, connection, shout: Shout): def after_shouts_update(_mapper, _connection, shout: Shout):
# Main query to get authors associated with the shout through ShoutAuthor # Main query to get authors associated with the shout through ShoutAuthor
authors_query = ( authors_query = (
select(Author) select(Author)
@ -155,7 +155,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
@event.listens_for(Author, 'after_insert') @event.listens_for(Author, 'after_insert')
@event.listens_for(Author, 'after_update') @event.listens_for(Author, 'after_update')
def after_author_update(mapper, connection, author: Author): def after_author_update(_mapper, _connection, author: Author):
q = select(Author).where(Author.id == author.id) q = select(Author).where(Author.id == author.id)
result = get_with_stat(q) result = get_with_stat(q)
if result: if result:
@ -164,28 +164,28 @@ def after_author_update(mapper, connection, author: Author):
@event.listens_for(TopicFollower, 'after_insert') @event.listens_for(TopicFollower, 'after_insert')
def after_topic_follower_insert(mapper, connection, target: TopicFollower): def after_topic_follower_insert(_mapper, _connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, True) handle_topic_follower_change(target.topic, target.follower, True)
) )
@event.listens_for(TopicFollower, 'after_delete') @event.listens_for(TopicFollower, 'after_delete')
def after_topic_follower_delete(mapper, connection, target: TopicFollower): def after_topic_follower_delete(_mapper, _connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, False) handle_topic_follower_change(target.topic, target.follower, False)
) )
@event.listens_for(AuthorFollower, 'after_insert') @event.listens_for(AuthorFollower, 'after_insert')
def after_author_follower_insert(mapper, connection, target: AuthorFollower): def after_author_follower_insert(_mapper, _connection, target: AuthorFollower):
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, True) handle_author_follower_change(target.author, target.follower, True)
) )
@event.listens_for(AuthorFollower, 'after_delete') @event.listens_for(AuthorFollower, 'after_delete')
def after_author_follower_delete(mapper, connection, target: AuthorFollower): def after_author_follower_delete(_mapper, _connection, target: AuthorFollower):
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, False) handle_author_follower_change(target.author, target.follower, False)
) )