format and lint orm

This commit is contained in:
2022-09-03 13:50:14 +03:00
parent 85892a88bc
commit a89a44f660
55 changed files with 4811 additions and 4174 deletions

View File

@@ -8,68 +8,76 @@ from base.resolvers import mutation, query
from auth.authenticate import login_required
from sqlalchemy import and_
@query.field("topicsAll")
async def topics_all(_, info, page = 1, size = 50):
topics = await TopicStorage.get_topics_all(page, size)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
async def topics_all(_, info, page=1, size=50):
topics = await TopicStorage.get_topics_all(page, size)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
@query.field("topicsByCommunity")
async def topics_by_community(_, info, community):
topics = await TopicStorage.get_topics_by_community(community)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
topics = await TopicStorage.get_topics_by_community(community)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
@query.field("topicsByAuthor")
async def topics_by_author(_, info, author):
slugs = set()
with local_session() as session:
shouts = session.query(Shout).\
filter(Shout.authors.any(User.slug == author))
for shout in shouts:
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
slugs = set()
with local_session() as session:
shouts = session.query(Shout).filter(Shout.authors.any(User.slug == author))
for shout in shouts:
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
@mutation.field("createTopic")
@login_required
async def create_topic(_, info, input):
new_topic = Topic.create(**input)
await TopicStorage.add_topic(new_topic)
new_topic = Topic.create(**input)
await TopicStorage.add_topic(new_topic)
return {"topic": new_topic}
return { "topic" : new_topic }
@mutation.field("updateTopic")
@login_required
async def update_topic(_, info, input):
slug = input["slug"]
slug = input["slug"]
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
return { "error" : "topic not found" }
if not topic:
return {"error": "topic not found"}
topic.update(input)
session.commit()
session.close()
topic.update(input)
session.commit()
session.close()
await TopicStorage.add_topic(topic)
await TopicStorage.add_topic(topic)
return {"topic": topic}
return { "topic" : topic }
def topic_follow(user, slug):
TopicFollower.create(
follower = user.slug,
topic = slug)
TopicFollower.create(follower=user.slug, topic=slug)
def topic_unfollow(user, slug):
with local_session() as session:
sub = session.query(TopicFollower).\
filter(and_(TopicFollower.follower == user.slug, TopicFollower.topic == slug)).\
first()
if not sub:
raise Exception("[resolvers.topics] follower not exist")
session.delete(sub)
session.commit()
with local_session() as session:
sub = (
session.query(TopicFollower)
.filter(
and_(TopicFollower.follower == user.slug, TopicFollower.topic == slug)
)
.first()
)
if not sub:
raise Exception("[resolvers.topics] follower not exist")
session.delete(sub)
session.commit()