Package bw :: Package aphrodite :: Module web
[frames] | no frames]

Module web

This module provides helper functions which can be used to auto generate AXML tag blocks in templates.

Classes
  widgets
This class contains helper functions for creating AXML Widgets
Functions
 
image_tag(source, alt=None, size=None, **options)
Returns an image tag for the specified 'source'
 
auto_discovery_link_tag(source, type="rss", **kwargs)
Returns a link tag allowing browsers and news readers to auto-detect RSS or ATOM feeds for the current page.
 
javascript_include_tag(*sources, **options)
Returns script include tags for the specified sources.
 
javascript_tag(content, **html_options)
Returns a javascript tag with the 'content' inside.
 
cdata_section(content="")
 
link_to(name, url="", **html_options)
Create a link of the given 'name' using a URL created by the set of 'options'.
 
convert_options_to_javascript(confirm=None, method=None, post=None, popup=None, **html_options)
 
confirm_javascript_function(confirm)
 
popup_javascript_function(popup)
 
method_javascript_function(method)
 
link_to_function(name, function, **html_options)
Returns a link that will trigger a javascript 'function' using the onclick handler and return false afterwards.
 
button_to_function(name, function, **html_options)
Returns a button that will trigger a javascript 'function' using the onclick handler and return false afterwards.
 
truncate(text, length=30, truncate_string="...")
Truncate a string to a limited number of characters
 
highlight(text, phrase, className='highlight')
Highlight a phrase in text
 
auto_link_email_addresses(text)
Convert all email addresses in text to mailto: links
 
auto_link_urls(text, **href_options)
Convert all urls in text to href links
 
auto_link(text, **href_options)
Turns all urls and email addresses into clickable links.
 
number_with_delimiter(number, delimiter=",", separator=".")
 
number_to_currency(number, units="$", precision=2, separator=".", delimiter=",")
Formats a 'number' into a currency string.
 
number_to_percentage(number, precision=2, separator=".")
Formats a 'number' into a percentage string
 
