<commit_before># Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
# Author - <NAME> <<EMAIL>>

from __future__ import unicode_literals
import frappe
import json
from frappe import _, DoesNotExistError
from frappe.boot import get_allowed_pages, get_allowed_reports
from frappe.cache_manager import build_domain_restriced_doctype_cache, build_domain_restriced_page_cache, build_table_count_cache

class Workspace:
	def __init__(self, page_name):
		self.page_name = page_name

	def build_cache(self):
		self.doc = frappe.get_doc("Desk Page", self.page_name)

		user = frappe.get_user()
		user.build_permissions()
		self.user = user

		self.allowed_pages = get_allowed_pages()
		self.allowed_reports = get_allowed_reports()

		self.table_counts = build_table_count_cache()
		self.restricted_doctypes = build_domain_restriced_doctype_cache()
		self.restricted_pages = build_domain_restriced_page_cache()

	def is_item_allowed(self, name, item_type):
		item_type = item_type.lower()

		if item_type == "doctype":
			return (name in self.user.can_read and name in self.restricted_doctypes)
		if item_type == "page":
			return (name in self.allowed_pages and name in self.restricted_pages)
		if item_type == "report":
			return name in self.allowed_reports
		if item_type == "help":
			return True

		return False

	def build_workspace(self):
		self.cards = {
			'label': self.doc.charts_label,
			'items': self.get_cards()
		}

		self.charts = {
			'label': self.doc.shortcuts_label,
			'items': self.get_charts()
		}

		self.shortcuts = {
			'label': self.doc.shortcuts_label,
			'items': self.get_shortcuts()
		}

	def get_cards(self):
		cards = self.doc.cards + get_custom_reports_and_doctypes(self.doc.module)
		default_country = frappe.db.get_default("country")

		def _doctype_contains_a_record(name):
			exists = self.table_counts.get(name)
			if not exists:
				if not frappe.db.get_value('DocType', name, 'issingle'):
					exists = frappe.db.count(name)
				else:
					exists = True
				self.table_counts[name] = exists
			return exists

		def _prepare_item(item):
			if item.dependencies:
				incomplete_dependencies = [d for d in item.dependencies if not _doctype_contains_a_record(d)]
				if len(incomplete_dependencies):
					item.incomplete_dependencies = incomplete_dependencies

			if item.onboard:
				# Mark Spotlights for initial
				if item.get("type") == "doctype":
					name = item.get("name")
					count = _doctype_contains_a_record(name)

					item["count"] = count

			return item

		new_data = []
		for section in cards:
			new_items = []
			if isinstance(section.links, str):
				links = json.loads(section.links)
			else:
				links = section.links

			for item in links:
				item = frappe._dict(item)

				# Condition: based on country
				if item.country and item.country != default_country:
					continue

				# Check if user is allowed to view
				if self.is_item_allowed(item.name, item.type):
					prepared_item = _prepare_item(item)
					new_items.append(item)

			if new_items:
				if isinstance(section, frappe._dict):
					new_section = section.copy()
				else:
					new_section = section.as_dict().copy()
				new_section["links"] = new_items
				new_section["label"] = section.title
				new_data.append(new_section)

		return new_data

	def get_charts(self):
		if frappe.has_permission("Dashboard Chart", throw=False):
			return [chart for chart in self.doc.charts]
		return []

	def get_shortcuts(self):
		items = []
		for item in self.doc.shortcuts:
			new_item = item.as_dict().copy()
			new_item['name'] = _(item.link_to)
			if self.is_item_allowed(item.link_to, item.type):
				if item.type == "Page":
					page = self.allowed_pages[item.link_to]
					new_item['label'] = _(page.get("title", frappe.unscrub(item.link_to)))<commit_msg>feat: mark query report in API<commit_after># Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
# Author - <NAME> <<EMAIL>>

from __future__ import unicode_literals
import frappe
import json
from frappe import _, DoesNotExistError
from frappe.boot import get_allowed_pages, get_allowed_reports
from frappe.cache_manager import build_domain_restriced_doctype_cache, build_domain_restriced_page_cache, build_table_count_cache

