# Copyright (c) 2021, JINGROW
# For license information, please see license.txt

from random import randrange

import jingrow
from jingrow.model.page import Page


class PageShareKey(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.types import DF

		expires_on: DF.Date | None
		key: DF.Data | None
		reference_pagename: DF.DynamicLink | None
		reference_pagetype: DF.Link | None

	# end: auto-generated types
	def before_insert(self):
		self.key = jingrow.generate_hash(length=randrange(25, 35))
		if not self.expires_on and not self.flags.no_expiry:
			self.expires_on = jingrow.utils.add_days(
				None, days=jingrow.get_system_settings("page_share_key_expiry") or 90
			)


def is_expired(expires_on):
	return expires_on and expires_on < jingrow.utils.getdate()
