1 | import pkg_resources |
---|
2 | |
---|
3 | pkg_resources.require( "WebHelpers" ) |
---|
4 | from webhelpers import * |
---|
5 | |
---|
6 | from galaxy.util.json import to_json_string |
---|
7 | from galaxy.util import hash_util |
---|
8 | from datetime import datetime, timedelta |
---|
9 | |
---|
10 | from cgi import escape |
---|
11 | |
---|
12 | # If the date is more than one week ago, then display the actual date instead of in words |
---|
13 | def time_ago( x ): |
---|
14 | delta = timedelta(weeks=1) |
---|
15 | |
---|
16 | if (datetime.utcnow() - x) > delta: # Greater than a week difference |
---|
17 | return x.strftime("%b %d, %Y") |
---|
18 | else: |
---|
19 | return date.distance_of_time_in_words( x, datetime.utcnow() ) + " ago" |
---|
20 | |
---|
21 | def iff( a, b, c ): |
---|
22 | if a: |
---|
23 | return b |
---|
24 | else: |
---|
25 | return c |
---|
26 | |
---|
27 | # Quick helpers for static content |
---|
28 | |
---|
29 | def css( *args ): |
---|
30 | """ |
---|
31 | Take a list of stylesheet names (no extension) and return appropriate string |
---|
32 | of link tags. |
---|
33 | |
---|
34 | TODO: This has a hardcoded "?v=X" to defeat caching. This should be done |
---|
35 | in a better way. |
---|
36 | """ |
---|
37 | return "\n".join( [ stylesheet_link_tag( "/static/style/" + name + ".css?v=3" ) for name in args ] ) |
---|
38 | |
---|
39 | def js( *args ): |
---|
40 | """ |
---|
41 | Take a list of javascript names (no extension) and return appropriate |
---|
42 | string of script tags. |
---|
43 | |
---|
44 | TODO: This has a hardcoded "?v=X" to defeat caching. This should be done |
---|
45 | in a better way. |
---|
46 | """ |
---|
47 | return "\n".join( [ javascript_include_tag( "/static/scripts/" + name + ".js?v=7" ) for name in args ] ) |
---|
48 | |
---|
49 | # Hashes |
---|
50 | |
---|
51 | def md5( s ): |
---|
52 | """ |
---|
53 | Return hex encoded md5 hash of string s |
---|
54 | """ |
---|
55 | m = hash_util.md5() |
---|
56 | m.update( s ) |
---|
57 | return m.hexdigest() |
---|
58 | |
---|
59 | # Unicode help |
---|
60 | |
---|
61 | def to_unicode( a_string ): |
---|
62 | """ |
---|
63 | Convert a string to unicode in utf-8 format; if string is already unicode, |
---|
64 | does nothing because string's encoding cannot be determined by introspection. |
---|
65 | """ |
---|
66 | a_string_type = type ( a_string ) |
---|
67 | if a_string_type is str: |
---|
68 | return unicode( a_string, 'utf-8' ) |
---|
69 | elif a_string_type is unicode: |
---|
70 | return a_string |
---|
71 | |
---|