configured isort, black, flake8
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
from sqlalchemy import and_, select, distinct, func
|
||||
from sqlalchemy import and_, distinct, func, select
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from auth.authenticate import login_required
|
||||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from orm.shout import ShoutTopic, ShoutAuthor
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from orm import User
|
||||
from orm.shout import ShoutAuthor, ShoutTopic
|
||||
from orm.topic import Topic, TopicFollower
|
||||
|
||||
|
||||
def add_topic_stat_columns(q):
|
||||
aliased_shout_author = aliased(ShoutAuthor)
|
||||
aliased_topic_follower = aliased(TopicFollower)
|
||||
|
||||
q = q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic).add_columns(
|
||||
func.count(distinct(ShoutTopic.shout)).label('shouts_stat')
|
||||
).outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout).add_columns(
|
||||
func.count(distinct(aliased_shout_author.user)).label('authors_stat')
|
||||
).outerjoin(aliased_topic_follower).add_columns(
|
||||
func.count(distinct(aliased_topic_follower.follower)).label('followers_stat')
|
||||
q = (
|
||||
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
|
||||
.add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat"))
|
||||
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
|
||||
.add_columns(func.count(distinct(aliased_shout_author.user)).label("authors_stat"))
|
||||
.outerjoin(aliased_topic_follower)
|
||||
.add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat"))
|
||||
)
|
||||
|
||||
q = q.group_by(Topic.id)
|
||||
@@ -28,11 +29,7 @@ def add_topic_stat_columns(q):
|
||||
|
||||
def add_stat(topic, stat_columns):
|
||||
[shouts_stat, authors_stat, followers_stat] = stat_columns
|
||||
topic.stat = {
|
||||
"shouts": shouts_stat,
|
||||
"authors": authors_stat,
|
||||
"followers": followers_stat
|
||||
}
|
||||
topic.stat = {"shouts": shouts_stat, "authors": authors_stat, "followers": followers_stat}
|
||||
|
||||
return topic
|
||||
|
||||
@@ -125,7 +122,8 @@ def topic_follow(user_id, slug):
|
||||
session.add(following)
|
||||
session.commit()
|
||||
return True
|
||||
except:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -133,18 +131,17 @@ def topic_unfollow(user_id, slug):
|
||||
try:
|
||||
with local_session() as session:
|
||||
sub = (
|
||||
session.query(TopicFollower).join(Topic).filter(
|
||||
and_(
|
||||
TopicFollower.follower == user_id,
|
||||
Topic.slug == slug
|
||||
)
|
||||
).first()
|
||||
session.query(TopicFollower)
|
||||
.join(Topic)
|
||||
.filter(and_(TopicFollower.follower == user_id, Topic.slug == slug))
|
||||
.first()
|
||||
)
|
||||
if sub:
|
||||
session.delete(sub)
|
||||
session.commit()
|
||||
return True
|
||||
except:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
return False
|
||||
|
||||
|
Reference in New Issue
Block a user