Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

__init__.py 42 KiB

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