1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-05 01:04:21 +00:00
royalnet/royalpack/stars/api_wiki_edit.py

65 lines
2 KiB
Python
Raw Normal View History

2020-02-11 18:53:18 +00:00
import uuid
import royalnet.utils as ru
from royalnet.backpack.tables import *
from royalnet.constellation.api import *
from ..tables import WikiPage
class ApiWikiEditStar(ApiStar):
path = "/api/wiki/edit/v1"
methods = ["POST"]
2020-03-09 20:21:07 +00:00
summary = "Edit the contents of a wiki page, or create a new one."
parameters = {
"id": "The id of the wiki page to edit. Leave empty to create a new one.",
"title": "The new title of the wiki page.",
"contents": "The new contents of the wiki page.",
"format": "The format of the wiki page. The default is markdown.",
"theme": "The theme of the wiki page. The default is default."
}
requires_auth = True
tags = ["wiki"]
2020-02-11 18:53:18 +00:00
async def api(self, data: ApiData) -> ru.JSON:
page_id = data.get("id")
title = data["title"]
contents = data["contents"]
format = data["format"]
theme = data["theme"]
WikiPageT = self.alchemy.get(WikiPage)
user = await data.user()
if not (user.role == "Admin" or user.role == "Member" or user.role == "Bot"):
raise ForbiddenError("You do not have sufficient permissions to edit this page.")
if page_id is None:
page = WikiPageT(
page_id=uuid.uuid4(),
title=title,
contents=contents,
format=format,
theme=theme
)
data.session.add(page)
else:
page = await ru.asyncify(
data.session
.query(WikiPageT)
.filter_by(page_id=uuid.UUID(page_id))
.one_or_none
)
if page is None:
raise NotFoundError(f"No page with the id {repr(page_id)} found.")
page.title = title
page.contents = contents
page.format = format
page.theme = theme
await data.session_commit()
return page.json_full()