/lib/galaxy/web/framework/helpers/__init__.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 114 lines · 54 code · 16 blank · 44 comment · 8 complexity · 13463cfcdcb5915caa5f9cf60bd8334a MD5 · raw file

  1. """
  2. Galaxy web framework helpers
  3. """
  4. import time
  5. from cgi import escape
  6. from datetime import datetime, timedelta
  7. from galaxy import eggs
  8. from galaxy.util import hash_util
  9. from galaxy.util.json import to_json_string
  10. eggs.require( "MarkupSafe" ) #required by WebHelpers
  11. eggs.require( "WebHelpers" )
  12. from webhelpers import date
  13. from webhelpers.html.tags import stylesheet_link, javascript_link
  14. eggs.require( "Routes" )
  15. from routes import url_for
  16. server_starttime = int(time.time())
  17. def time_ago( x ):
  18. """
  19. Convert a datetime to a string.
  20. """
  21. delta = timedelta(weeks=1)
  22. # If the date is more than one week ago, then display the actual date instead of in words
  23. if (datetime.utcnow() - x) > delta: # Greater than a week difference
  24. return x.strftime("%b %d, %Y")
  25. else:
  26. date_array = date.distance_of_time_in_words( x, datetime.utcnow() ).replace(",", "").split(" ")
  27. return "~%s %s ago" % (date_array[0], date_array[1])
  28. def iff( a, b, c ):
  29. """
  30. Ternary shortcut
  31. """
  32. if a:
  33. return b
  34. else:
  35. return c
  36. def truncate(content, length=100, suffix='...'):
  37. """
  38. Smart string truncation
  39. """
  40. if len(content) <= length:
  41. return content
  42. else:
  43. return content[:length].rsplit(' ', 1)[0] + suffix
  44. # Quick helpers for static content
  45. def css( *args ):
  46. """
  47. Take a list of stylesheet names (no extension) and return appropriate string
  48. of link tags.
  49. Cache-bust with time that server started running on
  50. """
  51. return "\n".join( [ stylesheet_link( url_for( "/static/style/%s.css?v=%s" % (name, server_starttime) ) ) for name in args ] )
  52. def js_helper( prefix, *args ):
  53. """
  54. Take a prefix and list of javascript names and return appropriate
  55. string of script tags.
  56. Cache-bust with time that server started running on
  57. """
  58. return "\n".join( [ javascript_link( url_for( "/%s%s.js?v=%s" % (prefix, name, server_starttime ) ) ) for name in args ] )
  59. def js( *args ):
  60. """
  61. Take a prefix and list of javascript names and return appropriate
  62. string of script tags.
  63. """
  64. return js_helper( 'static/scripts/', *args )
  65. def templates( *args ):
  66. """
  67. Take a list of template names (no extension) and return appropriate
  68. string of script tags.
  69. """
  70. return js_helper( 'static/scripts/templates/compiled/', *args )
  71. # Hashes
  72. def md5( s ):
  73. """
  74. Return hex encoded md5 hash of string s
  75. """
  76. m = hash_util.md5()
  77. m.update( s )
  78. return m.hexdigest()
  79. # Unicode help
  80. def to_unicode( a_string ):
  81. """
  82. Convert a string to unicode in utf-8 format; if string is already unicode,
  83. does nothing because string's encoding cannot be determined by introspection.
  84. """
  85. a_string_type = type ( a_string )
  86. if a_string_type is str:
  87. return unicode( a_string, 'utf-8' )
  88. elif a_string_type is unicode:
  89. return a_string
  90. def is_true ( val ):
  91. """
  92. Returns true if input is a boolean and true or is a string and looks like a true value.
  93. """
  94. return val == True or val in [ 'True', 'true', 'T', 't' ]