Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

769 рядки
19 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # util __init__.py
  23. from __future__ import unicode_literals
  24. import webnotes
  25. user_time_zone = None
  26. month_name = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
  27. month_name_full = ['','January','February','March','April','May','June','July','August','September','October','November','December']
  28. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image', 'Graph']
  29. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  30. def getCSVelement(v):
  31. """
  32. Returns the CSV value of `v`, For example:
  33. * apple becomes "apple"
  34. * hi"there becomes "hi""there"
  35. """
  36. v = cstr(v)
  37. if not v: return ''
  38. if (',' in v) or ('\n' in v) or ('"' in v):
  39. if '"' in v: v = v.replace('"', '""')
  40. return '"'+v+'"'
  41. else: return v or ''
  42. def get_fullname(profile):
  43. """get the full name (first name + last name) of the user from Profile"""
  44. p = webnotes.conn.sql("""select first_name, last_name from `tabProfile`
  45. where name=%s""", profile, as_dict=1)
  46. if p:
  47. p = p[0]
  48. full_name = (p['first_name'] and (p['first_name'] + ' ') or '') + (p['last_name'] or '')
  49. return full_name or profile
  50. else:
  51. return profile
  52. def extract_email_id(s):
  53. """
  54. Extract email id from email header format
  55. """
  56. if '<' in s:
  57. s = s.split('<')[1].split('>')[0]
  58. if s:
  59. s = s.strip().lower()
  60. return s
  61. def validate_email_add(email_str):
  62. """
  63. Validates the email string
  64. """
  65. s = extract_email_id(email_str)
  66. import re
  67. #return re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", email_str)
  68. return re.match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", s)
  69. def sendmail(recipients, sender='', msg='', subject='[No Subject]', parts=[], cc=[], attach=[]):
  70. """
  71. Send an email. For more details see :func:`email_lib.sendmail`
  72. """
  73. import webnotes.utils.email_lib
  74. return email_lib.sendmail(recipients, sender, msg, subject, parts, cc, attach)
  75. def get_request_site_address():
  76. """get app url from request"""
  77. import os
  78. return 'HTTPS' in os.environ.get('SERVER_PROTOCOL') and 'https://' or 'http://' \
  79. + os.environ.get('HTTP_HOST')
  80. def generate_hash():
  81. """
  82. Generates random hash for session id
  83. """
  84. import hashlib, time
  85. return hashlib.sha224(str(time.time())).hexdigest()
  86. def random_string(length):
  87. """generate a random string"""
  88. import string
  89. from random import choice
  90. return ''.join([choice(string.letters + string.digits) for i in range(length)])
  91. def db_exists(dt, dn):
  92. return webnotes.conn.sql('select name from `tab%s` where name="%s"' % (dt, dn))
  93. def load_json(arg):
  94. # already a dictionary?
  95. if not isinstance(arg, basestring):
  96. return arg
  97. import json
  98. return json.loads(arg, encoding='utf-8')
  99. # Get Traceback
  100. # ==============================================================================
  101. def getTraceback():
  102. """
  103. Returns the traceback of the Exception
  104. """
  105. import sys, traceback, string
  106. type, value, tb = sys.exc_info()
  107. body = "Traceback (innermost last):\n"
  108. list = traceback.format_tb(tb, None) + traceback.format_exception_only(type, value)
  109. body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1])
  110. if webnotes.logger:
  111. webnotes.logger.error('Db:'+(webnotes.conn and webnotes.conn.cur_db_name or '') + ' - ' + body)
  112. return body
  113. # Log
  114. # ==============================================================================
  115. def log(event, details):
  116. webnotes.logger.info(details)
  117. # Date and Time
  118. # ==============================================================================
  119. def getdate(string_date):
  120. """
  121. Coverts string date (yyyy-mm-dd) to datetime.date object
  122. """
  123. import datetime
  124. if type(string_date)==unicode:
  125. string_date = str(string_date)
  126. if type(string_date) in (datetime.datetime, datetime.date):
  127. return string_date
  128. if ' ' in string_date:
  129. string_date = string_date.split(' ')[0]
  130. t = string_date.split('-')
  131. if len(t)==3:
  132. return datetime.date(cint(t[0]), cint(t[1]), cint(t[2]))
  133. else:
  134. return ''
  135. def add_days(date, days, format='string'):
  136. """
  137. Adds `days` to the given `string_date`
  138. """
  139. import datetime
  140. if not date:
  141. date = now_datetime()
  142. if type(date) not in (datetime.datetime, datetime.date):
  143. date = getdate(date)
  144. dt = date + datetime.timedelta(days)
  145. if format=='string':
  146. return dt.strftime('%Y-%m-%d')
  147. else:
  148. return dt
  149. def add_months(string_date, months):
  150. import datetime
  151. return webnotes.conn.sql("select DATE_ADD('%s',INTERVAL '%s' MONTH)" % (getdate(string_date),months))[0][0]
  152. def add_years(string_date, years):
  153. import datetime
  154. return webnotes.conn.sql("select DATE_ADD('%s',INTERVAL '%s' YEAR)" % (getdate(string_date),years))[0][0]
  155. def date_diff(string_ed_date, string_st_date=None):
  156. import datetime
  157. return webnotes.conn.sql("SELECT DATEDIFF('%s','%s')" %(getdate(string_ed_date), getdate(string_st_date)))[0][0]
  158. def now_datetime():
  159. global user_time_zone
  160. from datetime import datetime
  161. from pytz import timezone
  162. # get localtime
  163. if not user_time_zone:
  164. user_time_zone = webnotes.conn.get_value('Control Panel', None, 'time_zone') \
  165. or 'Asia/Calcutta'
  166. # convert to UTC
  167. utcnow = timezone('UTC').localize(datetime.utcnow())
  168. # convert to user time zone
  169. return utcnow.astimezone(timezone(user_time_zone))
  170. def now():
  171. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  172. return now_datetime().strftime('%Y-%m-%d %H:%M:%S')
  173. def nowdate():
  174. """return current date as yyyy-mm-dd"""
  175. return now_datetime().strftime('%Y-%m-%d')
  176. def nowtime():
  177. """return current time in hh:mm"""
  178. return now_datetime().strftime('%H:%M')
  179. def get_first_day(dt, d_years=0, d_months=0):
  180. """
  181. Returns the first day of the month for the date specified by date object
  182. Also adds `d_years` and `d_months` if specified
  183. """
  184. import datetime
  185. # d_years, d_months are "deltas" to apply to dt
  186. y, m = dt.year + d_years, dt.month + d_months
  187. a, m = divmod(m-1, 12)
  188. return datetime.date(y+a, m+1, 1)
  189. def get_last_day(dt):
  190. """
  191. Returns last day of the month using:
  192. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  193. """
  194. import datetime
  195. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  196. user_format = None
  197. """
  198. User format specified in :term:`Control Panel`
  199. Examples:
  200. * dd-mm-yyyy
  201. * mm-dd-yyyy
  202. * dd/mm/yyyy
  203. """
  204. def formatdate(string_date):
  205. """
  206. Convers the given string date to :data:`user_format`
  207. """
  208. global user_format
  209. if not user_format:
  210. user_format = webnotes.conn.get_value('Control Panel', None, 'date_format')
  211. d = string_date.split('-');
  212. out = user_format
  213. return out.replace('dd', ('%.2i' % cint(d[2]))).replace('mm', ('%.2i' % cint(d[1]))).replace('yyyy', d[0])
  214. def dict_to_str(args, sep='&'):
  215. """
  216. Converts a dictionary to URL
  217. """
  218. import urllib
  219. t = []
  220. for k in args.keys():
  221. t.append(str(k)+'='+urllib.quote(str(args[k] or '')))
  222. return sep.join(t)
  223. def timestamps_equal(t1, t2):
  224. """Returns true if same the two string timestamps are same"""
  225. scrub = lambda x: x.replace(':', ' ').replace('-',' ').split()
  226. t1, t2 = scrub(t1), scrub(t2)
  227. if len(t1) != len(t2):
  228. return
  229. for i in range(len(t1)):
  230. if t1[i]!=t2[i]:
  231. return
  232. return 1
  233. def global_date_format(date):
  234. """returns date as 1 January 2012"""
  235. import datetime
  236. if isinstance(date, basestring):
  237. date = getdate(date)
  238. return cstr(cint(date.strftime('%d'))) + ' ' + month_name_full[int(date.strftime('%m'))] \
  239. + ' ' + date.strftime('%Y')
  240. # Datatype
  241. # ==============================================================================
  242. def isNull(v):
  243. """
  244. Returns true if v='' or v is `None`
  245. """
  246. return (v=='' or v==None)
  247. def has_common(l1, l2):
  248. """
  249. Returns true if there are common elements in lists l1 and l2
  250. """
  251. for l in l1:
  252. if l in l2:
  253. return 1
  254. return 0
  255. def flt(s):
  256. """
  257. Convert to float (ignore commas)
  258. """
  259. if isinstance(s, basestring): # if string
  260. s = s.replace(',','')
  261. try: tmp = float(s)
  262. except: tmp = 0
  263. return tmp
  264. def cint(s):
  265. """
  266. Convert to integer
  267. """
  268. try: tmp = int(float(s))
  269. except: tmp = 0
  270. return tmp
  271. def cstr(s):
  272. """
  273. Convert to string
  274. """
  275. if isinstance(s, basestring):
  276. return s
  277. elif s==None:
  278. return ''
  279. else:
  280. return str(s)
  281. def str_esc_quote(s):
  282. """
  283. Escape quotes
  284. """
  285. if s==None:return ''
  286. return s.replace("'","\'")
  287. def replace_newlines(s):
  288. """
  289. Replace newlines by '<br>'
  290. """
  291. if s==None:return ''
  292. return s.replace("\n","<br>")
  293. # ==============================================================================
  294. def parse_val(v):
  295. """
  296. Converts to simple datatypes from SQL query results
  297. """
  298. import datetime
  299. if type(v)==datetime.date:
  300. v = str(v)
  301. elif type(v)==datetime.timedelta:
  302. v = ':'.join(str(v).split(':')[:2])
  303. elif type(v)==datetime.datetime:
  304. v = str(v)
  305. elif type(v)==long: v=int(v)
  306. return v
  307. # ==============================================================================
  308. def fmt_money(amount, fmt = '%.2f'):
  309. """
  310. Convert to string with commas for thousands, millions etc
  311. """
  312. curr = webnotes.conn.get_value('Control Panel', None, 'currency_format') or 'Millions'
  313. val = 2
  314. if curr == 'Millions': val = 3
  315. if cstr(amount).find('.') == -1: temp = '00'
  316. else: temp = cstr(amount).split('.')[1]
  317. l = []
  318. minus = ''
  319. if flt(amount) < 0: minus = '-'
  320. amount = ''.join(cstr(amount).split(','))
  321. amount = cstr(abs(flt(amount))).split('.')[0]
  322. # main logic
  323. if len(cstr(amount)) > 3:
  324. nn = amount[len(amount)-3:]
  325. l.append(nn)
  326. amount = amount[0:len(amount)-3]
  327. while len(cstr(amount)) > val:
  328. nn = amount[len(amount)-val:]
  329. l.insert(0,nn)
  330. amount = amount[0:len(amount)-val]
  331. if len(amount) > 0: l.insert(0,amount)
  332. amount = ','.join(l)+'.'+temp
  333. amount = minus + amount
  334. return amount
  335. #
  336. # convet currency to words
  337. #
  338. def money_in_words(number, main_currency = None, fraction_currency=None):
  339. """
  340. Returns string in words with currency and fraction currency.
  341. """
  342. d = get_defaults()
  343. if not main_currency:
  344. main_currency = d.get('currency', 'INR')
  345. if not fraction_currency:
  346. fraction_currency = d.get('fraction_currency', 'paise')
  347. n = "%.2f" % flt(number)
  348. main, fraction = n.split('.')
  349. if len(fraction)==1: fraction += '0'
  350. out = main_currency + ' ' + in_words(main).title()
  351. if cint(fraction):
  352. out = out + ' and ' + in_words(fraction).title() + ' ' + fraction_currency
  353. return out + ' only.'
  354. #
  355. # convert number to words
  356. #
  357. def in_words(integer):
  358. """
  359. Returns string in words for the given integer.
  360. """
  361. in_million = webnotes.conn.get_default('currency_format')=='Millions' and 1 or 0
  362. n=int(integer)
  363. known = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
  364. 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen',
  365. 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety'}
  366. def psn(n, known, xpsn):
  367. import sys;
  368. if n in known: return known[n]
  369. bestguess, remainder = str(n), 0
  370. if n<=20:
  371. print >>sys.stderr, n, "How did this happen?"
  372. assert 0
  373. elif n < 100:
  374. bestguess= xpsn((n//10)*10, known, xpsn) + '-' + xpsn(n%10, known, xpsn)
  375. return bestguess
  376. elif n < 1000:
  377. bestguess= xpsn(n//100, known, xpsn) + ' ' + 'hundred'
  378. remainder = n%100
  379. else:
  380. if in_million:
  381. if n < 1000000:
  382. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  383. remainder = n%1000
  384. elif n < 1000000000:
  385. bestguess= xpsn(n//1000000, known, xpsn) + ' ' + 'million'
  386. remainder = n%1000000
  387. else:
  388. bestguess= xpsn(n//1000000000, known, xpsn) + ' ' + 'billion'
  389. remainder = n%1000000000
  390. else:
  391. if n < 100000:
  392. bestguess= xpsn(n//1000, known, xpsn) + ' ' + 'thousand'
  393. remainder = n%1000
  394. elif n < 10000000:
  395. bestguess= xpsn(n//100000, known, xpsn) + ' ' + 'lakh'
  396. remainder = n%100000
  397. else:
  398. bestguess= xpsn(n//10000000, known, xpsn) + ' ' + 'crore'
  399. remainder = n%10000000
  400. if remainder:
  401. if remainder >= 100:
  402. comma = ','
  403. else:
  404. comma = ''
  405. return bestguess + comma + ' ' + xpsn(remainder, known, xpsn)
  406. else:
  407. return bestguess
  408. return psn(n, known, psn)
  409. # Get Defaults
  410. # ==============================================================================
  411. def get_defaults(key=None):
  412. """
  413. Get dictionary of default values from the :term:`Control Panel`, or a value if key is passed
  414. """
  415. return webnotes.conn.get_defaults(key)
  416. def set_default(key, val):
  417. """
  418. Set / add a default value to :term:`Control Panel`
  419. """
  420. return webnotes.conn.set_default(key, val)
  421. #
  422. # Clear recycle bin
  423. #
  424. def clear_recycle_bin():
  425. sql = webnotes.conn.sql
  426. tl = sql('show tables')
  427. total_deleted = 0
  428. for t in tl:
  429. fl = [i[0] for i in sql('desc `%s`' % t[0])]
  430. if 'name' in fl:
  431. total_deleted += sql("select count(*) from `%s` where name like '__overwritten:%%'" % t[0])[0][0]
  432. sql("delete from `%s` where name like '__overwritten:%%'" % t[0])
  433. if 'parent' in fl:
  434. total_deleted += sql("select count(*) from `%s` where parent like '__oldparent:%%'" % t[0])[0][0]
  435. sql("delete from `%s` where parent like '__oldparent:%%'" % t[0])
  436. total_deleted += sql("select count(*) from `%s` where parent like 'oldparent:%%'" % t[0])[0][0]
  437. sql("delete from `%s` where parent like 'oldparent:%%'" % t[0])
  438. total_deleted += sql("select count(*) from `%s` where parent like 'old_parent:%%'" % t[0])[0][0]
  439. sql("delete from `%s` where parent like 'old_parent:%%'" % t[0])
  440. return "%s records deleted" % str(int(total_deleted))
  441. # Dictionary utils
  442. # ==============================================================================
  443. def remove_blanks(d):
  444. """
  445. Returns d with empty ('' or None) values stripped
  446. """
  447. empty_keys = []
  448. for key in d:
  449. if d[key]=='' or d[key]==None:
  450. # del d[key] raises runtime exception, using a workaround
  451. empty_keys.append(key)
  452. for key in empty_keys:
  453. del d[key]
  454. return d
  455. def pprint_dict(d, level=1, no_blanks=True):
  456. """
  457. Pretty print a dictionary with indents
  458. """
  459. if no_blanks:
  460. remove_blanks(d)
  461. # make indent
  462. indent, ret = '', ''
  463. for i in range(0,level): indent += '\t'
  464. # add lines
  465. comment, lines = '', []
  466. kl = d.keys()
  467. kl.sort()
  468. # make lines
  469. for key in kl:
  470. if key != '##comment':
  471. tmp = {key: d[key]}
  472. lines.append(indent + str(tmp)[1:-1] )
  473. # add comment string
  474. if '##comment' in kl:
  475. ret = ('\n' + indent) + '# ' + d['##comment'] + '\n'
  476. # open
  477. ret += indent + '{\n'
  478. # lines
  479. ret += indent + ',\n\t'.join(lines)
  480. # close
  481. ret += '\n' + indent + '}'
  482. return ret
  483. def get_common(d1,d2):
  484. """
  485. returns (list of keys) the common part of two dicts
  486. """
  487. return [p for p in d1 if p in d2 and d1[p]==d2[p]]
  488. def get_common_dict(d1, d2):
  489. """
  490. return common dictionary of d1 and d2
  491. """
  492. ret = {}
  493. for key in d1:
  494. if key in d2 and d2[key]==d1[key]:
  495. ret[key] = d1[key]
  496. return ret
  497. def get_diff_dict(d1, d2):
  498. """
  499. return common dictionary of d1 and d2
  500. """
  501. diff_keys = set(d2.keys()).difference(set(d1.keys()))
  502. ret = {}
  503. for d in diff_keys: ret[d] = d2[d]
  504. return ret
  505. def get_file_timestamp(fn):
  506. """
  507. Returns timestamp of the given file
  508. """
  509. import os
  510. from webnotes.utils import cint
  511. try:
  512. return str(cint(os.stat(fn).st_mtime))
  513. except OSError, e:
  514. if e.args[0]!=2:
  515. raise e
  516. else:
  517. return None
  518. # to be deprecated
  519. def make_esc(esc_chars):
  520. """
  521. Function generator for Escaping special characters
  522. """
  523. return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
  524. # esc / unescape characters -- used for command line
  525. def esc(s, esc_chars):
  526. """
  527. Escape special characters
  528. """
  529. for c in esc_chars:
  530. esc_str = '\\' + c
  531. s = s.replace(c, esc_str)
  532. return s
  533. def unesc(s, esc_chars):
  534. """
  535. UnEscape special characters
  536. """
  537. for c in esc_chars:
  538. esc_str = '\\' + c
  539. s = s.replace(esc_str, c)
  540. return s
  541. def strip_html(text):
  542. """
  543. removes anything enclosed in and including <>
  544. """
  545. import re
  546. return re.compile(r'<.*?>').sub('', text)
  547. def escape_html(text):
  548. html_escape_table = {
  549. "&": "&amp;",
  550. '"': "&quot;",
  551. "'": "&apos;",
  552. ">": "&gt;",
  553. "<": "&lt;",
  554. }
  555. return "".join(html_escape_table.get(c,c) for c in text)
  556. def get_doctype_label(dt=None):
  557. """
  558. Gets label of a doctype
  559. """
  560. if dt:
  561. res = webnotes.conn.sql("""\
  562. SELECT name, dt_label FROM `tabDocType Label`
  563. WHERE name=%s""", dt)
  564. return res and res[0][0] or dt
  565. else:
  566. res = webnotes.conn.sql("SELECT name, dt_label FROM `tabDocType Label`")
  567. dt_label_dict = {}
  568. for r in res:
  569. dt_label_dict[r[0]] = r[1]
  570. return dt_label_dict
  571. def get_label_doctype(label):
  572. """
  573. Gets doctype from its label
  574. """
  575. res = webnotes.conn.sql("""\
  576. SELECT name FROM `tabDocType Label`
  577. WHERE dt_label=%s""", label)
  578. return res and res[0][0] or label
  579. def get_system_managers_list():
  580. """Returns a list of system managers' email addresses"""
  581. system_managers_list = webnotes.conn.sql("""\
  582. SELECT DISTINCT p.name
  583. FROM tabUserRole ur, tabProfile p
  584. WHERE
  585. ur.parent = p.name AND
  586. ur.role='System Manager' AND
  587. p.docstatus<2 AND
  588. p.enabled=1 AND
  589. p.name not in ('Administrator', 'Guest')""", as_list=1)
  590. return [sysman[0] for sysman in system_managers_list]
  591. def pretty_date(iso_datetime):
  592. """
  593. Takes an ISO time and returns a string representing how
  594. long ago the date represents.
  595. Ported from PrettyDate by John Resig
  596. """
  597. if not iso_datetime: return ''
  598. from datetime import datetime
  599. import math
  600. if isinstance(iso_datetime, basestring):
  601. iso_datetime = datetime.strptime(iso_datetime, '%Y-%m-%d %H:%M:%S')
  602. now_dt = datetime.strptime(now(), '%Y-%m-%d %H:%M:%S')
  603. dt_diff = now_dt - iso_datetime
  604. # available only in python 2.7+
  605. # dt_diff_seconds = dt_diff.total_seconds()
  606. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  607. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  608. # differnt cases
  609. if dt_diff_seconds < 60.0:
  610. return 'just now'
  611. elif dt_diff_seconds < 120.0:
  612. return '1 minute ago'
  613. elif dt_diff_seconds < 3600.0:
  614. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  615. elif dt_diff_seconds < 7200.0:
  616. return '1 hour ago'
  617. elif dt_diff_seconds < 86400.0:
  618. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  619. elif dt_diff_days == 1.0:
  620. return 'Yesterday'
  621. elif dt_diff_days < 7.0:
  622. return '%s days ago' % cint(dt_diff_days)
  623. elif dt_diff_days < 31.0:
  624. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  625. elif dt_diff_days < 365.0:
  626. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  627. else:
  628. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))