25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1412 lines
44 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. """
  4. globals attached to frappe module
  5. + some utility functions that should probably be moved
  6. """
  7. from __future__ import unicode_literals, print_function
  8. from six import iteritems, binary_type, text_type, string_types
  9. from werkzeug.local import Local, release_local
  10. import os, sys, importlib, inspect, json
  11. # public
  12. from .exceptions import *
  13. from .utils.jinja import get_jenv, get_template, render_template, get_email_from_template
  14. __version__ = '10.0.25'
  15. __title__ = "Frappe Framework"
  16. local = Local()
  17. class _dict(dict):
  18. """dict like object that exposes keys as attributes"""
  19. def __getattr__(self, key):
  20. ret = self.get(key)
  21. if not ret and key.startswith("__"):
  22. raise AttributeError()
  23. return ret
  24. def __setattr__(self, key, value):
  25. self[key] = value
  26. def __getstate__(self):
  27. return self
  28. def __setstate__(self, d):
  29. self.update(d)
  30. def update(self, d):
  31. """update and return self -- the missing dict feature in python"""
  32. super(_dict, self).update(d)
  33. return self
  34. def copy(self):
  35. return _dict(dict(self).copy())
  36. def _(msg, lang=None):
  37. """Returns translated string in current lang, if exists."""
  38. from frappe.translate import get_full_dict
  39. if not hasattr(local, 'lang'):
  40. local.lang = lang or 'en'
  41. if not lang:
  42. lang = local.lang
  43. # msg should always be unicode
  44. msg = as_unicode(msg).strip()
  45. # return lang_full_dict according to lang passed parameter
  46. return get_full_dict(lang).get(msg) or msg
  47. def as_unicode(text, encoding='utf-8'):
  48. '''Convert to unicode if required'''
  49. if isinstance(text, text_type):
  50. return text
  51. elif text==None:
  52. return ''
  53. elif isinstance(text, binary_type):
  54. return text_type(text, encoding)
  55. else:
  56. return text_type(text)
  57. def get_lang_dict(fortype, name=None):
  58. """Returns the translated language dict for the given type and name.
  59. :param fortype: must be one of `doctype`, `page`, `report`, `include`, `jsfile`, `boot`
  60. :param name: name of the document for which assets are to be returned."""
  61. from frappe.translate import get_dict
  62. return get_dict(fortype, name)
  63. def set_user_lang(user, user_language=None):
  64. """Guess and set user language for the session. `frappe.local.lang`"""
  65. from frappe.translate import get_user_lang
  66. local.lang = get_user_lang(user)
  67. # local-globals
  68. db = local("db")
  69. conf = local("conf")
  70. form = form_dict = local("form_dict")
  71. request = local("request")
  72. response = local("response")
  73. session = local("session")
  74. user = local("user")
  75. flags = local("flags")
  76. error_log = local("error_log")
  77. debug_log = local("debug_log")
  78. message_log = local("message_log")
  79. lang = local("lang")
  80. def init(site, sites_path=None, new_site=False):
  81. """Initialize frappe for the current site. Reset thread locals `frappe.local`"""
  82. if getattr(local, "initialised", None):
  83. return
  84. if not sites_path:
  85. sites_path = '.'
  86. local.error_log = []
  87. local.message_log = []
  88. local.debug_log = []
  89. local.realtime_log = []
  90. local.flags = _dict({
  91. "ran_schedulers": [],
  92. "currently_saving": [],
  93. "redirect_location": "",
  94. "in_install_db": False,
  95. "in_install_app": False,
  96. "in_import": False,
  97. "in_test": False,
  98. "mute_messages": False,
  99. "ignore_links": False,
  100. "mute_emails": False,
  101. "has_dataurl": False,
  102. "new_site": new_site
  103. })
  104. local.rollback_observers = []
  105. local.test_objects = {}
  106. local.site = site
  107. local.sites_path = sites_path
  108. local.site_path = os.path.join(sites_path, site)
  109. local.request_ip = None
  110. local.response = _dict({"docs":[]})
  111. local.task_id = None
  112. local.conf = _dict(get_site_config())
  113. local.lang = local.conf.lang or "en"
  114. local.lang_full_dict = None
  115. local.module_app = None
  116. local.app_modules = None
  117. local.system_settings = _dict()
  118. local.user = None
  119. local.user_perms = None
  120. local.session = None
  121. local.role_permissions = {}
  122. local.valid_columns = {}
  123. local.new_doc_templates = {}
  124. local.link_count = {}
  125. local.jenv = None
  126. local.jloader =None
  127. local.cache = {}
  128. local.meta_cache = {}
  129. local.form_dict = _dict()
  130. local.session = _dict()
  131. setup_module_map()
  132. local.initialised = True
  133. def connect(site=None, db_name=None):
  134. """Connect to site database instance.
  135. :param site: If site is given, calls `frappe.init`.
  136. :param db_name: Optional. Will use from `site_config.json`."""
  137. from frappe.database import Database
  138. if site:
  139. init(site)
  140. local.db = Database(user=db_name or local.conf.db_name)
  141. set_user("Administrator")
  142. def get_site_config(sites_path=None, site_path=None):
  143. """Returns `site_config.json` combined with `sites/common_site_config.json`.
  144. `site_config` is a set of site wide settings like database name, password, email etc."""
  145. config = {}
  146. sites_path = sites_path or getattr(local, "sites_path", None)
  147. site_path = site_path or getattr(local, "site_path", None)
  148. if sites_path:
  149. common_site_config = os.path.join(sites_path, "common_site_config.json")
  150. if os.path.exists(common_site_config):
  151. config.update(get_file_json(common_site_config))
  152. if site_path:
  153. site_config = os.path.join(site_path, "site_config.json")
  154. if os.path.exists(site_config):
  155. config.update(get_file_json(site_config))
  156. elif local.site and not local.flags.new_site:
  157. print("{0} does not exist".format(local.site))
  158. sys.exit(1)
  159. #raise IncorrectSitePath, "{0} does not exist".format(site_config)
  160. return _dict(config)
  161. def get_conf(site=None):
  162. if hasattr(local, 'conf'):
  163. return local.conf
  164. else:
  165. # if no site, get from common_site_config.json
  166. with init_site(site):
  167. return local.conf
  168. class init_site:
  169. def __init__(self, site=None):
  170. '''If site==None, initialize it for empty site ('') to load common_site_config.json'''
  171. self.site = site or ''
  172. def __enter__(self):
  173. init(self.site)
  174. return local
  175. def __exit__(self, type, value, traceback):
  176. destroy()
  177. def destroy():
  178. """Closes connection and releases werkzeug local."""
  179. if db:
  180. db.close()
  181. release_local(local)
  182. # memcache
  183. redis_server = None
  184. def cache():
  185. """Returns memcache connection."""
  186. global redis_server
  187. if not redis_server:
  188. from frappe.utils.redis_wrapper import RedisWrapper
  189. redis_server = RedisWrapper.from_url(conf.get('redis_cache')
  190. or "redis://localhost:11311")
  191. return redis_server
  192. def get_traceback():
  193. """Returns error traceback."""
  194. from frappe.utils import get_traceback
  195. return get_traceback()
  196. def errprint(msg):
  197. """Log error. This is sent back as `exc` in response.
  198. :param msg: Message."""
  199. msg = as_unicode(msg)
  200. if not request or (not "cmd" in local.form_dict) or conf.developer_mode:
  201. print(msg.encode('utf-8'))
  202. error_log.append(msg)
  203. def log(msg):
  204. """Add to `debug_log`.
  205. :param msg: Message."""
  206. if not request:
  207. if conf.get("logging") or False:
  208. print(repr(msg))
  209. debug_log.append(as_unicode(msg))
  210. def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False):
  211. """Print a message to the user (via HTTP response).
  212. Messages are sent in the `__server_messages` property in the
  213. response JSON and shown in a pop-up / modal.
  214. :param msg: Message.
  215. :param title: [optional] Message title.
  216. :param raise_exception: [optional] Raise given exception and show message.
  217. :param as_table: [optional] If `msg` is a list of lists, render as HTML table.
  218. """
  219. from frappe.utils import encode
  220. out = _dict(message=msg)
  221. def _raise_exception():
  222. if raise_exception:
  223. if flags.rollback_on_exception:
  224. db.rollback()
  225. import inspect
  226. if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
  227. raise raise_exception(encode(msg))
  228. else:
  229. raise ValidationError(encode(msg))
  230. if flags.mute_messages:
  231. _raise_exception()
  232. return
  233. if as_table and type(msg) in (list, tuple):
  234. out.msg = '<table border="1px" style="border-collapse: collapse" cellpadding="2px">' + ''.join(['<tr>'+''.join(['<td>%s</td>' % c for c in r])+'</tr>' for r in msg]) + '</table>'
  235. if flags.print_messages and out.msg:
  236. print("Message: " + repr(out.msg).encode("utf-8"))
  237. if title:
  238. out.title = title
  239. if not indicator and raise_exception:
  240. indicator = 'red'
  241. if indicator:
  242. out.indicator = indicator
  243. if alert:
  244. out.alert = 1
  245. message_log.append(json.dumps(out))
  246. _raise_exception()
  247. def clear_messages():
  248. local.message_log = []
  249. def clear_last_message():
  250. if len(local.message_log) > 0:
  251. local.message_log = local.message_log[:-1]
  252. def throw(msg, exc=ValidationError, title=None):
  253. """Throw execption and show message (`msgprint`).
  254. :param msg: Message.
  255. :param exc: Exception class. Default `frappe.ValidationError`"""
  256. msgprint(msg, raise_exception=exc, title=title, indicator='red')
  257. def emit_js(js, user=False, **kwargs):
  258. from frappe.async import publish_realtime
  259. if user == False:
  260. user = session.user
  261. publish_realtime('eval_js', js, user=user, **kwargs)
  262. def create_folder(path, with_init=False):
  263. """Create a folder in the given path and add an `__init__.py` file (optional).
  264. :param path: Folder path.
  265. :param with_init: Create `__init__.py` in the new folder."""
  266. from frappe.utils import touch_file
  267. if not os.path.exists(path):
  268. os.makedirs(path)
  269. if with_init:
  270. touch_file(os.path.join(path, "__init__.py"))
  271. def set_user(username):
  272. """Set current user.
  273. :param username: **User** name to set as current user."""
  274. local.session.user = username
  275. local.session.sid = username
  276. local.cache = {}
  277. local.form_dict = _dict()
  278. local.jenv = None
  279. local.session.data = _dict()
  280. local.role_permissions = {}
  281. local.new_doc_templates = {}
  282. local.user_perms = None
  283. def get_user():
  284. from frappe.utils.user import UserPermissions
  285. if not local.user_perms:
  286. local.user_perms = UserPermissions(local.session.user)
  287. return local.user_perms
  288. def get_roles(username=None):
  289. """Returns roles of current user."""
  290. if not local.session:
  291. return ["Guest"]
  292. if username:
  293. import frappe.permissions
  294. return frappe.permissions.get_roles(username)
  295. else:
  296. return get_user().get_roles()
  297. def get_request_header(key, default=None):
  298. """Return HTTP request header.
  299. :param key: HTTP header key.
  300. :param default: Default value."""
  301. return request.headers.get(key, default)
  302. def sendmail(recipients=[], sender="", subject="No Subject", message="No Message",
  303. as_markdown=False, delayed=True, reference_doctype=None, reference_name=None,
  304. unsubscribe_method=None, unsubscribe_params=None, unsubscribe_message=None,
  305. attachments=None, content=None, doctype=None, name=None, reply_to=None,
  306. cc=[], bcc=[], message_id=None, in_reply_to=None, send_after=None, expose_recipients=None,
  307. send_priority=1, communication=None, retry=1, now=None, read_receipt=None, is_notification=False,
  308. inline_images=None, template=None, args=None, header=None):
  309. """Send email using user's default **Email Account** or global default **Email Account**.
  310. :param recipients: List of recipients.
  311. :param sender: Email sender. Default is current user.
  312. :param subject: Email Subject.
  313. :param message: (or `content`) Email Content.
  314. :param as_markdown: Convert content markdown to HTML.
  315. :param delayed: Send via scheduled email sender **Email Queue**. Don't send immediately. Default is true
  316. :param send_priority: Priority for Email Queue, default 1.
  317. :param reference_doctype: (or `doctype`) Append as communication to this DocType.
  318. :param reference_name: (or `name`) Append as communication to this document name.
  319. :param unsubscribe_method: Unsubscribe url with options email, doctype, name. e.g. `/api/method/unsubscribe`
  320. :param unsubscribe_params: Unsubscribe paramaters to be loaded on the unsubscribe_method [optional] (dict).
  321. :param attachments: List of attachments.
  322. :param reply_to: Reply-To Email Address.
  323. :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.
  324. :param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To.
  325. :param send_after: Send after the given datetime.
  326. :param expose_recipients: Display all recipients in the footer message - "This email was sent to"
  327. :param communication: Communication link to be set in Email Queue record
  328. :param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id
  329. :param template: Name of html template from templates/emails folder
  330. :param args: Arguments for rendering the template
  331. :param header: Append header in email
  332. """
  333. text_content = None
  334. if template:
  335. message, text_content = get_email_from_template(template, args)
  336. message = content or message
  337. if as_markdown:
  338. from markdown2 import markdown
  339. message = markdown(message)
  340. if not delayed:
  341. now = True
  342. from frappe.email import queue
  343. queue.send(recipients=recipients, sender=sender,
  344. subject=subject, message=message, text_content=text_content,
  345. reference_doctype = doctype or reference_doctype, reference_name = name or reference_name,
  346. unsubscribe_method=unsubscribe_method, unsubscribe_params=unsubscribe_params, unsubscribe_message=unsubscribe_message,
  347. attachments=attachments, reply_to=reply_to, cc=cc, bcc=bcc, message_id=message_id, in_reply_to=in_reply_to,
  348. send_after=send_after, expose_recipients=expose_recipients, send_priority=send_priority,
  349. communication=communication, now=now, read_receipt=read_receipt, is_notification=is_notification,
  350. inline_images=inline_images, header=header)
  351. whitelisted = []
  352. guest_methods = []
  353. xss_safe_methods = []
  354. def whitelist(allow_guest=False, xss_safe=False):
  355. """
  356. Decorator for whitelisting a function and making it accessible via HTTP.
  357. Standard request will be `/api/method/[path.to.method]`
  358. :param allow_guest: Allow non logged-in user to access this method.
  359. Use as:
  360. @frappe.whitelist()
  361. def myfunc(param1, param2):
  362. pass
  363. """
  364. def innerfn(fn):
  365. global whitelisted, guest_methods, xss_safe_methods
  366. whitelisted.append(fn)
  367. if allow_guest:
  368. guest_methods.append(fn)
  369. if xss_safe:
  370. xss_safe_methods.append(fn)
  371. return fn
  372. return innerfn
  373. def only_for(roles):
  374. """Raise `frappe.PermissionError` if the user does not have any of the given **Roles**.
  375. :param roles: List of roles to check."""
  376. if local.flags.in_test:
  377. return
  378. if not isinstance(roles, (tuple, list)):
  379. roles = (roles,)
  380. roles = set(roles)
  381. myroles = set(get_roles())
  382. if not roles.intersection(myroles):
  383. raise PermissionError
  384. def get_domain_data(module):
  385. try:
  386. domain_data = get_hooks('domains')
  387. if module in domain_data:
  388. return _dict(get_attr(get_hooks('domains')[module][0] + '.data'))
  389. else:
  390. return _dict()
  391. except ImportError:
  392. if local.flags.in_test:
  393. return _dict()
  394. else:
  395. raise
  396. def clear_cache(user=None, doctype=None):
  397. """Clear **User**, **DocType** or global cache.
  398. :param user: If user is given, only user cache is cleared.
  399. :param doctype: If doctype is given, only DocType cache is cleared."""
  400. import frappe.sessions
  401. if doctype:
  402. import frappe.model.meta
  403. frappe.model.meta.clear_cache(doctype)
  404. reset_metadata_version()
  405. elif user:
  406. frappe.sessions.clear_cache(user)
  407. else: # everything
  408. from frappe import translate
  409. frappe.sessions.clear_cache()
  410. translate.clear_cache()
  411. reset_metadata_version()
  412. local.cache = {}
  413. local.new_doc_templates = {}
  414. for fn in get_hooks("clear_cache"):
  415. get_attr(fn)()
  416. local.role_permissions = {}
  417. def has_permission(doctype=None, ptype="read", doc=None, user=None, verbose=False, throw=False):
  418. """Raises `frappe.PermissionError` if not permitted.
  419. :param doctype: DocType for which permission is to be check.
  420. :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`.
  421. :param doc: [optional] Checks User permissions for given doc.
  422. :param user: [optional] Check for given user. Default: current user."""
  423. if not doctype and doc:
  424. doctype = doc.doctype
  425. import frappe.permissions
  426. out = frappe.permissions.has_permission(doctype, ptype, doc=doc, verbose=verbose, user=user)
  427. if throw and not out:
  428. if doc:
  429. frappe.throw(_("No permission for {0}").format(doc.doctype + " " + doc.name))
  430. else:
  431. frappe.throw(_("No permission for {0}").format(doctype))
  432. return out
  433. def has_website_permission(doc=None, ptype='read', user=None, verbose=False, doctype=None):
  434. """Raises `frappe.PermissionError` if not permitted.
  435. :param doctype: DocType for which permission is to be check.
  436. :param ptype: Permission type (`read`, `write`, `create`, `submit`, `cancel`, `amend`). Default: `read`.
  437. :param doc: Checks User permissions for given doc.
  438. :param user: [optional] Check for given user. Default: current user."""
  439. if not user:
  440. user = session.user
  441. if doc:
  442. if isinstance(doc, string_types):
  443. doc = get_doc(doctype, doc)
  444. doctype = doc.doctype
  445. if doc.flags.ignore_permissions:
  446. return True
  447. # check permission in controller
  448. if hasattr(doc, 'has_website_permission'):
  449. return doc.has_website_permission(ptype, verbose=verbose)
  450. hooks = (get_hooks("has_website_permission") or {}).get(doctype, [])
  451. if hooks:
  452. for method in hooks:
  453. result = call(method, doc=doc, ptype=ptype, user=user, verbose=verbose)
  454. # if even a single permission check is Falsy
  455. if not result:
  456. return False
  457. # else it is Truthy
  458. return True
  459. else:
  460. return False
  461. def is_table(doctype):
  462. """Returns True if `istable` property (indicating child Table) is set for given DocType."""
  463. def get_tables():
  464. return db.sql_list("select name from tabDocType where istable=1")
  465. tables = cache().get_value("is_table", get_tables)
  466. return doctype in tables
  467. def get_precision(doctype, fieldname, currency=None, doc=None):
  468. """Get precision for a given field"""
  469. from frappe.model.meta import get_field_precision
  470. return get_field_precision(get_meta(doctype).get_field(fieldname), doc, currency)
  471. def generate_hash(txt=None, length=None):
  472. """Generates random hash for given text + current timestamp + random string."""
  473. import hashlib, time
  474. from .utils import random_string
  475. digest = hashlib.sha224(((txt or "") + repr(time.time()) + repr(random_string(8))).encode()).hexdigest()
  476. if length:
  477. digest = digest[:length]
  478. return digest
  479. def reset_metadata_version():
  480. """Reset `metadata_version` (Client (Javascript) build ID) hash."""
  481. v = generate_hash()
  482. cache().set_value("metadata_version", v)
  483. return v
  484. def new_doc(doctype, parent_doc=None, parentfield=None, as_dict=False):
  485. """Returns a new document of the given DocType with defaults set.
  486. :param doctype: DocType of the new document.
  487. :param parent_doc: [optional] add to parent document.
  488. :param parentfield: [optional] add against this `parentfield`."""
  489. from frappe.model.create_new import get_new_doc
  490. return get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict)
  491. def set_value(doctype, docname, fieldname, value=None):
  492. """Set document value. Calls `frappe.client.set_value`"""
  493. import frappe.client
  494. return frappe.client.set_value(doctype, docname, fieldname, value)
  495. def get_doc(*args, **kwargs):
  496. """Return a `frappe.model.document.Document` object of the given type and name.
  497. :param arg1: DocType name as string **or** document JSON.
  498. :param arg2: [optional] Document name as string.
  499. Examples:
  500. # insert a new document
  501. todo = frappe.get_doc({"doctype":"ToDo", "description": "test"})
  502. tood.insert()
  503. # open an existing document
  504. todo = frappe.get_doc("ToDo", "TD0001")
  505. """
  506. import frappe.model.document
  507. return frappe.model.document.get_doc(*args, **kwargs)
  508. def get_last_doc(doctype):
  509. """Get last created document of this type."""
  510. d = get_all(doctype, ["name"], order_by="creation desc", limit_page_length=1)
  511. if d:
  512. return get_doc(doctype, d[0].name)
  513. else:
  514. raise DoesNotExistError
  515. def get_single(doctype):
  516. """Return a `frappe.model.document.Document` object of the given Single doctype."""
  517. return get_doc(doctype, doctype)
  518. def get_meta(doctype, cached=True):
  519. """Get `frappe.model.meta.Meta` instance of given doctype name."""
  520. import frappe.model.meta
  521. return frappe.model.meta.get_meta(doctype, cached=cached)
  522. def get_meta_module(doctype):
  523. import frappe.modules
  524. return frappe.modules.load_doctype_module(doctype)
  525. def delete_doc(doctype=None, name=None, force=0, ignore_doctypes=None, for_reload=False,
  526. ignore_permissions=False, flags=None, ignore_on_trash=False, ignore_missing=True):
  527. """Delete a document. Calls `frappe.model.delete_doc.delete_doc`.
  528. :param doctype: DocType of document to be delete.
  529. :param name: Name of document to be delete.
  530. :param force: Allow even if document is linked. Warning: This may lead to data integrity errors.
  531. :param ignore_doctypes: Ignore if child table is one of these.
  532. :param for_reload: Call `before_reload` trigger before deleting.
  533. :param ignore_permissions: Ignore user permissions."""
  534. import frappe.model.delete_doc
  535. frappe.model.delete_doc.delete_doc(doctype, name, force, ignore_doctypes, for_reload,
  536. ignore_permissions, flags, ignore_on_trash, ignore_missing)
  537. def delete_doc_if_exists(doctype, name, force=0):
  538. """Delete document if exists."""
  539. if db.exists(doctype, name):
  540. delete_doc(doctype, name, force=force)
  541. def reload_doctype(doctype, force=False, reset_permissions=False):
  542. """Reload DocType from model (`[module]/[doctype]/[name]/[name].json`) files."""
  543. reload_doc(scrub(db.get_value("DocType", doctype, "module")), "doctype", scrub(doctype),
  544. force=force, reset_permissions=reset_permissions)
  545. def reload_doc(module, dt=None, dn=None, force=False, reset_permissions=False):
  546. """Reload Document from model (`[module]/[doctype]/[name]/[name].json`) files.
  547. :param module: Module name.
  548. :param dt: DocType name.
  549. :param dn: Document name.
  550. :param force: Reload even if `modified` timestamp matches.
  551. """
  552. import frappe.modules
  553. return frappe.modules.reload_doc(module, dt, dn, force=force, reset_permissions=reset_permissions)
  554. def rename_doc(*args, **kwargs):
  555. """Rename a document. Calls `frappe.model.rename_doc.rename_doc`"""
  556. from frappe.model.rename_doc import rename_doc
  557. return rename_doc(*args, **kwargs)
  558. def get_module(modulename):
  559. """Returns a module object for given Python module name using `importlib.import_module`."""
  560. return importlib.import_module(modulename)
  561. def scrub(txt):
  562. """Returns sluggified string. e.g. `Sales Order` becomes `sales_order`."""
  563. return txt.replace(' ','_').replace('-', '_').lower()
  564. def unscrub(txt):
  565. """Returns titlified string. e.g. `sales_order` becomes `Sales Order`."""
  566. return txt.replace('_',' ').replace('-', ' ').title()
  567. def get_module_path(module, *joins):
  568. """Get the path of the given module name.
  569. :param module: Module name.
  570. :param *joins: Join additional path elements using `os.path.join`."""
  571. module = scrub(module)
  572. return get_pymodule_path(local.module_app[module] + "." + module, *joins)
  573. def get_app_path(app_name, *joins):
  574. """Return path of given app.
  575. :param app: App name.
  576. :param *joins: Join additional path elements using `os.path.join`."""
  577. return get_pymodule_path(app_name, *joins)
  578. def get_site_path(*joins):
  579. """Return path of current site.
  580. :param *joins: Join additional path elements using `os.path.join`."""
  581. return os.path.join(local.site_path, *joins)
  582. def get_pymodule_path(modulename, *joins):
  583. """Return path of given Python module name.
  584. :param modulename: Python module name.
  585. :param *joins: Join additional path elements using `os.path.join`."""
  586. if not "public" in joins:
  587. joins = [scrub(part) for part in joins]
  588. return os.path.join(os.path.dirname(get_module(scrub(modulename)).__file__), *joins)
  589. def get_module_list(app_name):
  590. """Get list of modules for given all via `app/modules.txt`."""
  591. return get_file_items(os.path.join(os.path.dirname(get_module(app_name).__file__), "modules.txt"))
  592. def get_all_apps(with_internal_apps=True, sites_path=None):
  593. """Get list of all apps via `sites/apps.txt`."""
  594. if not sites_path:
  595. sites_path = local.sites_path
  596. apps = get_file_items(os.path.join(sites_path, "apps.txt"), raise_not_found=True)
  597. if with_internal_apps:
  598. for app in get_file_items(os.path.join(local.site_path, "apps.txt")):
  599. if app not in apps:
  600. apps.append(app)
  601. if "frappe" in apps:
  602. apps.remove("frappe")
  603. apps.insert(0, 'frappe')
  604. return apps
  605. def get_installed_apps(sort=False, frappe_last=False):
  606. """Get list of installed apps in current site."""
  607. if getattr(flags, "in_install_db", True):
  608. return []
  609. if not db:
  610. connect()
  611. installed = json.loads(db.get_global("installed_apps") or "[]")
  612. if sort:
  613. installed = [app for app in get_all_apps(True) if app in installed]
  614. if frappe_last:
  615. if 'frappe' in installed:
  616. installed.remove('frappe')
  617. installed.append('frappe')
  618. return installed
  619. def get_doc_hooks():
  620. '''Returns hooked methods for given doc. It will expand the dict tuple if required.'''
  621. if not hasattr(local, 'doc_events_hooks'):
  622. hooks = get_hooks('doc_events', {})
  623. out = {}
  624. for key, value in iteritems(hooks):
  625. if isinstance(key, tuple):
  626. for doctype in key:
  627. append_hook(out, doctype, value)
  628. else:
  629. append_hook(out, key, value)
  630. local.doc_events_hooks = out
  631. return local.doc_events_hooks
  632. def get_hooks(hook=None, default=None, app_name=None):
  633. """Get hooks via `app/hooks.py`
  634. :param hook: Name of the hook. Will gather all hooks for this name and return as a list.
  635. :param default: Default if no hook found.
  636. :param app_name: Filter by app."""
  637. def load_app_hooks(app_name=None):
  638. hooks = {}
  639. for app in [app_name] if app_name else get_installed_apps(sort=True):
  640. app = "frappe" if app=="webnotes" else app
  641. try:
  642. app_hooks = get_module(app + ".hooks")
  643. except ImportError:
  644. if local.flags.in_install_app:
  645. # if app is not installed while restoring
  646. # ignore it
  647. pass
  648. print('Could not find app "{0}"'.format(app_name))
  649. if not request:
  650. sys.exit(1)
  651. raise
  652. for key in dir(app_hooks):
  653. if not key.startswith("_"):
  654. append_hook(hooks, key, getattr(app_hooks, key))
  655. return hooks
  656. if app_name:
  657. hooks = _dict(load_app_hooks(app_name))
  658. else:
  659. hooks = _dict(cache().get_value("app_hooks", load_app_hooks))
  660. if hook:
  661. return hooks.get(hook) or (default if default is not None else [])
  662. else:
  663. return hooks
  664. def append_hook(target, key, value):
  665. '''appends a hook to the the target dict.
  666. If the hook key, exists, it will make it a key.
  667. If the hook value is a dict, like doc_events, it will
  668. listify the values against the key.
  669. '''
  670. if isinstance(value, dict):
  671. # dict? make a list of values against each key
  672. target.setdefault(key, {})
  673. for inkey in value:
  674. append_hook(target[key], inkey, value[inkey])
  675. else:
  676. # make a list
  677. target.setdefault(key, [])
  678. if not isinstance(value, list):
  679. value = [value]
  680. target[key].extend(value)
  681. def setup_module_map():
  682. """Rebuild map of all modules (internal)."""
  683. _cache = cache()
  684. if conf.db_name:
  685. local.app_modules = _cache.get_value("app_modules")
  686. local.module_app = _cache.get_value("module_app")
  687. if not (local.app_modules and local.module_app):
  688. local.module_app, local.app_modules = {}, {}
  689. for app in get_all_apps(True):
  690. if app=="webnotes": app="frappe"
  691. local.app_modules.setdefault(app, [])
  692. for module in get_module_list(app):
  693. module = scrub(module)
  694. local.module_app[module] = app
  695. local.app_modules[app].append(module)
  696. if conf.db_name:
  697. _cache.set_value("app_modules", local.app_modules)
  698. _cache.set_value("module_app", local.module_app)
  699. def get_file_items(path, raise_not_found=False, ignore_empty_lines=True):
  700. """Returns items from text file as a list. Ignores empty lines."""
  701. import frappe.utils
  702. content = read_file(path, raise_not_found=raise_not_found)
  703. if content:
  704. content = frappe.utils.strip(content)
  705. return [p.strip() for p in content.splitlines() if (not ignore_empty_lines) or (p.strip() and not p.startswith("#"))]
  706. else:
  707. return []
  708. def get_file_json(path):
  709. """Read a file and return parsed JSON object."""
  710. with open(path, 'r') as f:
  711. return json.load(f)
  712. def read_file(path, raise_not_found=False):
  713. """Open a file and return its content as Unicode."""
  714. if isinstance(path, text_type):
  715. path = path.encode("utf-8")
  716. if os.path.exists(path):
  717. with open(path, "r") as f:
  718. return as_unicode(f.read())
  719. elif raise_not_found:
  720. raise IOError("{} Not Found".format(path))
  721. else:
  722. return None
  723. def get_attr(method_string):
  724. """Get python method object from its name."""
  725. app_name = method_string.split(".")[0]
  726. if not local.flags.in_install and app_name not in get_installed_apps():
  727. throw(_("App {0} is not installed").format(app_name), AppNotInstalledError)
  728. modulename = '.'.join(method_string.split('.')[:-1])
  729. methodname = method_string.split('.')[-1]
  730. return getattr(get_module(modulename), methodname)
  731. def call(fn, *args, **kwargs):
  732. """Call a function and match arguments."""
  733. if isinstance(fn, string_types):
  734. fn = get_attr(fn)
  735. if hasattr(fn, 'fnargs'):
  736. fnargs = fn.fnargs
  737. else:
  738. fnargs, varargs, varkw, defaults = inspect.getargspec(fn)
  739. newargs = {}
  740. for a in kwargs:
  741. if (a in fnargs) or varkw:
  742. newargs[a] = kwargs.get(a)
  743. if "flags" in newargs:
  744. del newargs["flags"]
  745. return fn(*args, **newargs)
  746. def make_property_setter(args, ignore_validate=False, validate_fields_for_doctype=True):
  747. """Create a new **Property Setter** (for overriding DocType and DocField properties).
  748. If doctype is not specified, it will create a property setter for all fields with the
  749. given fieldname"""
  750. args = _dict(args)
  751. if not args.doctype_or_field:
  752. args.doctype_or_field = 'DocField'
  753. if not args.property_type:
  754. args.property_type = db.get_value('DocField',
  755. {'parent': 'DocField', 'fieldname': args.property}, 'fieldtype') or 'Data'
  756. if not args.doctype:
  757. doctype_list = db.sql_list('select distinct parent from tabDocField where fieldname=%s', args.fieldname)
  758. else:
  759. doctype_list = [args.doctype]
  760. for doctype in doctype_list:
  761. if not args.property_type:
  762. args.property_type = db.get_value('DocField',
  763. {'parent': doctype, 'fieldname': args.fieldname}, 'fieldtype') or 'Data'
  764. ps = get_doc({
  765. 'doctype': "Property Setter",
  766. 'doctype_or_field': args.doctype_or_field,
  767. 'doc_type': doctype,
  768. 'field_name': args.fieldname,
  769. 'property': args.property,
  770. 'value': args.value,
  771. 'property_type': args.property_type or "Data",
  772. '__islocal': 1
  773. })
  774. ps.flags.ignore_validate = ignore_validate
  775. ps.flags.validate_fields_for_doctype = validate_fields_for_doctype
  776. ps.validate_fieldtype_change()
  777. ps.insert()
  778. def import_doc(path, ignore_links=False, ignore_insert=False, insert=False):
  779. """Import a file using Data Import."""
  780. from frappe.core.doctype.data_import import data_import
  781. data_import.import_doc(path, ignore_links=ignore_links, ignore_insert=ignore_insert, insert=insert)
  782. def copy_doc(doc, ignore_no_copy=True):
  783. """ No_copy fields also get copied."""
  784. import copy
  785. def remove_no_copy_fields(d):
  786. for df in d.meta.get("fields", {"no_copy": 1}):
  787. if hasattr(d, df.fieldname):
  788. d.set(df.fieldname, None)
  789. fields_to_clear = ['name', 'owner', 'creation', 'modified', 'modified_by']
  790. if not local.flags.in_test:
  791. fields_to_clear.append("docstatus")
  792. if not isinstance(doc, dict):
  793. d = doc.as_dict()
  794. else:
  795. d = doc
  796. newdoc = get_doc(copy.deepcopy(d))
  797. newdoc.set("__islocal", 1)
  798. for fieldname in (fields_to_clear + ['amended_from', 'amendment_date']):
  799. newdoc.set(fieldname, None)
  800. if not ignore_no_copy:
  801. remove_no_copy_fields(newdoc)
  802. for i, d in enumerate(newdoc.get_all_children()):
  803. d.set("__islocal", 1)
  804. for fieldname in fields_to_clear:
  805. d.set(fieldname, None)
  806. if not ignore_no_copy:
  807. remove_no_copy_fields(d)
  808. return newdoc
  809. def compare(val1, condition, val2):
  810. """Compare two values using `frappe.utils.compare`
  811. `condition` could be:
  812. - "^"
  813. - "in"
  814. - "not in"
  815. - "="
  816. - "!="
  817. - ">"
  818. - "<"
  819. - ">="
  820. - "<="
  821. - "not None"
  822. - "None"
  823. """
  824. import frappe.utils
  825. return frappe.utils.compare(val1, condition, val2)
  826. def respond_as_web_page(title, html, success=None, http_status_code=None,
  827. context=None, indicator_color=None, primary_action='/', primary_label = None, fullpage=False,
  828. width=None):
  829. """Send response as a web page with a message rather than JSON. Used to show permission errors etc.
  830. :param title: Page title and heading.
  831. :param message: Message to be shown.
  832. :param success: Alert message.
  833. :param http_status_code: HTTP status code
  834. :param context: web template context
  835. :param indicator_color: color of indicator in title
  836. :param primary_action: route on primary button (default is `/`)
  837. :param primary_label: label on primary button (defaut is "Home")
  838. :param fullpage: hide header / footer
  839. :param width: Width of message in pixels
  840. """
  841. local.message_title = title
  842. local.message = html
  843. local.response['type'] = 'page'
  844. local.response['route'] = 'message'
  845. if http_status_code:
  846. local.response['http_status_code'] = http_status_code
  847. if not context:
  848. context = {}
  849. if not indicator_color:
  850. if success:
  851. indicator_color = 'green'
  852. elif http_status_code and http_status_code > 300:
  853. indicator_color = 'red'
  854. else:
  855. indicator_color = 'blue'
  856. context['indicator_color'] = indicator_color
  857. context['primary_label'] = primary_label
  858. context['primary_action'] = primary_action
  859. context['error_code'] = http_status_code
  860. context['fullpage'] = fullpage
  861. if width:
  862. context['card_width'] = width
  863. local.response['context'] = context
  864. def redirect_to_message(title, html, http_status_code=None, context=None, indicator_color=None):
  865. """Redirects to /message?id=random
  866. Similar to respond_as_web_page, but used to 'redirect' and show message pages like success, failure, etc. with a detailed message
  867. :param title: Page title and heading.
  868. :param message: Message to be shown.
  869. :param http_status_code: HTTP status code.
  870. Example Usage:
  871. frappe.redirect_to_message(_('Thank you'), "<div><p>You will receive an email at test@example.com</p></div>")
  872. """
  873. message_id = generate_hash(length=8)
  874. message = {
  875. 'context': context or {},
  876. 'http_status_code': http_status_code or 200
  877. }
  878. message['context'].update({
  879. 'header': title,
  880. 'title': title,
  881. 'message': html
  882. })
  883. if indicator_color:
  884. message['context'].update({
  885. "indicator_color": indicator_color
  886. })
  887. cache().set_value("message_id:{0}".format(message_id), message, expires_in_sec=60)
  888. location = '/message?id={0}'.format(message_id)
  889. if not getattr(local, 'is_ajax', False):
  890. local.response["type"] = "redirect"
  891. local.response["location"] = location
  892. else:
  893. return location
  894. def build_match_conditions(doctype, as_condition=True):
  895. """Return match (User permissions) for given doctype as list or SQL."""
  896. import frappe.desk.reportview
  897. return frappe.desk.reportview.build_match_conditions(doctype, as_condition)
  898. def get_list(doctype, *args, **kwargs):
  899. """List database query via `frappe.model.db_query`. Will also check for permissions.
  900. :param doctype: DocType on which query is to be made.
  901. :param fields: List of fields or `*`.
  902. :param filters: List of filters (see example).
  903. :param order_by: Order By e.g. `modified desc`.
  904. :param limit_page_start: Start results at record #. Default 0.
  905. :param limit_page_length: No of records in the page. Default 20.
  906. Example usage:
  907. # simple dict filter
  908. frappe.get_list("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"})
  909. # filter as a list of lists
  910. frappe.get_list("ToDo", fields="*", filters = [["modified", ">", "2014-01-01"]])
  911. # filter as a list of dicts
  912. frappe.get_list("ToDo", fields="*", filters = {"description": ("like", "test%")})
  913. """
  914. import frappe.model.db_query
  915. return frappe.model.db_query.DatabaseQuery(doctype).execute(None, *args, **kwargs)
  916. def get_all(doctype, *args, **kwargs):
  917. """List database query via `frappe.model.db_query`. Will **not** check for conditions.
  918. Parameters are same as `frappe.get_list`
  919. :param doctype: DocType on which query is to be made.
  920. :param fields: List of fields or `*`. Default is: `["name"]`.
  921. :param filters: List of filters (see example).
  922. :param order_by: Order By e.g. `modified desc`.
  923. :param limit_page_start: Start results at record #. Default 0.
  924. :param limit_page_length: No of records in the page. Default 20.
  925. Example usage:
  926. # simple dict filter
  927. frappe.get_all("ToDo", fields=["name", "description"], filters = {"owner":"test@example.com"})
  928. # filter as a list of lists
  929. frappe.get_all("ToDo", fields=["*"], filters = [["modified", ">", "2014-01-01"]])
  930. # filter as a list of dicts
  931. frappe.get_all("ToDo", fields=["*"], filters = {"description": ("like", "test%")})
  932. """
  933. kwargs["ignore_permissions"] = True
  934. if not "limit_page_length" in kwargs:
  935. kwargs["limit_page_length"] = 0
  936. return get_list(doctype, *args, **kwargs)
  937. def get_value(*args, **kwargs):
  938. """Returns a document property or list of properties.
  939. Alias for `frappe.db.get_value`
  940. :param doctype: DocType name.
  941. :param filters: Filters like `{"x":"y"}` or name of the document. `None` if Single DocType.
  942. :param fieldname: Column name.
  943. :param ignore: Don't raise exception if table, column is missing.
  944. :param as_dict: Return values as dict.
  945. :param debug: Print query in error log.
  946. """
  947. return db.get_value(*args, **kwargs)
  948. def as_json(obj, indent=1):
  949. from frappe.utils.response import json_handler
  950. return json.dumps(obj, indent=indent, sort_keys=True, default=json_handler)
  951. def are_emails_muted():
  952. from frappe.utils import cint
  953. return flags.mute_emails or cint(conf.get("mute_emails") or 0) or False
  954. def get_test_records(doctype):
  955. """Returns list of objects from `test_records.json` in the given doctype's folder."""
  956. from frappe.modules import get_doctype_module, get_module_path
  957. path = os.path.join(get_module_path(get_doctype_module(doctype)), "doctype", scrub(doctype), "test_records.json")
  958. if os.path.exists(path):
  959. with open(path, "r") as f:
  960. return json.loads(f.read())
  961. else:
  962. return []
  963. def format_value(*args, **kwargs):
  964. """Format value with given field properties.
  965. :param value: Value to be formatted.
  966. :param df: (Optional) DocField object with properties `fieldtype`, `options` etc."""
  967. import frappe.utils.formatters
  968. return frappe.utils.formatters.format_value(*args, **kwargs)
  969. def format(*args, **kwargs):
  970. """Format value with given field properties.
  971. :param value: Value to be formatted.
  972. :param df: (Optional) DocField object with properties `fieldtype`, `options` etc."""
  973. import frappe.utils.formatters
  974. return frappe.utils.formatters.format_value(*args, **kwargs)
  975. def get_print(doctype=None, name=None, print_format=None, style=None, html=None, as_pdf=False, doc=None, output = None, no_letterhead = 0):
  976. """Get Print Format for given document.
  977. :param doctype: DocType of document.
  978. :param name: Name of document.
  979. :param print_format: Print Format name. Default 'Standard',
  980. :param style: Print Format style.
  981. :param as_pdf: Return as PDF. Default False."""
  982. from frappe.website.render import build_page
  983. from frappe.utils.pdf import get_pdf
  984. local.form_dict.doctype = doctype
  985. local.form_dict.name = name
  986. local.form_dict.format = print_format
  987. local.form_dict.style = style
  988. local.form_dict.doc = doc
  989. local.form_dict.no_letterhead = no_letterhead
  990. if not html:
  991. html = build_page("printview")
  992. if as_pdf:
  993. return get_pdf(html, output = output)
  994. else:
  995. return html
  996. def attach_print(doctype, name, file_name=None, print_format=None, style=None, html=None, doc=None, lang=None):
  997. from frappe.utils import scrub_urls
  998. if not file_name: file_name = name
  999. file_name = file_name.replace(' ','').replace('/','-')
  1000. print_settings = db.get_singles_dict("Print Settings")
  1001. _lang = local.lang
  1002. #set lang as specified in print format attachment
  1003. if lang: local.lang = lang
  1004. local.flags.ignore_print_permissions = True
  1005. if int(print_settings.send_print_as_pdf or 0):
  1006. out = {
  1007. "fname": file_name + ".pdf",
  1008. "fcontent": get_print(doctype, name, print_format=print_format, style=style, html=html, as_pdf=True, doc=doc)
  1009. }
  1010. else:
  1011. out = {
  1012. "fname": file_name + ".html",
  1013. "fcontent": scrub_urls(get_print(doctype, name, print_format=print_format, style=style, html=html, doc=doc)).encode("utf-8")
  1014. }
  1015. local.flags.ignore_print_permissions = False
  1016. #reset lang to original local lang
  1017. local.lang = _lang
  1018. return out
  1019. def publish_progress(*args, **kwargs):
  1020. """Show the user progress for a long request
  1021. :param percent: Percent progress
  1022. :param title: Title
  1023. :param doctype: Optional, for DocType
  1024. :param name: Optional, for Document name
  1025. """
  1026. import frappe.async
  1027. return frappe.async.publish_progress(*args, **kwargs)
  1028. def publish_realtime(*args, **kwargs):
  1029. """Publish real-time updates
  1030. :param event: Event name, like `task_progress` etc.
  1031. :param message: JSON message object. For async must contain `task_id`
  1032. :param room: Room in which to publish update (default entire site)
  1033. :param user: Transmit to user
  1034. :param doctype: Transmit to doctype, docname
  1035. :param docname: Transmit to doctype, docname
  1036. :param after_commit: (default False) will emit after current transaction is committed
  1037. """
  1038. import frappe.async
  1039. return frappe.async.publish_realtime(*args, **kwargs)
  1040. def local_cache(namespace, key, generator, regenerate_if_none=False):
  1041. """A key value store for caching within a request
  1042. :param namespace: frappe.local.cache[namespace]
  1043. :param key: frappe.local.cache[namespace][key] used to retrieve value
  1044. :param generator: method to generate a value if not found in store
  1045. """
  1046. if namespace not in local.cache:
  1047. local.cache[namespace] = {}
  1048. if key not in local.cache[namespace]:
  1049. local.cache[namespace][key] = generator()
  1050. elif local.cache[namespace][key]==None and regenerate_if_none:
  1051. # if key exists but the previous result was None
  1052. local.cache[namespace][key] = generator()
  1053. return local.cache[namespace][key]
  1054. def enqueue(*args, **kwargs):
  1055. '''
  1056. Enqueue method to be executed using a background worker
  1057. :param method: method string or method object
  1058. :param queue: (optional) should be either long, default or short
  1059. :param timeout: (optional) should be set according to the functions
  1060. :param event: this is passed to enable clearing of jobs from queues
  1061. :param async: (optional) if async=False, the method is executed immediately, else via a worker
  1062. :param job_name: (optional) can be used to name an enqueue call, which can be used to prevent duplicate calls
  1063. :param kwargs: keyword arguments to be passed to the method
  1064. '''
  1065. import frappe.utils.background_jobs
  1066. return frappe.utils.background_jobs.enqueue(*args, **kwargs)
  1067. def enqueue_doc(*args, **kwargs):
  1068. '''
  1069. Enqueue method to be executed using a background worker
  1070. :param doctype: DocType of the document on which you want to run the event
  1071. :param name: Name of the document on which you want to run the event
  1072. :param method: method string or method object
  1073. :param queue: (optional) should be either long, default or short
  1074. :param timeout: (optional) should be set according to the functions
  1075. :param kwargs: keyword arguments to be passed to the method
  1076. '''
  1077. import frappe.utils.background_jobs
  1078. return frappe.utils.background_jobs.enqueue_doc(*args, **kwargs)
  1079. def get_doctype_app(doctype):
  1080. def _get_doctype_app():
  1081. doctype_module = local.db.get_value("DocType", doctype, "module")
  1082. return local.module_app[scrub(doctype_module)]
  1083. return local_cache("doctype_app", doctype, generator=_get_doctype_app)
  1084. loggers = {}
  1085. log_level = None
  1086. def logger(module=None, with_more_info=True):
  1087. '''Returns a python logger that uses StreamHandler'''
  1088. from frappe.utils.logger import get_logger
  1089. return get_logger(module or 'default', with_more_info=with_more_info)
  1090. def log_error(message=None, title=None):
  1091. '''Log error to Error Log'''
  1092. return get_doc(dict(doctype='Error Log', error=as_unicode(message or get_traceback()),
  1093. method=title)).insert(ignore_permissions=True)
  1094. def get_desk_link(doctype, name):
  1095. return '<a href="#Form/{0}/{1}" style="font-weight: bold;">{2} {1}</a>'.format(doctype, name, _(doctype))
  1096. def bold(text):
  1097. return '<b>{0}</b>'.format(text)
  1098. def safe_eval(code, eval_globals=None, eval_locals=None):
  1099. '''A safer `eval`'''
  1100. whitelisted_globals = {
  1101. "int": int,
  1102. "float": float,
  1103. "long": int,
  1104. "round": round
  1105. }
  1106. if '__' in code:
  1107. throw('Illegal rule {0}. Cannot use "__"'.format(bold(code)))
  1108. if not eval_globals:
  1109. eval_globals = {}
  1110. eval_globals['__builtins__'] = {}
  1111. eval_globals.update(whitelisted_globals)
  1112. return eval(code, eval_globals, eval_locals)
  1113. def get_system_settings(key):
  1114. if key not in local.system_settings:
  1115. local.system_settings.update({key: db.get_single_value('System Settings', key)})
  1116. return local.system_settings.get(key)
  1117. def get_active_domains():
  1118. from frappe.core.doctype.domain_settings.domain_settings import get_active_domains
  1119. return get_active_domains()