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

import jingrow
from jingrow.model.page import Page


class CustomRole(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.core.pagetype.has_role.has_role import HasRole
		from jingrow.types import DF

		page: DF.Link | None
		ref_pagetype: DF.Data | None
		report: DF.Link | None
		roles: DF.Table[HasRole]

	# end: auto-generated types
	def validate(self):
		if self.report and not self.ref_pagetype:
			self.ref_pagetype = jingrow.db.get_value("Report", self.report, "ref_pagetype")


def get_custom_allowed_roles(field, name):
	allowed_roles = []
	custom_role = jingrow.db.get_value("Custom Role", {field: name}, "name")
	if custom_role:
		custom_role_pg = jingrow.get_pg("Custom Role", custom_role)
		allowed_roles = [d.role for d in custom_role_pg.roles]

	return allowed_roles
