core/resolvers/collection.py

102 lines
3.1 KiB
Python
Raw Normal View History

2022-08-11 11:22:10 +00:00
from datetime import datetime
2022-08-11 11:22:10 +00:00
from sqlalchemy import and_
from auth.authenticate import login_required
from base.orm import local_session
from base.resolvers import mutation, query
from orm.collection import Collection, ShoutCollection
from orm.user import User
2022-09-03 10:50:14 +00:00
2022-08-11 11:22:10 +00:00
@mutation.field("createCollection")
@login_required
2022-09-07 16:19:06 +00:00
async def create_collection(_, _info, inp):
2022-09-04 17:20:38 +00:00
# auth = info.context["request"].auth
# user_id = auth.user_id
2022-09-03 10:50:14 +00:00
collection = Collection.create(
2022-09-07 16:19:06 +00:00
slug=inp.get("slug", ""),
title=inp.get("title", ""),
desc=inp.get("desc", ""),
pic=inp.get("pic", ""),
2022-09-03 10:50:14 +00:00
)
return {"collection": collection}
2022-08-11 11:22:10 +00:00
@mutation.field("updateCollection")
@login_required
2022-09-07 16:19:06 +00:00
async def update_collection(_, info, inp):
2022-09-03 10:50:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
collection_slug = inp.get("slug", "")
2022-09-03 10:50:14 +00:00
with local_session() as session:
owner = session.query(User).filter(User.id == user_id) # note list here
collection = (
session.query(Collection).filter(Collection.slug == collection_slug).first()
)
editors = [e.slug for e in collection.editors]
if not collection:
return {"error": "invalid collection id"}
if collection.createdBy not in (owner + editors):
return {"error": "access denied"}
2022-09-07 16:19:06 +00:00
collection.title = inp.get("title", "")
collection.desc = inp.get("desc", "")
collection.pic = inp.get("pic", "")
2022-09-03 10:50:14 +00:00
collection.updatedAt = datetime.now()
session.commit()
2022-08-11 11:22:10 +00:00
@mutation.field("deleteCollection")
@login_required
async def delete_collection(_, info, slug):
2022-09-03 10:50:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
collection = session.query(Collection).filter(Collection.slug == slug).first()
if not collection:
return {"error": "invalid collection slug"}
if collection.owner != user_id:
return {"error": "access denied"}
collection.deletedAt = datetime.now()
session.add(collection)
2022-09-03 10:50:14 +00:00
session.commit()
return {}
2022-08-11 11:22:10 +00:00
2022-08-13 10:05:46 +00:00
@query.field("getUserCollections")
2022-09-07 16:19:06 +00:00
async def get_user_collections(_, _info, userslug):
2022-09-03 10:50:14 +00:00
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(
2022-09-04 17:20:38 +00:00
and_(Collection.createdBy == userslug, bool(Collection.publishedAt))
2022-09-03 10:50:14 +00:00
)
.all()
)
for c in collections:
shouts = (
session.query(ShoutCollection)
.filter(ShoutCollection.collection == c.id)
.all()
)
c.amount = len(shouts)
return collections
2022-08-11 11:22:10 +00:00
@query.field("getMyColelctions")
@login_required
2022-08-13 10:05:46 +00:00
async def get_my_collections(_, info):
2022-09-03 10:50:14 +00:00
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
collections = (
session.query(Collection).when(Collection.createdBy == user_id).all()
)
return collections