<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class NumberCard(Document):
	pass


def get_permission_query_conditions(user):

	if not user:
		user = frappe.session.user

	if user == 'Administrator':
		return

	roles = frappe.get_roles(user)
	if "System Manager" in roles:
		return None

	allowed_doctypes = tuple(frappe.permissions.get_doctypes_with_read())

	return '''
			`tabNumber Card`.`document_type` in {allowed_doctypes}
		'''.format(
			allowed_doctypes=allowed_doctypes,
		)

def has_permission(doc, ptype, user):
	roles = frappe.get_roles(user)
	if "System Manager" in roles:
		return True

	allowed_doctypes = tuple(frappe.permissions.get_doctypes_with_read())
	if doc.document_type in allowed_doctypes:
		return True

	return False

@frappe.whitelist()
def get_result(doc, to_date=None):
	doc = frappe.parse_json(doc)
	fields = []
	sql_function_map = {
		'Count': 'count',
		'Sum': 'sum',
		'Average': 'avg',
		'Minimum': 'min',
		'Maximum': 'max'
	}

	function = sql_function_map[doc.function]

	if function == 'count':
		fields = ['{function}(*) as result'.format(function=function)]
	else:
		fields = ['{function}({based_on}) as result'.format(function=function, based_on=doc.aggregate_function_based_on)]

	filters = frappe.parse_json(doc.filters_json)

	if to_date:
		filters.append([doc.document_type, 'creation', '<', to_date, False])

	number = frappe.db.get_all(doc.document_type, fields = fields, filters = filters)[0]['result']

	frappe.db.set_value('Number Card', doc.name, 'previous_result', number)
	return number

@frappe.whitelist()
def get_percentage_difference(doc, result):
	doc = frappe.parse_json(doc)
	result = frappe.parse_json(result)

	doc = frappe.get_doc('Number Card', doc.name)

	if not doc.get('show_percentage_stats'):
		return

	previous_result = calculate_previous_result(doc)
	difference = (result - previous_result)/100.0

	return difference


def calculate_previous_result(doc):
	from frappe.utils import add_to_date

	current_date = frappe.utils.now()
	if doc.stats_time_interval == 'Daily':
		previous_date = add_to_date(current_date, days=-1)
	elif doc.stats_time_interval == 'Weekly':
		previous_date = add_to_date(current_date, weeks=-1)
	elif doc.stats_time_interval == 'Monthly':
		previous_date = add_to_date(current_date, months=-1)
	else:
		previous_date = add_to_date(current_date, years=-1)

	number = get_result(doc, previous_date)
	return number

@frappe.whitelist()
def create_number_card(args):
	args = frappe.parse_json(args)
	doc = frappe.new_doc('Number Card')

	doc.update(args)
	doc.insert(ignore_permissions=True)
	return doc

def get_cards_for_user(doctype, txt, searchfield, start, page_len, filters):
	or_filters = {'owner': frappe.session.user, 'is_public': 1}
	return frappe.db.get_list('Number Card',
		fields=['name', 'label'],
		filters=filters,
		or_filters=or_filters,
		as_list = 1)<commit_msg>fix: add search fields to number cards link query<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2020, <NAME> and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class NumberCard(Document):
	pass


def get_permission_query_conditions(user):

	if not user:
		user = frappe.session.user

	if user == 'Administrator':
		return

	roles = frappe.get_roles(user)
	if "System Manager" in roles:
		return None

	allowed_doctypes = tuple(frappe.permissions.get_doctypes_with_read())

	return '''
			`tabNumber Card`.`document_type` in {allowed_doctypes}
		'''.format(
			allowed_doctypes=allowed_doctypes,
		)

def has_permission(doc, ptype, user):
	roles = frappe.get_roles(user)
	if "System Manager" in roles:
		return True

	allowed_doctypes = tuple(frappe.permissions.get_doctypes_with_read())
	if doc.document_type in allowed_doctypes:
		return True

	return False

@frappe.whitelist()
def get_result(doc, to_date=None):
	doc = frappe.parse_json(doc)
	fields = []
	sql_function_map = {
		'Count': 'count',
		'Sum': 'sum',
		'Average': 'avg',
		'Minimum': 'min',
		'Maximum': 'max'
	}

	function = sql_function_map[doc.function]

	if function == 'count':
		fields = ['{function}(*) as result'.format(function=function)]
	else:
		fields = ['{function}({based_on}) as result'.format(function=function, based_on=doc.aggregate_function_based_on)]

	filters = frappe.parse_json(doc.filters_json)

	if to_date:
		filters.append([doc.document_type, 'creation', '<', to_date, False])

	number = frappe.db.get_all(doc.document_type, fields = fields, filters = filters)[0]['result']

	frappe.db.set_value('Number Card', doc.name, 'previous_result', number)
	return number

@frappe.whitelist()
def get_percentage_difference(doc, result):
	doc = frappe.parse_json(doc)
	result = frappe.parse_json(result)

	doc = frappe.get_doc('Number Card', doc.name)

	if not doc.get('show_percentage_stats'):
		return

	previous_result = calculate_previous_result(doc)
	difference = (result - previous_result)/100.0

	return difference


def calculate_previous_result(doc):
	from frappe.utils import add_to_date

	current_date = frappe.utils.now()
	if doc.stats_time_interval == 'Daily':
		previous_date = add_to_date(current_date, days=-1)
	elif doc.stats_time_interval == 'Weekly':
		previous_date = add_to_date(current_date, weeks=-1)
	elif doc.stats_time_interval == 'Monthly':
		previous_date = add_to_date(current_date, months=-1)
	else:
		previous_date = add_to_date(current_date, years=-1)

	number = get_result(doc, previous_date)
	return number

@frappe.whitelist()
def create_number_card(args):
	args = frappe.parse_json(args)
	doc = frappe.new_doc('Number Card')

	doc.update(args)
	doc.insert(ignore_permissions=True)
	return doc

def get_cards_for_user(doctype, txt, searchfield, start, page_len, filters):
	meta = frappe.get_meta(doctype)
	searchfields = meta.get_search_fields()
	search_conditions = []

	if txt:
		for field in searchfields:
			search_conditions.append('`tab{doctype}`.`{field}` like %(txt)s'.format(field=field, doctype=doctype, txt=txt))

		search_conditions = ' or '.join(search_conditions)

	search_conditions = 'and (' + search_conditions +')' if search_conditions else ''
	conditions, values = frappe.db.build_conditions(filters)
	values['txt'] = '%' + txt + '%'

	return frappe.db.sql(
		'''select
			`tabNumber Card`.name, `tabNumber Card`.label, `tabNumber Card`.document_type
		from
			`tabNumber Card`
		where
			{conditions} and
			(`tabNumber Card`.owner = '{user}' or
			`tabNumber Card`.is_public = 1)
			{search_conditions}
	'''.format(
		filters=filters,
		user = frappe.session.user,
		search_conditions = search_conditions,
		conditions=conditions
	), values)

{'fraction_non_alphanumeric': 0.12705335077772933, 'fraction_numerical': 0.004506468963512139, 'mean_word_length': 5.472248353715899, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, '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': '4930841', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2419, 'n_words': 505}