Utilities

These functions are not dependent on Flask. They implement common patterns in Flask-based applications.

class coaster.LabeledEnum

Labeled enumerations. Declarate an enumeration with values and labels (for use in UI):

>>> class MY_ENUM(LabeledEnum):
...    FIRST = (1, "First")
...    SECOND = (2, "Second")

LabeledEnum will convert any attribute that is a 2-tuple into a value and label pair. Access values as direct attributes of the enumeration:

>>> MY_ENUM.FIRST
1
>>> MY_ENUM.SECOND
2

Access labels via dictionary lookup on the enumeration:

>>> MY_ENUM[MY_ENUM.FIRST]
'First'
>>> MY_ENUM[2]
'Second'

Retrieve a full list of values and labels with .items():

>>> MY_ENUM.items()
[(1, 'First'), (2, 'Second')]
coaster.buid()[source]

Return a new random id that is exactly 22 characters long. See http://en.wikipedia.org/wiki/Base64#Variants_summary_table for URL-safe Base64

>>> len(newid())
22
>>> newid() == newid()
False
>>> isinstance(newid(), unicode)
True
coaster.check_password(reference, attempt)[source]

Compare a reference password with the user attempt.

>>> check_password('{PLAIN}foo', 'foo')
True
>>> check_password(u'{PLAIN}bar', 'bar')
True
>>> check_password(u'{UNKNOWN}baz', 'baz')
False
>>> check_password(u'no-encoding', u'no-encoding')
False
>>> check_password(u'{SSHA}q/uVU8r15k/9QhRi92CWUwMJu2DM6TUSpp25', u're-foo')
True
>>> check_password('{SSHA}q/uVU8r15k/9QhRi92CWUwMJu2DM6TUSpp25', 're-foo')
True
coaster.format_currency(value, decimals=2)[source]

Return a number suitably formatted for display as currency, with thousands separated by commas and up to two decimal points.

>>> format_currency(1000)
'1,000'
>>> format_currency(100)
'100'
>>> format_currency(999.95)
'999.95'
>>> format_currency(99.95)
'99.95'
>>> format_currency(100000)
'100,000'
>>> format_currency(1000.00)
'1,000'
>>> format_currency(1000.41)
'1,000.41'
>>> format_currency(23.21, decimals=3)
'23.210'
>>> format_currency(123456789.123456789)
'123,456,789.12'
coaster.get_email_domain(email)[source]

Return the domain component of an email address. Returns None if the provided string cannot be parsed as an email address.

>>> get_email_domain('test@example.com')
'example.com'
>>> get_email_domain('test+trailing@example.com')
'example.com'
>>> get_email_domain('foobar')
coaster.getbool(value)[source]

Returns a boolean from any of a range of values. Returns None for unrecognized values. Numbers other than 0 and 1 are considered unrecognized.

>>> getbool(True)
True
>>> getbool(1)
True
>>> getbool('1')
True
>>> getbool('t')
True
>>> getbool(2)
>>> getbool(0)
False
>>> getbool(False)
False
>>> getbool('n')
False
coaster.make_name(text, delim=u'-', maxlength=50, checkused=None)[source]

Generate a Unicode name slug. If a checkused filter is provided, it will be called with the candidate. If it returns True, make_name will add counter numbers starting from 1 until a suitable candidate is found.

>>> make_name('This is a title')
u'this-is-a-title'
>>> make_name('Invalid URL/slug here')
u'invalid-url-slug-here'
>>> make_name('this.that')
u'this-that'
>>> make_name('this:that')
u'this-that'
>>> make_name("How 'bout this?")
u'how-bout-this'
>>> make_name(u"How’s that?")
u'hows-that'
>>> make_name(u'K & D')
u'k-d'
>>> make_name('billion+ pageviews')
u'billion-pageviews'
>>> make_name(u'हिन्दी slug!') == u'हिन्दी-slug'
True
>>> make_name(u'__name__', delim=u'_')
u'name'
>>> make_name(u'how_about_this', delim=u'_')
u'how_about_this'
>>> make_name(u'and-that', delim=u'_')
u'and_that'
>>> make_name(u'Umlauts in Mötörhead')
u'umlauts-in-motorhead'
>>> make_name('Candidate', checkused=lambda c: c in ['candidate', 'candidate1'])
u'candidate2'
>>> make_name('Long title, but snipped', maxlength=20)
u'long-title-but-snipp'
>>> len(make_name('Long title, but snipped', maxlength=20))
20
>>> make_name('Long candidate', maxlength=10, checkused=lambda c: c in ['long-candi', 'long-cand1'])
u'long-cand2'
coaster.make_password(password, encoding=u'SSHA')[source]

Make a password with PLAIN or SSHA encoding.

>>> make_password('foo', encoding='PLAIN')
u'{PLAIN}foo'
>>> make_password(u'bar', encoding='PLAIN')
u'{PLAIN}bar'
>>> make_password(u're-foo')[:6]
u'{SSHA}'
>>> make_password('bar-foo')[:6]
u'{SSHA}'
>>> make_password('foo') == make_password('foo')
False
>>> check_password(make_password('ascii'), 'ascii')
True
>>> check_password(make_password('mixed'), u'mixed')
True
>>> check_password(make_password(u'unicode'), u'unicode')
True
coaster.md5sum(data)[source]

Return md5sum of data as a 32-character string.

>>> md5sum('random text')
'd9b9bec3f4cc5482e7c5ef43143e563a'
>>> md5sum(u'random text')
'd9b9bec3f4cc5482e7c5ef43143e563a'
>>> len(md5sum('random text'))
32
coaster.newid()

Return a new random id that is exactly 22 characters long. See http://en.wikipedia.org/wiki/Base64#Variants_summary_table for URL-safe Base64

>>> len(newid())
22
>>> newid() == newid()
False
>>> isinstance(newid(), unicode)
True
coaster.newpin(digits=4)[source]

Return a random numeric string with the specified number of digits, default 4.

>>> len(newpin())
4
>>> len(newpin(5))
5
>>> isinstance(int(newpin()), int)
True
coaster.newsecret()[source]

Make a secret key for email confirmation and all that stuff.

>>> len(newsecret())
44
>>> newsecret() == newsecret()
False
coaster.sanitize_html(value, valid_tags={'em': [], 'blockquote': [], 'h3': [], 'h6': [], 'h4': [], 'h5': [], 'br': [], 'strong': [], 'a': ['href', 'title', 'target'], 'ol': [], 'sub': [], 'ul': [], 'li': [], 'p': [], 'sup': []})[source]

Strips unwanted markup out of HTML.

Deprecated since version 0.2.5: Use the bleach library instead.

coaster.simplify_text(text)[source]

Simplify text to allow comparison.

>>> simplify_text("Awesome Coder wanted at Awesome Company")
'awesome coder wanted at awesome company'
>>> simplify_text("Awesome Coder, wanted  at Awesome Company! ")
'awesome coder wanted at awesome company'
>>> simplify_text(u"Awesome Coder, wanted  at Awesome Company! ")
u'awesome coder wanted at awesome company'
coaster.sorted_timezones()

Return a list of timezones sorted by offset from UTC.

coaster.valid_username(candidate)[source]

Check if a username is valid.

>>> valid_username('example person')
False
>>> valid_username('example_person')
False
>>> valid_username('exampleperson')
True
>>> valid_username('example-person')
True
>>> valid_username('a')
True
>>> valid_username('a-') or valid_username('ab-') or valid_username('-a') or valid_username('-ab')
False

Related Topics

This Page