<commit_before>from __future__ import unicode_literals
__author__ = 'luissaguas'
from frappe import _
import frappe

import json, os

from frappe.utils import cint
from jasper_erpnext_report.utils.file import write_file, get_extension
import jasper_erpnext_report.utils.utils as utils
import JasperServer as Js, JasperLocal as Jl, JasperBase as Jb
from jasper_erpnext_report.utils.cache import redis_transation
from jasper_erpnext_report import jasperserverlib

from io import BytesIO
from PyPDF2 import PdfFileMerger
import cStringIO
import zipfile, hashlib

_logger = frappe.get_logger(__name__)

class JasperRoot(Jb.JasperBase):
	def __init__(self, doc=None):
		self.jps = None
		self.jpl = None
		frappe.local.jasper_session_obj = self
		super(JasperRoot, self).__init__(doc)

	def get_server(self, origin):
		if origin == "server":
			if not self.jps:
				self.jps = Js.JasperServer(self.doc)
				return self.jps
		elif origin == "local":
			if not self.jpl:
				self.jpl = Jl.JasperLocal(self.doc)
				return self.jpl

	#called in jasperserverconfig.py at the save button was pushed. Just save the data
	def config(self):
		_logger.info("JasperServerSession update_cache self.doc {}".format(self.doc))
		if not self.doc:
			frappe.throw(_("Something was wrong, there is no document." ))

		from jasper_erpnext_report import pyjnius
		print "use local and pyjnius local? {} pyjnius {}".format(self.use_local(), pyjnius)
		if self.use_local() and not pyjnius:
			frappe.throw(_("You don't have local server. Install python module pyjnius first." ))

		if self.data["data"]:
			self.update(force_cache=True, force_db=True)
		else:
			#delete all db old sessions if exist
			self.delete_jasper_session()
			self.createJasperSession()

		return

	def login(self):
		self.get_server("server")
		self.jps.login()
		if self.doc.import_all_reports:
			self.jps.import_all_jasper_reports(self.doc)
		if self.jps.is_login:
			self.doc.jasper_server_name = self._get_server_info(force=True)
		else:
			self.doc.jasper_server_name = "Not connected!"

		self.update(force_cache=True, force_db=True)
		return self.doc.jasper_server_name

	def _get_server_info(self, force=False):
		if not force and self.doc.jasper_server_name:
			return self.doc.jasper_server_name
		return self.jps.get_server_info()

	def _get_reports_list(self, filters_report=None, filters_param=None, force=False, cachename="report_list_all", update=False):
		ret = self.get_reports_list_from_db(filters_report=filters_report, filters_param=filters_param)
		#check to see if there is any report by now. If there are reports don't check the server
		#jasperserverlib sign if it was imported jasperserver, a library to connect to the jasperreport server
		if self.user == "Administrator" and ret is None and self.use_server() and jasperserverlib:
			#called from client. Don't let him change old reports attributes
			import_only_new = self.data['data'].get('import_only_new')
			self.data['data']['import_only_new'] = 1
			self.get_server("server")
			self.jps.import_all_jasper_reports(self.data['data'], force=force)
			self.data['data']['import_only_new'] = import_only_new
			#could be recursive but there is no need for resume again because decorator
			ret = self.get_reports_list_from_db(filters_report=filters_report, filters_param=filters_param)

		in_transation = frappe.utils.cint(frappe.__version__.split(".", 1)[0]) > 4
		data = None
		if ret:
			ptime = self.data['data'].get('jasper_polling_time')
			ret['jasper_polling_time'] = ptime
		if ret and not update:
			data = utils.insert_list_all_memcache_db(ret, cachename=cachename, in_transation=in_transation)
		elif ret:
			data = utils.update_list_all_memcache_db(ret, cachename=cachename, in_transation=in_transation)
		return data

	def get_reports_list_for_all(self):
		if self.sid == 'Guest':
			return None
		data = {}
		#dirt = utils.jaspersession_get_value("report_list_dirt_all")
		#dirt = self.is_dirt("report_list_dirt_all")
		#dirt = self.is_cache_dirt("report_list_dirt_all", "user:" + self.user + "_report_list_all")
		dirt = utils.jaspersession_get_value("report_list_dirt_all") or False

		#dirt if redis not cache
		if not dirt:
			#data = utils.get_jasper_session_data_from_cache("user:" + self.user + "_report_list_all")
			#if not data:

			data = utils.get_jasper_session_data_from_cache("report_list_all")
			#if data:
			#	dirt = utils.jaspersession_get_value("report_list_dirt_all") or frappe.utils.now()
			#	all_last_update = data.get('last_updated')
			#	time_diff = frappe.utils.time_diff_in_seconds(dirt, all_last_update)
			#	if time_diff > 0:
			#		data = None
			#else:
			#	if not self.check_server_status():
			#		self.remove_server_docs(data)
			#	return data
		#if for some reason there is no cache get it from db
		if not data:
			#frappe.cache().delete_value("jasper:report_list_all")
		#	frappe.cache().delete_value("jasper:user:" + self.user + "_report_list_all")
			r_filters=["`tabJasper Reports`.jasper_doctype is NULL", "`tabJasper Reports`.report is NULL"]
			ldata = self._get_reports_list(filters_report=r_filters)
			cached = redis_transation(ldata, "report_list_all")
			if ldata:
				data = ldata.get("data", None)
			if cached and data:
				utils.jaspersession_set_value("report_list_dirt_all", False)
				#utils.jaspersession_set_value("report_list_dirt_all", frappe.utils.now())
				utils.jaspersession_set_value("report_list_dirt_doc", frappe.utils.now())

		if data:
			#utils.jaspersession_set_value("report_list_dirt_all", frappe.utils.now())
			data.pop('session_expiry',None)
			data.pop('last_updated', None)

			self.filter_perm_roles(data)
			if not self.check_server_status():
				self.remove_server_docs(data)
			try:
				data['mail_enabled'] = cint(frappe.db.get_single_value("Outgoing Email Settings", "enabled"))
			except:
				data['mail_enabled'] = "disabled"

			print "data before pop data {}".format(data.get("size"))
			#utils.insert_list_all_memcache_db(data, cachename="user:" + self.user + "_report_list_all")
			#utils.jaspersession_set_value("user:" + self.user + "_report_list_all", frappe.utils.now())

		return data

	"""
	def is_cache_dirt(self, lstdirt, what):
		#data = utils.get_jasper_session_data_from_cache(what)
		#if data:
		is_dirt = False
		user_last_update = utils.jaspersession_get_value(what)
		dirt = utils.jaspersession_get_value(lstdirt)
		if not user_last_update or not dirt:
			utils.jaspersession_set_value(what, frappe.utils.now())
			return False
		#user_last_update = data.get('last_updated')
		#user_last_update = data<commit_msg>Fix origin when there is no reports.<commit_after>from __future__ import unicode_literals
__author__ = 'luissaguas'
from frappe import _
import frappe

import json, os

from frappe.utils import cint
from jasper_erpnext_report.utils.file import write_file, get_extension
import jasper_erpnext_report.utils.utils as utils
import JasperServer as Js, JasperLocal as Jl, JasperBase as Jb
from jasper_erpnext_report.utils.cache import redis_transation
from jasper_erpnext_report import jasperserverlib

from io import BytesIO
from PyPDF2 import PdfFileMerger
import cStringIO
import zipfile, hashlib

_logger = frappe.get_logger(__name__)

class JasperRoot(Jb.JasperBase):
	def __init__(self, doc=None):
		self.jps = None
		self.jpl = None
		frappe.local.jasper_session_obj = self
		super(JasperRoot, self).__init__(doc)

	def get_server(self, origin):
		if origin == "server":
			if not self.jps:
				self.jps = Js.JasperServer(self.doc)
				return self.jps
		elif origin == "local":
			if not self.jpl:
				self.jpl = Jl.JasperLocal(self.doc)
				return self.jpl

	#called in jasperserverconfig.py at the save button was pushed. Just save the data
	def config(self):
		_logger.info("JasperServerSession update_cache self.doc {}".format(self.doc))
		if not self.doc:
			frappe.throw(_("Something was wrong, there is no document." ))

		from jasper_erpnext_report import pyjnius
		print "use local and pyjnius local? {} pyjnius {}".format(self.use_local(), pyjnius)
		if self.use_local() and not pyjnius:
			frappe.throw(_("You don't have local server. Install python module pyjnius first." ))

		if self.data["data"]:
			self.update(force_cache=True, force_db=True)
		else:
			#delete all db old sessions if exist
			self.delete_jasper_session()
			self.createJasperSession()

		return

	def login(self):
		self.get_server("server")
		self.jps.login()
		if self.doc.import_all_reports:
			self.jps.import_all_jasper_reports(self.doc)
		if self.jps.is_login:
			self.doc.jasper_server_name = self._get_server_info(force=True)
		else:
			self.doc.jasper_server_name = "Not connected!"

		self.update(force_cache=True, force_db=True)
		return self.doc.jasper_server_name

	def _get_server_info(self, force=False):
		if not force and self.doc.jasper_server_name:
			return self.doc.jasper_server_name
		return self.jps.get_server_info()

	def _get_reports_list(self, filters_report=None, filters_param=None, force=False, cachename="report_list_all", update=False):
		ret = self.get_reports_list_from_db(filters_report=filters_report, filters_param=filters_param)
		#check to see if there is any report by now. If there are reports don't check the server
		#jasperserverlib sign if it was imported jasperserver, a library to connect to the jasperreport server
		if self.user == "Administrator" and ret is None and self.use_server() and jasperserverlib:
			#called from client. Don't let him change old reports attributes
			import_only_new = self.data['data'].get('import_only_new')
			self.data['data']['import_only_new'] = 1
			self.get_server("server")
			self.jps.import_all_jasper_reports(self.data['data'], force=force)
			self.data['data']['import_only_new'] = import_only_new
			#could be recursive but there is no need for resume again because decorator
			ret = self.get_reports_list_from_db(filters_report=filters_report, filters_param=filters_param)

		in_transation = frappe.utils.cint(frappe.__version__.split(".", 1)[0]) > 4
		data = None
		if ret:
			ptime = self.data['data'].get('jasper_polling_time')
			ret['jasper_polling_time'] = ptime
		if ret and not update:
			data = utils.insert_list_all_memcache_db(ret, cachename=cachename, in_transation=in_transation)
		elif ret:
			data = utils.update_list_all_memcache_db(ret, cachename=cachename, in_transation=in_transation)
		else:
			data = {"data":{"origin": self.get_report_origin()}}
		return data

	def get_reports_list_for_all(self):
		if self.sid == 'Guest':
			return None
		data = {}
		#dirt = utils.jaspersession_get_value("report_list_dirt_all")
		#dirt = self.is_dirt("report_list_dirt_all")
		#dirt = self.is_cache_dirt("report_list_dirt_all", "user:" + self.user + "_report_list_all")
		dirt = utils.jaspersession_get_value("report_list_dirt_all") or False

		#dirt if redis not cache
		if not dirt:
			#data = utils.get_jasper_session_data_from_cache("user:" + self.user + "_report_list_all")
			#if not data:

			data = utils.get_jasper_session_data_from_cache("report_list_all")
			#if data:
			#	dirt = utils.jaspersession_get_value("report_list_dirt_all") or frappe.utils.now()
			#	all_last_update = data.get('last_updated')
			#	time_diff = frappe.utils.time_diff_in_seconds(dirt, all_last_update)
			#	if time_diff > 0:
			#		data = None
			#else:
			#	if not self.check_server_status():
			#		self.remove_server_docs(data)
			#	return data
		#if for some reason there is no cache get it from db
		if not data:
			#frappe.cache().delete_value("jasper:report_list_all")
		#	frappe.cache().delete_value("jasper:user:" + self.user + "_report_list_all")
			r_filters=["`tabJasper Reports`.jasper_doctype is NULL", "`tabJasper Reports`.report is NULL"]
			ldata = self._get_reports_list(filters_report=r_filters)
			cached = redis_transation(ldata, "report_list_all")
			if ldata:
				data = ldata.get("data", None)
			if cached and data:
				utils.jaspersession_set_value("report_list_dirt_all", False)
				#utils.jaspersession_set_value("report_list_dirt_all", frappe.utils.now())
				utils.jaspersession_set_value("report_list_dirt_doc", frappe.utils.now())

		if data:
			#utils.jaspersession_set_value("report_list_dirt_all", frappe.utils.now())
			data.pop('session_expiry', None)
			data.pop('last_updated', None)

			self.filter_perm_roles(data)
			if not self.check_server_status():
				self.remove_server_docs(data)
			try:
				data['mail_enabled'] = cint(frappe.db.get_single_value("Outgoing Email Settings", "enabled"))
			except:
				data['mail_enabled'] = "disabled"

			print "data before pop data {}".format(data.get("size"))
			#utils.insert_list_all_memcache_db(data, cachename="user:" + self.user + "_report_list_all")
			#utils.jaspersession_set_value("user:" + self.user + "_report_list_all", frappe.utils.now())

		return data

	"""
	def is_cache_dirt(self, lstdirt, what):
		#data = utils.get_jasper_session_data_from_cache(what)
		#if data:
		is_dirt = False
		user_last_update = utils.jaspersession_get_value(what)
		dirt = utils.jaspersession_get_value(lstdirt)
		if not user_last_update or not dirt:
			utils.jaspersession_set_value(what, frappe.utils.now())
			return False
		#user_last_update = data.get('last_updated')
		#user_last_update = data
{'fraction_non_alphanumeric': 0.10806720145300439, 'fraction_numerical': 0.0009081277433025579, 'mean_word_length': 5.1465116279069765, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, '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': '1676369', 'n_tokens_mistral': 5141, 'n_tokens_neox': 4718, 'n_words': 990}