Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

653 rindas
16 KiB

  1. # util __init__.py
  2. import webnotes
  3. user_time_zone = None
  4. month_name = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
  5. month_name_full = ['','January','February','March','April','May','June','July','August','September','October','November','December']
  6. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image', 'Graph']
  7. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  8. def getCSVelement(v):
  9. """
  10. Returns the CSV value of `v`, For example:
  11. * apple becomes "apple"
  12. * hi"there becomes "hi""there"
  13. """
  14. v = cstr(v)
  15. if not v: return ''
  16. if (',' in v) or ('\n' in v) or ('"' in v):
  17. if '"' in v: v = v.replace('"', '""')
  18. return '"'+v+'"'
  19. else: return v or ''
  20. def extract_email_id(s):
  21. """
  22. Extract email id from email header format
  23. """
  24. if '<' in s:
  25. s = s.split('<')[1].split('>')[0]
  26. if s:
  27. s = s.strip().lower()
  28. return s
  29. def validate_email_add(email_str):
  30. """
  31. Validates the email string
  32. """
  33. s = extract_email_id(email_str)
  34. import re
  35. #return re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", email_str)
  36. return re.match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", s)
  37. def sendmail(recipients, sender='', msg='', subject='[No Subject]', parts=[], cc=[], attach=[]):
  38. """
  39. Send an email. For more details see :func:`email_lib.sendmail`
  40. """
  41. import webnotes.utils.email_lib
  42. return email_lib.sendmail(recipients, sender, msg, subject, parts, cc, attach)
  43. def generate_hash():
  44. """
  45. Generates random hash for session id
  46. """
  47. import hashlib, time
  48. return hashlib.sha224(str(time.time())).hexdigest()
  49. def random_string(length):
  50. """generate a random string"""
  51. import string
  52. from random import choice
  53. return ''.join([choice(string.letters + string.digits) for i in range(length)])
  54. def db_exists(dt, dn):
  55. return webnotes.conn.sql('select name from `tab%s` where name="%s"' % (dt, dn))
  56. def load_json(arg):
  57. # already a dictionary?
  58. if type(arg)!=str:
  59. return arg
  60. try: import json
  61. except: import simplejson as json
  62. #return json.loads(unicode(arg, 'iso-8859-15'))
  63. return json.loads(arg)
  64. # Get Traceback
  65. # ==============================================================================
  66. def getTraceback():
  67. """
  68. Returns the traceback of the Exception
  69. """
  70. import sys, traceback, string
  71. type, value, tb = sys.exc_info()
  72. body = "Traceback (innermost last):\n"
  73. list = traceback.format_tb(tb, None) + traceback.format_exception_only(type, value)
  74. body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1])
  75. if webnotes.logger:
  76. webnotes.logger.error('Db:'+(webnotes.conn and webnotes.conn.cur_db_name or '') + ' - ' + body)
  77. return body
  78. # Log
  79. # ==============================================================================
  80. def log(event, details):
  81. webnotes.logger.info(details)
  82. # Date and Time
  83. # ==============================================================================
  84. def getdate(string_date):
  85. """
  86. Coverts string date (yyyy-mm-dd) to datetime.date object
  87. """
  88. import datetime
  89. if type(string_date)==unicode:
  90. string_date = str(string_date)
  91. if type(string_date) in (datetime.datetime, datetime.date):
  92. return string_date
  93. if ' ' in string_date:
  94. string_date = string_date.split(' ')[0]
  95. t = string_date.split('-')
  96. if len(t)==3:
  97. return datetime.date(cint(t[0]), cint(t[1]), cint(t[2]))
  98. else:
  99. return ''
  100. def add_days(date, days, format='string'):
  101. """
  102. Adds `days` to the given `string_date`
  103. """
  104. import datetime
  105. if not date:
  106. date = now_datetime()
  107. if type(date) not in (datetime.datetime, datetime.date):
  108. date = getdate(date)
  109. dt = date + datetime.timedelta(days)
  110. if format=='string':
  111. return dt.strftime('%Y-%m-%d')
  112. else:
  113. return dt
  114. def add_months(string_date, months):
  115. import datetime
  116. return webnotes.conn.sql("select DATE_ADD('%s',INTERVAL '%s' MONTH)" % (getdate(string_date),months))[0][0]
  117. def add_years(string_date, years):
  118. import datetime
  119. return webnotes.conn.sql("select DATE_ADD('%s',INTERVAL '%s' YEAR)" % (getdate(string_date),years))[0][0]
  120. def date_diff(string_ed_date, string_st_date=None):
  121. import datetime
  122. return webnotes.conn.sql("SELECT DATEDIFF('%s','%s')" %(getdate(string_ed_date), getdate(string_st_date)))[0][0]
  123. def now_datetime():
  124. global user_time_zone
  125. from datetime import datetime
  126. from pytz import timezone
  127. # get localtime
  128. if not user_time_zone:
  129. user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') or 'Asia/Calcutta'
  130. # convert to UTC
  131. utcnow = timezone('UTC').localize(datetime.utcnow())
  132. # convert to user time zone
  133. return utcnow.astimezone(timezone(user_time_zone))
  134. def now():
  135. """
  136. Returns `time.strftime('%Y-%m-%d %H:%M:%S')`
  137. """
  138. return now_datetime().strftime('%Y-%m-%d %H:%M:%S')
  139. def nowdate():
  140. """
  141. Returns time.strftime('%Y-%m-%d')
  142. """
  143. return now_datetime().strftime('%Y-%m-%d')
  144. def get_first_day(dt, d_years=0, d_months=0):
  145. """
  146. Returns the first day of the month for the date specified by date object
  147. Also adds `d_years` and `d_months` if specified
  148. """
  149. import datetime
  150. # d_years, d_months are "deltas" to apply to dt
  151. y, m = dt.year + d_years, dt.month + d_months
  152. a, m = divmod(m-1, 12)
  153. return datetime.date(y+a, m+1, 1)
  154. def get_last_day(dt):
  155. """
  156. Returns last day of the month using:
  157. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  158. """
  159. import datetime
  160. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  161. user_format = None
  162. """
  163. User format specified in :term:`Control Panel`
  164. Examples:
  165. * dd-mm-yyyy
  166. * mm-dd-yyyy
  167. * dd/mm/yyyy
  168. """
  169. def formatdate(string_date):
  170. """
  171. Convers the given string date to :data:`user_format`
  172. """
  173. global user_format
  174. if not user_format:
  175. user_format = webnotes.conn.get_value('Control Panel', None, 'date_format')
  176. d = string_date.split('-');
  177. out = user_format
  178. return out.replace('dd', ('%.2i' % cint(d[2]))).replace('mm', ('%.2i' % cint(d[1]))).replace('yyyy', d[0])
  179. def dict_to_str(args, sep='&'):
  180. """
  181. Converts a dictionary to URL
  182. """
  183. import urllib
  184. t = []
  185. for k in args.keys():
  186. t.append(str(k)+'='+urllib.quote(str(args[k] or '')))
  187. return sep.join(t)
  188. def timestamps_equal(t1, t2):
  189. """Returns true if same the two string timestamps are same"""
  190. scrub = lambda x: x.replace(':', ' ').replace('-',' ').split()
  191. t1, t2 = scrub(t1), scrub(t2)
  192. if len(t1) != len(t2):
  193. return
  194. for i in range(len(t1)):
  195. if t1[i]!=t2[i]:
  196. return
  197. return 1
  198. def global_date_format(date):
  199. import datetime
  200. if type(date) in (str, unicode):
  201. date = getdate(date)
  202. return date.strftime('%d') + ' ' + month_name_full[int(date.strftime('%m'))] + ' ' + date.strftime('%Y')
  203. # Datatype
  204. # ==============================================================================
  205. def isNull(v):
  206. """
  207. Returns true if v='' or v is `None`
  208. """
  209. return (v=='' or v==None)
  210. def has_common(l1, l2):
  211. """
  212. Returns true if there are common elements in lists l1 and l2
  213. """
  214. for l in l1:
  215. if l in l2:
  216. return 1
  217. return 0
  218. def flt(s):
  219. """
  220. Convert to float (ignore commas)
  221. """
  222. if type(s)==str: # if string
  223. s = s.replace(',','')
  224. try: tmp = float(s)
  225. except: tmp = 0
  226. return tmp
  227. def cint(s):
  228. """
  229. Convert to integer
  230. """
  231. try: tmp = int(float(s))
  232. except: tmp = 0
  233. return tmp
  234. def cstr(s):
  235. """
  236. Convert to string
  237. """
  238. if type(s) in (str, unicode):
  239. return s
  240. elif s==None:
  241. return ''
  242. else:
  243. return str(s)
  244. def str_esc_quote(s):
  245. """
  246. Escape quotes
  247. """
  248. if s==None:return ''
  249. return s.replace("'","\'")
  250. def replace_newlines(s):
  251. """
  252. Replace newlines by '<br>'
  253. """
  254. if s==None:return ''
  255. return s.replace("\n","<br>")
  256. # ==============================================================================
  257. def parse_val(v):
  258. """
  259. Converts to simple datatypes from SQL query results
  260. """
  261. import datetime
  262. if type(v)==datetime.date:
  263. v = str(v)
  264. elif type(v)==datetime.timedelta:
  265. v = ':'.join(str(v).split(':')[:2])
  266. elif type(v)==datetime.datetime:
  267. v = str(v)
  268. elif type(v)==long: v=int(v)
  269. return v
  270. # ==============================================================================
  271. def fmt_money(amount, fmt = '%.2f'):
  272. """
  273. Convert to string with commas for thousands, millions etc
  274. """
  275. curr = webnotes.conn.get_value('Control Panel', None, 'currency_format') or 'Millions'
  276. val = 2
  277. if curr == 'Millions': val = 3
  278. if cstr(amount).find('.') == -1: temp = '00'
  279. else: temp = cstr(amount).split('.')[1]
  280. l = []
  281. minus = ''
  282. if flt(amount) < 0: minus = '-'
  283. amount = ''.join(cstr(amount).split(','))
  284. amount = cstr(abs(flt(amount))).split('.')[0]
  285. # main logic
  286. if len(cstr(amount)) > 3:
  287. nn = amount[len(amount)-3:]
  288. l.append(nn)
  289. amount = amount[0:len(amount)-3]
  290. while len(cstr(amount)) > val:
  291. nn = amount[len(amount)-val:]
  292. l.insert(0,nn)
  293. amount = amount[0:len(amount)-val]
  294. if len(amount) > 0: l.insert(0,amount)
  295. amount = ','.join(l)+'.'+temp
  296. amount = minus + amount
  297. return amount
  298. #
  299. # convet currency to words
  300. #
  301. def money_in_words(number, main_currency = None, fraction_currency=None):
  302. """
  303. Returns string in words with currency and fraction currency.
  304. """
  305. d = get_defaults()
  306. if not main_currency:
  307. main_currency = d.get('currency', 'INR')
  308. if not fraction_currency:
  309. fraction_currency = d.get('fraction_currency', 'paise')
  310. n = "%.2f" % flt(number)
  311. main, fraction = n.split('.')
  312. if len(fraction)==1: fraction += '0'
  313. out = main_currency + ' ' + in_words(main).title()
  314. if cint(fraction):
  315. out = out + ' and ' + in_words(fraction).title() + ' ' + fraction_currency
  316. return out + ' only.'
  317. #
  318. # convert number to words
  319. #
  320. def in_words(integer):
  321. """
  322. Returns string in words for the given integer.
  323. """
  324. in_million = webnotes.conn.get_value('Control Panel',None,'currency_format')=='Millions' and 1 or 0
  325. n=int(integer)
  326. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  327. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  328. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  329. def psn(n, known, xpsn):
  330. import sys;
  331. if n in known: return known[n]
  332. bestguess, remainder = str(n), 0
  333. if n<=20:
  334. print >>sys.stderr, n, "How did this happen?"
  335. assert 0
  336. elif n < 100:
  337. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  338. return bestguess
  339. elif n < 1000:
  340. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  341. remainder = n%100
  342. else:
  343. if in_million:
  344. if n < 1000000:
  345. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  346. remainder = n%1000
  347. elif n < 1000000000:
  348. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  349. remainder = n%1000000
  350. else:
  351. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  352. remainder = n%1000000000
  353. else:
  354. if n < 100000:
  355. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  356. remainder = n%1000
  357. elif n < 10000000:
  358. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  359. remainder = n%100000
  360. else:
  361. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  362. remainder = n%10000000
  363. if remainder:
  364. if remainder >= 100:
  365. comma = ','
  366. else:
  367. comma = ''
  368. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  369. else:
  370. return bestguess
  371. return psn(n, known, psn)
  372. # Get Defaults
  373. # ==============================================================================
  374. def get_defaults(key=None):
  375. """
  376. Get dictionary of default values from the :term:`Control Panel`, or a value if key is passed
  377. """
  378. return webnotes.conn.get_defaults(key)
  379. def set_default(key, val):
  380. """
  381. Set / add a default value to :term:`Control Panel`
  382. """
  383. return webnotes.conn.set_default(key, val)
  384. #
  385. # Clear recycle bin
  386. #
  387. def clear_recycle_bin():
  388. sql = webnotes.conn.sql
  389. tl = sql('show tables')
  390. total_deleted = 0
  391. for t in tl:
  392. fl = [i[0] for i in sql('desc `%s`' % t[0])]
  393. if 'name' in fl:
  394. total_deleted += sql("select count(*) from `%s` where name like '__overwritten:%%'" % t[0])[0][0]
  395. sql("delete from `%s` where name like '__overwritten:%%'" % t[0])
  396. if 'parent' in fl:
  397. total_deleted += sql("select count(*) from `%s` where parent like '__oldparent:%%'" % t[0])[0][0]
  398. sql("delete from `%s` where parent like '__oldparent:%%'" % t[0])
  399. total_deleted += sql("select count(*) from `%s` where parent like 'oldparent:%%'" % t[0])[0][0]
  400. sql("delete from `%s` where parent like 'oldparent:%%'" % t[0])
  401. total_deleted += sql("select count(*) from `%s` where parent like 'old_parent:%%'" % t[0])[0][0]
  402. sql("delete from `%s` where parent like 'old_parent:%%'" % t[0])
  403. return "%s records deleted" % str(int(total_deleted))
  404. # Send Error Report
  405. # ==============================================================================
  406. def send_error_report():
  407. sql = webnotes.conn.sql
  408. m = ''
  409. company = webnotes.conn.get_value('Control Panel',None,'company_name') or ''
  410. if company: m = 'Company : ' + company
  411. form = webnotes.form
  412. err_msg = '''
  413. %s <br>
  414. Comment: %s
  415. Err Msg : %s
  416. ''' % (m, form.getvalue('msg') or '', form.getvalue('err_msg'))
  417. sendmail([webnotes.conn.get_value('Control Panel',None,'support_email_id') or 'support@iwebnotes.com'], sender=webnotes.session['user'], msg=err_msg, subject='Error Report '+m)
  418. # Dictionary utils
  419. # ==============================================================================
  420. def remove_blanks(d):
  421. """
  422. Returns d with empty ('' or None) values stripped
  423. """
  424. empty_keys = []
  425. for key in d:
  426. if d[key]=='' or d[key]==None:
  427. # del d[key] raises runtime exception, using a workaround
  428. empty_keys.append(key)
  429. for key in empty_keys:
  430. del d[key]
  431. return d
  432. def pprint_dict(d, level=1, no_blanks=True):
  433. """
  434. Pretty print a dictionary with indents
  435. """
  436. if no_blanks:
  437. remove_blanks(d)
  438. # make indent
  439. indent, ret = '', ''
  440. for i in range(0,level): indent += '\t'
  441. # add lines
  442. comment, lines = '', []
  443. kl = d.keys()
  444. kl.sort()
  445. # make lines
  446. for key in kl:
  447. if key != '##comment':
  448. tmp = {key: d[key]}
  449. lines.append(indent + str(tmp)[1:-1] )
  450. # add comment string
  451. if '##comment' in kl:
  452. ret = ('\n' + indent) + '# ' + d['##comment'] + '\n'
  453. # open
  454. ret += indent + '{\n'
  455. # lines
  456. ret += indent + ',\n\t'.join(lines)
  457. # close
  458. ret += '\n' + indent + '}'
  459. return ret
  460. def get_common(d1,d2):
  461. """
  462. returns (list of keys) the common part of two dicts
  463. """
  464. return [p for p in d1 if p in d2 and d1[p]==d2[p]]
  465. def get_common_dict(d1, d2):
  466. """
  467. return common dictionary of d1 and d2
  468. """
  469. ret = {}
  470. for key in d1:
  471. if key in d2 and d2[key]==d1[key]:
  472. ret[key] = d1[key]
  473. return ret
  474. def get_diff_dict(d1, d2):
  475. """
  476. return common dictionary of d1 and d2
  477. """
  478. diff_keys = set(d2.keys()).difference(set(d1.keys()))
  479. ret = {}
  480. for d in diff_keys: ret[d] = d2[d]
  481. return ret
  482. def get_file_timestamp(fn):
  483. """
  484. Returns timestamp of the given file
  485. """
  486. import os
  487. from webnotes.utils import cint
  488. try:
  489. return str(cint(os.stat(fn).st_mtime))
  490. except OSError, e:
  491. if e.args[0]!=2:
  492. raise e
  493. else:
  494. return None
  495. def make_esc(esc_chars):
  496. """
  497. Function generator for Escaping special characters
  498. """
  499. return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
  500. def get_doctype_label(dt=None):
  501. """
  502. Gets label of a doctype
  503. """
  504. if dt:
  505. res = webnotes.conn.sql("""\
  506. SELECT name, dt_label FROM `tabDocType Label`
  507. WHERE name=%s""", dt)
  508. return res and res[0][0] or dt
  509. else:
  510. res = webnotes.conn.sql("SELECT name, dt_label FROM `tabDocType Label`")
  511. dt_label_dict = {}
  512. for r in res:
  513. dt_label_dict[r[0]] = r[1]
  514. return dt_label_dict
  515. def get_label_doctype(label):
  516. """
  517. Gets doctype from its label
  518. """
  519. res = webnotes.conn.sql("""\
  520. SELECT name FROM `tabDocType Label`
  521. WHERE dt_label=%s""", label)
  522. return res and res[0][0] or label