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

# License: MIT. See LICENSE

import jingrow
from jingrow import _
from jingrow.model.page import Page


class Blogger(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

		avatar: DF.AttachImage | None
		bio: DF.SmallText | None
		disabled: DF.Check
		full_name: DF.Data
		short_name: DF.Data
		user: DF.Link | None

	# end: auto-generated types
	def validate(self):
		if self.user and not jingrow.db.exists("User", self.user):
			# for data import
			jingrow.get_pg(
				{"pagetype": "User", "email": self.user, "first_name": self.user.split("@", 1)[0]}
			).insert()

	def on_update(self):
		"if user is set, then update all older blogs"

		from jingrow.website.pagetype.blog_post.blog_post import clear_blog_cache

		clear_blog_cache()

		if self.user:
			for blog in jingrow.db.sql_list(
				"""select name from `tabBlog Post` where owner=%s
				and ifnull(blogger,'')=''""",
				self.user,
			):
				b = jingrow.get_pg("Blog Post", blog)
				b.blogger = self.name
				b.save()

			jingrow.permissions.add_user_permission("Blogger", self.name, self.user)
