# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE

import xhiveframework


@xhiveframework.whitelist()
def get(name):
	"""
	Return the :term:`doclist` of the `Page` specified by `name`
	"""
	page = xhiveframework.get_doc("Page", name)
	if page.is_permitted():
		page.load_assets()
		docs = xhiveframework._dict(page.as_dict())
		if getattr(page, "_dynamic_page", None):
			docs["_dynamic_page"] = 1

		return docs
	else:
		xhiveframework.response["403"] = 1
		raise xhiveframework.PermissionError("No read permission for Page %s" % (page.title or name))


@xhiveframework.whitelist(allow_guest=True)
def getpage():
	"""
	Load the page from `xhiveframework.form` and send it via `xhiveframework.response`
	"""
	page = xhiveframework.form_dict.get("name")
	doc = get(page)

	xhiveframework.response.docs.append(doc)
