No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1501 líneas
42 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. import operator
  6. import json
  7. import re, datetime, math, time
  8. from six.moves.urllib.parse import quote, urljoin
  9. from six import iteritems, text_type, string_types, integer_types
  10. from frappe.desk.utils import slug
  11. DATE_FORMAT = "%Y-%m-%d"
  12. TIME_FORMAT = "%H:%M:%S.%f"
  13. DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT
  14. def is_invalid_date_string(date_string):
  15. # dateutil parser does not agree with dates like "0001-01-01" or "0000-00-00"
  16. return (not date_string) or (date_string or "").startswith(("0001-01-01", "0000-00-00"))
  17. # datetime functions
  18. def getdate(string_date=None):
  19. """
  20. Converts string date (yyyy-mm-dd) to datetime.date object.
  21. If no input is provided, current date is returned.
  22. """
  23. from dateutil import parser
  24. from dateutil.parser._parser import ParserError
  25. if not string_date:
  26. return get_datetime().date()
  27. if isinstance(string_date, datetime.datetime):
  28. return string_date.date()
  29. elif isinstance(string_date, datetime.date):
  30. return string_date
  31. if is_invalid_date_string(string_date):
  32. return None
  33. try:
  34. return parser.parse(string_date).date()
  35. except ParserError:
  36. frappe.throw(frappe._('{} is not a valid date string.').format(
  37. frappe.bold(string_date)
  38. ), title=frappe._('Invalid Date'))
  39. def get_datetime(datetime_str=None):
  40. from dateutil import parser
  41. if datetime_str is None:
  42. return now_datetime()
  43. if isinstance(datetime_str, (datetime.datetime, datetime.timedelta)):
  44. return datetime_str
  45. elif isinstance(datetime_str, (list, tuple)):
  46. return datetime.datetime(datetime_str)
  47. elif isinstance(datetime_str, datetime.date):
  48. return datetime.datetime.combine(datetime_str, datetime.time())
  49. if is_invalid_date_string(datetime_str):
  50. return None
  51. try:
  52. return datetime.datetime.strptime(datetime_str, DATETIME_FORMAT)
  53. except ValueError:
  54. return parser.parse(datetime_str)
  55. def to_timedelta(time_str):
  56. from dateutil import parser
  57. if isinstance(time_str, string_types):
  58. t = parser.parse(time_str)
  59. return datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond)
  60. else:
  61. return time_str
  62. def add_to_date(date, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, as_string=False, as_datetime=False):
  63. """Adds `days` to the given date"""
  64. from dateutil import parser
  65. from dateutil.parser._parser import ParserError
  66. from dateutil.relativedelta import relativedelta
  67. if date==None:
  68. date = now_datetime()
  69. if hours:
  70. as_datetime = True
  71. if isinstance(date, string_types):
  72. as_string = True
  73. if " " in date:
  74. as_datetime = True
  75. try:
  76. date = parser.parse(date)
  77. except ParserError:
  78. frappe.throw(frappe._("Please select a valid date filter"), title=frappe._("Invalid Date"))
  79. date = date + relativedelta(years=years, months=months, weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds)
  80. if as_string:
  81. if as_datetime:
  82. return date.strftime(DATETIME_FORMAT)
  83. else:
  84. return date.strftime(DATE_FORMAT)
  85. else:
  86. return date
  87. def add_days(date, days):
  88. return add_to_date(date, days=days)
  89. def add_months(date, months):
  90. return add_to_date(date, months=months)
  91. def add_years(date, years):
  92. return add_to_date(date, years=years)
  93. def date_diff(string_ed_date, string_st_date):
  94. return (getdate(string_ed_date) - getdate(string_st_date)).days
  95. def month_diff(string_ed_date, string_st_date):
  96. ed_date = getdate(string_ed_date)
  97. st_date = getdate(string_st_date)
  98. return (ed_date.year - st_date.year) * 12 + ed_date.month - st_date.month + 1
  99. def time_diff(string_ed_date, string_st_date):
  100. return get_datetime(string_ed_date) - get_datetime(string_st_date)
  101. def time_diff_in_seconds(string_ed_date, string_st_date):
  102. return time_diff(string_ed_date, string_st_date).total_seconds()
  103. def time_diff_in_hours(string_ed_date, string_st_date):
  104. return round(float(time_diff(string_ed_date, string_st_date).total_seconds()) / 3600, 6)
  105. def now_datetime():
  106. dt = convert_utc_to_user_timezone(datetime.datetime.utcnow())
  107. return dt.replace(tzinfo=None)
  108. def get_timestamp(date):
  109. return time.mktime(getdate(date).timetuple())
  110. def get_eta(from_time, percent_complete):
  111. diff = time_diff(now_datetime(), from_time).total_seconds()
  112. return str(datetime.timedelta(seconds=(100 - percent_complete) / percent_complete * diff))
  113. def _get_time_zone():
  114. return frappe.db.get_system_setting('time_zone') or 'Asia/Kolkata' # Default to India ?!
  115. def get_time_zone():
  116. if frappe.local.flags.in_test:
  117. return _get_time_zone()
  118. return frappe.cache().get_value("time_zone", _get_time_zone)
  119. def convert_utc_to_timezone(utc_timestamp, time_zone):
  120. from pytz import timezone, UnknownTimeZoneError
  121. utcnow = timezone('UTC').localize(utc_timestamp)
  122. try:
  123. return utcnow.astimezone(timezone(time_zone))
  124. except UnknownTimeZoneError:
  125. return utcnow
  126. def get_datetime_in_timezone(time_zone):
  127. utc_timestamp = datetime.datetime.utcnow()
  128. return convert_utc_to_timezone(utc_timestamp, time_zone)
  129. def convert_utc_to_user_timezone(utc_timestamp):
  130. time_zone = get_time_zone()
  131. return convert_utc_to_timezone(utc_timestamp, time_zone)
  132. def now():
  133. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  134. if frappe.flags.current_date:
  135. return getdate(frappe.flags.current_date).strftime(DATE_FORMAT) + " " + \
  136. now_datetime().strftime(TIME_FORMAT)
  137. else:
  138. return now_datetime().strftime(DATETIME_FORMAT)
  139. def nowdate():
  140. """return current date as yyyy-mm-dd"""
  141. return now_datetime().strftime(DATE_FORMAT)
  142. def today():
  143. return nowdate()
  144. def get_abbr(string, max_len=2):
  145. abbr=''
  146. for part in string.split(' '):
  147. if len(abbr) < max_len and part:
  148. abbr += part[0]
  149. return abbr or '?'
  150. def nowtime():
  151. """return current time in hh:mm"""
  152. return now_datetime().strftime(TIME_FORMAT)
  153. def get_first_day(dt, d_years=0, d_months=0, as_str=False):
  154. """
  155. Returns the first day of the month for the date specified by date object
  156. Also adds `d_years` and `d_months` if specified
  157. """
  158. dt = getdate(dt)
  159. # d_years, d_months are "deltas" to apply to dt
  160. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  161. year = dt.year + d_years + overflow_years
  162. return datetime.date(year, month + 1, 1).strftime(DATE_FORMAT) if as_str else datetime.date(year, month + 1, 1)
  163. def get_quarter_start(dt, as_str=False):
  164. date = getdate(dt)
  165. quarter = (date.month - 1) // 3 + 1
  166. first_date_of_quarter = datetime.date(date.year, ((quarter - 1) * 3) + 1, 1)
  167. return first_date_of_quarter.strftime(DATE_FORMAT) if as_str else first_date_of_quarter
  168. def get_first_day_of_week(dt, as_str=False):
  169. dt = getdate(dt)
  170. date = dt - datetime.timedelta(days=dt.weekday())
  171. return date.strftime(DATE_FORMAT) if as_str else date
  172. def get_year_start(dt, as_str=False):
  173. dt = getdate(dt)
  174. date = datetime.date(dt.year, 1, 1)
  175. return date.strftime(DATE_FORMAT) if as_str else date
  176. def get_last_day_of_week(dt):
  177. dt = get_first_day_of_week(dt)
  178. return dt + datetime.timedelta(days=6)
  179. def get_last_day(dt):
  180. """
  181. Returns last day of the month using:
  182. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  183. """
  184. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  185. def get_quarter_ending(date):
  186. date = getdate(date)
  187. # find the earliest quarter ending date that is after
  188. # the given date
  189. for month in (3, 6, 9, 12):
  190. quarter_end_month = getdate('{}-{}-01'.format(date.year, month))
  191. quarter_end_date = getdate(get_last_day(quarter_end_month))
  192. if date <= quarter_end_date:
  193. date = quarter_end_date
  194. break
  195. return date
  196. def get_year_ending(date):
  197. ''' returns year ending of the given date '''
  198. date = getdate(date)
  199. # first day of next year (note year starts from 1)
  200. date = add_to_date('{}-01-01'.format(date.year), months = 12)
  201. # last day of this month
  202. return add_to_date(date, days=-1)
  203. def get_time(time_str):
  204. from dateutil import parser
  205. if isinstance(time_str, datetime.datetime):
  206. return time_str.time()
  207. elif isinstance(time_str, datetime.time):
  208. return time_str
  209. else:
  210. if isinstance(time_str, datetime.timedelta):
  211. time_str = str(time_str)
  212. return parser.parse(time_str).time()
  213. def get_datetime_str(datetime_obj):
  214. if isinstance(datetime_obj, string_types):
  215. datetime_obj = get_datetime(datetime_obj)
  216. return datetime_obj.strftime(DATETIME_FORMAT)
  217. def get_date_str(date_obj):
  218. if isinstance(date_obj, string_types):
  219. date_obj = get_datetime(date_obj)
  220. return date_obj.strftime(DATE_FORMAT)
  221. def get_time_str(timedelta_obj):
  222. if isinstance(timedelta_obj, string_types):
  223. timedelta_obj = to_timedelta(timedelta_obj)
  224. hours, remainder = divmod(timedelta_obj.seconds, 3600)
  225. minutes, seconds = divmod(remainder, 60)
  226. return "{0}:{1}:{2}".format(hours, minutes, seconds)
  227. def get_user_date_format():
  228. """Get the current user date format. The result will be cached."""
  229. if getattr(frappe.local, "user_date_format", None) is None:
  230. frappe.local.user_date_format = frappe.db.get_default("date_format")
  231. return frappe.local.user_date_format or "yyyy-mm-dd"
  232. get_user_format = get_user_date_format # for backwards compatibility
  233. def get_user_time_format():
  234. """Get the current user time format. The result will be cached."""
  235. if getattr(frappe.local, "user_time_format", None) is None:
  236. frappe.local.user_time_format = frappe.db.get_default("time_format")
  237. return frappe.local.user_time_format or "HH:mm:ss"
  238. def format_date(string_date=None, format_string=None):
  239. """Converts the given string date to :data:`user_date_format`
  240. User format specified in defaults
  241. Examples:
  242. * dd-mm-yyyy
  243. * mm-dd-yyyy
  244. * dd/mm/yyyy
  245. """
  246. import babel.dates
  247. from babel.core import UnknownLocaleError
  248. if not string_date:
  249. return ''
  250. date = getdate(string_date)
  251. if not format_string:
  252. format_string = get_user_date_format()
  253. format_string = format_string.replace("mm", "MM")
  254. try:
  255. formatted_date = babel.dates.format_date(
  256. date, format_string,
  257. locale=(frappe.local.lang or "").replace("-", "_"))
  258. except UnknownLocaleError:
  259. format_string = format_string.replace("MM", "%m").replace("dd", "%d").replace("yyyy", "%Y")
  260. formatted_date = date.strftime(format_string)
  261. return formatted_date
  262. formatdate = format_date # For backwards compatibility
  263. def format_time(time_string=None, format_string=None):
  264. """Converts the given string time to :data:`user_time_format`
  265. User format specified in defaults
  266. Examples:
  267. * HH:mm:ss
  268. * HH:mm
  269. """
  270. import babel.dates
  271. from babel.core import UnknownLocaleError
  272. if not time_string:
  273. return ''
  274. time_ = get_time(time_string)
  275. if not format_string:
  276. format_string = get_user_time_format()
  277. try:
  278. formatted_time = babel.dates.format_time(
  279. time_, format_string,
  280. locale=(frappe.local.lang or "").replace("-", "_"))
  281. except UnknownLocaleError:
  282. formatted_time = time_.strftime("%H:%M:%S")
  283. return formatted_time
  284. def format_datetime(datetime_string, format_string=None):
  285. """Converts the given string time to :data:`user_datetime_format`
  286. User format specified in defaults
  287. Examples:
  288. * dd-mm-yyyy HH:mm:ss
  289. * mm-dd-yyyy HH:mm
  290. """
  291. import babel.dates
  292. from babel.core import UnknownLocaleError
  293. if not datetime_string:
  294. return
  295. datetime = get_datetime(datetime_string)
  296. if not format_string:
  297. format_string = (
  298. get_user_date_format().replace("mm", "MM")
  299. + ' ' + get_user_time_format())
  300. try:
  301. formatted_datetime = babel.dates.format_datetime(datetime, format_string, locale=(frappe.local.lang or "").replace("-", "_"))
  302. except UnknownLocaleError:
  303. formatted_datetime = datetime.strftime('%Y-%m-%d %H:%M:%S')
  304. return formatted_datetime
  305. def format_duration(seconds, hide_days=False):
  306. """Converts the given duration value in float(seconds) to duration format
  307. example: converts 12885 to '3h 34m 45s' where 12885 = seconds in float
  308. """
  309. seconds = cint(seconds)
  310. total_duration = {
  311. 'days': math.floor(seconds / (3600 * 24)),
  312. 'hours': math.floor(seconds % (3600 * 24) / 3600),
  313. 'minutes': math.floor(seconds % 3600 / 60),
  314. 'seconds': math.floor(seconds % 60)
  315. }
  316. if hide_days:
  317. total_duration['hours'] = math.floor(seconds / 3600)
  318. total_duration['days'] = 0
  319. duration = ''
  320. if total_duration:
  321. if total_duration.get('days'):
  322. duration += str(total_duration.get('days')) + 'd'
  323. if total_duration.get('hours'):
  324. duration += ' ' if len(duration) else ''
  325. duration += str(total_duration.get('hours')) + 'h'
  326. if total_duration.get('minutes'):
  327. duration += ' ' if len(duration) else ''
  328. duration += str(total_duration.get('minutes')) + 'm'
  329. if total_duration.get('seconds'):
  330. duration += ' ' if len(duration) else ''
  331. duration += str(total_duration.get('seconds')) + 's'
  332. return duration
  333. def duration_to_seconds(duration):
  334. """Converts the given duration formatted value to duration value in seconds
  335. example: converts '3h 34m 45s' to 12885 (value in seconds)
  336. """
  337. validate_duration_format(duration)
  338. value = 0
  339. if 'd' in duration:
  340. val = duration.split('d')
  341. days = val[0]
  342. value += cint(days) * 24 * 60 * 60
  343. duration = val[1]
  344. if 'h' in duration:
  345. val = duration.split('h')
  346. hours = val[0]
  347. value += cint(hours) * 60 * 60
  348. duration = val[1]
  349. if 'm' in duration:
  350. val = duration.split('m')
  351. mins = val[0]
  352. value += cint(mins) * 60
  353. duration = val[1]
  354. if 's' in duration:
  355. val = duration.split('s')
  356. secs = val[0]
  357. value += cint(secs)
  358. return value
  359. def validate_duration_format(duration):
  360. import re
  361. is_valid_duration = re.match("^(?:(\d+d)?((^|\s)\d+h)?((^|\s)\d+m)?((^|\s)\d+s)?)$", duration)
  362. if not is_valid_duration:
  363. frappe.throw(frappe._("Value {0} must be in the valid duration format: d h m s").format(frappe.bold(duration)))
  364. def get_weekdays():
  365. return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  366. def get_weekday(datetime=None):
  367. if not datetime:
  368. datetime = now_datetime()
  369. weekdays = get_weekdays()
  370. return weekdays[datetime.weekday()]
  371. def get_timespan_date_range(timespan):
  372. today = nowdate()
  373. date_range_map = {
  374. "last week": lambda: (get_first_day_of_week(add_to_date(today, days=-7)), get_last_day_of_week(add_to_date(today, days=-7))),
  375. "last month": lambda: (get_first_day(add_to_date(today, months=-1)), get_last_day(add_to_date(today, months=-1))),
  376. "last quarter": lambda: (get_quarter_start(add_to_date(today, months=-3)), get_quarter_ending(add_to_date(today, months=-3))),
  377. "last 6 months": lambda: (get_quarter_start(add_to_date(today, months=-6)), get_quarter_ending(add_to_date(today, months=-3))),
  378. "last year": lambda: (get_year_start(add_to_date(today, years=-1)), get_year_ending(add_to_date(today, years=-1))),
  379. "yesterday": lambda: (add_to_date(today, days=-1),) * 2,
  380. "today": lambda: (today, today),
  381. "tomorrow": lambda: (add_to_date(today, days=1),) * 2,
  382. "this week": lambda: (get_first_day_of_week(today), today),
  383. "this month": lambda: (get_first_day(today), today),
  384. "this quarter": lambda: (get_quarter_start(today), today),
  385. "this year": lambda: (get_year_start(today), today),
  386. "next week": lambda: (get_first_day_of_week(add_to_date(today, days=7)), get_last_day_of_week(add_to_date(today, days=7))),
  387. "next month": lambda: (get_first_day(add_to_date(today, months=1)), get_last_day(add_to_date(today, months=1))),
  388. "next quarter": lambda: (get_quarter_start(add_to_date(today, months=3)), get_quarter_ending(add_to_date(today, months=3))),
  389. "next 6 months": lambda: (get_quarter_start(add_to_date(today, months=3)), get_quarter_ending(add_to_date(today, months=6))),
  390. "next year": lambda: (get_year_start(add_to_date(today, years=1)), get_year_ending(add_to_date(today, years=1))),
  391. }
  392. if timespan in date_range_map:
  393. return date_range_map[timespan]()
  394. def global_date_format(date, format="long"):
  395. """returns localized date in the form of January 1, 2012"""
  396. import babel.dates
  397. date = getdate(date)
  398. formatted_date = babel.dates.format_date(date, locale=(frappe.local.lang or "en").replace("-", "_"), format=format)
  399. return formatted_date
  400. def has_common(l1, l2):
  401. """Returns truthy value if there are common elements in lists l1 and l2"""
  402. return set(l1) & set(l2)
  403. def cast_fieldtype(fieldtype, value):
  404. if fieldtype in ("Currency", "Float", "Percent"):
  405. value = flt(value)
  406. elif fieldtype in ("Int", "Check"):
  407. value = cint(value)
  408. elif fieldtype in ("Data", "Text", "Small Text", "Long Text",
  409. "Text Editor", "Select", "Link", "Dynamic Link"):
  410. value = cstr(value)
  411. elif fieldtype == "Date":
  412. value = getdate(value)
  413. elif fieldtype == "Datetime":
  414. value = get_datetime(value)
  415. elif fieldtype == "Time":
  416. value = to_timedelta(value)
  417. return value
  418. def flt(s, precision=None):
  419. """Convert to float (ignoring commas in string)
  420. :param s: Number in string or other numeric format.
  421. :param precision: optional argument to specify precision for rounding.
  422. :returns: Converted number in python float type.
  423. Returns 0 if input can not be converted to float.
  424. Examples:
  425. >>> flt("43.5", precision=0)
  426. 44
  427. >>> flt("42.5", precision=0)
  428. 42
  429. >>> flt("10,500.5666", precision=2)
  430. 10500.57
  431. >>> flt("a")
  432. 0.0
  433. """
  434. if isinstance(s, string_types):
  435. s = s.replace(',','')
  436. try:
  437. num = float(s)
  438. if precision is not None:
  439. num = rounded(num, precision)
  440. except Exception:
  441. num = 0.0
  442. return num
  443. def cint(s, default=0):
  444. """Convert to integer
  445. :param s: Number in string or other numeric format.
  446. :returns: Converted number in python integer type.
  447. Returns default if input can not be converted to integer.
  448. Examples:
  449. >>> cint("100")
  450. 100
  451. >>> cint("a")
  452. 0
  453. """
  454. try:
  455. return int(float(s))
  456. except Exception:
  457. return default
  458. def floor(s):
  459. """
  460. A number representing the largest integer less than or equal to the specified number
  461. Parameters
  462. ----------
  463. s : int or str or Decimal object
  464. The mathematical value to be floored
  465. Returns
  466. -------
  467. int
  468. number representing the largest integer less than or equal to the specified number
  469. """
  470. try: num = cint(math.floor(flt(s)))
  471. except: num = 0
  472. return num
  473. def ceil(s):
  474. """
  475. The smallest integer greater than or equal to the given number
  476. Parameters
  477. ----------
  478. s : int or str or Decimal object
  479. The mathematical value to be ceiled
  480. Returns
  481. -------
  482. int
  483. smallest integer greater than or equal to the given number
  484. """
  485. try: num = cint(math.ceil(flt(s)))
  486. except: num = 0
  487. return num
  488. def cstr(s, encoding='utf-8'):
  489. return frappe.as_unicode(s, encoding)
  490. def rounded(num, precision=0):
  491. """round method for round halfs to nearest even algorithm aka banker's rounding - compatible with python3"""
  492. precision = cint(precision)
  493. multiplier = 10 ** precision
  494. # avoid rounding errors
  495. num = round(num * multiplier if precision else num, 8)
  496. floor_num = math.floor(num)
  497. decimal_part = num - floor_num
  498. if not precision and decimal_part == 0.5:
  499. num = floor_num if (floor_num % 2 == 0) else floor_num + 1
  500. else:
  501. if decimal_part == 0.5:
  502. num = floor_num + 1
  503. else:
  504. num = round(num)
  505. return (num / multiplier) if precision else num
  506. def remainder(numerator, denominator, precision=2):
  507. precision = cint(precision)
  508. multiplier = 10 ** precision
  509. if precision:
  510. _remainder = ((numerator * multiplier) % (denominator * multiplier)) / multiplier
  511. else:
  512. _remainder = numerator % denominator
  513. return flt(_remainder, precision)
  514. def safe_div(numerator, denominator, precision=2):
  515. """
  516. SafeMath division that returns zero when divided by zero.
  517. """
  518. precision = cint(precision)
  519. if denominator == 0:
  520. _res = 0.0
  521. else:
  522. _res = float(numerator) / denominator
  523. return flt(_res, precision)
  524. def round_based_on_smallest_currency_fraction(value, currency, precision=2):
  525. smallest_currency_fraction_value = flt(frappe.db.get_value("Currency",
  526. currency, "smallest_currency_fraction_value", cache=True))
  527. if smallest_currency_fraction_value:
  528. remainder_val = remainder(value, smallest_currency_fraction_value, precision)
  529. if remainder_val > (smallest_currency_fraction_value / 2):
  530. value += smallest_currency_fraction_value - remainder_val
  531. else:
  532. value -= remainder_val
  533. else:
  534. value = rounded(value)
  535. return flt(value, precision)
  536. def encode(obj, encoding="utf-8"):
  537. if isinstance(obj, list):
  538. out = []
  539. for o in obj:
  540. if isinstance(o, text_type):
  541. out.append(o.encode(encoding))
  542. else:
  543. out.append(o)
  544. return out
  545. elif isinstance(obj, text_type):
  546. return obj.encode(encoding)
  547. else:
  548. return obj
  549. def parse_val(v):
  550. """Converts to simple datatypes from SQL query results"""
  551. if isinstance(v, (datetime.date, datetime.datetime)):
  552. v = text_type(v)
  553. elif isinstance(v, datetime.timedelta):
  554. v = ":".join(text_type(v).split(":")[:2])
  555. elif isinstance(v, integer_types):
  556. v = int(v)
  557. return v
  558. def fmt_money(amount, precision=None, currency=None):
  559. """
  560. Convert to string with commas for thousands, millions etc
  561. """
  562. number_format = frappe.db.get_default("number_format") or "#,###.##"
  563. if precision is None:
  564. precision = cint(frappe.db.get_default('currency_precision')) or None
  565. decimal_str, comma_str, number_format_precision = get_number_format_info(number_format)
  566. if precision is None:
  567. precision = number_format_precision
  568. # 40,000 -> 40,000.00
  569. # 40,000.00000 -> 40,000.00
  570. # 40,000.23000 -> 40,000.23
  571. if isinstance(amount, string_types):
  572. amount = flt(amount, precision)
  573. if decimal_str:
  574. decimals_after = str(round(amount % 1, precision))
  575. parts = decimals_after.split('.')
  576. parts = parts[1] if len(parts) > 1 else parts[0]
  577. decimals = parts
  578. if precision > 2:
  579. if len(decimals) < 3:
  580. if currency:
  581. fraction = frappe.db.get_value("Currency", currency, "fraction_units", cache=True) or 100
  582. precision = len(cstr(fraction)) - 1
  583. else:
  584. precision = number_format_precision
  585. elif len(decimals) < precision:
  586. precision = len(decimals)
  587. amount = '%.*f' % (precision, round(flt(amount), precision))
  588. if amount.find('.') == -1:
  589. decimals = ''
  590. else:
  591. decimals = amount.split('.')[1]
  592. parts = []
  593. minus = ''
  594. if flt(amount) < 0:
  595. minus = '-'
  596. amount = cstr(abs(flt(amount))).split('.')[0]
  597. if len(amount) > 3:
  598. parts.append(amount[-3:])
  599. amount = amount[:-3]
  600. val = number_format=="#,##,###.##" and 2 or 3
  601. while len(amount) > val:
  602. parts.append(amount[-val:])
  603. amount = amount[:-val]
  604. parts.append(amount)
  605. parts.reverse()
  606. amount = comma_str.join(parts) + ((precision and decimal_str) and (decimal_str + decimals) or "")
  607. if amount != '0':
  608. amount = minus + amount
  609. if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes":
  610. symbol = frappe.db.get_value("Currency", currency, "symbol", cache=True) or currency
  611. amount = symbol + " " + amount
  612. return amount
  613. number_format_info = {
  614. "#,###.##": (".", ",", 2),
  615. "#.###,##": (",", ".", 2),
  616. "# ###.##": (".", " ", 2),
  617. "# ###,##": (",", " ", 2),
  618. "#'###.##": (".", "'", 2),
  619. "#, ###.##": (".", ", ", 2),
  620. "#,##,###.##": (".", ",", 2),
  621. "#,###.###": (".", ",", 3),
  622. "#.###": ("", ".", 0),
  623. "#,###": ("", ",", 0)
  624. }
  625. def get_number_format_info(format):
  626. return number_format_info.get(format) or (".", ",", 2)
  627. #
  628. # convert currency to words
  629. #
  630. def money_in_words(number, main_currency = None, fraction_currency=None):
  631. """
  632. Returns string in words with currency and fraction currency.
  633. """
  634. from frappe.utils import get_defaults
  635. _ = frappe._
  636. try:
  637. # note: `flt` returns 0 for invalid input and we don't want that
  638. number = float(number)
  639. except ValueError:
  640. return ""
  641. number = flt(number)
  642. if number < 0:
  643. return ""
  644. d = get_defaults()
  645. if not main_currency:
  646. main_currency = d.get('currency', 'INR')
  647. if not fraction_currency:
  648. fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction", cache=True) or _("Cent")
  649. number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \
  650. frappe.db.get_default("number_format") or "#,###.##"
  651. fraction_length = get_number_format_info(number_format)[2]
  652. n = "%.{0}f".format(fraction_length) % number
  653. numbers = n.split('.')
  654. main, fraction = numbers if len(numbers) > 1 else [n, '00']
  655. if len(fraction) < fraction_length:
  656. zeros = '0' * (fraction_length - len(fraction))
  657. fraction += zeros
  658. in_million = True
  659. if number_format == "#,##,###.##": in_million = False
  660. # 0.00
  661. if main == '0' and fraction in ['00', '000']:
  662. out = "{0} {1}".format(main_currency, _('Zero'))
  663. # 0.XX
  664. elif main == '0':
  665. out = _(in_words(fraction, in_million).title()) + ' ' + fraction_currency
  666. else:
  667. out = main_currency + ' ' + _(in_words(main, in_million).title())
  668. if cint(fraction):
  669. out = out + ' ' + _('and') + ' ' + _(in_words(fraction, in_million).title()) + ' ' + fraction_currency
  670. return out + ' ' + _('only.')
  671. #
  672. # convert number to words
  673. #
  674. def in_words(integer, in_million=True):
  675. """
  676. Returns string in words for the given integer.
  677. """
  678. from num2words import num2words
  679. locale = 'en_IN' if not in_million else frappe.local.lang
  680. integer = int(integer)
  681. try:
  682. ret = num2words(integer, lang=locale)
  683. except NotImplementedError:
  684. ret = num2words(integer, lang='en')
  685. except OverflowError:
  686. ret = num2words(integer, lang='en')
  687. return ret.replace('-', ' ')
  688. def is_html(text):
  689. if not isinstance(text, str):
  690. return False
  691. return re.search('<[^>]+>', text)
  692. def is_image(filepath):
  693. from mimetypes import guess_type
  694. # filepath can be https://example.com/bed.jpg?v=129
  695. filepath = (filepath or "").split('?')[0]
  696. return (guess_type(filepath)[0] or "").startswith("image/")
  697. def get_thumbnail_base64_for_image(src):
  698. from os.path import exists as file_exists
  699. from PIL import Image
  700. from frappe.core.doctype.file.file import get_local_image
  701. from frappe import safe_decode, cache
  702. if not src:
  703. frappe.throw('Invalid source for image: {0}'.format(src))
  704. if not src.startswith('/files') or '..' in src:
  705. return
  706. if src.endswith('.svg'):
  707. return
  708. def _get_base64():
  709. file_path = frappe.get_site_path("public", src.lstrip("/"))
  710. if not file_exists(file_path):
  711. return
  712. try:
  713. image, unused_filename, extn = get_local_image(src)
  714. except IOError:
  715. return
  716. original_size = image.size
  717. size = 50, 50
  718. image.thumbnail(size, Image.ANTIALIAS)
  719. base64_string = image_to_base64(image, extn)
  720. return {
  721. 'base64': 'data:image/{0};base64,{1}'.format(extn, safe_decode(base64_string)),
  722. 'width': original_size[0],
  723. 'height': original_size[1]
  724. }
  725. return cache().hget('thumbnail_base64', src, generator=_get_base64)
  726. def image_to_base64(image, extn):
  727. import base64
  728. from io import BytesIO
  729. buffered = BytesIO()
  730. if extn.lower() in ('jpg', 'jpe'):
  731. extn = 'JPEG'
  732. image.save(buffered, extn)
  733. img_str = base64.b64encode(buffered.getvalue())
  734. return img_str
  735. # from Jinja2 code
  736. _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
  737. def strip_html(text):
  738. """removes anything enclosed in and including <>"""
  739. return _striptags_re.sub("", text)
  740. def escape_html(text):
  741. if not isinstance(text, string_types):
  742. return text
  743. html_escape_table = {
  744. "&": "&amp;",
  745. '"': "&quot;",
  746. "'": "&apos;",
  747. ">": "&gt;",
  748. "<": "&lt;",
  749. }
  750. return "".join(html_escape_table.get(c,c) for c in text)
  751. def pretty_date(iso_datetime):
  752. """
  753. Takes an ISO time and returns a string representing how
  754. long ago the date represents.
  755. Ported from PrettyDate by John Resig
  756. """
  757. from frappe import _
  758. if not iso_datetime: return ''
  759. import math
  760. if isinstance(iso_datetime, string_types):
  761. iso_datetime = datetime.datetime.strptime(iso_datetime, DATETIME_FORMAT)
  762. now_dt = datetime.datetime.strptime(now(), DATETIME_FORMAT)
  763. dt_diff = now_dt - iso_datetime
  764. # available only in python 2.7+
  765. # dt_diff_seconds = dt_diff.total_seconds()
  766. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  767. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  768. # differnt cases
  769. if dt_diff_seconds < 60.0:
  770. return _('just now')
  771. elif dt_diff_seconds < 120.0:
  772. return _('1 minute ago')
  773. elif dt_diff_seconds < 3600.0:
  774. return _('{0} minutes ago').format(cint(math.floor(dt_diff_seconds / 60.0)))
  775. elif dt_diff_seconds < 7200.0:
  776. return _('1 hour ago')
  777. elif dt_diff_seconds < 86400.0:
  778. return _('{0} hours ago').format(cint(math.floor(dt_diff_seconds / 3600.0)))
  779. elif dt_diff_days == 1.0:
  780. return _('Yesterday')
  781. elif dt_diff_days < 7.0:
  782. return _('{0} days ago').format(cint(dt_diff_days))
  783. elif dt_diff_days < 12:
  784. return _('1 week ago')
  785. elif dt_diff_days < 31.0:
  786. return _('{0} weeks ago').format(cint(math.ceil(dt_diff_days / 7.0)))
  787. elif dt_diff_days < 46:
  788. return _('1 month ago')
  789. elif dt_diff_days < 365.0:
  790. return _('{0} months ago').format(cint(math.ceil(dt_diff_days / 30.0)))
  791. elif dt_diff_days < 550.0:
  792. return _('1 year ago')
  793. else:
  794. return '{0} years ago'.format(cint(math.floor(dt_diff_days / 365.0)))
  795. def comma_or(some_list, add_quotes=True):
  796. return comma_sep(some_list, frappe._("{0} or {1}"), add_quotes)
  797. def comma_and(some_list ,add_quotes=True):
  798. return comma_sep(some_list, frappe._("{0} and {1}"), add_quotes)
  799. def comma_sep(some_list, pattern, add_quotes=True):
  800. if isinstance(some_list, (list, tuple)):
  801. # list(some_list) is done to preserve the existing list
  802. some_list = [text_type(s) for s in list(some_list)]
  803. if not some_list:
  804. return ""
  805. elif len(some_list) == 1:
  806. return some_list[0]
  807. else:
  808. some_list = ["'%s'" % s for s in some_list] if add_quotes else ["%s" % s for s in some_list]
  809. return pattern.format(", ".join(frappe._(s) for s in some_list[:-1]), some_list[-1])
  810. else:
  811. return some_list
  812. def new_line_sep(some_list):
  813. if isinstance(some_list, (list, tuple)):
  814. # list(some_list) is done to preserve the existing list
  815. some_list = [text_type(s) for s in list(some_list)]
  816. if not some_list:
  817. return ""
  818. elif len(some_list) == 1:
  819. return some_list[0]
  820. else:
  821. some_list = ["%s" % s for s in some_list]
  822. return format("\n ".join(some_list))
  823. else:
  824. return some_list
  825. def filter_strip_join(some_list, sep):
  826. """given a list, filter None values, strip spaces and join"""
  827. return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
  828. def get_url(uri=None, full_address=False):
  829. """get app url from request"""
  830. host_name = frappe.local.conf.host_name or frappe.local.conf.hostname
  831. if uri and (uri.startswith("http://") or uri.startswith("https://")):
  832. return uri
  833. if not host_name:
  834. request_host_name = get_host_name_from_request()
  835. if request_host_name:
  836. host_name = request_host_name
  837. elif frappe.local.site:
  838. protocol = 'http://'
  839. if frappe.local.conf.ssl_certificate:
  840. protocol = 'https://'
  841. elif frappe.local.conf.wildcard:
  842. domain = frappe.local.conf.wildcard.get('domain')
  843. if domain and frappe.local.site.endswith(domain) and frappe.local.conf.wildcard.get('ssl_certificate'):
  844. protocol = 'https://'
  845. host_name = protocol + frappe.local.site
  846. else:
  847. host_name = frappe.db.get_value("Website Settings", "Website Settings",
  848. "subdomain")
  849. if not host_name:
  850. host_name = "http://localhost"
  851. if host_name and not (host_name.startswith("http://") or host_name.startswith("https://")):
  852. host_name = "http://" + host_name
  853. if not uri and full_address:
  854. uri = frappe.get_request_header("REQUEST_URI", "")
  855. port = frappe.conf.http_port or frappe.conf.webserver_port
  856. if not (frappe.conf.restart_supervisor_on_update or frappe.conf.restart_systemd_on_update) and host_name and not url_contains_port(host_name) and port:
  857. host_name = host_name + ':' + str(port)
  858. url = urljoin(host_name, uri) if uri else host_name
  859. return url
  860. def get_host_name_from_request():
  861. if hasattr(frappe.local, "request") and frappe.local.request and frappe.local.request.host:
  862. protocol = 'https://' if 'https' == frappe.get_request_header('X-Forwarded-Proto', "") else 'http://'
  863. return protocol + frappe.local.request.host
  864. def url_contains_port(url):
  865. parts = url.split(':')
  866. return len(parts) > 2
  867. def get_host_name():
  868. return get_url().rsplit("//", 1)[-1]
  869. def get_link_to_form(doctype, name, label=None):
  870. if not label: label = name
  871. return """<a href="{0}">{1}</a>""".format(get_url_to_form(doctype, name), label)
  872. def get_link_to_report(name, label=None, report_type=None, doctype=None, filters=None):
  873. if not label: label = name
  874. if filters:
  875. conditions = []
  876. for k,v in iteritems(filters):
  877. if isinstance(v, list):
  878. for value in v:
  879. conditions.append(str(k)+'='+'["'+str(value[0]+'"'+','+'"'+str(value[1])+'"]'))
  880. else:
  881. conditions.append(str(k)+"="+str(v))
  882. filters = "&".join(conditions)
  883. return """<a href='{0}'>{1}</a>""".format(get_url_to_report_with_filters(name, filters, report_type, doctype), label)
  884. else:
  885. return """<a href='{0}'>{1}</a>""".format(get_url_to_report(name, report_type, doctype), label)
  886. def get_absolute_url(doctype, name):
  887. return "/app/{0}/{1}".format(quoted(slug(doctype)), quoted(name))
  888. def get_url_to_form(doctype, name):
  889. return get_url(uri = "/app/{0}/{1}".format(quoted(slug(doctype)), quoted(name)))
  890. def get_url_to_list(doctype):
  891. return get_url(uri = "/app/{0}".format(quoted(slug(doctype))))
  892. def get_url_to_report(name, report_type = None, doctype = None):
  893. if report_type == "Report Builder":
  894. return get_url(uri = "/app/{0}/view/report/{1}".format(quoted(slug(doctype)), quoted(name)))
  895. else:
  896. return get_url(uri = "/app/query-report/{0}".format(quoted(name)))
  897. def get_url_to_report_with_filters(name, filters, report_type = None, doctype = None):
  898. if report_type == "Report Builder":
  899. return get_url(uri = "/app/{0}/view/report?{1}".format(quoted(doctype), filters))
  900. else:
  901. return get_url(uri = "/app/query-report/{0}?{1}".format(quoted(name), filters))
  902. operator_map = {
  903. # startswith
  904. "^": lambda a, b: (a or "").startswith(b),
  905. # in or not in a list
  906. "in": lambda a, b: operator.contains(b, a),
  907. "not in": lambda a, b: not operator.contains(b, a),
  908. # comparison operators
  909. "=": lambda a, b: operator.eq(a, b),
  910. "!=": lambda a, b: operator.ne(a, b),
  911. ">": lambda a, b: operator.gt(a, b),
  912. "<": lambda a, b: operator.lt(a, b),
  913. ">=": lambda a, b: operator.ge(a, b),
  914. "<=": lambda a, b: operator.le(a, b),
  915. "not None": lambda a, b: a and True or False,
  916. "None": lambda a, b: (not a) and True or False
  917. }
  918. def evaluate_filters(doc, filters):
  919. '''Returns true if doc matches filters'''
  920. if isinstance(filters, dict):
  921. for key, value in iteritems(filters):
  922. f = get_filter(None, {key:value})
  923. if not compare(doc.get(f.fieldname), f.operator, f.value, f.fieldtype):
  924. return False
  925. elif isinstance(filters, (list, tuple)):
  926. for d in filters:
  927. f = get_filter(None, d)
  928. if not compare(doc.get(f.fieldname), f.operator, f.value, f.fieldtype):
  929. return False
  930. return True
  931. def compare(val1, condition, val2, fieldtype=None):
  932. ret = False
  933. if fieldtype:
  934. val2 = cast_fieldtype(fieldtype, val2)
  935. if condition in operator_map:
  936. ret = operator_map[condition](val1, val2)
  937. return ret
  938. def get_filter(doctype, f, filters_config=None):
  939. """Returns a _dict like
  940. {
  941. "doctype":
  942. "fieldname":
  943. "operator":
  944. "value":
  945. "fieldtype":
  946. }
  947. """
  948. from frappe.model import default_fields, optional_fields
  949. if isinstance(f, dict):
  950. key, value = next(iter(f.items()))
  951. f = make_filter_tuple(doctype, key, value)
  952. if not isinstance(f, (list, tuple)):
  953. frappe.throw(frappe._("Filter must be a tuple or list (in a list)"))
  954. if len(f) == 3:
  955. f = (doctype, f[0], f[1], f[2])
  956. elif len(f) > 4:
  957. f = f[0:4]
  958. elif len(f) != 4:
  959. frappe.throw(frappe._("Filter must have 4 values (doctype, fieldname, operator, value): {0}").format(str(f)))
  960. f = frappe._dict(doctype=f[0], fieldname=f[1], operator=f[2], value=f[3])
  961. sanitize_column(f.fieldname)
  962. if not f.operator:
  963. # if operator is missing
  964. f.operator = "="
  965. valid_operators = ("=", "!=", ">", "<", ">=", "<=", "like", "not like", "in", "not in", "is",
  966. "between", "descendants of", "ancestors of", "not descendants of", "not ancestors of",
  967. "timespan", "previous", "next")
  968. if filters_config:
  969. additional_operators = []
  970. for key in filters_config:
  971. additional_operators.append(key.lower())
  972. valid_operators = tuple(set(valid_operators + tuple(additional_operators)))
  973. if f.operator.lower() not in valid_operators:
  974. frappe.throw(frappe._("Operator must be one of {0}").format(", ".join(valid_operators)))
  975. if f.doctype and (f.fieldname not in default_fields + optional_fields):
  976. # verify fieldname belongs to the doctype
  977. meta = frappe.get_meta(f.doctype)
  978. if not meta.has_field(f.fieldname):
  979. # try and match the doctype name from child tables
  980. for df in meta.get_table_fields():
  981. if frappe.get_meta(df.options).has_field(f.fieldname):
  982. f.doctype = df.options
  983. break
  984. try:
  985. df = frappe.get_meta(f.doctype).get_field(f.fieldname)
  986. except frappe.exceptions.DoesNotExistError:
  987. df = None
  988. f.fieldtype = df.fieldtype if df else None
  989. return f
  990. def make_filter_tuple(doctype, key, value):
  991. '''return a filter tuple like [doctype, key, operator, value]'''
  992. if isinstance(value, (list, tuple)):
  993. return [doctype, key, value[0], value[1]]
  994. else:
  995. return [doctype, key, "=", value]
  996. def make_filter_dict(filters):
  997. '''convert this [[doctype, key, operator, value], ..]
  998. to this { key: (operator, value), .. }
  999. '''
  1000. _filter = frappe._dict()
  1001. for f in filters:
  1002. _filter[f[1]] = (f[2], f[3])
  1003. return _filter
  1004. def sanitize_column(column_name):
  1005. from frappe import _
  1006. import sqlparse
  1007. regex = re.compile("^.*[,'();].*")
  1008. column_name = sqlparse.format(column_name, strip_comments=True, keyword_case="lower")
  1009. blacklisted_keywords = ['select', 'create', 'insert', 'delete', 'drop', 'update', 'case', 'and', 'or']
  1010. def _raise_exception():
  1011. frappe.throw(_("Invalid field name {0}").format(column_name), frappe.DataError)
  1012. if 'ifnull' in column_name:
  1013. if regex.match(column_name):
  1014. # to avoid and, or
  1015. if any(' {0} '.format(keyword) in column_name.split() for keyword in blacklisted_keywords):
  1016. _raise_exception()
  1017. # to avoid select, delete, drop, update and case
  1018. elif any(keyword in column_name.split() for keyword in blacklisted_keywords):
  1019. _raise_exception()
  1020. elif regex.match(column_name):
  1021. _raise_exception()
  1022. def scrub_urls(html):
  1023. html = expand_relative_urls(html)
  1024. # encoding should be responsibility of the composer
  1025. # html = quote_urls(html)
  1026. return html
  1027. def expand_relative_urls(html):
  1028. # expand relative urls
  1029. url = get_url()
  1030. if url.endswith("/"): url = url[:-1]
  1031. def _expand_relative_urls(match):
  1032. to_expand = list(match.groups())
  1033. if not to_expand[2].startswith('mailto') and not to_expand[2].startswith('data:'):
  1034. if not to_expand[2].startswith("/"):
  1035. to_expand[2] = "/" + to_expand[2]
  1036. to_expand.insert(2, url)
  1037. if 'url' in to_expand[0] and to_expand[1].startswith('(') and to_expand[-1].endswith(')'):
  1038. # background-image: url('/assets/...') - workaround for wkhtmltopdf print-media-type
  1039. to_expand.append(' !important')
  1040. return "".join(to_expand)
  1041. html = re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?!http)[^\'" >]+)([\'"]?)', _expand_relative_urls, html)
  1042. # background-image: url('/assets/...')
  1043. html = re.sub('(:[\s]?url)(\([\'"]?)((?!http)[^\'" >]+)([\'"]?\))', _expand_relative_urls, html)
  1044. return html
  1045. def quoted(url):
  1046. return cstr(quote(encode(url), safe=b"~@#$&()*!+=:;,.?/'"))
  1047. def quote_urls(html):
  1048. def _quote_url(match):
  1049. groups = list(match.groups())
  1050. groups[2] = quoted(groups[2])
  1051. return "".join(groups)
  1052. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?:http)[^\'">]+)([\'"]?)',
  1053. _quote_url, html)
  1054. def unique(seq):
  1055. """use this instead of list(set()) to preserve order of the original list.
  1056. Thanks to Stackoverflow: http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order"""
  1057. seen = set()
  1058. seen_add = seen.add
  1059. return [ x for x in seq if not (x in seen or seen_add(x)) ]
  1060. def strip(val, chars=None):
  1061. # \ufeff is no-width-break, \u200b is no-width-space
  1062. return (val or "").replace("\ufeff", "").replace("\u200b", "").strip(chars)
  1063. def to_markdown(html):
  1064. from html2text import html2text
  1065. from six.moves import html_parser as HTMLParser
  1066. text = None
  1067. try:
  1068. text = html2text(html or '')
  1069. except HTMLParser.HTMLParseError:
  1070. pass
  1071. return text
  1072. def md_to_html(markdown_text):
  1073. from markdown2 import markdown as _markdown, MarkdownError
  1074. extras = {
  1075. 'fenced-code-blocks': None,
  1076. 'tables': None,
  1077. 'header-ids': None,
  1078. 'toc': None,
  1079. 'highlightjs-lang': None,
  1080. 'html-classes': {
  1081. 'table': 'table table-bordered',
  1082. 'img': 'screenshot'
  1083. }
  1084. }
  1085. html = None
  1086. try:
  1087. html = UnicodeWithAttrs(_markdown(markdown_text or '', extras=extras))
  1088. except MarkdownError:
  1089. pass
  1090. return html
  1091. def markdown(markdown_text):
  1092. return md_to_html(markdown_text)
  1093. def is_subset(list_a, list_b):
  1094. '''Returns whether list_a is a subset of list_b'''
  1095. return len(list(set(list_a) & set(list_b))) == len(list_a)
  1096. def generate_hash(*args, **kwargs):
  1097. return frappe.generate_hash(*args, **kwargs)
  1098. def guess_date_format(date_string):
  1099. DATE_FORMATS = [
  1100. r"%d/%b/%y",
  1101. r"%d-%m-%Y",
  1102. r"%m-%d-%Y",
  1103. r"%Y-%m-%d",
  1104. r"%d-%m-%y",
  1105. r"%m-%d-%y",
  1106. r"%y-%m-%d",
  1107. r"%y-%b-%d",
  1108. r"%d/%m/%Y",
  1109. r"%m/%d/%Y",
  1110. r"%Y/%m/%d",
  1111. r"%d/%m/%y",
  1112. r"%m/%d/%y",
  1113. r"%y/%m/%d",
  1114. r"%d.%m.%Y",
  1115. r"%m.%d.%Y",
  1116. r"%Y.%m.%d",
  1117. r"%d.%m.%y",
  1118. r"%m.%d.%y",
  1119. r"%y.%m.%d",
  1120. r"%d %b %Y",
  1121. r"%d %B %Y",
  1122. ]
  1123. TIME_FORMATS = [
  1124. r"%H:%M:%S.%f",
  1125. r"%H:%M:%S",
  1126. r"%H:%M",
  1127. r"%I:%M:%S.%f %p",
  1128. r"%I:%M:%S %p",
  1129. r"%I:%M %p",
  1130. ]
  1131. def _get_date_format(date_str):
  1132. for f in DATE_FORMATS:
  1133. try:
  1134. # if date is parsed without any exception
  1135. # capture the date format
  1136. datetime.datetime.strptime(date_str, f)
  1137. return f
  1138. except ValueError:
  1139. pass
  1140. def _get_time_format(time_str):
  1141. for f in TIME_FORMATS:
  1142. try:
  1143. # if time is parsed without any exception
  1144. # capture the time format
  1145. datetime.datetime.strptime(time_str, f)
  1146. return f
  1147. except ValueError:
  1148. pass
  1149. date_format = None
  1150. time_format = None
  1151. date_string = date_string.strip()
  1152. # check if date format can be guessed
  1153. date_format = _get_date_format(date_string)
  1154. if date_format:
  1155. return date_format
  1156. # date_string doesnt look like date, it can have a time part too
  1157. # split the date string into date and time parts
  1158. if " " in date_string:
  1159. date_str, time_str = date_string.split(" ", 1)
  1160. date_format = _get_date_format(date_str) or ''
  1161. time_format = _get_time_format(time_str) or ''
  1162. if date_format and time_format:
  1163. return (date_format + ' ' + time_format).strip()
  1164. def validate_json_string(string):
  1165. try:
  1166. json.loads(string)
  1167. except (TypeError, ValueError):
  1168. raise frappe.ValidationError
  1169. def get_user_info_for_avatar(user_id):
  1170. user_info = {
  1171. "email": user_id,
  1172. "image": "",
  1173. "name": user_id
  1174. }
  1175. try:
  1176. user_info["email"] = frappe.get_cached_value("User", user_id, "email")
  1177. user_info["name"] = frappe.get_cached_value("User", user_id, "fullname")
  1178. user_info["image"] = frappe.get_cached_value("User", user_id, "user_image")
  1179. except Exception:
  1180. frappe.local.message_log = []
  1181. return user_info
  1182. class UnicodeWithAttrs(text_type):
  1183. def __init__(self, text):
  1184. self.toc_html = text.toc_html
  1185. self.metadata = text.metadata