new create shout flow
This commit is contained in:
@@ -27,11 +27,11 @@ async def create_shout(_, info, inp):
|
||||
new_shout = Shout.create(**{
|
||||
"title": inp.get("title"),
|
||||
"subtitle": inp.get('subtitle'),
|
||||
"body": inp.get("body"),
|
||||
"body": inp.get("body", ''),
|
||||
"authors": inp.get("authors", []),
|
||||
"slug": inp.get("slug"),
|
||||
"mainTopic": inp.get("mainTopic"),
|
||||
"visibility": "community",
|
||||
"visibility": "owner",
|
||||
"createdBy": auth.user_id
|
||||
})
|
||||
|
||||
@@ -81,21 +81,24 @@ async def create_shout(_, info, inp):
|
||||
|
||||
session.commit()
|
||||
|
||||
# TODO
|
||||
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
|
||||
# TODO
|
||||
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
|
||||
|
||||
if new_shout.slug is None:
|
||||
new_shout.slug = f"draft-{new_shout.id}"
|
||||
session.commit()
|
||||
|
||||
return {"shout": new_shout}
|
||||
|
||||
|
||||
@mutation.field("updateShout")
|
||||
@login_required
|
||||
async def update_shout(_, info, inp):
|
||||
async def update_shout(_, info, slug, inp):
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
slug = inp["slug"]
|
||||
|
||||
with local_session() as session:
|
||||
user = session.query(User).filter(User.id == auth.user_id).first()
|
||||
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
||||
|
||||
if not shout:
|
||||
return {"error": "shout not found"}
|
||||
|
||||
@@ -108,18 +111,38 @@ async def update_shout(_, info, inp):
|
||||
else:
|
||||
shout.update(inp)
|
||||
shout.updatedAt = datetime.now(tz=timezone.utc)
|
||||
session.add(shout)
|
||||
|
||||
if inp.get("topics"):
|
||||
# remove old links
|
||||
links = session.query(ShoutTopic).where(ShoutTopic.shout == shout.id).all()
|
||||
for topiclink in links:
|
||||
session.delete(topiclink)
|
||||
# add new topic links
|
||||
for topic in inp.get("topics", []):
|
||||
ShoutTopic.create(shout=slug, topic=topic)
|
||||
# for topic_slug in inp.get("topics", []):
|
||||
# topic = session.query(Topic).filter(Topic.slug == topic_slug).first()
|
||||
# shout_topic = ShoutTopic.create(shout=shout.id, topic=topic.id)
|
||||
# session.add(shout_topic)
|
||||
session.commit()
|
||||
# GitTask(inp, user.username, user.email, "update shout %s" % slug)
|
||||
|
||||
GitTask(inp, user.username, user.email, "update shout %s" % slug)
|
||||
return {"shout": shout}
|
||||
|
||||
|
||||
@mutation.field("publishShout")
|
||||
@login_required
|
||||
async def publish_shout(_, info, slug, inp):
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
||||
if not shout:
|
||||
return {"error": "shout not found"}
|
||||
|
||||
else:
|
||||
shout.update(inp)
|
||||
shout.visibility = "community"
|
||||
shout.updatedAt = datetime.now(tz=timezone.utc)
|
||||
session.commit()
|
||||
|
||||
return {"shout": shout}
|
||||
|
||||
|
@@ -1,11 +1,11 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy.orm import joinedload, aliased
|
||||
from sqlalchemy.sql.expression import desc, asc, select, func, case
|
||||
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_
|
||||
|
||||
from auth.authenticate import login_required
|
||||
from auth.credentials import AuthCredentials
|
||||
from base.exceptions import ObjectNotExist
|
||||
from base.exceptions import ObjectNotExist, OperationNotAllowed
|
||||
from base.orm import local_session
|
||||
from base.resolvers import query
|
||||
from orm import ViewedEntry, TopicFollower
|
||||
@@ -70,7 +70,6 @@ def apply_filters(q, filters, user_id=None):
|
||||
|
||||
return q
|
||||
|
||||
|
||||
@query.field("loadShout")
|
||||
async def load_shout(_, info, slug):
|
||||
with local_session() as session:
|
||||
@@ -196,6 +195,38 @@ async def load_shouts_by(_, info, options):
|
||||
|
||||
return shouts
|
||||
|
||||
@query.field("loadDrafts")
|
||||
async def get_drafts(_, info, options):
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
user_id = auth.user_id
|
||||
|
||||
q = select(Shout).options(
|
||||
joinedload(Shout.authors),
|
||||
joinedload(Shout.topics),
|
||||
).where(
|
||||
and_(Shout.deletedAt.is_(None), Shout.createdBy == user_id)
|
||||
)
|
||||
|
||||
q = apply_filters(q, options.get("filters", {}), user_id)
|
||||
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'))
|
||||
|
||||
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
||||
offset = options.get("offset", 0)
|
||||
limit = options.get("limit", 10)
|
||||
|
||||
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
|
||||
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
for [shout] in session.execute(q).unique():
|
||||
shouts.append(shout)
|
||||
|
||||
return shouts
|
||||
|
||||
|
||||
|
||||
@query.field("myFeed")
|
||||
@login_required
|
||||
|
Reference in New Issue
Block a user