from urllib.parse import quote_plus

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


class NotPermittedPage(TemplatePage):
	def __init__(self, path=None, http_status_code=None, exception=""):
		jingrow.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={quote_plus(jingrow.request.path)}"
		if jingrow.request.path.startswith("/app/") or jingrow.request.path == "/app":
			action = "/login"
		jingrow.local.message_title = _("Not Permitted")
		jingrow.local.response["context"] = dict(
			indicator_color="red", primary_action=action, primary_label=_("Login"), fullpage=True
		)
		self.set_standard_path("message")
		return super().render()
