collections fix, single shout fix

This commit is contained in:
tonyrewin 2022-08-17 12:07:04 +03:00
parent 04aaaab609
commit 3358ba8b99
2 changed files with 29 additions and 13 deletions

View File

@ -1,4 +1,4 @@
from orm.collection import Collection
from orm.collection import Collection, ShoutCollection
from base.orm import local_session
from orm.user import User
from base.resolvers import mutation, query
@ -68,6 +68,25 @@ async def get_user_collections(_, info, userslug):
query(Collection).\
where(and_(Collection.createdBy == userslug, Collection.publishedAt != None)).\
all()
for c in collections:
shouts = session.query(ShoutCollection).filter(ShoutCollection.collection == c.id).all()
c.amount = len(shouts)
return collections
@query.field("getMyCollections")
async def get_user_collections(_, info, userslug):
collections = []
with local_session() as session:
user = session.query(User).filter(User.slug == userslug).first()
if user:
# TODO: check rights here
collections = session.\
query(Collection).\
where(and_(Collection.createdBy == userslug, Collection.publishedAt != None)).\
all()
for c in collections:
shouts = session.query(ShoutCollection).filter(ShoutCollection.collection == c.id).all()
c.amount = len(shouts)
return collections
@query.field("getMyColelctions")

View File

@ -55,23 +55,20 @@ async def get_shout_by_slug(_, info, slug):
all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
selected_fields = set(["authors", "topics"]).intersection(all_fields)
select_options = [selectinload(getattr(Shout, field)) for field in selected_fields]
shout = {}
with local_session() as session:
try: s = text(open('src/queries/shout-by-slug.sql', 'r').read() % slug)
except: pass
shout_q = session.query(Shout).\
shout = session.query(Shout).\
options(select_options).\
filter(Shout.slug == slug)
print(shout_q.statement)
shout = shout_q.first()
for a in shout.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(slug, a.slug)
filter(Shout.slug == slug).first()
if not shout:
print(f"shout with slug {slug} not exist")
return {"error" : "shout not found"}
if not shout:
print(f"shout with slug {slug} not exist")
return {"error" : "shout not found"}
else:
for a in shout.authors:
a.caption = await ShoutAuthorStorage.get_author_caption(slug, a.slug)
return shout
@query.field("shoutsByTopics")