core/resolvers/zine/load.py

138 lines
4.2 KiB
Python
Raw Normal View History

2022-11-23 14:09:35 +00:00
from datetime import datetime, timedelta, timezone
2022-11-28 13:37:05 +00:00
from sqlalchemy.orm import joinedload, aliased
2022-11-29 12:36:46 +00:00
from sqlalchemy.sql.expression import desc, asc, select, func
from base.orm import local_session
2022-11-21 08:13:57 +00:00
from base.resolvers import query
from orm import ViewedEntry
from orm.shout import Shout, ShoutAuthor
2022-11-29 12:36:46 +00:00
from orm.reaction import Reaction
2022-11-28 22:16:39 +00:00
from resolvers.zine._common import add_common_stat_columns
2022-11-15 02:36:30 +00:00
2022-11-28 20:29:02 +00:00
def add_stat_columns(q):
q = q.outerjoin(ViewedEntry).add_columns(func.sum(ViewedEntry.amount).label('viewed_stat'))
2022-11-25 18:31:53 +00:00
2022-11-28 22:16:39 +00:00
return add_common_stat_columns(q)
2022-11-22 03:11:26 +00:00
def apply_filters(q, filters, user=None):
2022-11-28 20:29:02 +00:00
2022-11-21 08:13:57 +00:00
if filters.get("reacted") and user:
2022-11-29 12:36:46 +00:00
q.join(Reaction, Reaction.createdBy == user.id)
2022-11-28 20:29:02 +00:00
2022-11-26 01:44:40 +00:00
v = filters.get("visibility")
if v == "public":
2022-11-21 08:13:57 +00:00
q = q.filter(Shout.visibility == filters.get("visibility"))
2022-11-26 01:44:40 +00:00
if v == "community":
q = q.filter(Shout.visibility.in_(["public", "community"]))
2022-11-28 20:29:02 +00:00
2022-11-21 08:13:57 +00:00
if filters.get("layout"):
q = q.filter(Shout.layout == filters.get("layout"))
if filters.get("author"):
q = q.filter(Shout.authors.any(slug=filters.get("author")))
if filters.get("topic"):
q = q.filter(Shout.topics.any(slug=filters.get("topic")))
if filters.get("title"):
q = q.filter(Shout.title.ilike(f'%{filters.get("title")}%'))
if filters.get("body"):
q = q.filter(Shout.body.ilike(f'%{filters.get("body")}%s'))
if filters.get("days"):
2022-11-23 14:09:35 +00:00
before = datetime.now(tz=timezone.utc) - timedelta(days=int(filters.get("days")) or 30)
2022-11-21 08:13:57 +00:00
q = q.filter(Shout.createdAt > before)
2022-11-25 18:31:53 +00:00
return q
@query.field("loadShout")
async def load_shout(_, info, slug):
2022-11-18 02:19:10 +00:00
with local_session() as session:
q = select(Shout).options(
2022-11-23 21:53:53 +00:00
joinedload(Shout.authors),
joinedload(Shout.topics),
)
2022-11-25 18:31:53 +00:00
q = add_stat_columns(q)
q = q.filter(
2022-11-18 02:19:10 +00:00
Shout.slug == slug
).filter(
Shout.deletedAt.is_(None)
).group_by(Shout.id)
2022-11-25 18:31:53 +00:00
[shout, viewed_stat, reacted_stat, commented_stat, rating_stat] = session.execute(q).unique().one()
2022-11-25 18:31:53 +00:00
shout.stat = {
"viewed": viewed_stat,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
}
2022-11-29 12:36:46 +00:00
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
2022-11-25 18:31:53 +00:00
for author in shout.authors:
2022-11-29 19:13:03 +00:00
if author.id == author_caption.userId:
2022-11-25 18:31:53 +00:00
author.caption = author_caption.caption
2022-11-25 18:31:53 +00:00
return shout
2022-11-23 21:53:53 +00:00
@query.field("loadShouts")
async def load_shouts_by(_, info, options):
2022-11-15 02:36:30 +00:00
"""
:param options: {
filters: {
layout: 'audio',
visibility: "public",
author: 'discours',
topic: 'culture',
title: 'something',
body: 'something else',
days: 30
}
offset: 0
limit: 50
2022-11-25 18:31:53 +00:00
order_by: 'createdAt' | 'commented' | 'reacted' | 'rating'
2022-11-21 08:13:57 +00:00
order_by_desc: true
2022-11-15 02:36:30 +00:00
}
:return: Shout[]
"""
2022-11-17 20:33:03 +00:00
q = select(Shout).options(
2022-11-23 21:53:53 +00:00
joinedload(Shout.authors),
joinedload(Shout.topics),
2022-11-15 02:36:30 +00:00
).where(
Shout.deletedAt.is_(None)
)
2022-11-25 18:31:53 +00:00
q = add_stat_columns(q)
2022-11-21 08:13:57 +00:00
user = info.context["request"].user
2022-11-28 20:29:02 +00:00
q = apply_filters(q, options.get("filters", {}), user)
2022-11-25 18:31:53 +00:00
order_by = options.get("order_by", Shout.createdAt)
if order_by == 'reacted':
aliased_reaction = aliased(Reaction)
q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted'))
2022-11-25 18:31:53 +00:00
order_by_desc = options.get('order_by_desc', True)
2022-11-21 16:01:09 +00:00
query_order_by = desc(order_by) if order_by_desc else asc(order_by)
2022-11-21 04:47:59 +00:00
offset = options.get("offset", 0)
limit = options.get("limit", 10)
2022-11-25 18:31:53 +00:00
2022-11-20 03:41:05 +00:00
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
with local_session() as session:
2022-11-25 18:31:53 +00:00
shouts = []
2022-11-25 18:31:53 +00:00
for [shout, viewed_stat, reacted_stat, commented_stat, rating_stat] in session.execute(q).unique():
shouts.append(shout)
2022-11-25 18:31:53 +00:00
shout.stat = {
"viewed": viewed_stat,
"reacted": reacted_stat,
"commented": commented_stat,
"rating": rating_stat
}
return shouts