topicsByAuthor fix
This commit is contained in:
parent
365c2f25e7
commit
e38d478ef2
|
@ -1,13 +1,12 @@
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
from services.zine.topics import TopicStorage
|
from services.zine.topics import TopicStorage
|
||||||
from orm.shout import Shout
|
|
||||||
from orm.user import User
|
|
||||||
from services.stat.topicstat import TopicStat
|
from services.stat.topicstat import TopicStat
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
from base.resolvers import mutation, query
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from sqlalchemy import and_
|
from sqlalchemy import and_
|
||||||
import random
|
import random
|
||||||
|
from services.zine.shoutscache import ShoutsCache
|
||||||
|
|
||||||
|
|
||||||
@query.field("topicsAll")
|
@query.field("topicsAll")
|
||||||
|
@ -27,49 +26,45 @@ async def topics_by_community(_, info, community):
|
||||||
|
|
||||||
|
|
||||||
@query.field("topicsByAuthor")
|
@query.field("topicsByAuthor")
|
||||||
async def topics_by_author(_, info, author):
|
async def topics_by_author(_, _info, author):
|
||||||
slugs = set()
|
topics = await ShoutsCache.get_shouts_by_author(author)
|
||||||
with local_session() as session:
|
author_topics = set()
|
||||||
shouts = session.query(Shout).filter(Shout.authors.any(User.slug == author))
|
for tpc in topics:
|
||||||
for shout in shouts:
|
tpc = await TopicStorage.topics[tpc.slug]
|
||||||
slugs.update([topic.slug for topic in shout.topics])
|
tpc.stat = await TopicStat.get_stat(tpc.slug)
|
||||||
return await TopicStorage.get_topics(slugs)
|
author_topics.add(tpc)
|
||||||
|
return list(author_topics)
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createTopic")
|
@mutation.field("createTopic")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_topic(_, info, input):
|
async def create_topic(_, _info, inp):
|
||||||
new_topic = Topic.create(**input)
|
new_topic = Topic.create(**inp)
|
||||||
await TopicStorage.add_topic(new_topic)
|
await TopicStorage.update_topic(new_topic)
|
||||||
|
|
||||||
return {"topic": new_topic}
|
return {"topic": new_topic}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("updateTopic")
|
@mutation.field("updateTopic")
|
||||||
@login_required
|
@login_required
|
||||||
async def update_topic(_, info, input):
|
async def update_topic(_, _info, inp):
|
||||||
slug = input["slug"]
|
slug = inp["slug"]
|
||||||
|
|
||||||
session = local_session()
|
session = local_session()
|
||||||
topic = session.query(Topic).filter(Topic.slug == slug).first()
|
topic = session.query(Topic).filter(Topic.slug == slug).first()
|
||||||
|
|
||||||
if not topic:
|
if not topic:
|
||||||
return {"error": "topic not found"}
|
return {"error": "topic not found"}
|
||||||
|
topic.update(**inp)
|
||||||
topic.update(input)
|
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
await TopicStorage.update_topic(topic.slug)
|
||||||
await TopicStorage.add_topic(topic)
|
|
||||||
|
|
||||||
return {"topic": topic}
|
return {"topic": topic}
|
||||||
|
|
||||||
|
|
||||||
def topic_follow(user, slug):
|
async def topic_follow(user, slug):
|
||||||
TopicFollower.create(follower=user.slug, topic=slug)
|
TopicFollower.create(follower=user.slug, topic=slug)
|
||||||
|
await TopicStorage.update_topic(slug)
|
||||||
|
|
||||||
|
|
||||||
def topic_unfollow(user, slug):
|
async def topic_unfollow(user, slug):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
sub = (
|
sub = (
|
||||||
session.query(TopicFollower)
|
session.query(TopicFollower)
|
||||||
|
@ -80,8 +75,10 @@ def topic_unfollow(user, slug):
|
||||||
)
|
)
|
||||||
if not sub:
|
if not sub:
|
||||||
raise Exception("[resolvers.topics] follower not exist")
|
raise Exception("[resolvers.topics] follower not exist")
|
||||||
session.delete(sub)
|
else:
|
||||||
|
session.delete(sub)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
await TopicStorage.update_topic(slug)
|
||||||
|
|
||||||
|
|
||||||
@query.field("topicsRandom")
|
@query.field("topicsRandom")
|
||||||
|
@ -93,4 +90,5 @@ async def topics_random(_, info, amount=12):
|
||||||
topic.stat = topic_stat
|
topic.stat = topic_stat
|
||||||
if topic_stat["shouts"] > 2:
|
if topic_stat["shouts"] > 2:
|
||||||
normalized_topics.push(topic)
|
normalized_topics.push(topic)
|
||||||
return random.sample(normalized_topics, k=min(len(normalized_topics), 12))
|
sample_length = min(len(normalized_topics), amount)
|
||||||
|
return random.sample(normalized_topics, sample_length)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user