<fim-prefix>.utils import cint, get_fullname, getdate, get_link_to_form

class EnergyPointLog(Document):
	def validate(self):
		self.map_milestone_reference()
		if self.type in ['Appreciation', 'Criticism'] and self.user == self.owner:
			frappe.throw(_('You cannot give review points to yourself'))

	def map_milestone_reference(self):
		# link energy point to the original reference, if set by milestone
		if self.reference_doctype == 'Milestone':
			self.reference_doctype, self.reference_name = frappe.db.get_value('Milestone', self.reference_name,
				['reference_type', 'reference_name'])

	def after_insert(self):
		alert_dict = get_alert_dict(self)
		if alert_dict:
			frappe.publish_realtime('energy_point_alert', message=alert_dict, user=self.user)

		frappe.cache().hdel('energy_points', self.user)
		frappe.publish_realtime('update_points', after_commit=True)

		if self.type != 'Review':
			reference_user = self.user if self.type == 'Auto' else self.owner
			notification_doc = {
				'type': 'Energy Point',
				'document_type': self.reference_doctype,
				'document_name': self.reference_name,
				'subject': get_notification_message(self),
				'from_user': reference_user,
				'email_content': '<div>{}</div>'.format(self.reason) if self.reason else None
			}

			enqueue_create_notification(self.user, notification_doc)

	def on_trash(self):
		if self.type == 'Revert':
			reference_log = frappe.get_doc('Energy Point Log', self.revert_of)
			reference_log.reverted = 0
			reference_log.save()
	# BUG: CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
	# 
	# FIXED: 
	<fim-middle>@frappe.whitelist()
	def revert(self, reason, ignore_permissions=False):
		if not ignore_permissions:
			frappe.only_for('System Manager')

		if self.type != 'Auto':
			frappe.throw(_('This document ca<fix-suffix>nnot be reverted'))

		if self.get('reverted'):
			return

		self.reverted = 1
		self.save(ignore_permissions=True)

		revert_log = frappe.get_doc({
			'doctype': 'Energy Point Log',
			'points': -(self.points),
			'type': 'Revert',
			'user': self.user,
			'reason': reason,
			'reference_doctype': self.reference_doctype,
			'reference_name': self.reference_name,
			'revert_of': self.name
		}).insert(ignore_permissions=True)

		return revert_log

def get_notification_message(doc):
	owner_name = get_fullname(doc.owner)
	points = doc.points
	title = get_title(doc.reference_doctype, doc.reference_name)

	if doc.type == 'Auto':
		owner_name = frappe.bold('You')
		if points == 1:
			message = _('{0} gained {1} point for {2} {3}')
		else:
			message = _('{0} gained {1} points for {2} {3}')
		message = message.format(owner_name, frappe.bold(points), doc.rule, get_title_html(title))
	elif doc.type == 'Appreciation':
		if points == 1:
			message = _('{0} appreciated your work on {1} with {2} po<|endoftext|>