import xhiveframework
from xhiveframework import _
from xhiveframework.utils import cstr
from xhiveframework.website.page_renderers.template_page import TemplatePage


class NotPermittedPage(TemplatePage):
	def __init__(self, path=None, http_status_code=None, exception=""):
		xhiveframework.local.message = cstr(exception)
		super().__init__(path=path, http_status_code=http_status_code)
		self.http_status_code = 403

	def can_render(self):
		return True

	def render(self):
		action = f"/login?redirect-to={xhiveframework.request.path}"
		if xhiveframework.request.path.startswith("/app/") or xhiveframework.request.path == "/app":
			action = "/login"
		xhiveframework.local.message_title = _("Not Permitted")
		xhiveframework.local.response["context"] = dict(
			indicator_color="red", primary_action=action, primary_label=_("Login"), fullpage=True
		)
		self.set_standard_path("message")
		return super().render()
