core/resolvers/create/drafts.py

190 lines
6.0 KiB
Python
Raw Normal View History

from auth.authenticate import login_required
2022-12-01 14:45:19 +00:00
from auth.credentials import AuthCredentials
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import query, mutation
2023-02-17 14:30:38 +00:00
from orm.draft import DraftCollab, DraftAuthor
2022-07-10 18:52:57 +00:00
from orm.shout import Shout
2023-02-17 14:30:38 +00:00
from orm.topic import Topic
2022-07-10 18:52:57 +00:00
from orm.user import User
2023-02-17 14:30:38 +00:00
from datetime import datetime, timezone
from transliterate import translit
import re
2022-09-03 10:50:14 +00:00
2023-01-13 11:04:45 +00:00
@query.field("loadDrafts")
2022-09-02 10:23:33 +00:00
@login_required
2023-01-16 08:32:36 +00:00
async def load_drafts(_, info):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2023-01-13 11:04:45 +00:00
drafts = []
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-01-13 11:04:45 +00:00
drafts = session.query(DraftCollab).filter(auth.user_id in DraftCollab.authors)
2023-01-16 08:32:36 +00:00
return drafts
2022-09-02 10:23:33 +00:00
2023-02-17 14:30:38 +00:00
@mutation.field("createDraft") # TODO
@login_required
2023-01-16 08:32:36 +00:00
async def create_draft(_, info, draft_input):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2023-02-17 14:30:38 +00:00
draft_input['createdBy'] = auth.user_id
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-01-16 08:32:36 +00:00
collab = DraftCollab.create(**draft_input)
session.add(collab)
session.commit()
# TODO: email notify to all authors
return {}
2023-02-17 14:30:38 +00:00
@mutation.field("deleteDraft")
2023-01-13 11:04:45 +00:00
@login_required
async def delete_draft(_, info, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
2023-02-17 14:30:38 +00:00
d = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if auth.user_id not in d.authors:
2023-01-16 08:32:36 +00:00
# raise BaseHttpException("only owner can remove coauthors")
return {
"error": "Only authors can update a draft"
}
2023-02-17 14:30:38 +00:00
elif not d:
2023-01-16 08:32:36 +00:00
return {
"error": "There is no draft with this id"
}
else:
2023-02-17 14:30:38 +00:00
session.delete(d)
2023-01-16 08:32:36 +00:00
session.commit()
return {}
2023-01-16 08:32:36 +00:00
2023-02-17 14:30:38 +00:00
@mutation.field("updateDraft") # TODO: draft input type
@login_required
2023-01-16 08:32:36 +00:00
async def update_draft(_, info, draft_input):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-02-17 14:30:38 +00:00
d = session.query(
DraftCollab
).where(
DraftCollab.id == draft_input.id
).one() # raises Error when not found
if auth.user_id not in d.authors:
2023-01-13 11:04:45 +00:00
# raise BaseHttpException("only owner can remove coauthors")
return {
"error": "Only authors can update draft"
}
2023-02-17 14:30:38 +00:00
elif not d:
2023-01-13 11:04:45 +00:00
return {
"error": "There is no draft with this id"
}
2022-11-24 17:53:39 +00:00
else:
2023-01-16 08:32:36 +00:00
draft_input["updatedAt"] = datetime.now(tz=timezone.utc)
2023-02-17 14:30:38 +00:00
d.update(draft_input)
2022-11-24 17:53:39 +00:00
session.commit()
2022-09-03 10:50:14 +00:00
# TODO: email notify
return {}
2022-11-24 17:53:39 +00:00
2023-02-17 14:30:38 +00:00
2023-01-13 11:04:45 +00:00
@mutation.field("inviteAuthor")
@login_required
async def invite_coauthor(_, info, author: int = 0, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
2022-11-24 17:53:39 +00:00
2023-01-13 11:04:45 +00:00
with local_session() as session:
c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if auth.user_id not in c.authors:
# raise BaseHttpException("you are not in authors list")
return {
"error": "You are not in authors list"
}
2023-01-16 08:32:36 +00:00
elif c.id:
2023-01-13 11:04:45 +00:00
invited_user = session.query(User).where(User.id == author).one()
2023-01-16 08:32:36 +00:00
da = DraftAuthor.create({
"accepted": False,
"collab": c.id,
"author": invited_user.id
})
session.add(da)
2023-01-13 11:04:45 +00:00
session.commit()
2023-01-16 08:32:36 +00:00
else:
return {
"error": "Draft is not found"
}
2023-01-13 11:04:45 +00:00
# TODO: email notify
return {}
2023-02-17 14:30:38 +00:00
def get_slug(src):
slug = translit(src, "ru", reversed=True).replace(".", "-").lower()
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
return slug
2023-01-13 11:04:45 +00:00
@mutation.field("inviteAccept")
2022-11-24 17:53:39 +00:00
@login_required
2023-01-13 11:04:45 +00:00
async def accept_coauthor(_, info, draft: int):
2022-12-01 14:45:19 +00:00
auth: AuthCredentials = info.context["request"].auth
2022-11-24 17:53:39 +00:00
with local_session() as session:
2023-02-17 14:30:38 +00:00
d = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if not d:
return {
"error": "Draft id was not found"
}
else:
a = session.query(DraftAuthor).where(DraftAuthor.collab == draft).filter(
DraftAuthor.author == auth.user_id).one()
if not a.accepted:
a.accepted = True
session.commit()
# TODO: email notify
return {}
elif a.accepted:
return {
"error": "You have accepted invite before"
}
else:
# raise BaseHttpException("only invited can accept")
return {
"error": "You don't have an invitation yet"
}
@mutation.field("draftToShout")
@login_required
async def draft_to_shout(_, info, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
d = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if auth.user_id not in d.authors:
# raise BaseHttpException("you are not in authors list")
2023-01-16 08:32:36 +00:00
return {
2023-02-17 14:30:38 +00:00
"error": "You are not in authors list"
2023-01-16 08:32:36 +00:00
}
2023-02-17 14:30:38 +00:00
elif d.id:
draft_authors = [a.author for a in d.authors]
draft_topics = [t.topic for t in d.topics]
authors = session.query(User).where(User.id._in(draft_authors)).all()
topics = session.query(Topic).where(Topic.id._in(draft_topics)).all()
new_shout = Shout.create({
"authors": authors,
"body": d.body,
"title": d.title,
"subtitle": d.subtitle or "",
"topics": topics,
"media": d.media,
"slug": d.slug or get_slug(d.title),
"layout": d.layout or "article"
})
session.add(new_shout)
session.commit()
2022-11-24 17:53:39 +00:00
else:
2023-01-13 11:04:45 +00:00
return {
2023-02-17 14:30:38 +00:00
"error": "Draft is not found"
2023-01-13 11:04:45 +00:00
}
2023-02-17 14:30:38 +00:00
# TODO: email notify
return {}