number_to_human_size(value, precision=1)
Format the value like a 'human-readable' file size (i.e.
 
seconds_to_human_time(value)
Format the value into human readable time.
 
time_ago_in_words(from_time, to_time=time.time())
Return from_time in human readable format.
 
escape_javascript(javascript)
Escape carriage returns and single and double quotes for JavaScript segments.
 
escape_quotes(term)
 
html_escape(s)
HTML-escape a string or object
 
fix_double_escape(escaped)
Fix double-escaped entities, such as &, {, etc
 
escape_once(html)
Escapes a given string without affecting existing escaped entities.
 
options_for_select(container, selected=None)
Creates select options from 'container' which can be a (list, tuple, dict) If container is a list or tuple, then the text is the first element in the entry and the value is the second element.
 
options_for_select_from_dicts(container, name_key, value_key=None, selected=None)
Creates select options from dicts in a container
 
options_for_select_from_objects(container, name_attr, value_attr=None, selected=None)
Creates select options from objects in a container
 
list_of_links(container, **html_options)
 
list_of_links_from_dicts(container, name_key, value_key=None, **html_options)
Creates a list of links from dicts in a container
 
list_of_links_from_objects(container, name_attr, value_attr=None, **html_options)
Creates a list of links from objects in a container
 
property_list(container, **html_options)
Creates a table with property names/keys in the left column and property values in the right column.
 
property_list_from_dicts(container, name_key, value_key=None, **html_options)
Create a property list from a list of dictionaries where each dictionary contains a key-value pair.
 
property_list_from_objects(container, name_attr, value_attr=None, **html_options)
Create a property list from a list of objects where each object contains a key-value pair.
Variables
  AUTO_LINK_RE = re.compile(r...

Imports: cherrypy, os, urlparse, re, math, time, cgi, mimetypes, localtime, strftime, Axml, json


Function Details

image_tag(source, alt=None, size=None, **options)

 

Returns an image tag for the specified 'source'

Examples:

>>> image_tag("xml.png")
'<img alt="Xml" src="/resources/images/xml.png" />'
Parameters:
  • source - The source URL of the image. The URL is prepended with "[appname]/resources/images" unless it begins with "http://"
  • alt - The image's alt tag. Defaults to the image's filename (title cased)
  • size - The image's size, specified as XxY. "30x25" becomes width="30", height="25". "x60" becomes height="60". Optional.

auto_discovery_link_tag(source, type="rss", **kwargs)

 

Returns a link tag allowing browsers and news readers to auto-detect RSS or ATOM feeds for the current page.

Parameters:
  • source - The URL of the feed. It is prepended with the application name unless it is already there
  • type - The extension or the mime-type of the feed. If the extension is provided, the mime-type is guessed. Defaults to rss.

javascript_include_tag(*sources, **options)

 

Returns script include tags for the specified sources.

Parameters:
  • sources - Each source's URL path is prepended with "[appname]/resources/js" unless the full path is specified.
  • options - Additional attributes to added to the script tag.

    Specify the keyword argument ``defer=True`` to enable the script defer attribute.

    >>>print javascript_include_tag('myapp.js', 'myotherapp.js') <script src="/resources/js/myapp.js" type="text/javascript"></script> <script src="/resources/js/myotherapp.js" type="text/javascript"></script>

javascript_tag(content, **html_options)

 

Returns a javascript tag with the 'content' inside.

Parameters:
  • content - The content to put inside the script tag. Example:
    >>> javascript_tag("alert('This is an Alert')")
    '<script type="text/javascript">\n//<![CDATA[\nalert(\'This is an Alert\')\n//]]>\n</script>'

link_to(name, url="", **html_options)

 

Create a link of the given 'name' using a URL created by the set of 'options'.

Parameters:
  • url - The url to link to.

    If 'confirm' is passed in html_options, the link is guarded by a confirm popup with the value as question.

    If 'popup'=True is passed, then the link opens in a popup window.

link_to_function(name, function, **html_options)

 

Returns a link that will trigger a javascript 'function' using the onclick handler and return false afterwards.

Example:

       >>>link_to_function("Greeting", "alert('Hello World!')")
       <a href="#" onclick="alert('Hello World!'); return false;">Greeting</a>
Parameters:
  • name - The content of the link tag
  • function - The javascript function call

button_to_function(name, function, **html_options)

 

Returns a button that will trigger a javascript 'function' using the onclick handler and return false afterwards.

Example:

       >>>button_to_function("Greeting", "alert('Hello World!')")
       <input type="button" value="Greeting" onclick="alert('Hello World!'); " />
Parameters:
  • name - The content of the link tag.
  • function - The javascript function call.

truncate(text, length=30, truncate_string="...")

 

Truncate a string to a limited number of characters

Parameters:
  • text - The string to truncate
  • length - The maximum length of the string. Defaults to 30. Includes truncate_string.
  • truncate_string - The suffix to use when truncating a string. Default: "..."

highlight(text, phrase, className='highlight')

 

Highlight a phrase in text

Parameters:
  • text - The text in which to look for phrases.
  • phrase - The phrase to look for
  • className - The class name to use to do highlighting. Defaults to "highlight"

auto_link_email_addresses(text)

 

Convert all email addresses in text to mailto: links

Parameters:
  • text - The text to convert

auto_link_urls(text, **href_options)

 

Convert all urls in text to href links

Parameters:
  • text - The text to convert
  • href_options - The additional attributes to add to the a tag.

auto_link(text, **href_options)

 

Turns all urls and email addresses into clickable links.

Example:

       auto_link("Go to http://www.planetpython.com and say hello to guido@python.org")
       'Go to <a href="http://www.planetpython.com">http://www.planetpython.com</a> and say hello to <a href="mailto:guido@python.org">guido@python.org</a>'   
Parameters:
  • text - The text to convert
  • href_options - The additional attributes to add to the a tag.

number_to_currency(number, units="$", precision=2, separator=".", delimiter=",")

 

Formats a 'number' into a currency string.

Parameters:
  • number - The number to convert
  • precision - Indicates the level of precision. Defaults to 2
  • unit - Sets the currency type. Defaults to "$". Use &pound; for GBP.
  • separator - Used to set what the unit separation should be. Defaults to "."
  • delimiter - The delimiter character to use. Defaults to ","

    Examples:

    >>> number_to_currency(1234567890.50, unit="&pound;", separator=",", delimiter="")
    '&pound;1234567890,50'

number_to_percentage(number, precision=2, separator=".")

 

Formats a 'number' into a percentage string

Parameters:
  • number - The number as a float between 0 and 1.

number_to_human_size(value, precision=1)

 

Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 bytes, etc).

Parameters:
  • value - The number in bytes.

Note: Modified from http://svn.subway.python-hosting.com/trunk/subway/helpers.py

seconds_to_human_time(value)

 

Format the value into human readable time.

Parameters:
  • value - The number in seconds.

Note: Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since

time_ago_in_words(from_time, to_time=time.time())

 

Return from_time in human readable format. e.g. If from_time is a few minutes before to_time, The string is "A few minutes ago". If if it is after to_time, then the string is "In a few minutes."

Parameters:
  • from_time - The timestamp to convert
  • to_time - The reference timestamp. Defaults to the current time.

html_escape(s)

 

HTML-escape a string or object

This converts any non-string objects passed into it to strings (actually, using ``unicode()``). All values returned are non-unicode strings (using ``&#num;`` entities for all non-ASCII characters).

None is treated specially, and returns the empty string.

escape_once(html)

 

Escapes a given string without affecting existing escaped entities.

>>> escape_once("1 < 2 &amp; 3")
'1 &lt; 2 &amp; 3'

options_for_select(container, selected=None)

 

Creates select options from 'container' which can be a (list, tuple, dict) If container is a list or tuple, then the text is the first element in the entry and the value is the second element. If it is a dictionary, then the key is the text and the value is the value.

property_list(container, **html_options)

 

Creates a table with property names/keys in the left column and property values in the right column.

Example:

       property_list(p.getObject("root"))
Parameters:
  • container - The dictionary like object containing property name-value pairs
  • html_options - Additional attributes to be added to the table tag.

property_list_from_dicts(container, name_key, value_key=None, **html_options)

 

Create a property list from a list of dictionaries where each dictionary contains a key-value pair.

Parameters:
  • container - The list of dictionaries
  • name_key - The key of the property name (or property name-value tuple)
  • value_key - The key of the property value (If this is none, then param_key must point to a tuple)
  • html_options - Additional attributes to add to the table tag.

property_list_from_objects(container, name_attr, value_attr=None, **html_options)

 

Create a property list from a list of objects where each object contains a key-value pair.

Parameters:
  • container - The list of objects
  • name_attr - The name of the attribute which contains the property name (or property name-value tuple)
  • value_attr - The name of the attribute which contains the property value (If this is none, then value_attr must point to a tuple)
  • html_options - Additional attributes to add to the table tag.

Variables Details

AUTO_LINK_RE

Value:
re.compile(r"""
				(                          # leading text
				  <\w+.*?>|                # leading HTML tag, or
				  [^=!:'"/]|               # leading punctuation, or 
				  ^                        # beginning of line
				)
				(
				  (?:https?://)|           # protocol spec, or
...