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.
 
 
 
 
 
 

608 rivejä
17 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. # IMPORTANT: only import safe functions as this module will be included in jinja environment
  5. import frappe
  6. import operator
  7. import re, urllib, datetime, math
  8. import babel.dates
  9. # datetime functions
  10. def getdate(string_date):
  11. """
  12. Coverts string date (yyyy-mm-dd) to datetime.date object
  13. """
  14. if isinstance(string_date, datetime.date):
  15. return string_date
  16. elif isinstance(string_date, datetime.datetime):
  17. return string_date.date()
  18. if " " in string_date:
  19. string_date = string_date.split(" ")[0]
  20. return datetime.datetime.strptime(string_date, "%Y-%m-%d").date()
  21. def add_to_date(date, years=0, months=0, days=0):
  22. """Adds `days` to the given date"""
  23. format = isinstance(date, basestring)
  24. if date:
  25. date = getdate(date)
  26. else:
  27. raise Exception, "Start date required"
  28. from dateutil.relativedelta import relativedelta
  29. date += relativedelta(years=years, months=months, days=days)
  30. if format:
  31. return date.strftime("%Y-%m-%d")
  32. else:
  33. return date
  34. def add_days(date, days):
  35. return add_to_date(date, days=days)
  36. def add_months(date, months):
  37. return add_to_date(date, months=months)
  38. def add_years(date, years):
  39. return add_to_date(date, years=years)
  40. def date_diff(string_ed_date, string_st_date):
  41. return (getdate(string_ed_date) - getdate(string_st_date)).days
  42. def time_diff(string_ed_date, string_st_date):
  43. return get_datetime(string_ed_date) - get_datetime(string_st_date)
  44. def time_diff_in_seconds(string_ed_date, string_st_date):
  45. return time_diff(string_ed_date, string_st_date).total_seconds()
  46. def time_diff_in_hours(string_ed_date, string_st_date):
  47. return round(float(time_diff(string_ed_date, string_st_date).total_seconds()) / 3600, 6)
  48. def now_datetime():
  49. return convert_utc_to_user_timezone(datetime.datetime.utcnow())
  50. def get_user_time_zone():
  51. if getattr(frappe.local, "user_time_zone", None) is None:
  52. frappe.local.user_time_zone = frappe.cache().get_value("time_zone")
  53. if not frappe.local.user_time_zone:
  54. frappe.local.user_time_zone = frappe.db.get_default('time_zone') or 'Asia/Calcutta'
  55. frappe.cache().set_value("time_zone", frappe.local.user_time_zone)
  56. return frappe.local.user_time_zone
  57. def convert_utc_to_user_timezone(utc_timestamp):
  58. from pytz import timezone, UnknownTimeZoneError
  59. utcnow = timezone('UTC').localize(utc_timestamp)
  60. try:
  61. return utcnow.astimezone(timezone(get_user_time_zone()))
  62. except UnknownTimeZoneError:
  63. return utcnow
  64. def now():
  65. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  66. if getattr(frappe.local, "current_date", None):
  67. return getdate(frappe.local.current_date).strftime("%Y-%m-%d") + " " + \
  68. now_datetime().strftime('%H:%M:%S.%f')
  69. else:
  70. return now_datetime().strftime('%Y-%m-%d %H:%M:%S.%f')
  71. def nowdate():
  72. """return current date as yyyy-mm-dd"""
  73. return now_datetime().strftime('%Y-%m-%d')
  74. def today():
  75. return nowdate()
  76. def nowtime():
  77. """return current time in hh:mm"""
  78. return now_datetime().strftime('%H:%M:%S.%f')
  79. def get_first_day(dt, d_years=0, d_months=0):
  80. """
  81. Returns the first day of the month for the date specified by date object
  82. Also adds `d_years` and `d_months` if specified
  83. """
  84. dt = getdate(dt)
  85. # d_years, d_months are "deltas" to apply to dt
  86. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  87. year = dt.year + d_years + overflow_years
  88. return datetime.date(year, month + 1, 1)
  89. def get_last_day(dt):
  90. """
  91. Returns last day of the month using:
  92. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  93. """
  94. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  95. def get_datetime(datetime_str):
  96. try:
  97. return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S.%f')
  98. except TypeError:
  99. if isinstance(datetime_str, datetime.datetime):
  100. return datetime_str.replace(tzinfo=None)
  101. else:
  102. raise
  103. except ValueError:
  104. if datetime_str=='0000-00-00 00:00:00.000000':
  105. return None
  106. return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
  107. def get_datetime_str(datetime_obj):
  108. if isinstance(datetime_obj, basestring):
  109. datetime_obj = get_datetime(datetime_obj)
  110. return datetime_obj.strftime('%Y-%m-%d %H:%M:%S.%f')
  111. def formatdate(string_date=None, format_string=None):
  112. """
  113. Convers the given string date to :data:`user_format`
  114. User format specified in defaults
  115. Examples:
  116. * dd-mm-yyyy
  117. * mm-dd-yyyy
  118. * dd/mm/yyyy
  119. """
  120. date = getdate(string_date) if string_date else now_datetime().date()
  121. if format_string:
  122. return babel.dates.format_date(date, format_string or "medium", locale=(frappe.local.lang or "").replace("-", "_"))
  123. else:
  124. if getattr(frappe.local, "user_format", None) is None:
  125. frappe.local.user_format = frappe.db.get_default("date_format")
  126. out = frappe.local.user_format or "yyyy-mm-dd"
  127. try:
  128. return out.replace("dd", date.strftime("%d"))\
  129. .replace("mm", date.strftime("%m"))\
  130. .replace("yyyy", date.strftime("%Y"))
  131. except ValueError, e:
  132. raise frappe.ValidationError, str(e)
  133. def global_date_format(date):
  134. """returns date as 1 January 2012"""
  135. formatted_date = getdate(date).strftime("%d %B %Y")
  136. return formatted_date.startswith("0") and formatted_date[1:] or formatted_date
  137. def has_common(l1, l2):
  138. """Returns truthy value if there are common elements in lists l1 and l2"""
  139. return set(l1) & set(l2)
  140. def flt(s, precision=None):
  141. """Convert to float (ignore commas)"""
  142. if isinstance(s, basestring):
  143. s = s.replace(',','')
  144. try:
  145. num = float(s)
  146. if precision is not None:
  147. num = rounded(num, precision)
  148. except Exception:
  149. num = 0
  150. return num
  151. def cint(s):
  152. """Convert to integer"""
  153. try: num = int(float(s))
  154. except: num = 0
  155. return num
  156. def cstr(s):
  157. if isinstance(s, unicode):
  158. return s
  159. elif s==None:
  160. return ''
  161. elif isinstance(s, basestring):
  162. return unicode(s, 'utf-8')
  163. else:
  164. return unicode(s)
  165. def rounded(num, precision=0):
  166. """round method for round halfs to nearest even algorithm"""
  167. precision = cint(precision)
  168. multiplier = 10 ** precision
  169. # avoid rounding errors
  170. num = round(num * multiplier if precision else num, 8)
  171. floor = math.floor(num)
  172. decimal_part = num - floor
  173. if decimal_part == 0.5:
  174. num = floor if (floor % 2 == 0) else floor + 1
  175. else:
  176. num = round(num)
  177. return (num / multiplier) if precision else num
  178. def encode(obj, encoding="utf-8"):
  179. if isinstance(obj, list):
  180. out = []
  181. for o in obj:
  182. if isinstance(o, unicode):
  183. out.append(o.encode(encoding))
  184. else:
  185. out.append(o)
  186. return out
  187. elif isinstance(obj, unicode):
  188. return obj.encode(encoding)
  189. else:
  190. return obj
  191. def parse_val(v):
  192. """Converts to simple datatypes from SQL query results"""
  193. if isinstance(v, (datetime.date, datetime.datetime)):
  194. v = unicode(v)
  195. elif isinstance(v, datetime.timedelta):
  196. v = ":".join(unicode(v).split(":")[:2])
  197. elif isinstance(v, long):
  198. v = int(v)
  199. return v
  200. def fmt_money(amount, precision=None, currency=None):
  201. """
  202. Convert to string with commas for thousands, millions etc
  203. """
  204. number_format = None
  205. if currency:
  206. number_format = frappe.db.get_value("Currency", currency, "number_format")
  207. if not number_format:
  208. number_format = frappe.db.get_default("number_format") or "#,###.##"
  209. decimal_str, comma_str, number_format_precision = get_number_format_info(number_format)
  210. if precision is None:
  211. precision = number_format_precision
  212. amount = '%.*f' % (precision, flt(amount))
  213. if amount.find('.') == -1:
  214. decimals = ''
  215. else:
  216. decimals = amount.split('.')[1]
  217. parts = []
  218. minus = ''
  219. if flt(amount) < 0:
  220. minus = '-'
  221. amount = cstr(abs(flt(amount))).split('.')[0]
  222. if len(amount) > 3:
  223. parts.append(amount[-3:])
  224. amount = amount[:-3]
  225. val = number_format=="#,##,###.##" and 2 or 3
  226. while len(amount) > val:
  227. parts.append(amount[-val:])
  228. amount = amount[:-val]
  229. parts.append(amount)
  230. parts.reverse()
  231. amount = comma_str.join(parts) + ((precision and decimal_str) and (decimal_str + decimals) or "")
  232. amount = minus + amount
  233. if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes":
  234. symbol = frappe.db.get_value("Currency", currency, "symbol") or currency
  235. amount = symbol + " " + amount
  236. return amount
  237. number_format_info = {
  238. "#,###.##": (".", ",", 2),
  239. "#.###,##": (",", ".", 2),
  240. "# ###.##": (".", " ", 2),
  241. "# ###,##": (",", " ", 2),
  242. "#'###.##": (".", "'", 2),
  243. "#, ###.##": (".", ", ", 2),
  244. "#,##,###.##": (".", ",", 2),
  245. "#,###.###": (".", ",", 3),
  246. "#.###": ("", ".", 0),
  247. "#,###": ("", ",", 0)
  248. }
  249. def get_number_format_info(format):
  250. return number_format_info.get(format) or (".", ",", 2)
  251. #
  252. # convet currency to words
  253. #
  254. def money_in_words(number, main_currency = None, fraction_currency=None):
  255. """
  256. Returns string in words with currency and fraction currency.
  257. """
  258. from frappe.utils import get_defaults
  259. if not number or flt(number) < 0:
  260. return ""
  261. d = get_defaults()
  262. if not main_currency:
  263. main_currency = d.get('currency', 'INR')
  264. if not fraction_currency:
  265. fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction") or "Cent"
  266. n = "%.2f" % flt(number)
  267. main, fraction = n.split('.')
  268. if len(fraction)==1: fraction += '0'
  269. number_format = frappe.db.get_value("Currency", main_currency, "number_format") or \
  270. frappe.db.get_default("number_format") or "#,###.##"
  271. in_million = True
  272. if number_format == "#,##,###.##": in_million = False
  273. out = main_currency + ' ' + in_words(main, in_million).title()
  274. if cint(fraction):
  275. out = out + ' and ' + in_words(fraction, in_million).title() + ' ' + fraction_currency
  276. return out + ' only.'
  277. #
  278. # convert number to words
  279. #
  280. def in_words(integer, in_million=True):
  281. """
  282. Returns string in words for the given integer.
  283. """
  284. n=int(integer)
  285. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  286. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  287. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  288. def psn(n, known, xpsn):
  289. import sys;
  290. if n in known: return known[n]
  291. bestguess, remainder = str(n), 0
  292. if n<=20:
  293. frappe.errprint(sys.stderr)
  294. frappe.errprint(n)
  295. frappe.errprint("How did this happen?")
  296. assert 0
  297. elif n < 100:
  298. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  299. return bestguess
  300. elif n < 1000:
  301. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  302. remainder = n%100
  303. else:
  304. if in_million:
  305. if n < 1000000:
  306. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  307. remainder = n%1000
  308. elif n < 1000000000:
  309. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  310. remainder = n%1000000
  311. else:
  312. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  313. remainder = n%1000000000
  314. else:
  315. if n < 100000:
  316. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  317. remainder = n%1000
  318. elif n < 10000000:
  319. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  320. remainder = n%100000
  321. else:
  322. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  323. remainder = n%10000000
  324. if remainder:
  325. if remainder >= 100:
  326. comma = ','
  327. else:
  328. comma = ''
  329. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  330. else:
  331. return bestguess
  332. return psn(n, known, psn)
  333. def is_html(text):
  334. out = False
  335. for key in ["<br>", "<p", "<img", "<div"]:
  336. if key in text:
  337. out = True
  338. break
  339. return out
  340. # from Jinja2 code
  341. _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
  342. def strip_html(text):
  343. """removes anything enclosed in and including <>"""
  344. return _striptags_re.sub("", text)
  345. def escape_html(text):
  346. html_escape_table = {
  347. "&": "&amp;",
  348. '"': "&quot;",
  349. "'": "&apos;",
  350. ">": "&gt;",
  351. "<": "&lt;",
  352. }
  353. return "".join(html_escape_table.get(c,c) for c in text)
  354. def pretty_date(iso_datetime):
  355. """
  356. Takes an ISO time and returns a string representing how
  357. long ago the date represents.
  358. Ported from PrettyDate by John Resig
  359. """
  360. if not iso_datetime: return ''
  361. import math
  362. if isinstance(iso_datetime, basestring):
  363. iso_datetime = datetime.datetime.strptime(iso_datetime, '%Y-%m-%d %H:%M:%S.%f')
  364. now_dt = datetime.datetime.strptime(now(), '%Y-%m-%d %H:%M:%S.%f')
  365. dt_diff = now_dt - iso_datetime
  366. # available only in python 2.7+
  367. # dt_diff_seconds = dt_diff.total_seconds()
  368. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  369. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  370. # differnt cases
  371. if dt_diff_seconds < 60.0:
  372. return 'just now'
  373. elif dt_diff_seconds < 120.0:
  374. return '1 minute ago'
  375. elif dt_diff_seconds < 3600.0:
  376. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  377. elif dt_diff_seconds < 7200.0:
  378. return '1 hour ago'
  379. elif dt_diff_seconds < 86400.0:
  380. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  381. elif dt_diff_days == 1.0:
  382. return 'Yesterday'
  383. elif dt_diff_days < 7.0:
  384. return '%s days ago' % cint(dt_diff_days)
  385. elif dt_diff_days < 31.0:
  386. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  387. elif dt_diff_days < 365.0:
  388. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  389. else:
  390. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))
  391. def comma_or(some_list):
  392. return comma_sep(some_list, " or ")
  393. def comma_and(some_list):
  394. return comma_sep(some_list, " and ")
  395. def comma_sep(some_list, sep):
  396. if isinstance(some_list, (list, tuple)):
  397. # list(some_list) is done to preserve the existing list
  398. some_list = [unicode(s) for s in list(some_list)]
  399. if not some_list:
  400. return ""
  401. elif len(some_list) == 1:
  402. return some_list[0]
  403. else:
  404. some_list = ["'%s'" % s for s in some_list]
  405. return ", ".join(some_list[:-1]) + sep + some_list[-1]
  406. else:
  407. return some_list
  408. def filter_strip_join(some_list, sep):
  409. """given a list, filter None values, strip spaces and join"""
  410. return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
  411. def get_url(uri=None, full_address=False):
  412. """get app url from request"""
  413. host_name = frappe.local.conf.host_name
  414. if not host_name:
  415. if hasattr(frappe.local, "request") and frappe.local.request and frappe.local.request.host:
  416. protocol = 'https' == frappe.get_request_header('X-Forwarded-Proto', "") and 'https://' or 'http://'
  417. host_name = protocol + frappe.local.request.host
  418. elif frappe.local.site:
  419. host_name = "http://{}".format(frappe.local.site)
  420. else:
  421. host_name = frappe.db.get_value("Website Settings", "Website Settings",
  422. "subdomain")
  423. if host_name and "http" not in host_name:
  424. host_name = "http://" + host_name
  425. if not host_name:
  426. host_name = "http://localhost"
  427. if not uri and full_address:
  428. uri = frappe.get_request_header("REQUEST_URI", "")
  429. url = urllib.basejoin(host_name, uri) if uri else host_name
  430. return url
  431. def get_url_to_form(doctype, name, label=None):
  432. if not label: label = name
  433. return """<a href="/desk#!Form/%(doctype)s/%(name)s">%(label)s</a>""" % locals()
  434. operator_map = {
  435. # startswith
  436. "^": lambda (a, b): (a or "").startswith(b),
  437. # in or not in a list
  438. "in": lambda (a, b): operator.contains(b, a),
  439. "not in": lambda (a, b): not operator.contains(b, a),
  440. # comparison operators
  441. "=": lambda (a, b): operator.eq(a, b),
  442. "!=": lambda (a, b): operator.ne(a, b),
  443. ">": lambda (a, b): operator.gt(a, b),
  444. "<": lambda (a, b): operator.lt(a, b),
  445. ">=": lambda (a, b): operator.ge(a, b),
  446. "<=": lambda (a, b): operator.le(a, b),
  447. "not None": lambda (a, b): a and True or False,
  448. "None": lambda (a, b): (not a) and True or False
  449. }
  450. def compare(val1, condition, val2):
  451. ret = False
  452. if condition in operator_map:
  453. ret = operator_map[condition]((val1, val2))
  454. return ret
  455. def scrub_urls(html):
  456. html = expand_relative_urls(html)
  457. html = quote_urls(html)
  458. return html
  459. def expand_relative_urls(html):
  460. # expand relative urls
  461. url = get_url()
  462. if url.endswith("/"): url = url[:-1]
  463. def _expand_relative_urls(match):
  464. to_expand = list(match.groups())
  465. if not to_expand[2].startswith("/"):
  466. to_expand[2] = "/" + to_expand[2]
  467. to_expand.insert(2, url)
  468. return "".join(to_expand)
  469. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?!http)[^\'" >]+)([\'"]?)', _expand_relative_urls, html)
  470. def quoted(url):
  471. return cstr(urllib.quote(encode(url), safe=b"~@#$&()*!+=:;,.?/'"))
  472. def quote_urls(html):
  473. def _quote_url(match):
  474. groups = list(match.groups())
  475. groups[2] = quoted(groups[2])
  476. return "".join(groups)
  477. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?:http)[^\'">]+)([\'"]?)',
  478. _quote_url, html)
  479. def unique(seq):
  480. """use this instead of list(set()) to preserve order of the original list.
  481. Thanks to Stackoverflow: http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order"""
  482. seen = set()
  483. seen_add = seen.add
  484. return [ x for x in seq if not (x in seen or seen_add(x)) ]
  485. def strip(val, chars=None):
  486. # \ufeff is no-width-break, \u200b is no-width-space
  487. return (val or "").replace("\ufeff", "").replace("\u200b", "").strip(chars)