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

926 lines
23 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. # util __init__.py
  4. from __future__ import unicode_literals
  5. from werkzeug.test import Client
  6. import os
  7. import re
  8. import urllib
  9. import webnotes
  10. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable',
  11. 'Button', 'Image', 'Graph']
  12. default_fields = ['doctype', 'name', 'owner', 'creation', 'modified', 'modified_by',
  13. 'parent', 'parentfield', 'parenttype', 'idx', 'docstatus']
  14. # used in import_docs.py
  15. # TODO: deprecate it
  16. def getCSVelement(v):
  17. """
  18. Returns the CSV value of `v`, For example:
  19. * apple becomes "apple"
  20. * hi"there becomes "hi""there"
  21. """
  22. v = cstr(v)
  23. if not v: return ''
  24. if (',' in v) or ('\n' in v) or ('"' in v):
  25. if '"' in v: v = v.replace('"', '""')
  26. return '"'+v+'"'
  27. else: return v or ''
  28. def get_fullname(profile):
  29. """get the full name (first name + last name) of the user from Profile"""
  30. if not hasattr(webnotes.local, "fullnames"):
  31. webnotes.local.fullnames = {}
  32. if not webnotes.local.fullnames.get(profile):
  33. p = webnotes.conn.get_value("Profile", profile, ["first_name", "last_name"], as_dict=True)
  34. if p:
  35. webnotes.local.fullnames[profile] = " ".join(filter(None,
  36. [p.get('first_name'), p.get('last_name')])) or profile
  37. else:
  38. webnotes.local.fullnames[profile] = profile
  39. return webnotes.local.fullnames.get(profile)
  40. def get_formatted_email(user):
  41. """get email id of user formatted as: John Doe <johndoe@example.com>"""
  42. if user == "Administrator":
  43. return user
  44. from email.utils import formataddr
  45. fullname = get_fullname(user)
  46. return formataddr((fullname, user))
  47. def extract_email_id(email):
  48. """fetch only the email part of the email id"""
  49. from email.utils import parseaddr
  50. fullname, email_id = parseaddr(email)
  51. if isinstance(email_id, basestring) and not isinstance(email_id, unicode):
  52. email_id = email_id.decode("utf-8", "ignore")
  53. return email_id
  54. def validate_email_add(email_str):
  55. """Validates the email string"""
  56. email = extract_email_id(email_str)
  57. 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])?", email.lower())
  58. def get_request_site_address(full_address=False):
  59. """get app url from request"""
  60. host_name = webnotes.local.conf.host_name
  61. if not host_name:
  62. if webnotes.request:
  63. protocol = 'https' == webnotes.get_request_header('X-Forwarded-Proto', "") and 'https://' or 'http://'
  64. host_name = protocol + webnotes.request.host
  65. elif webnotes.local.site:
  66. return "http://" + webnotes.local.site
  67. else:
  68. return "http://localhost"
  69. if full_address:
  70. return host_name + webnotes.get_request_header("REQUEST_URI", "")
  71. else:
  72. return host_name
  73. def random_string(length):
  74. """generate a random string"""
  75. import string
  76. from random import choice
  77. return ''.join([choice(string.letters + string.digits) for i in range(length)])
  78. def get_traceback():
  79. """
  80. Returns the traceback of the Exception
  81. """
  82. import sys, traceback
  83. exc_type, value, tb = sys.exc_info()
  84. trace_list = traceback.format_tb(tb, None) + \
  85. traceback.format_exception_only(exc_type, value)
  86. body = "Traceback (innermost last):\n" + "%-20s %s" % \
  87. (unicode((b"").join(trace_list[:-1]), 'utf-8'), unicode(trace_list[-1], 'utf-8'))
  88. if webnotes.logger:
  89. webnotes.logger.error('Db:'+(webnotes.conn and webnotes.conn.cur_db_name or '') \
  90. + ' - ' + body)
  91. return body
  92. def log(event, details):
  93. webnotes.logger.info(details)
  94. # datetime functions
  95. def getdate(string_date):
  96. """
  97. Coverts string date (yyyy-mm-dd) to datetime.date object
  98. """
  99. import datetime
  100. if isinstance(string_date, datetime.date):
  101. return string_date
  102. elif isinstance(string_date, datetime.datetime):
  103. return datetime.date()
  104. if " " in string_date:
  105. string_date = string_date.split(" ")[0]
  106. return datetime.datetime.strptime(string_date, "%Y-%m-%d").date()
  107. def add_to_date(date, years=0, months=0, days=0):
  108. """Adds `days` to the given date"""
  109. format = isinstance(date, basestring)
  110. if date:
  111. date = getdate(date)
  112. else:
  113. raise Exception, "Start date required"
  114. from dateutil.relativedelta import relativedelta
  115. date += relativedelta(years=years, months=months, days=days)
  116. if format:
  117. return date.strftime("%Y-%m-%d")
  118. else:
  119. return date
  120. def add_days(date, days):
  121. return add_to_date(date, days=days)
  122. def add_months(date, months):
  123. return add_to_date(date, months=months)
  124. def add_years(date, years):
  125. return add_to_date(date, years=years)
  126. def date_diff(string_ed_date, string_st_date):
  127. return (getdate(string_ed_date) - getdate(string_st_date)).days
  128. def time_diff(string_ed_date, string_st_date):
  129. return get_datetime(string_ed_date) - get_datetime(string_st_date)
  130. def time_diff_in_seconds(string_ed_date, string_st_date):
  131. return time_diff(string_ed_date, string_st_date).seconds
  132. def time_diff_in_hours(string_ed_date, string_st_date):
  133. return round(float(time_diff(string_ed_date, string_st_date).seconds) / 3600, 6)
  134. def now_datetime():
  135. from datetime import datetime
  136. return convert_utc_to_user_timezone(datetime.utcnow())
  137. def get_user_time_zone():
  138. if getattr(webnotes.local, "user_time_zone", None) is None:
  139. webnotes.local.user_time_zone = webnotes.cache().get_value("time_zone")
  140. if not webnotes.local.user_time_zone:
  141. webnotes.local.user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') \
  142. or 'Asia/Calcutta'
  143. webnotes.cache().set_value("time_zone", webnotes.local.user_time_zone)
  144. return webnotes.local.user_time_zone
  145. def convert_utc_to_user_timezone(utc_timestamp):
  146. from pytz import timezone, UnknownTimeZoneError
  147. utcnow = timezone('UTC').localize(utc_timestamp)
  148. try:
  149. return utcnow.astimezone(timezone(get_user_time_zone()))
  150. except UnknownTimeZoneError:
  151. return utcnow
  152. def now():
  153. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  154. if getattr(webnotes.local, "current_date", None):
  155. return getdate(webnotes.local.current_date).strftime("%Y-%m-%d") + " " + \
  156. now_datetime().strftime('%H:%M:%S')
  157. else:
  158. return now_datetime().strftime('%Y-%m-%d %H:%M:%S')
  159. def nowdate():
  160. """return current date as yyyy-mm-dd"""
  161. return now_datetime().strftime('%Y-%m-%d')
  162. def today():
  163. return nowdate()
  164. def nowtime():
  165. """return current time in hh:mm"""
  166. return now_datetime().strftime('%H:%M')
  167. def get_first_day(dt, d_years=0, d_months=0):
  168. """
  169. Returns the first day of the month for the date specified by date object
  170. Also adds `d_years` and `d_months` if specified
  171. """
  172. import datetime
  173. dt = getdate(dt)
  174. # d_years, d_months are "deltas" to apply to dt
  175. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  176. year = dt.year + d_years + overflow_years
  177. return datetime.date(year, month + 1, 1)
  178. def get_last_day(dt):
  179. """
  180. Returns last day of the month using:
  181. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  182. """
  183. import datetime
  184. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  185. def get_datetime(datetime_str):
  186. from datetime import datetime
  187. if isinstance(datetime_str, datetime):
  188. return datetime_str.replace(microsecond=0, tzinfo=None)
  189. return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
  190. def get_datetime_str(datetime_obj):
  191. if isinstance(datetime_obj, basestring):
  192. datetime_obj = get_datetime(datetime_obj)
  193. return datetime_obj.strftime('%Y-%m-%d %H:%M:%S')
  194. def formatdate(string_date=None):
  195. """
  196. Convers the given string date to :data:`user_format`
  197. User format specified in :term:`Control Panel`
  198. Examples:
  199. * dd-mm-yyyy
  200. * mm-dd-yyyy
  201. * dd/mm/yyyy
  202. """
  203. if string_date:
  204. string_date = getdate(string_date)
  205. else:
  206. string_date = now_datetime().date()
  207. if getattr(webnotes.local, "user_format", None) is None:
  208. webnotes.local.user_format = webnotes.conn.get_default("date_format")
  209. out = webnotes.local.user_format
  210. return out.replace("dd", string_date.strftime("%d"))\
  211. .replace("mm", string_date.strftime("%m"))\
  212. .replace("yyyy", string_date.strftime("%Y"))
  213. def global_date_format(date):
  214. """returns date as 1 January 2012"""
  215. formatted_date = getdate(date).strftime("%d %B %Y")
  216. return formatted_date.startswith("0") and formatted_date[1:] or formatted_date
  217. def dict_to_str(args, sep='&'):
  218. """
  219. Converts a dictionary to URL
  220. """
  221. t = []
  222. for k in args.keys():
  223. t.append(str(k)+'='+urllib.quote(str(args[k] or '')))
  224. return sep.join(t)
  225. def timestamps_equal(t1, t2):
  226. """Returns true if same the two string timestamps are same"""
  227. scrub = lambda x: x.replace(':', ' ').replace('-',' ').split()
  228. t1, t2 = scrub(t1), scrub(t2)
  229. if len(t1) != len(t2):
  230. return
  231. for i in range(len(t1)):
  232. if t1[i]!=t2[i]:
  233. return
  234. return 1
  235. def has_common(l1, l2):
  236. """Returns truthy value if there are common elements in lists l1 and l2"""
  237. return set(l1) & set(l2)
  238. def flt(s, precision=None):
  239. """Convert to float (ignore commas)"""
  240. if isinstance(s, basestring):
  241. s = s.replace(',','')
  242. try:
  243. num = float(s)
  244. if precision is not None:
  245. num = _round(num, precision)
  246. except Exception:
  247. num = 0
  248. return num
  249. def cint(s):
  250. """Convert to integer"""
  251. try: num = int(float(s))
  252. except: num = 0
  253. return num
  254. def cstr(s):
  255. if isinstance(s, unicode):
  256. return s
  257. elif s==None:
  258. return ''
  259. elif isinstance(s, basestring):
  260. return unicode(s, 'utf-8')
  261. else:
  262. return unicode(s)
  263. def _round(num, precision=0):
  264. """round method for round halfs to nearest even algorithm"""
  265. precision = cint(precision)
  266. multiplier = 10 ** precision
  267. # avoid rounding errors
  268. num = round(num * multiplier if precision else num, 8)
  269. import math
  270. floor = math.floor(num)
  271. decimal_part = num - floor
  272. if decimal_part == 0.5:
  273. num = floor if (floor % 2 == 0) else floor + 1
  274. else:
  275. num = round(num)
  276. return (num / multiplier) if precision else num
  277. def encode(obj, encoding="utf-8"):
  278. if isinstance(obj, list):
  279. out = []
  280. for o in obj:
  281. if isinstance(o, unicode):
  282. out.append(o.encode(encoding))
  283. else:
  284. out.append(o)
  285. return out
  286. elif isinstance(obj, unicode):
  287. return obj.encode(encoding)
  288. else:
  289. return obj
  290. def parse_val(v):
  291. """Converts to simple datatypes from SQL query results"""
  292. import datetime
  293. if isinstance(v, (datetime.date, datetime.datetime)):
  294. v = unicode(v)
  295. elif isinstance(v, datetime.timedelta):
  296. v = ":".join(unicode(v).split(":")[:2])
  297. elif isinstance(v, long):
  298. v = int(v)
  299. return v
  300. def fmt_money(amount, precision=None, currency=None):
  301. """
  302. Convert to string with commas for thousands, millions etc
  303. """
  304. number_format = webnotes.conn.get_default("number_format") or "#,###.##"
  305. decimal_str, comma_str, precision = get_number_format_info(number_format)
  306. amount = '%.*f' % (precision, flt(amount))
  307. if amount.find('.') == -1:
  308. decimals = ''
  309. else:
  310. decimals = amount.split('.')[1]
  311. parts = []
  312. minus = ''
  313. if flt(amount) < 0:
  314. minus = '-'
  315. amount = cstr(abs(flt(amount))).split('.')[0]
  316. if len(amount) > 3:
  317. parts.append(amount[-3:])
  318. amount = amount[:-3]
  319. val = number_format=="#,##,###.##" and 2 or 3
  320. while len(amount) > val:
  321. parts.append(amount[-val:])
  322. amount = amount[:-val]
  323. parts.append(amount)
  324. parts.reverse()
  325. amount = comma_str.join(parts) + (precision and (decimal_str + decimals) or "")
  326. amount = minus + amount
  327. if currency:
  328. symbol = webnotes.conn.get_value("Currency", currency, "symbol")
  329. if symbol:
  330. amount = symbol + " " + amount
  331. return amount
  332. number_format_info = {
  333. "#.###": ("", ".", 0),
  334. "#,###": ("", ",", 0),
  335. "#,###.##": (".", ",", 2),
  336. "#,##,###.##": (".", ",", 2),
  337. "#.###,##": (",", ".", 2),
  338. "# ###.##": (".", " ", 2),
  339. "#,###.###": (".", ",", 3),
  340. }
  341. def get_number_format_info(format):
  342. return number_format_info.get(format) or (".", ",", 2)
  343. #
  344. # convet currency to words
  345. #
  346. def money_in_words(number, main_currency = None, fraction_currency=None):
  347. """
  348. Returns string in words with currency and fraction currency.
  349. """
  350. d = get_defaults()
  351. if not main_currency:
  352. main_currency = d.get('currency', 'INR')
  353. if not fraction_currency:
  354. fraction_currency = webnotes.conn.get_value("Currency", main_currency, "fraction") or "Cent"
  355. n = "%.2f" % flt(number)
  356. main, fraction = n.split('.')
  357. if len(fraction)==1: fraction += '0'
  358. number_format = webnotes.conn.get_value("Currency", main_currency, "number_format") or \
  359. webnotes.conn.get_default("number_format") or "#,###.##"
  360. in_million = True
  361. if number_format == "#,##,###.##": in_million = False
  362. out = main_currency + ' ' + in_words(main, in_million).title()
  363. if cint(fraction):
  364. out = out + ' and ' + in_words(fraction, in_million).title() + ' ' + fraction_currency
  365. return out + ' only.'
  366. #
  367. # convert number to words
  368. #
  369. def in_words(integer, in_million=True):
  370. """
  371. Returns string in words for the given integer.
  372. """
  373. n=int(integer)
  374. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  375. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  376. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  377. def psn(n, known, xpsn):
  378. import sys;
  379. if n in known: return known[n]
  380. bestguess, remainder = str(n), 0
  381. if n<=20:
  382. webnotes.errprint(sys.stderr)
  383. webnotes.errprint(n)
  384. webnotes.errprint("How did this happen?")
  385. assert 0
  386. elif n < 100:
  387. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  388. return bestguess
  389. elif n < 1000:
  390. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  391. remainder = n%100
  392. else:
  393. if in_million:
  394. if n < 1000000:
  395. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  396. remainder = n%1000
  397. elif n < 1000000000:
  398. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  399. remainder = n%1000000
  400. else:
  401. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  402. remainder = n%1000000000
  403. else:
  404. if n < 100000:
  405. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  406. remainder = n%1000
  407. elif n < 10000000:
  408. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  409. remainder = n%100000
  410. else:
  411. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  412. remainder = n%10000000
  413. if remainder:
  414. if remainder >= 100:
  415. comma = ','
  416. else:
  417. comma = ''
  418. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  419. else:
  420. return bestguess
  421. return psn(n, known, psn)
  422. # Get Defaults
  423. # ==============================================================================
  424. def get_defaults(key=None):
  425. """
  426. Get dictionary of default values from the :term:`Control Panel`, or a value if key is passed
  427. """
  428. return webnotes.conn.get_defaults(key)
  429. def set_default(key, val):
  430. """
  431. Set / add a default value to :term:`Control Panel`
  432. """
  433. return webnotes.conn.set_default(key, val)
  434. def remove_blanks(d):
  435. """
  436. Returns d with empty ('' or None) values stripped
  437. """
  438. empty_keys = []
  439. for key in d:
  440. if d[key]=='' or d[key]==None:
  441. # del d[key] raises runtime exception, using a workaround
  442. empty_keys.append(key)
  443. for key in empty_keys:
  444. del d[key]
  445. return d
  446. def pprint_dict(d, level=1, no_blanks=True):
  447. """
  448. Pretty print a dictionary with indents
  449. """
  450. if no_blanks:
  451. remove_blanks(d)
  452. # make indent
  453. indent, ret = '', ''
  454. for i in range(0,level): indent += '\t'
  455. # add lines
  456. comment, lines = '', []
  457. kl = d.keys()
  458. kl.sort()
  459. # make lines
  460. for key in kl:
  461. if key != '##comment':
  462. tmp = {key: d[key]}
  463. lines.append(indent + str(tmp)[1:-1] )
  464. # add comment string
  465. if '##comment' in kl:
  466. ret = ('\n' + indent) + '# ' + d['##comment'] + '\n'
  467. # open
  468. ret += indent + '{\n'
  469. # lines
  470. ret += indent + ',\n\t'.join(lines)
  471. # close
  472. ret += '\n' + indent + '}'
  473. return ret
  474. def get_common(d1,d2):
  475. """
  476. returns (list of keys) the common part of two dicts
  477. """
  478. return [p for p in d1 if p in d2 and d1[p]==d2[p]]
  479. def get_common_dict(d1, d2):
  480. """
  481. return common dictionary of d1 and d2
  482. """
  483. ret = {}
  484. for key in d1:
  485. if key in d2 and d2[key]==d1[key]:
  486. ret[key] = d1[key]
  487. return ret
  488. def get_diff_dict(d1, d2):
  489. """
  490. return common dictionary of d1 and d2
  491. """
  492. diff_keys = set(d2.keys()).difference(set(d1.keys()))
  493. ret = {}
  494. for d in diff_keys: ret[d] = d2[d]
  495. return ret
  496. def get_file_timestamp(fn):
  497. """
  498. Returns timestamp of the given file
  499. """
  500. from webnotes.utils import cint
  501. try:
  502. return str(cint(os.stat(fn).st_mtime))
  503. except OSError, e:
  504. if e.args[0]!=2:
  505. raise
  506. else:
  507. return None
  508. # to be deprecated
  509. def make_esc(esc_chars):
  510. """
  511. Function generator for Escaping special characters
  512. """
  513. return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
  514. # esc / unescape characters -- used for command line
  515. def esc(s, esc_chars):
  516. """
  517. Escape special characters
  518. """
  519. if not s:
  520. return ""
  521. for c in esc_chars:
  522. esc_str = '\\' + c
  523. s = s.replace(c, esc_str)
  524. return s
  525. def unesc(s, esc_chars):
  526. """
  527. UnEscape special characters
  528. """
  529. for c in esc_chars:
  530. esc_str = '\\' + c
  531. s = s.replace(esc_str, c)
  532. return s
  533. def is_html(text):
  534. out = False
  535. for key in ["<br>", "<p", "<img", "<div"]:
  536. if key in text:
  537. out = True
  538. break
  539. return out
  540. def strip_html(text):
  541. """
  542. removes anything enclosed in and including <>
  543. """
  544. return re.compile(r'<.*?>').sub('', text)
  545. def escape_html(text):
  546. html_escape_table = {
  547. "&": "&amp;",
  548. '"': "&quot;",
  549. "'": "&apos;",
  550. ">": "&gt;",
  551. "<": "&lt;",
  552. }
  553. return "".join(html_escape_table.get(c,c) for c in text)
  554. def get_doctype_label(dt=None):
  555. """
  556. Gets label of a doctype
  557. """
  558. if dt:
  559. res = webnotes.conn.sql("""\
  560. SELECT name, dt_label FROM `tabDocType Label`
  561. WHERE name=%s""", dt)
  562. return res and res[0][0] or dt
  563. else:
  564. res = webnotes.conn.sql("SELECT name, dt_label FROM `tabDocType Label`")
  565. dt_label_dict = {}
  566. for r in res:
  567. dt_label_dict[r[0]] = r[1]
  568. return dt_label_dict
  569. def get_label_doctype(label):
  570. """
  571. Gets doctype from its label
  572. """
  573. res = webnotes.conn.sql("""\
  574. SELECT name FROM `tabDocType Label`
  575. WHERE dt_label=%s""", label)
  576. return res and res[0][0] or label
  577. def pretty_date(iso_datetime):
  578. """
  579. Takes an ISO time and returns a string representing how
  580. long ago the date represents.
  581. Ported from PrettyDate by John Resig
  582. """
  583. if not iso_datetime: return ''
  584. from datetime import datetime
  585. import math
  586. if isinstance(iso_datetime, basestring):
  587. iso_datetime = datetime.strptime(iso_datetime, '%Y-%m-%d %H:%M:%S')
  588. now_dt = datetime.strptime(now(), '%Y-%m-%d %H:%M:%S')
  589. dt_diff = now_dt - iso_datetime
  590. # available only in python 2.7+
  591. # dt_diff_seconds = dt_diff.total_seconds()
  592. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  593. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  594. # differnt cases
  595. if dt_diff_seconds < 60.0:
  596. return 'just now'
  597. elif dt_diff_seconds < 120.0:
  598. return '1 minute ago'
  599. elif dt_diff_seconds < 3600.0:
  600. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  601. elif dt_diff_seconds < 7200.0:
  602. return '1 hour ago'
  603. elif dt_diff_seconds < 86400.0:
  604. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  605. elif dt_diff_days == 1.0:
  606. return 'Yesterday'
  607. elif dt_diff_days < 7.0:
  608. return '%s days ago' % cint(dt_diff_days)
  609. elif dt_diff_days < 31.0:
  610. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  611. elif dt_diff_days < 365.0:
  612. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  613. else:
  614. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))
  615. def execute_in_shell(cmd, verbose=0):
  616. # using Popen instead of os.system - as recommended by python docs
  617. from subprocess import Popen
  618. import tempfile
  619. with tempfile.TemporaryFile() as stdout:
  620. with tempfile.TemporaryFile() as stderr:
  621. p = Popen(cmd, shell=True, stdout=stdout, stderr=stderr)
  622. p.wait()
  623. stdout.seek(0)
  624. out = stdout.read()
  625. stderr.seek(0)
  626. err = stderr.read()
  627. if verbose:
  628. if err: print err
  629. if out: print out
  630. return err, out
  631. def comma_or(some_list):
  632. return comma_sep(some_list, " or ")
  633. def comma_and(some_list):
  634. return comma_sep(some_list, " and ")
  635. def comma_sep(some_list, sep):
  636. if isinstance(some_list, (list, tuple)):
  637. # list(some_list) is done to preserve the existing list
  638. some_list = [unicode(s) for s in list(some_list)]
  639. if not some_list:
  640. return ""
  641. elif len(some_list) == 1:
  642. return some_list[0]
  643. else:
  644. some_list = ["'%s'" % s for s in some_list]
  645. return ", ".join(some_list[:-1]) + sep + some_list[-1]
  646. else:
  647. return some_list
  648. def filter_strip_join(some_list, sep):
  649. """given a list, filter None values, strip spaces and join"""
  650. return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
  651. def get_path(*path, **kwargs):
  652. base = kwargs.get('base')
  653. if not base:
  654. base = webnotes.local.site_path
  655. return os.path.join(base, *path)
  656. def get_site_base_path(sites_dir=None, hostname=None):
  657. return webnotes.local.site_path
  658. def get_site_path(*path):
  659. return get_path(base=get_site_base_path(), *path)
  660. def get_files_path():
  661. return get_site_path("public", "files")
  662. def get_backups_path():
  663. return get_site_path("public", "backup")
  664. def get_url(uri=None):
  665. url = get_request_site_address()
  666. if not url or "localhost" in url:
  667. subdomain = webnotes.conn.get_value("Website Settings", "Website Settings",
  668. "subdomain")
  669. if subdomain:
  670. if "http" not in subdomain:
  671. url = "http://" + subdomain
  672. if uri:
  673. url = urllib.basejoin(url, uri)
  674. return url
  675. def get_url_to_form(doctype, name, base_url=None, label=None):
  676. if not base_url:
  677. base_url = get_url()
  678. if not label: label = name
  679. return """<a href="%(base_url)s/app.html#!Form/%(doctype)s/%(name)s">%(label)s</a>""" % locals()
  680. def encode_dict(d, encoding="utf-8"):
  681. for key in d:
  682. if isinstance(d[key], basestring) and isinstance(d[key], unicode):
  683. d[key] = d[key].encode(encoding)
  684. return d
  685. def decode_dict(d, encoding="utf-8"):
  686. for key in d:
  687. if isinstance(d[key], basestring) and not isinstance(d[key], unicode):
  688. d[key] = d[key].decode(encoding, "ignore")
  689. return d
  690. import operator
  691. operator_map = {
  692. # startswith
  693. "^": lambda (a, b): (a or "").startswith(b),
  694. # in or not in a list
  695. "in": lambda (a, b): operator.contains(b, a),
  696. "not in": lambda (a, b): not operator.contains(b, a),
  697. # comparison operators
  698. "=": lambda (a, b): operator.eq(a, b),
  699. "!=": lambda (a, b): operator.ne(a, b),
  700. ">": lambda (a, b): operator.gt(a, b),
  701. "<": lambda (a, b): operator.lt(a, b),
  702. ">=": lambda (a, b): operator.ge(a, b),
  703. "<=": lambda (a, b): operator.le(a, b),
  704. "not None": lambda (a, b): a and True or False,
  705. "None": lambda (a, b): (not a) and True or False
  706. }
  707. def compare(val1, condition, val2):
  708. ret = False
  709. if condition in operator_map:
  710. ret = operator_map[condition]((val1, val2))
  711. return ret
  712. def get_site_name(hostname):
  713. return hostname.split(':')[0]
  714. def get_disk_usage():
  715. """get disk usage of files folder"""
  716. files_path = get_files_path()
  717. if not os.path.exists(files_path):
  718. return 0
  719. err, out = execute_in_shell("du -hsm {files_path}".format(files_path=files_path))
  720. return cint(out.split("\n")[-2].split("\t")[0])
  721. def scrub_urls(html):
  722. html = expand_relative_urls(html)
  723. html = quote_urls(html)
  724. return html
  725. def expand_relative_urls(html):
  726. # expand relative urls
  727. url = get_url()
  728. if not url.endswith("/"): url += "/"
  729. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?!http)[^\'" >]+)([\'"]?)',
  730. '\g<1>\g<2>{}\g<3>\g<4>'.format(url), html)
  731. def quote_urls(html):
  732. def _quote_url(match):
  733. groups = list(match.groups())
  734. groups[2] = urllib.quote(groups[2], safe="/:")
  735. return "".join(groups)
  736. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?:http)[^\'">]+)([\'"]?)',
  737. _quote_url, html)
  738. def touch_file(path):
  739. with open(path, 'a'):
  740. os.utime(path, None)
  741. return True
  742. def get_test_client():
  743. from webnotes.app import application
  744. return Client(application)