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.

__init__.py 20 KiB

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