commit
45e335b72a
|
@ -1,7 +1,7 @@
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
from sqlalchemy.orm import joinedload, aliased
|
from sqlalchemy.orm import joinedload, aliased
|
||||||
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_
|
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_, text, nulls_last
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
|
@ -23,7 +23,7 @@ def add_stat_columns(q):
|
||||||
).label('reacted_stat'),
|
).label('reacted_stat'),
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
(aliased_reaction.body.is_not(None), 1),
|
(aliased_reaction.kind == ReactionKind.COMMENT, 1),
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
).label('commented_stat'),
|
).label('commented_stat'),
|
||||||
|
@ -39,7 +39,11 @@ def add_stat_columns(q):
|
||||||
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
||||||
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
||||||
else_=0)
|
else_=0)
|
||||||
).label('rating_stat'))
|
).label('rating_stat'),
|
||||||
|
func.max(case(
|
||||||
|
(aliased_reaction.kind != ReactionKind.COMMENT, None),
|
||||||
|
else_=aliased_reaction.createdAt
|
||||||
|
)).label('last_comment'))
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -95,7 +99,7 @@ async def load_shout(_, info, slug=None, shout_id=None):
|
||||||
).group_by(Shout.id)
|
).group_by(Shout.id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
[shout, reacted_stat, commented_stat, rating_stat] = session.execute(q).first()
|
[shout, reacted_stat, commented_stat, rating_stat, last_comment] = session.execute(q).first()
|
||||||
|
|
||||||
viewed_stat_query = select().select_from(
|
viewed_stat_query = select().select_from(
|
||||||
Shout
|
Shout
|
||||||
|
@ -180,22 +184,19 @@ async def load_shouts_by(_, info, options):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
q = apply_filters(q, options.get("filters", {}), auth.user_id)
|
q = apply_filters(q, options.get("filters", {}), auth.user_id)
|
||||||
|
|
||||||
order_by = options.get("order_by", Shout.createdAt)
|
order_by = options.get("order_by", Shout.publishedAt)
|
||||||
if order_by == 'reacted':
|
|
||||||
aliased_reaction = aliased(Reaction)
|
|
||||||
q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted'))
|
|
||||||
|
|
||||||
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
||||||
offset = options.get("offset", 0)
|
offset = options.get("offset", 0)
|
||||||
limit = options.get("limit", 10)
|
limit = options.get("limit", 10)
|
||||||
|
|
||||||
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
|
q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset)
|
||||||
|
|
||||||
shouts = []
|
shouts = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_map = {}
|
shouts_map = {}
|
||||||
|
|
||||||
for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique():
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": 0,
|
"viewed": 0,
|
||||||
|
@ -238,8 +239,7 @@ async def get_my_feed(_, info, options):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
subquery = select(Shout.id)
|
subquery = select(Shout.id).join(
|
||||||
subquery = subquery.join(
|
|
||||||
ShoutAuthor
|
ShoutAuthor
|
||||||
).join(
|
).join(
|
||||||
AuthorFollower, AuthorFollower.follower == user_id
|
AuthorFollower, AuthorFollower.follower == user_id
|
||||||
|
@ -264,20 +264,17 @@ async def get_my_feed(_, info, options):
|
||||||
q = apply_filters(q, options.get("filters", {}), user_id)
|
q = apply_filters(q, options.get("filters", {}), user_id)
|
||||||
|
|
||||||
order_by = options.get("order_by", Shout.publishedAt)
|
order_by = options.get("order_by", Shout.publishedAt)
|
||||||
if order_by == 'reacted':
|
|
||||||
aliased_reaction = aliased(Reaction)
|
|
||||||
q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted'))
|
|
||||||
|
|
||||||
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
||||||
offset = options.get("offset", 0)
|
offset = options.get("offset", 0)
|
||||||
limit = options.get("limit", 10)
|
limit = options.get("limit", 10)
|
||||||
|
|
||||||
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
|
q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset)
|
||||||
|
|
||||||
shouts = []
|
shouts = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_map = {}
|
shouts_map = {}
|
||||||
for [shout, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique():
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": 0,
|
"viewed": 0,
|
||||||
|
|
26
test.py
26
test.py
|
@ -1,26 +0,0 @@
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.orm import joinedload
|
|
||||||
|
|
||||||
from ai.preprocess import get_clear_text
|
|
||||||
from base.orm import local_session
|
|
||||||
from orm import Shout, Topic
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
with local_session() as session:
|
|
||||||
q = select(Shout).options(
|
|
||||||
joinedload(Shout.authors),
|
|
||||||
joinedload(Shout.topics),
|
|
||||||
).where(
|
|
||||||
Shout.deletedAt.is_(None)
|
|
||||||
)
|
|
||||||
|
|
||||||
for [shout] in session.execute(q).unique():
|
|
||||||
print(shout.topics)
|
|
||||||
# clear_shout_body = get_clear_text(shout.body)
|
|
||||||
# print(clear_shout_body)
|
|
||||||
#
|
|
||||||
|
|
||||||
topics_q = select(Topic)
|
|
||||||
for [topic] in session.execute(topics_q):
|
|
||||||
print(topic.body)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user