You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1901 line
56 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: MIT. See LICENSE
  3. """
  4. Frappe - Low Code Open Source Framework in Python and JS
  5. Frappe, pronounced fra-pay, is a full stack, batteries-included, web
  6. framework written in Python and Javascript with MariaDB as the database.
  7. It is the framework which powers ERPNext. It is pretty generic and can
  8. be used to build database driven apps.
  9. Read the documentation: https://frappeframework.com/docs
  10. """
  11. import os, warnings
  12. STANDARD_USERS = ('Guest', 'Administrator')
  13. _dev_server = os.environ.get('DEV_SERVER', False)
  14. if _dev_server:
  15. warnings.simplefilter('always', DeprecationWarning)
  16. warnings.simplefilter('always', PendingDeprecationWarning)
  17. from werkzeug.local import Local, release_local
  18. import sys, importlib, inspect, json
  19. import typing
  20. import click
  21. # Local application imports
  22. from .exceptions import *
  23. from .utils.jinja import (get_jenv, get_template, render_template, get_email_from_template, get_jloader)
  24. from .utils.lazy_loader import lazy_import
  25. from frappe.query_builder import (
  26. get_query_builder,
  27. patch_query_execute,
  28. patch_query_aggregation,
  29. )
  30. from frappe.utils.data import cstr
  31. __version__ = '14.0.0-dev'
  32. __title__ = "Frappe Framework"
  33. local = Local()
  34. controllers = {}
  35. class _dict(dict):
  36. """dict like object that exposes keys as attributes"""
  37. def __getattr__(self, key):
  38. ret = self.get(key)
  39. # "__deepcopy__" exception added to fix frappe#14833 via DFP
  40. if not ret and key.startswith("__") and key != "__deepcopy__":
  41. raise AttributeError()
  42. return ret
  43. def __setattr__(self, key, value):
  44. self[key] = value
  45. def __getstate__(self):
  46. return self
  47. def __setstate__(self, d):
  48. self.update(d)
  49. def update(self, d):
  50. """update and return self -- the missing dict feature in python"""
  51. super(_dict, self).update(d)
  52. return self
  53. def copy(self):
  54. return _dict(dict(self).copy())
  55. def _(msg, lang=None, context=None):
  56. """Returns translated string in current lang, if exists.
  57. Usage:
  58. _('Change')
  59. _('Change', context='Coins')
  60. """
  61. from frappe.translate import get_full_dict
  62. from frappe.utils import strip_html_tags, is_html
  63. if not hasattr(local, 'lang'):
  64. local.lang = lang or 'en'
  65. if not lang:
  66. lang = local.lang
  67. non_translated_string = msg
  68. if is_html(msg):
  69. msg = strip_html_tags(msg)
  70. # msg should always be unicode
  71. msg = as_unicode(msg).strip()
  72. translated_string = ''
  73. if context:
  74. string_key = '{msg}:{context}'.format(msg=msg, context=context)
  75. translated_string = get_full_dict(lang).get(string_key)
  76. if not translated_string:
  77. translated_string = get_full_dict(lang).get(msg)
  78. # return lang_full_dict according to lang passed parameter
  79. return translated_string or non_translated_string
  80. def as_unicode(text, encoding='utf-8'):
  81. '''Convert to unicode if required'''
  82. if isinstance(text, str):
  83. return text
  84. elif text is None:
  85. return ''
  86. elif isinstance(text, bytes):
  87. return str(text, encoding)
  88. else:
  89. return str(text)
  90. def get_lang_dict(fortype, name=None):
  91. """Returns the translated language dict for the given type and name.
  92. :param fortype: must be one of `doctype`, `page`, `report`, `include`, `jsfile`, `boot`
  93. :param name: name of the document for which assets are to be returned."""
  94. from frappe.translate import get_dict
  95. return get_dict(fortype, name)
  96. def set_user_lang(user, user_language=None):
  97. """Guess and set user language for the session. `frappe.local.lang`"""
  98. from frappe.translate import get_user_lang
  99. local.lang = get_user_lang(user)
  100. # local-globals
  101. db = local("db")
  102. qb = local("qb")
  103. conf = local("conf")
  104. form = form_dict = local("form_dict")
  105. request = local("request")
  106. response = local("response")
  107. session = local("session")
  108. user = local("user")
  109. flags = local("flags")
  110. error_log = local("error_log")
  111. debug_log = local("debug_log")
  112. message_log = local("message_log")
  113. lang = local("lang")
  114. # This if block is never executed when running the code. It is only used for
  115. # telling static code analyzer where to find dynamically defined attributes.
  116. if typing.TYPE_CHECKING:
  117. from frappe.utils.redis_wrapper import RedisWrapper
  118. from frappe.database.mariadb.database import MariaDBDatabase
  119. from frappe.database.postgres.database import PostgresDatabase
  120. from frappe.query_builder.builder import MariaDB, Postgres
  121. db: typing.Union[MariaDBDatabase, PostgresDatabase]
  122. qb: typing.Union[MariaDB, Postgres]
  123. # end: static analysis hack
  124. def init(site, sites_path=None, new_site=False):
  125. """Initialize frappe for the current site. Reset thread locals `frappe.local`"""
  126. if getattr(local, "initialised", None):
  127. return
  128. if not sites_path:
  129. sites_path = '.'
  130. local.error_log = []
  131. local.message_log = []
  132. local.debug_log = []
  133. local.realtime_log = []
  134. local.flags = _dict({
  135. "currently_saving": [],
  136. "redirect_location": "",
  137. "in_install_db": False,
  138. "in_install_app": False,
  139. "in_import": False,
  140. "in_test": False,
  141. "mute_messages": False,
  142. "ignore_links": False,
  143. "mute_emails": False,
  144. "has_dataurl": False,
  145. "new_site": new_site
  146. })
  147. local.rollback_observers = []
  148. local.before_commit = []
  149. local.test_objects = {}
  150. local.site = site
  151. local.sites_path = sites_path
  152. local.site_path = os.path.join(sites_path, site)
  153. local.all_apps = None
  154. local.request_ip = None
  155. local.response = _dict({"docs":[]})
  156. local.task_id = None
  157. local.conf = _dict(get_site_config())
  158. local.lang = local.conf.lang or "en"
  159. local.lang_full_dict = None
  160. local.module_app = None
  161. local.app_modules = None
  162. local.system_settings = _dict()
  163. local.user = None
  164. local.user_perms = None
  165. local.session = None
  166. local.role_permissions = {}
  167. local.valid_columns = {}
  168. local.new_doc_templates = {}
  169. local.link_count = {}
  170. local.jenv = None
  171. local.jloader =None
  172. local.cache = {}
  173. local.document_cache = {}
  174. local.meta_cache = {}
  175. local.autoincremented_status_map = {site: -1}
  176. local.form_dict = _dict()
  177. local.session = _dict()
  178. local.dev_server = _dev_server
  179. local.qb = get_query_builder(local.conf.db_type or "mariadb")
  180. setup_module_map()
  181. patch_query_execute()
  182. patch_query_aggregation()
  183. local.initialised = True
  184. def connect(site=None, db_name=None, set_admin_as_user=True):
  185. """Connect to site database instance.
  186. :param site: If site is given, calls `frappe.init`.
  187. :param db_name: Optional. Will use from `site_config.json`.
  188. :param set_admin_as_user: Set Administrator as current user.
  189. """
  190. from frappe.database import get_db
  191. if site:
  192. init(site)
  193. local.db = get_db(user=db_name or local.conf.db_name)
  194. if set_admin_as_user:
  195. set_user("Administrator")
  196. def connect_replica():
  197. from frappe.database import get_db
  198. user = local.conf.db_name
  199. password = local.conf.db_password
  200. port = local.conf.replica_db_port
  201. if local.conf.different_credentials_for_replica:
  202. user = local.conf.replica_db_name
  203. password = local.conf.replica_db_password
  204. local.replica_db = get_db(host=local.conf.replica_host, user=user, password=password, port=port)
  205. # swap db connections
  206. local.primary_db = local.db
  207. local.db = local.replica_db
  208. def get_site_config(sites_path=None, site_path=None):
  209. """Returns `site_config.json` combined with `sites/common_site_config.json`.
  210. `site_config` is a set of site wide settings like database name, password, email etc."""
  211. config = {}
  212. sites_path = sites_path or getattr(local, "sites_path", None)
  213. site_path = site_path or getattr(local, "site_path", None)
  214. if sites_path:
  215. common_site_config = os.path.join(sites_path, "common_site_config.json")
  216. if os.path.exists(common_site_config):
  217. try:
  218. config.update(get_file_json(common_site_config))
  219. except Exception as error:
  220. click.secho("common_site_config.json is invalid", fg="red")
  221. print(error)
  222. if site_path:
  223. site_config = os.path.join(site_path, "site_config.json")
  224. if os.path.exists(site_config):
  225. try:
  226. config.update(get_file_json(site_config))
  227. except Exception as error:
  228. click.secho("{0}/site_config.json is invalid".format(local.site), fg="red")
  229. print(error)
  230. elif local.site and not local.flags.new_site:
  231. raise IncorrectSitePath("{0} does not exist".format(local.site))
  232. return _dict(config)
  233. def get_conf(site=None):
  234. if hasattr(local, 'conf'):
  235. return local.conf
  236. else:
  237. # if no site, get from common_site_config.json
  238. with init_site(site):
  239. return local.conf
  240. class init_site:
  241. def __init__(self, site=None):
  242. '''If site is None, initialize it for empty site ('') to load common_site_config.json'''
  243. self.site = site or ''
  244. def __enter__(self):
  245. init(self.site)
  246. return local
  247. def __exit__(self, type, value, traceback):
  248. destroy()
  249. def destroy():
  250. """Closes connection and releases werkzeug local."""
  251. if db:
  252. db.close()
  253. release_local(local)
  254. redis_server = None
  255. def cache() -> "RedisWrapper":
  256. """Returns redis connection."""
  257. global redis_server
  258. if not redis_server:
  259. from frappe.utils.redis_wrapper import RedisWrapper
  260. redis_server = RedisWrapper.from_url(conf.get('redis_cache')
  261. or "redis://localhost:11311")
  262. return redis_server
  263. def get_traceback():
  264. """Returns error traceback."""
  265. from frappe.utils import get_traceback
  266. return get_traceback()
  267. def errprint(msg):
  268. """Log error. This is sent back as `exc` in response.
  269. :param msg: Message."""
  270. msg = as_unicode(msg)
  271. if not request or (not "cmd" in local.form_dict) or conf.developer_mode:
  272. print(msg)
  273. error_log.append({"exc": msg})
  274. def print_sql(enable=True):
  275. return cache().set_value('flag_print_sql', enable)
  276. def log(msg):
  277. """Add to `debug_log`.
  278. :param msg: Message."""
  279. if not request:
  280. if conf.get("logging") or False:
  281. print(repr(msg))
  282. debug_log.append(as_unicode(msg))
  283. def msgprint(msg, title=None, raise_exception=0, as_table=False, as_list=False, indicator=None, alert=False, primary_action=None, is_minimizable=None, wide=None):
  284. """Print a message to the user (via HTTP response).
  285. Messages are sent in the `__server_messages` property in the
  286. response JSON and shown in a pop-up / modal.
  287. :param msg: Message.
  288. :param title: [optional] Message title. Default: "Message".
  289. :param raise_exception: [optional] Raise given exception and show message.
  290. :param as_table: [optional] If `msg` is a list of lists, render as HTML table.
  291. :param as_list: [optional] If `msg` is a list, render as un-ordered list.
  292. :param primary_action: [optional] Bind a primary server/client side action.
  293. :param is_minimizable: [optional] Allow users to minimize the modal
  294. :param wide: [optional] Show wide modal
  295. """
  296. from frappe.utils import strip_html_tags
  297. msg = safe_decode(msg)
  298. out = _dict(message=msg)
  299. def _raise_exception():
  300. if raise_exception:
  301. if flags.rollback_on_exception:
  302. db.rollback()
  303. import inspect
  304. if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
  305. raise raise_exception(msg)
  306. else:
  307. raise ValidationError(msg)
  308. if flags.mute_messages:
  309. _raise_exception()
  310. return
  311. if as_table and type(msg) in (list, tuple):
  312. out.as_table = 1
  313. if as_list and type(msg) in (list, tuple) and len(msg) > 1:
  314. out.as_list = 1
  315. if flags.print_messages and out.message:
  316. print(f"Message: {strip_html_tags(out.message)}")
  317. out.title = title or _("Message", context="Default title of the message dialog")
  318. if not indicator and raise_exception:
  319. indicator = 'red'
  320. if indicator:
  321. out.indicator = indicator
  322. if is_minimizable:
  323. out.is_minimizable = is_minimizable
  324. if alert:
  325. out.alert = 1
  326. if raise_exception:
  327. out.raise_exception = 1
  328. if primary_action:
  329. out.primary_action = primary_action
  330. if wide:
  331. out.wide = wide
  332. message_log.append(json.dumps(out))
  333. if raise_exception and hasattr(raise_exception, '__name__'):
  334. local.response['exc_type'] = raise_exception.__name__
  335. _raise_exception()
  336. def clear_messages():
  337. local.message_log = []
  338. def get_message_log():
  339. log = []
  340. for msg_out in local.message_log:
  341. log.append(json.loads(msg_out))
  342. return log
  343. def clear_last_message():
  344. if len(local.message_log) > 0:
  345. local.message_log = local.message_log[:-1]
  346. def throw(msg, exc=ValidationError, title=None, is_minimizable=None, wide=None, as_list=False):
  347. """Throw execption and show message (`msgprint`).
  348. :param msg: Message.
  349. :param exc: Exception class. Default `frappe.ValidationError`"""
  350. msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable, wide=wide, as_list=as_list)
  351. def emit_js(js, user=False, **kwargs):
  352. if user is False:
  353. user = session.user
  354. publish_realtime('eval_js', js, user=user, **kwargs)
  355. def create_folder(path, with_init=False):
  356. """Create a folder in the given path and add an `__init__.py` file (optional).
  357. :param path: Folder path.
  358. :param with_init: Create `__init__.py` in the new folder."""
  359. from frappe.utils import touch_file
  360. if not os.path.exists(path):
  361. os.makedirs(path)
  362. if with_init:
  363. touch_file(os.path.join(path, "__init__.py"))
  364. def set_user(username):
  365. """Set current user.
  366. :param username: **User** name to set as current user."""
  367. local.session.user = username
  368. local.session.sid = username
  369. local.cache = {}
  370. local.form_dict = _dict()
  371. local.jenv = None
  372. local.session.data = _dict()
  373. local.role_permissions = {}
  374. local.new_doc_templates = {}
  375. local.user_perms = None
  376. def get_user():
  377. from frappe.utils.user import UserPermissions
  378. if not local.user_perms:
  379. local.user_perms = UserPermissions(local.session.user)
  380. return local.user_perms
  381. def get_roles(username=None):
  382. """Returns roles of current user."""
  383. if not local.session:
  384. return ["Guest"]
  385. import frappe.permissions
  386. return frappe.permissions.get_roles(username or local.session.user)
  387. def get_request_header(key, default=None):
  388. """Return HTTP request header.
  389. :param key: HTTP header key.
  390. :param default: Default value."""
  391. return request.headers.get(key, default)
  392. def sendmail(recipients=None, sender="", subject="No Subject", message="No Message",
  393. as_markdown=False, delayed=True, reference_doctype=None, reference_name=None,
  394. unsubscribe_method=None, unsubscribe_params=None, unsubscribe_message=None, add_unsubscribe_link=1,
  395. attachments=None, content=None, doctype=None, name=None, reply_to=None, queue_separately=False,
  396. cc=None, bcc=None, message_id=None, in_reply_to=None, send_after=None, expose_recipients=None,
  397. send_priority=1, communication=None, retry=1, now=None, read_receipt=None, is_notification=False,
  398. inline_images=None, template=None, args=None, header=None, print_letterhead=False, with_container=False):
  399. """Send email using user's default **Email Account** or global default **Email Account**.
  400. :param recipients: List of recipients.
  401. :param sender: Email sender. Default is current user or default outgoing account.
  402. :param subject: Email Subject.
  403. :param message: (or `content`) Email Content.
  404. :param as_markdown: Convert content markdown to HTML.
  405. :param delayed: Send via scheduled email sender **Email Queue**. Don't send immediately. Default is true
  406. :param send_priority: Priority for Email Queue, default 1.
  407. :param reference_doctype: (or `doctype`) Append as communication to this DocType.
  408. :param reference_name: (or `name`) Append as communication to this document name.
  409. :param unsubscribe_method: Unsubscribe url with options email, doctype, name. e.g. `/api/method/unsubscribe`
  410. :param unsubscribe_params: Unsubscribe paramaters to be loaded on the unsubscribe_method [optional] (dict).
  411. :param attachments: List of attachments.
  412. :param reply_to: Reply-To Email Address.
  413. :param message_id: Used for threading. If a reply is received to this email, Message-Id is sent back as In-Reply-To in received email.
  414. :param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To.
  415. :param send_after: Send after the given datetime.
  416. :param expose_recipients: Display all recipients in the footer message - "This email was sent to"
  417. :param communication: Communication link to be set in Email Queue record
  418. :param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id
  419. :param template: Name of html template from templates/emails folder
  420. :param args: Arguments for rendering the template
  421. :param header: Append header in email
  422. :param with_container: Wraps email inside a styled container
  423. """
  424. if recipients is None:
  425. recipients = []
  426. if cc is None:
  427. cc = []
  428. if bcc is None:
  429. bcc = []
  430. text_content = None
  431. if template:
  432. message, text_content = get_email_from_template(template, args)
  433. message = content or message
  434. if as_markdown:
  435. from frappe.utils import md_to_html
  436. message = md_to_html(message)
  437. if not delayed:
  438. now = True
  439. from frappe.email.doctype.email_queue.email_queue import QueueBuilder
  440. builder = QueueBuilder(recipients=recipients, sender=sender,
  441. subject=subject, message=message, text_content=text_content,
  442. reference_doctype = doctype or reference_doctype, reference_name = name or reference_name, add_unsubscribe_link=add_unsubscribe_link,
  443. unsubscribe_method=unsubscribe_method, unsubscribe_params=unsubscribe_params, unsubscribe_message=unsubscribe_message,
  444. attachments=attachments, reply_to=reply_to, cc=cc, bcc=bcc, message_id=message_id, in_reply_to=in_reply_to,
  445. send_after=send_after, expose_recipients=expose_recipients, send_priority=send_priority, queue_separately=queue_separately,
  446. communication=communication, read_receipt=read_receipt, is_notification=is_notification,
  447. inline_images=inline_images, header=header, print_letterhead=print_letterhead, with_container=with_container)
  448. # build email queue and send the email if send_now is True.
  449. builder.process(send_now=now)
  450. whitelisted = []
  451. guest_methods = []
  452. xss_safe_methods = []
  453. allowed_http_methods_for_whitelisted_func = {}
  454. def whitelist(allow_guest=False, xss_safe=False, methods=None):
  455. """
  456. Decorator for whitelisting a function and making it accessible via HTTP.
  457. Standard request will be `/api/method/[path.to.method]`
  458. :param allow_guest: Allow non logged-in user to access this method.
  459. :param methods: Allowed http method to access the method.
  460. Use as:
  461. @frappe.whitelist()
  462. def myfunc(param1, param2):
  463. pass
  464. """
  465. if not methods:
  466. methods = ['GET', 'POST', 'PUT', 'DELETE']
  467. def innerfn(fn):
  468. global whitelisted, guest_methods, xss_safe_methods, allowed_http_methods_for_whitelisted_func
  469. # get function from the unbound / bound method
  470. # this is needed because functions can be compared, but not methods
  471. method = None
  472. if hasattr(fn, '__func__'):
  473. method = fn
  474. fn = method.__func__
  475. whitelisted.append(fn)
  476. allowed_http_methods_for_whitelisted_func[fn] = methods
  477. if allow_guest:
  478. guest_methods.append(fn)
  479. if xss_safe:
  480. xss_safe_methods.append(fn)
  481. return method or fn
  482. return innerfn
  483. def is_whitelisted(method):
  484. from frappe.utils import sanitize_html
  485. is_guest = session['user'] == 'Guest'
  486. if method not in whitelisted or is_guest and method not in guest_methods:
  487. throw(_("Not permitted"), PermissionError)
  488. if is_guest and method not in xss_safe_methods:
  489. # strictly sanitize form_dict
  490. # escapes html characters like <> except for predefined tags like a, b, ul etc.
  491. for key, value in form_dict.items():
  492. if isinstance(value, str):
  493. form_dict[key] = sanitize_html(value)
  494. def read_only():
  495. def innfn(fn):
  496. def wrapper_fn(*args, **kwargs):
  497. if conf.read_from_replica:
  498. connect_replica()
  499. try:
  500. retval = fn(*args, **get_newargs(fn, kwargs))
  501. finally:
  502. if local and hasattr(local, 'primary_db'):
  503. local.db.close()
  504. local.db = local.primary_db
  505. return retval
  506. return wrapper_fn
  507. return innfn
  508. def write_only():
  509. # if replica connection exists, we have to replace it momentarily with the primary connection
  510. def innfn(fn):
  511. def wrapper_fn(*args, **kwargs):
  512. primary_db = getattr(local, "primary_db", None)
  513. replica_db = getattr(local, "replica_db", None)
  514. in_read_only = getattr(local, "db", None) != primary_db
  515. # switch to primary connection
  516. if in_read_only and primary_db:
  517. local.db = local.primary_db
  518. try:
  519. retval = fn(*args, **get_newargs(fn, kwargs))
  520. finally:
  521. # switch back to replica connection
  522. if in_read_only and replica_db:
  523. local.db = replica_db
  524. return retval
  525. return wrapper_fn
  526. return innfn
  527. def only_for(roles, message=False):
  528. """Raise `frappe.PermissionError` if the user does not have any of the given **Roles**.
  529. :param roles: List of roles to check."""
  530. if local.flags.in_test:
  531. return
  532. if not isinstance(roles, (tuple, list)):
  533. roles = (roles,)
  534. roles = set(roles)
  535. myroles = set(get_roles())
  536. if not roles.intersection(myroles):
  537. if message:
  538. msgprint(_('This action is only allowed for {}').format(bold(', '.join(roles))), _('Not Permitted'))
  539. raise PermissionError
  540. def get_domain_data(module):
  541. try:
  542. domain_data = get_hooks('domains')
  543. if module in domain_data:
  544. return _dict(get_attr(get_hooks('domains')[module][0] + '.data'))
  545. else:
  546. return _dict()
  547. except ImportError:
  548. if local.flags.in_test:
  549. return _dict()
  550. else:
  551. raise
  552. def clear_cache(user=None, doctype=None):
  553. """Clear **User**, **DocType** or global cache.
  554. :param user: If user is given, only user cache is cleared.
  555. :param doctype: If doctype is given, only DocType cache is cleared."""
  556. import frappe.cache_manager
  557. if doctype:
  558. frappe.cache_manager.clear_doctype_cache(doctype)
  559. reset_metadata_version()
  560. elif user:
  561. frappe.cache_manager.clear_user_cache(user)
  562. else: # everything
  563. from frappe import translate
  564. frappe.cache_manager.clear_user_cache()
  565. frappe.cache_manager.clear_domain_cache()
  566. translate.clear_cache()
  567. reset_metadata_version()
  568. local.cache = {}
  569. local.new_doc_templates = {}
  570. for fn in get_hooks("clear_cache"):
  571. get_attr(fn)()
  572. local.role_permissions = {}
  573. def only_has_select_perm(doctype, user=None, ignore_permissions=False):
  574. if ignore_permissions:
  575. return False
  576. if not user:
  577. user = local.session.user
  578. import frappe.permissions
  579. permissions = frappe.permissions.get_role_permissions(doctype, user=user)
  580. if permissions.get('select') and not permissions.get('read'):
  581. return True
  582. else:
  583. return False
  584. def has_permission(doctype=None, ptype="read", doc=None, user=None, verbose=False, throw=False, parent_doctype=None):
  585. """Raises `frappe.PermissionError` if not permitted.
  586. :param doctype: DocType for which permission is to be check.
  587. :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`.
  588. :param doc: [optional] Checks User permissions for given doc.
  589. :param user: [optional] Check for given user. Default: current user.
  590. :param parent_doctype: Required when checking permission for a child DocType (unless doc is specified)."""
  591. import frappe.permissions
  592. if not doctype and doc:
  593. doctype = doc.doctype
  594. out = frappe.permissions.has_permission(doctype, ptype, doc=doc, verbose=verbose, user=user,
  595. raise_exception=throw, parent_doctype=parent_doctype)
  596. if throw and not out:
  597. # mimics frappe.throw
  598. document_label = f"{doc.doctype} {doc.name}" if doc else doctype
  599. msgprint(
  600. _("No permission for {0}").format(document_label),
  601. raise_exception=ValidationError,
  602. title=None,
  603. indicator='red',
  604. is_minimizable=None,
  605. wide=None,
  606. as_list=False
  607. )
  608. return out
  609. def has_website_permission(doc=None, ptype='read', user=None, verbose=False, doctype=None):
  610. """Raises `frappe.PermissionError` if not permitted.
  611. :param doctype: DocType for which permission is to be check.
  612. :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`.
  613. :param doc: Checks User permissions for given doc.
  614. :param user: [optional] Check for given user. Default: current user."""
  615. if not user:
  616. user = session.user
  617. if doc:
  618. if isinstance(doc, str):
  619. doc = get_doc(doctype, doc)
  620. doctype = doc.doctype
  621. if doc.flags.ignore_permissions:
  622. return True
  623. # check permission in controller
  624. if hasattr(doc, 'has_website_permission'):
  625. return doc.has_website_permission(ptype, user, verbose=verbose)
  626. hooks = (get_hooks("has_website_permission") or {}).get(doctype, [])
  627. if hooks:
  628. for method in hooks:
  629. result = call(method, doc=doc, ptype=ptype, user=user, verbose=verbose)
  630. # if even a single permission check is Falsy
  631. if not result:
  632. return False
  633. # else it is Truthy
  634. return True
  635. else:
  636. return False
  637. def is_table(doctype):
  638. """Returns True if `istable` property (indicating child Table) is set for given DocType."""
  639. def get_tables():
  640. return db.get_values(
  641. "DocType", filters={"istable": 1}, order_by=None, pluck=True
  642. )
  643. tables = cache().get_value("is_table", get_tables)
  644. return doctype in tables
  645. def get_precision(doctype, fieldname, currency=None, doc=None):
  646. """Get precision for a given field"""
  647. from frappe.model.meta import get_field_precision
  648. return get_field_precision(get_meta(doctype).get_field(fieldname), doc, currency)
  649. def generate_hash(txt=None, length=None):
  650. """Generates random hash for given text + current timestamp + random string."""
  651. import hashlib, time
  652. from .utils import random_string
  653. digest = hashlib.sha224(((txt or "") + repr(time.time()) + repr(random_string(8))).encode()).hexdigest()
  654. if length:
  655. digest = digest[:length]
  656. return digest
  657. def reset_metadata_version():
  658. """Reset `metadata_version` (Client (Javascript) build ID) hash."""
  659. v = generate_hash()
  660. cache().set_value("metadata_version", v)
  661. return v
  662. def new_doc(doctype, parent_doc=None, parentfield=None, as_dict=False):
  663. """Returns a new document of the given DocType with defaults set.
  664. :param doctype: DocType of the new document.
  665. :param parent_doc: [optional] add to parent document.
  666. :param parentfield: [optional] add against this `parentfield`."""
  667. from frappe.model.create_new import get_new_doc
  668. return get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict)
  669. def set_value(doctype, docname, fieldname, value=None):
  670. """Set document value. Calls `frappe.client.set_value`"""
  671. import frappe.client
  672. return frappe.client.set_value(doctype, docname, fieldname, value)
  673. def get_cached_doc(*args, **kwargs):
  674. if key := can_cache_doc(args):
  675. # local cache
  676. doc = local.document_cache.get(key)
  677. if doc:
  678. return doc
  679. # redis cache
  680. doc = cache().hget('document_cache', key)
  681. if doc:
  682. doc = get_doc(doc)
  683. local.document_cache[key] = doc
  684. return doc
  685. # database
  686. doc = get_doc(*args, **kwargs)
  687. return doc
  688. def can_cache_doc(args):
  689. """
  690. Determine if document should be cached based on get_doc params.
  691. Returns cache key if doc can be cached, None otherwise.
  692. """
  693. if not args:
  694. return
  695. doctype = args[0]
  696. name = doctype if len(args) == 1 else args[1]
  697. # Only cache if both doctype and name are strings
  698. if isinstance(doctype, str) and isinstance(name, str):
  699. return get_document_cache_key(doctype, name)
  700. def get_document_cache_key(doctype, name):
  701. return f'{doctype}::{name}'
  702. def clear_document_cache(doctype, name):
  703. cache().hdel("last_modified", doctype)
  704. key = get_document_cache_key(doctype, name)
  705. if key in local.document_cache:
  706. del local.document_cache[key]
  707. cache().hdel('document_cache', key)
  708. def get_cached_value(doctype, name, fieldname, as_dict=False):
  709. doc = get_cached_doc(doctype, name)
  710. if isinstance(fieldname, str):
  711. if as_dict:
  712. throw('Cannot make dict for single fieldname')
  713. return doc.get(fieldname)
  714. values = [doc.get(f) for f in fieldname]
  715. if as_dict:
  716. return _dict(zip(fieldname, values))
  717. return values
  718. def get_doc(*args, **kwargs):
  719. """Return a `frappe.model.document.Document` object of the given type and name.
  720. :param arg1: DocType name as string **or** document JSON.
  721. :param arg2: [optional] Document name as string.
  722. Examples:
  723. # insert a new document
  724. todo = frappe.get_doc({"doctype":"ToDo", "description": "test"})
  725. todo.insert()
  726. # open an existing document
  727. todo = frappe.get_doc("ToDo", "TD0001")
  728. """
  729. import frappe.model.document
  730. doc = frappe.model.document.get_doc(*args, **kwargs)
  731. # set in cache
  732. if key := can_cache_doc(args):
  733. local.document_cache[key] = doc
  734. cache().hset('document_cache', key, doc.as_dict())
  735. return doc
  736. def get_last_doc(doctype, filters=None, order_by="creation desc"):
  737. """Get last created document of this type."""
  738. d = get_all(
  739. doctype,
  740. filters=filters,
  741. limit_page_length=1,
  742. order_by=order_by,
  743. pluck="name"
  744. )
  745. if d:
  746. return get_doc(doctype, d[0])
  747. else:
  748. raise DoesNotExistError
  749. def get_single(doctype):
  750. """Return a `frappe.model.document.Document` object of the given Single doctype."""
  751. return get_doc(doctype, doctype)
  752. def get_meta(doctype, cached=True):
  753. """Get `frappe.model.meta.Meta` instance of given doctype name."""
  754. import frappe.model.meta
  755. return frappe.model.meta.get_meta(doctype, cached=cached)
  756. def get_meta_module(doctype):
  757. import frappe.modules
  758. return frappe.modules.load_doctype_module(doctype)
  759. def delete_doc(doctype=None, name=None, force=0, ignore_doctypes=None, for_reload=False,
  760. ignore_permissions=False, flags=None, ignore_on_trash=False, ignore_missing=True, delete_permanently=False):
  761. """Delete a document. Calls `frappe.model.delete_doc.delete_doc`.
  762. :param doctype: DocType of document to be delete.
  763. :param name: Name of document to be delete.
  764. :param force: Allow even if document is linked. Warning: This may lead to data integrity errors.
  765. :param ignore_doctypes: Ignore if child table is one of these.
  766. :param for_reload: Call `before_reload` trigger before deleting.
  767. :param ignore_permissions: Ignore user permissions.
  768. :param delete_permanently: Do not create a Deleted Document for the document."""
  769. import frappe.model.delete_doc
  770. frappe.model.delete_doc.delete_doc(doctype, name, force, ignore_doctypes, for_reload,
  771. ignore_permissions, flags, ignore_on_trash, ignore_missing, delete_permanently)
  772. def delete_doc_if_exists(doctype, name, force=0):
  773. """Delete document if exists."""
  774. if db.exists(doctype, name):
  775. delete_doc(doctype, name, force=force)
  776. def reload_doctype(doctype, force=False, reset_permissions=False):
  777. """Reload DocType from model (`[module]/[doctype]/[name]/[name].json`) files."""
  778. reload_doc(scrub(db.get_value("DocType", doctype, "module")), "doctype", scrub(doctype),
  779. force=force, reset_permissions=reset_permissions)
  780. def reload_doc(module, dt=None, dn=None, force=False, reset_permissions=False):
  781. """Reload Document from model (`[module]/[doctype]/[name]/[name].json`) files.
  782. :param module: Module name.
  783. :param dt: DocType name.
  784. :param dn: Document name.
  785. :param force: Reload even if `modified` timestamp matches.
  786. """
  787. import frappe.modules
  788. return frappe.modules.reload_doc(module, dt, dn, force=force, reset_permissions=reset_permissions)
  789. @whitelist()
  790. def rename_doc(*args, **kwargs):
  791. """
  792. Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type "Link"
  793. Calls `frappe.model.rename_doc.rename_doc`
  794. """
  795. kwargs.pop('ignore_permissions', None)
  796. kwargs.pop('cmd', None)
  797. from frappe.model.rename_doc import rename_doc
  798. return rename_doc(*args, **kwargs)
  799. def get_module(modulename):
  800. """Returns a module object for given Python module name using `importlib.import_module`."""
  801. return importlib.import_module(modulename)
  802. def scrub(txt):
  803. """Returns sluggified string. e.g. `Sales Order` becomes `sales_order`."""
  804. return cstr(txt).replace(' ', '_').replace('-', '_').lower()
  805. def unscrub(txt):
  806. """Returns titlified string. e.g. `sales_order` becomes `Sales Order`."""
  807. return txt.replace('_', ' ').replace('-', ' ').title()
  808. def get_module_path(module, *joins):
  809. """Get the path of the given module name.
  810. :param module: Module name.
  811. :param *joins: Join additional path elements using `os.path.join`."""
  812. module = scrub(module)
  813. return get_pymodule_path(local.module_app[module] + "." + module, *joins)
  814. def get_app_path(app_name, *joins):
  815. """Return path of given app.
  816. :param app: App name.
  817. :param *joins: Join additional path elements using `os.path.join`."""
  818. return get_pymodule_path(app_name, *joins)
  819. def get_site_path(*joins):
  820. """Return path of current site.
  821. :param *joins: Join additional path elements using `os.path.join`."""
  822. return os.path.join(local.site_path, *joins)
  823. def get_pymodule_path(modulename, *joins):
  824. """Return path of given Python module name.
  825. :param modulename: Python module name.
  826. :param *joins: Join additional path elements using `os.path.join`."""
  827. if not "public" in joins:
  828. joins = [scrub(part) for part in joins]
  829. return os.path.join(os.path.dirname(get_module(scrub(modulename)).__file__ or ''), *joins)
  830. def get_module_list(app_name):
  831. """Get list of modules for given all via `app/modules.txt`."""
  832. return get_file_items(os.path.join(os.path.dirname(get_module(app_name).__file__), "modules.txt"))
  833. def get_all_apps(with_internal_apps=True, sites_path=None):
  834. """Get list of all apps via `sites/apps.txt`."""
  835. if not sites_path:
  836. sites_path = local.sites_path
  837. apps = get_file_items(os.path.join(sites_path, "apps.txt"), raise_not_found=True)
  838. if with_internal_apps:
  839. for app in get_file_items(os.path.join(local.site_path, "apps.txt")):
  840. if app not in apps:
  841. apps.append(app)
  842. if "frappe" in apps:
  843. apps.remove("frappe")
  844. apps.insert(0, 'frappe')
  845. return apps
  846. def get_installed_apps(sort=False, frappe_last=False):
  847. """Get list of installed apps in current site."""
  848. if getattr(flags, "in_install_db", True):
  849. return []
  850. if not db:
  851. connect()
  852. if not local.all_apps:
  853. local.all_apps = cache().get_value('all_apps', get_all_apps)
  854. installed = json.loads(db.get_global("installed_apps") or "[]")
  855. if sort:
  856. installed = [app for app in local.all_apps if app in installed]
  857. if frappe_last:
  858. if 'frappe' in installed:
  859. installed.remove('frappe')
  860. installed.append('frappe')
  861. return installed
  862. def get_doc_hooks():
  863. '''Returns hooked methods for given doc. It will expand the dict tuple if required.'''
  864. if not hasattr(local, 'doc_events_hooks'):
  865. hooks = get_hooks('doc_events', {})
  866. out = {}
  867. for key, value in hooks.items():
  868. if isinstance(key, tuple):
  869. for doctype in key:
  870. append_hook(out, doctype, value)
  871. else:
  872. append_hook(out, key, value)
  873. local.doc_events_hooks = out
  874. return local.doc_events_hooks
  875. def get_hooks(hook=None, default=None, app_name=None):
  876. """Get hooks via `app/hooks.py`
  877. :param hook: Name of the hook. Will gather all hooks for this name and return as a list.
  878. :param default: Default if no hook found.
  879. :param app_name: Filter by app."""
  880. def load_app_hooks(app_name=None):
  881. hooks = {}
  882. for app in [app_name] if app_name else get_installed_apps(sort=True):
  883. app = "frappe" if app=="webnotes" else app
  884. try:
  885. app_hooks = get_module(app + ".hooks")
  886. except ImportError:
  887. if local.flags.in_install_app:
  888. # if app is not installed while restoring
  889. # ignore it
  890. pass
  891. print('Could not find app "{0}"'.format(app_name))
  892. if not request:
  893. sys.exit(1)
  894. raise
  895. for key in dir(app_hooks):
  896. if not key.startswith("_"):
  897. append_hook(hooks, key, getattr(app_hooks, key))
  898. return hooks
  899. no_cache = conf.developer_mode or False
  900. if app_name:
  901. hooks = _dict(load_app_hooks(app_name))
  902. else:
  903. if no_cache:
  904. hooks = _dict(load_app_hooks())
  905. else:
  906. hooks = _dict(cache().get_value("app_hooks", load_app_hooks))
  907. if hook:
  908. return hooks.get(hook) or (default if default is not None else [])
  909. else:
  910. return hooks
  911. def append_hook(target, key, value):
  912. '''appends a hook to the the target dict.
  913. If the hook key, exists, it will make it a key.
  914. If the hook value is a dict, like doc_events, it will
  915. listify the values against the key.
  916. '''
  917. if isinstance(value, dict):
  918. # dict? make a list of values against each key
  919. target.setdefault(key, {})
  920. for inkey in value:
  921. append_hook(target[key], inkey, value[inkey])
  922. else:
  923. # make a list
  924. target.setdefault(key, [])
  925. if not isinstance(value, list):
  926. value = [value]
  927. target[key].extend(value)
  928. def setup_module_map():
  929. """Rebuild map of all modules (internal)."""
  930. _cache = cache()
  931. if conf.db_name:
  932. local.app_modules = _cache.get_value("app_modules")
  933. local.module_app = _cache.get_value("module_app")
  934. if not (local.app_modules and local.module_app):
  935. local.module_app, local.app_modules = {}, {}
  936. for app in get_all_apps(with_internal_apps=True):
  937. local.app_modules.setdefault(app, [])
  938. for module in get_module_list(app):
  939. module = scrub(module)
  940. local.module_app[module] = app
  941. local.app_modules[app].append(module)
  942. if conf.db_name:
  943. _cache.set_value("app_modules", local.app_modules)
  944. _cache.set_value("module_app", local.module_app)
  945. def get_file_items(path, raise_not_found=False, ignore_empty_lines=True):
  946. """Returns items from text file as a list. Ignores empty lines."""
  947. import frappe.utils
  948. content = read_file(path, raise_not_found=raise_not_found)
  949. if content:
  950. content = frappe.utils.strip(content)
  951. return [
  952. p.strip() for p in content.splitlines()
  953. if (not ignore_empty_lines) or (p.strip() and not p.startswith("#"))
  954. ]
  955. else:
  956. return []
  957. def get_file_json(path):
  958. """Read a file and return parsed JSON object."""
  959. with open(path, 'r') as f:
  960. return json.load(f)
  961. def read_file(path, raise_not_found=False):
  962. """Open a file and return its content as Unicode."""
  963. if isinstance(path, str):
  964. path = path.encode("utf-8")
  965. if os.path.exists(path):
  966. with open(path, "r") as f:
  967. return as_unicode(f.read())
  968. elif raise_not_found:
  969. raise IOError("{} Not Found".format(path))
  970. else:
  971. return None
  972. def get_attr(method_string):
  973. """Get python method object from its name."""
  974. app_name = method_string.split(".")[0]
  975. if not local.flags.in_uninstall and not local.flags.in_install and app_name not in get_installed_apps():
  976. throw(_("App {0} is not installed").format(app_name), AppNotInstalledError)
  977. modulename = '.'.join(method_string.split('.')[:-1])
  978. methodname = method_string.split('.')[-1]
  979. return getattr(get_module(modulename), methodname)
  980. def call(fn, *args, **kwargs):
  981. """Call a function and match arguments."""
  982. if isinstance(fn, str):
  983. fn = get_attr(fn)
  984. newargs = get_newargs(fn, kwargs)
  985. return fn(*args, **newargs)
  986. def get_newargs(fn, kwargs):
  987. if hasattr(fn, 'fnargs'):
  988. fnargs = fn.fnargs
  989. else:
  990. fnargs = inspect.getfullargspec(fn).args
  991. fnargs.extend(inspect.getfullargspec(fn).kwonlyargs)
  992. varkw = inspect.getfullargspec(fn).varkw
  993. newargs = {}
  994. for a in kwargs:
  995. if (a in fnargs) or varkw:
  996. newargs[a] = kwargs.get(a)
  997. newargs.pop("ignore_permissions", None)
  998. newargs.pop("flags", None)
  999. return newargs
  1000. def make_property_setter(args, ignore_validate=False, validate_fields_for_doctype=True):
  1001. """Create a new **Property Setter** (for overriding DocType and DocField properties).
  1002. If doctype is not specified, it will create a property setter for all fields with the
  1003. given fieldname"""
  1004. args = _dict(args)
  1005. if not args.doctype_or_field:
  1006. args.doctype_or_field = 'DocField'
  1007. if not args.property_type:
  1008. args.property_type = db.get_value('DocField',
  1009. {'parent': 'DocField', 'fieldname': args.property}, 'fieldtype') or 'Data'
  1010. if not args.doctype:
  1011. doctype_list = db.sql_list('select distinct parent from tabDocField where fieldname=%s', args.fieldname)
  1012. else:
  1013. doctype_list = [args.doctype]
  1014. for doctype in doctype_list:
  1015. if not args.property_type:
  1016. args.property_type = db.get_value('DocField',
  1017. {'parent': doctype, 'fieldname': args.fieldname}, 'fieldtype') or 'Data'
  1018. ps = get_doc({
  1019. 'doctype': "Property Setter",
  1020. 'doctype_or_field': args.doctype_or_field,
  1021. 'doc_type': doctype,
  1022. 'field_name': args.fieldname,
  1023. 'row_name': args.row_name,
  1024. 'property': args.property,
  1025. 'value': args.value,
  1026. 'property_type': args.property_type or "Data",
  1027. '__islocal': 1
  1028. })
  1029. ps.flags.ignore_validate = ignore_validate
  1030. ps.flags.validate_fields_for_doctype = validate_fields_for_doctype
  1031. ps.validate_fieldtype_change()
  1032. ps.insert()
  1033. def import_doc(path):
  1034. """Import a file using Data Import."""
  1035. from frappe.core.doctype.data_import.data_import import import_doc
  1036. import_doc(path)
  1037. def copy_doc(doc, ignore_no_copy=True):
  1038. """ No_copy fields also get copied."""
  1039. import copy
  1040. def remove_no_copy_fields(d):
  1041. for df in d.meta.get("fields", {"no_copy": 1}):
  1042. if hasattr(d, df.fieldname):
  1043. d.set(df.fieldname, None)
  1044. fields_to_clear = ['name', 'owner', 'creation', 'modified', 'modified_by']
  1045. if not local.flags.in_test:
  1046. fields_to_clear.append("docstatus")
  1047. if not isinstance(doc, dict):
  1048. d = doc.as_dict()
  1049. else:
  1050. d = doc
  1051. newdoc = get_doc(copy.deepcopy(d))
  1052. newdoc.set("__islocal", 1)
  1053. for fieldname in (fields_to_clear + ['amended_from', 'amendment_date']):
  1054. newdoc.set(fieldname, None)
  1055. if not ignore_no_copy:
  1056. remove_no_copy_fields(newdoc)
  1057. for i, d in enumerate(newdoc.get_all_children()):
  1058. d.set("__islocal", 1)
  1059. for fieldname in fields_to_clear:
  1060. d.set(fieldname, None)
  1061. if not ignore_no_copy:
  1062. remove_no_copy_fields(d)
  1063. return newdoc
  1064. def compare(val1, condition, val2):
  1065. """Compare two values using `frappe.utils.compare`
  1066. `condition` could be:
  1067. - "^"
  1068. - "in"
  1069. - "not in"
  1070. - "="
  1071. - "!="
  1072. - ">"
  1073. - "<"
  1074. - ">="
  1075. - "<="
  1076. - "not None"
  1077. - "None"
  1078. """
  1079. import frappe.utils
  1080. return frappe.utils.compare(val1, condition, val2)
  1081. def respond_as_web_page(title, html, success=None, http_status_code=None, context=None,
  1082. indicator_color=None, primary_action='/', primary_label = None, fullpage=False,
  1083. width=None, template='message'):
  1084. """Send response as a web page with a message rather than JSON. Used to show permission errors etc.
  1085. :param title: Page title and heading.
  1086. :param message: Message to be shown.
  1087. :param success: Alert message.
  1088. :param http_status_code: HTTP status code
  1089. :param context: web template context
  1090. :param indicator_color: color of indicator in title
  1091. :param primary_action: route on primary button (default is `/`)
  1092. :param primary_label: label on primary button (default is "Home")
  1093. :param fullpage: hide header / footer
  1094. :param width: Width of message in pixels
  1095. :param template: Optionally pass view template
  1096. """
  1097. local.message_title = title
  1098. local.message = html
  1099. local.response['type'] = 'page'
  1100. local.response['route'] = template
  1101. local.no_cache = 1
  1102. if http_status_code:
  1103. local.response['http_status_code'] = http_status_code
  1104. if not context:
  1105. context = {}
  1106. if not indicator_color:
  1107. if success:
  1108. indicator_color = 'green'
  1109. elif http_status_code and http_status_code > 300:
  1110. indicator_color = 'red'
  1111. else:
  1112. indicator_color = 'blue'
  1113. context['indicator_color'] = indicator_color
  1114. context['primary_label'] = primary_label
  1115. context['primary_action'] = primary_action
  1116. context['error_code'] = http_status_code
  1117. context['fullpage'] = fullpage
  1118. if width:
  1119. context['card_width'] = width
  1120. local.response['context'] = context
  1121. def redirect_to_message(title, html, http_status_code=None, context=None, indicator_color=None):
  1122. """Redirects to /message?id=random
  1123. Similar to respond_as_web_page, but used to 'redirect' and show message pages like success, failure, etc. with a detailed message
  1124. :param title: Page title and heading.
  1125. :param message: Message to be shown.
  1126. :param http_status_code: HTTP status code.
  1127. Example Usage:
  1128. frappe.redirect_to_message(_('Thank you'), "<div><p>You will receive an email at test@example.com</p></div>")
  1129. """
  1130. message_id = generate_hash(length=8)
  1131. message = {
  1132. 'context': context or {},
  1133. 'http_status_code': http_status_code or 200
  1134. }
  1135. message['context'].update({
  1136. 'header': title,
  1137. 'title': title,
  1138. 'message': html
  1139. })
  1140. if indicator_color:
  1141. message['context'].update({
  1142. "indicator_color": indicator_color
  1143. })
  1144. cache().set_value("message_id:{0}".format(message_id), message, expires_in_sec=60)
  1145. location = '/message?id={0}'.format(message_id)
  1146. if not getattr(local, 'is_ajax', False):
  1147. local.response["type"] = "redirect"
  1148. local.response["location"] = location
  1149. else:
  1150. return location
  1151. def build_match_conditions(doctype, as_condition=True):
  1152. """Return match (User permissions) for given doctype as list or SQL."""
  1153. import frappe.desk.reportview
  1154. return frappe.desk.reportview.build_match_conditions(doctype, as_condition=as_condition)
  1155. def get_list(doctype, *args, **kwargs):
  1156. """List database query via `frappe.model.db_query`. Will also check for permissions.
  1157. :param doctype: DocType on which query is to be made.
  1158. :param fields: List of fields or `*`.
  1159. :param filters: List of filters (see example).
  1160. :param order_by: Order By e.g. `modified desc`.
  1161. :param limit_page_start: Start results at record #. Default 0.
  1162. :param limit_page_length: No of records in the page. Default 20.
  1163. Example usage:
  1164. # simple dict filter
  1165. frappe.get_list("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"})
  1166. # filter as a list of lists
  1167. frappe.get_list("ToDo", fields="*", filters = [["modified", ">", "2014-01-01"]])
  1168. # filter as a list of dicts
  1169. frappe.get_list("ToDo", fields="*", filters = {"description": ("like", "test%")})
  1170. """
  1171. import frappe.model.db_query
  1172. return frappe.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs)
  1173. def get_all(doctype, *args, **kwargs):
  1174. """List database query via `frappe.model.db_query`. Will **not** check for permissions.
  1175. Parameters are same as `frappe.get_list`
  1176. :param doctype: DocType on which query is to be made.
  1177. :param fields: List of fields or `*`. Default is: `["name"]`.
  1178. :param filters: List of filters (see example).
  1179. :param order_by: Order By e.g. `modified desc`.
  1180. :param limit_start: Start results at record #. Default 0.
  1181. :param limit_page_length: No of records in the page. Default 20.
  1182. Example usage:
  1183. # simple dict filter
  1184. frappe.get_all("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"})
  1185. # filter as a list of lists
  1186. frappe.get_all("ToDo", fields=["*"], filters = [["modified", ">", "2014-01-01"]])
  1187. # filter as a list of dicts
  1188. frappe.get_all("ToDo", fields=["*"], filters = {"description": ("like", "test%")})
  1189. """
  1190. kwargs["ignore_permissions"] = True
  1191. if not "limit_page_length" in kwargs:
  1192. kwargs["limit_page_length"] = 0
  1193. return get_list(doctype, *args, **kwargs)
  1194. def get_value(*args, **kwargs):
  1195. """Returns a document property or list of properties.
  1196. Alias for `frappe.db.get_value`
  1197. :param doctype: DocType name.
  1198. :param filters: Filters like `{"x":"y"}` or name of the document. `None` if Single DocType.
  1199. :param fieldname: Column name.
  1200. :param ignore: Don't raise exception if table, column is missing.
  1201. :param as_dict: Return values as dict.
  1202. :param debug: Print query in error log.
  1203. """
  1204. return db.get_value(*args, **kwargs)
  1205. def as_json(obj, indent=1):
  1206. from frappe.utils.response import json_handler
  1207. try:
  1208. return json.dumps(obj, indent=indent, sort_keys=True, default=json_handler, separators=(',', ': '))
  1209. except TypeError:
  1210. return json.dumps(obj, indent=indent, default=json_handler, separators=(',', ': '))
  1211. def are_emails_muted():
  1212. from frappe.utils import cint
  1213. return flags.mute_emails or cint(conf.get("mute_emails") or 0) or False
  1214. def get_test_records(doctype):
  1215. """Returns list of objects from `test_records.json` in the given doctype's folder."""
  1216. from frappe.modules import get_doctype_module, get_module_path
  1217. path = os.path.join(get_module_path(get_doctype_module(doctype)), "doctype", scrub(doctype), "test_records.json")
  1218. if os.path.exists(path):
  1219. with open(path, "r") as f:
  1220. return json.loads(f.read())
  1221. else:
  1222. return []
  1223. def format_value(*args, **kwargs):
  1224. """Format value with given field properties.
  1225. :param value: Value to be formatted.
  1226. :param df: (Optional) DocField object with properties `fieldtype`, `options` etc."""
  1227. import frappe.utils.formatters
  1228. return frappe.utils.formatters.format_value(*args, **kwargs)
  1229. def format(*args, **kwargs):
  1230. """Format value with given field properties.
  1231. :param value: Value to be formatted.
  1232. :param df: (Optional) DocField object with properties `fieldtype`, `options` etc."""
  1233. import frappe.utils.formatters
  1234. return frappe.utils.formatters.format_value(*args, **kwargs)
  1235. def get_print(doctype=None, name=None, print_format=None, style=None, html=None,
  1236. as_pdf=False, doc=None, output=None, no_letterhead=0, password=None, pdf_options=None):
  1237. """Get Print Format for given document.
  1238. :param doctype: DocType of document.
  1239. :param name: Name of document.
  1240. :param print_format: Print Format name. Default 'Standard',
  1241. :param style: Print Format style.
  1242. :param as_pdf: Return as PDF. Default False.
  1243. :param password: Password to encrypt the pdf with. Default None"""
  1244. from frappe.website.serve import get_response_content
  1245. from frappe.utils.pdf import get_pdf
  1246. local.form_dict.doctype = doctype
  1247. local.form_dict.name = name
  1248. local.form_dict.format = print_format
  1249. local.form_dict.style = style
  1250. local.form_dict.doc = doc
  1251. local.form_dict.no_letterhead = no_letterhead
  1252. pdf_options = pdf_options or {}
  1253. if password:
  1254. pdf_options['password'] = password
  1255. if not html:
  1256. html = get_response_content("printview")
  1257. if as_pdf:
  1258. return get_pdf(html, options=pdf_options, output=output)
  1259. else:
  1260. return html
  1261. def attach_print(doctype, name, file_name=None, print_format=None,
  1262. style=None, html=None, doc=None, lang=None, print_letterhead=True, password=None):
  1263. from frappe.utils import scrub_urls
  1264. if not file_name: file_name = name
  1265. file_name = file_name.replace(' ','').replace('/','-')
  1266. print_settings = db.get_singles_dict("Print Settings")
  1267. _lang = local.lang
  1268. #set lang as specified in print format attachment
  1269. if lang: local.lang = lang
  1270. local.flags.ignore_print_permissions = True
  1271. no_letterhead = not print_letterhead
  1272. kwargs = dict(
  1273. print_format=print_format,
  1274. style=style,
  1275. html=html,
  1276. doc=doc,
  1277. no_letterhead=no_letterhead,
  1278. password=password
  1279. )
  1280. content = ''
  1281. if int(print_settings.send_print_as_pdf or 0):
  1282. ext = ".pdf"
  1283. kwargs["as_pdf"] = True
  1284. content = get_print(doctype, name, **kwargs)
  1285. else:
  1286. ext = ".html"
  1287. content = scrub_urls(get_print(doctype, name, **kwargs)).encode('utf-8')
  1288. out = {
  1289. "fname": file_name + ext,
  1290. "fcontent": content
  1291. }
  1292. local.flags.ignore_print_permissions = False
  1293. #reset lang to original local lang
  1294. local.lang = _lang
  1295. return out
  1296. def publish_progress(*args, **kwargs):
  1297. """Show the user progress for a long request
  1298. :param percent: Percent progress
  1299. :param title: Title
  1300. :param doctype: Optional, for document type
  1301. :param docname: Optional, for document name
  1302. :param description: Optional description
  1303. """
  1304. import frappe.realtime
  1305. return frappe.realtime.publish_progress(*args, **kwargs)
  1306. def publish_realtime(*args, **kwargs):
  1307. """Publish real-time updates
  1308. :param event: Event name, like `task_progress` etc.
  1309. :param message: JSON message object. For async must contain `task_id`
  1310. :param room: Room in which to publish update (default entire site)
  1311. :param user: Transmit to user
  1312. :param doctype: Transmit to doctype, docname
  1313. :param docname: Transmit to doctype, docname
  1314. :param after_commit: (default False) will emit after current transaction is committed
  1315. """
  1316. import frappe.realtime
  1317. return frappe.realtime.publish_realtime(*args, **kwargs)
  1318. def local_cache(namespace, key, generator, regenerate_if_none=False):
  1319. """A key value store for caching within a request
  1320. :param namespace: frappe.local.cache[namespace]
  1321. :param key: frappe.local.cache[namespace][key] used to retrieve value
  1322. :param generator: method to generate a value if not found in store
  1323. """
  1324. if namespace not in local.cache:
  1325. local.cache[namespace] = {}
  1326. if key not in local.cache[namespace]:
  1327. local.cache[namespace][key] = generator()
  1328. elif local.cache[namespace][key] is None and regenerate_if_none:
  1329. # if key exists but the previous result was None
  1330. local.cache[namespace][key] = generator()
  1331. return local.cache[namespace][key]
  1332. def enqueue(*args, **kwargs):
  1333. '''
  1334. Enqueue method to be executed using a background worker
  1335. :param method: method string or method object
  1336. :param queue: (optional) should be either long, default or short
  1337. :param timeout: (optional) should be set according to the functions
  1338. :param event: this is passed to enable clearing of jobs from queues
  1339. :param is_async: (optional) if is_async=False, the method is executed immediately, else via a worker
  1340. :param job_name: (optional) can be used to name an enqueue call, which can be used to prevent duplicate calls
  1341. :param kwargs: keyword arguments to be passed to the method
  1342. '''
  1343. import frappe.utils.background_jobs
  1344. return frappe.utils.background_jobs.enqueue(*args, **kwargs)
  1345. def task(**task_kwargs):
  1346. def decorator_task(f):
  1347. f.enqueue = lambda **fun_kwargs: enqueue(f, **task_kwargs, **fun_kwargs)
  1348. return f
  1349. return decorator_task
  1350. def enqueue_doc(*args, **kwargs):
  1351. '''
  1352. Enqueue method to be executed using a background worker
  1353. :param doctype: DocType of the document on which you want to run the event
  1354. :param name: Name of the document on which you want to run the event
  1355. :param method: method string or method object
  1356. :param queue: (optional) should be either long, default or short
  1357. :param timeout: (optional) should be set according to the functions
  1358. :param kwargs: keyword arguments to be passed to the method
  1359. '''
  1360. import frappe.utils.background_jobs
  1361. return frappe.utils.background_jobs.enqueue_doc(*args, **kwargs)
  1362. def get_doctype_app(doctype):
  1363. def _get_doctype_app():
  1364. doctype_module = local.db.get_value("DocType", doctype, "module")
  1365. return local.module_app[scrub(doctype_module)]
  1366. return local_cache("doctype_app", doctype, generator=_get_doctype_app)
  1367. loggers = {}
  1368. log_level = None
  1369. def logger(module=None, with_more_info=False, allow_site=True, filter=None, max_size=100_000, file_count=20):
  1370. '''Returns a python logger that uses StreamHandler'''
  1371. from frappe.utils.logger import get_logger
  1372. return get_logger(module=module, with_more_info=with_more_info, allow_site=allow_site, filter=filter, max_size=max_size, file_count=file_count)
  1373. def log_error(message=None, title=_("Error")):
  1374. '''Log error to Error Log'''
  1375. # AI ALERT:
  1376. # the title and message may be swapped
  1377. # the better API for this is log_error(title, message), and used in many cases this way
  1378. # this hack tries to be smart about whats a title (single line ;-)) and fixes it
  1379. if message:
  1380. if '\n' in title:
  1381. error, title = title, message
  1382. else:
  1383. error = message
  1384. else:
  1385. error = get_traceback()
  1386. return get_doc(dict(doctype='Error Log', error=as_unicode(error),
  1387. method=title)).insert(ignore_permissions=True)
  1388. def get_desk_link(doctype, name):
  1389. html = '<a href="/app/Form/{doctype}/{name}" style="font-weight: bold;">{doctype_local} {name}</a>'
  1390. return html.format(
  1391. doctype=doctype,
  1392. name=name,
  1393. doctype_local=_(doctype)
  1394. )
  1395. def bold(text):
  1396. return '<strong>{0}</strong>'.format(text)
  1397. def safe_eval(code, eval_globals=None, eval_locals=None):
  1398. '''A safer `eval`'''
  1399. whitelisted_globals = {
  1400. "int": int,
  1401. "float": float,
  1402. "long": int,
  1403. "round": round
  1404. }
  1405. UNSAFE_ATTRIBUTES = {
  1406. # Generator Attributes
  1407. "gi_frame", "gi_code",
  1408. # Coroutine Attributes
  1409. "cr_frame", "cr_code", "cr_origin",
  1410. # Async Generator Attributes
  1411. "ag_code", "ag_frame",
  1412. # Traceback Attributes
  1413. "tb_frame", "tb_next",
  1414. # Format Attributes
  1415. "format", "format_map",
  1416. }
  1417. for attribute in UNSAFE_ATTRIBUTES:
  1418. if attribute in code:
  1419. throw('Illegal rule {0}. Cannot use "{1}"'.format(bold(code), attribute))
  1420. if '__' in code:
  1421. throw('Illegal rule {0}. Cannot use "__"'.format(bold(code)))
  1422. if not eval_globals:
  1423. eval_globals = {}
  1424. eval_globals['__builtins__'] = {}
  1425. eval_globals.update(whitelisted_globals)
  1426. return eval(code, eval_globals, eval_locals)
  1427. def get_system_settings(key):
  1428. if key not in local.system_settings:
  1429. local.system_settings.update({key: db.get_single_value('System Settings', key)})
  1430. return local.system_settings.get(key)
  1431. def get_active_domains():
  1432. from frappe.core.doctype.domain_settings.domain_settings import get_active_domains
  1433. return get_active_domains()
  1434. def get_version(doctype, name, limit=None, head=False, raise_err=True):
  1435. '''
  1436. Returns a list of version information of a given DocType.
  1437. Note: Applicable only if DocType has changes tracked.
  1438. Example
  1439. >>> frappe.get_version('User', 'foobar@gmail.com')
  1440. >>>
  1441. [
  1442. {
  1443. "version": [version.data], # Refer Version DocType get_diff method and data attribute
  1444. "user": "admin@gmail.com", # User that created this version
  1445. "creation": <datetime.datetime> # Creation timestamp of that object.
  1446. }
  1447. ]
  1448. '''
  1449. meta = get_meta(doctype)
  1450. if meta.track_changes:
  1451. names = db.get_all('Version', filters={
  1452. 'ref_doctype': doctype,
  1453. 'docname': name,
  1454. 'order_by': 'creation' if head else None,
  1455. 'limit': limit
  1456. }, as_list=1)
  1457. from frappe.utils import squashify, dictify, safe_json_loads
  1458. versions = []
  1459. for name in names:
  1460. name = squashify(name)
  1461. doc = get_doc('Version', name)
  1462. data = doc.data
  1463. data = safe_json_loads(data)
  1464. data = dictify(dict(
  1465. version=data,
  1466. user=doc.owner,
  1467. creation=doc.creation
  1468. ))
  1469. versions.append(data)
  1470. return versions
  1471. else:
  1472. if raise_err:
  1473. raise ValueError(_('{0} has no versions tracked.').format(doctype))
  1474. @whitelist(allow_guest=True)
  1475. def ping():
  1476. return "pong"
  1477. def safe_encode(param, encoding='utf-8'):
  1478. try:
  1479. param = param.encode(encoding)
  1480. except Exception:
  1481. pass
  1482. return param
  1483. def safe_decode(param, encoding='utf-8'):
  1484. try:
  1485. param = param.decode(encoding)
  1486. except Exception:
  1487. pass
  1488. return param
  1489. def parse_json(val):
  1490. from frappe.utils import parse_json
  1491. return parse_json(val)
  1492. def mock(type, size=1, locale='en'):
  1493. import faker
  1494. results = []
  1495. fake = faker.Faker(locale)
  1496. if type not in dir(fake):
  1497. raise ValueError('Not a valid mock type.')
  1498. else:
  1499. for i in range(size):
  1500. data = getattr(fake, type)()
  1501. results.append(data)
  1502. from frappe.utils import squashify
  1503. return squashify(results)
  1504. def validate_and_sanitize_search_inputs(fn):
  1505. from frappe.desk.search import validate_and_sanitize_search_inputs as func
  1506. return func(fn)