class Workspace:
	def __init__(self, page_name):
		self.page_name = page_name

	def build_cache(self):
		self.doc = frappe.get_doc("Desk Page", self.page_name)

		user = frappe.get_user()
		user.build_permissions()
		self.user = user

		self.allowed_pages = get_allowed_pages()
		self.allowed_reports = get_allowed_reports()

		self.table_counts = build_table_count_cache()
		self.restricted_doctypes = build_domain_restriced_doctype_cache()
		self.restricted_pages = build_domain_restriced_page_cache()

	def is_item_allowed(self, name, item_type):
		item_type = item_type.lower()

		if item_type == "doctype":
			return (name in self.user.can_read and name in self.restricted_doctypes)
		if item_type == "page":
			return (name in self.allowed_pages and name in self.restricted_pages)
		if item_type == "report":
			return name in self.allowed_reports
		if item_type == "help":
			return True

		return False

	def build_workspace(self):
		self.cards = {
			'label': self.doc.charts_label,
			'items': self.get_cards()
		}

		self.charts = {
			'label': self.doc.shortcuts_label,
			'items': self.get_charts()
		}

		self.shortcuts = {
			'label': self.doc.shortcuts_label,
			'items': self.get_shortcuts()
		}

	def get_cards(self):
		cards = self.doc.cards + get_custom_reports_and_doctypes(self.doc.module)
		default_country = frappe.db.get_default("country")

		def _doctype_contains_a_record(name):
			exists = self.table_counts.get(name)
			if not exists:
				if not frappe.db.get_value('DocType', name, 'issingle'):
					exists = frappe.db.count(name)
				else:
					exists = True
				self.table_counts[name] = exists
			return exists

		def _prepare_item(item):
			if item.dependencies:
				incomplete_dependencies = [d for d in item.dependencies if not _doctype_contains_a_record(d)]
				if len(incomplete_dependencies):
					item.incomplete_dependencies = incomplete_dependencies

			if item.onboard:
				# Mark Spotlights for initial
				if item.get("type") == "doctype":
					name = item.get("name")
					count = _doctype_contains_a_record(name)

					item["count"] = count

			return item

		new_data = []
		for section in cards:
			new_items = []
			if isinstance(section.links, str):
				links = json.loads(section.links)
			else:
				links = section.links

			for item in links:
				item = frappe._dict(item)

				# Condition: based on country
				if item.country and item.country != default_country:
					continue

				# Check if user is allowed to view
				if self.is_item_allowed(item.name, item.type):
					prepared_item = _prepare_item(item)
					new_items.append(item)

			if new_items:
				if isinstance(section, frappe._dict):
					new_section = section.copy()
				else:
					new_section = section.as_dict().copy()
				new_section["links"] = new_items
				new_section["label"] = section.title
				new_data.append(new_section)

		return new_data

	def get_charts(self):
		if frappe.has_permission("Dashboard Chart", throw=False):
			return [chart for chart in self.doc.charts]
		return []

	def get_shortcuts(self):
		items = []
		for item in self.doc.shortcuts:
			new_item = item.as_dict().copy()
			new_item['name'] = _(item.link_to)
			if self.is_item_allowed(item.link_to, item.type):
				if item.type == "Page":
					page = self.allowed_pages[item.link_to]
					new_item['label'] = _(page.get("title", frappe.unscrub(item.link_to)))
				if item.type == "Report":
					report = self.allowed_reports.get(item.link_to, {})
					if report.get("report_type") in ["Query Report", "Script Report"]:
						new_item['is_query_report'] = 1
{'fraction_non_alphanumeric': 0.108762098828324, 'fraction_numerical': 0.0011462047885888945, 'mean_word_length': 4.024312220089572, 'pattern_counts': {'":': 13, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4701638', 'n_tokens_mistral': 3145, 'n_tokens_neox': 2635, 'n_words': 586}