diff --git a/resolvers/collection.py b/resolvers/collection.py index e7091fcd..2a1a7f20 100644 --- a/resolvers/collection.py +++ b/resolvers/collection.py @@ -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") diff --git a/resolvers/zine.py b/resolvers/zine.py index 3cb772f8..171f6bbc 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -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")