# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

import jingrow


def get(name):
	"""
	Return the :term:`doclist` of the `Page` specified by `name`
	"""
	page = jingrow.get_pg("Page", name)
	if page.is_permitted():
		page.load_assets()
		pgs = jingrow._dict(page.as_dict())
		if getattr(page, "_dynamic_page", None):
			pgs["_dynamic_page"] = 1

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


@jingrow.whitelist(allow_guest=True)
def getpg(name: str):
	"""
	Load the page from `jingrow.form` and send it via `jingrow.response`
	"""

	pg = get(name)
	jingrow.response.pgs.append(pg)
