Table Of Contents

Previous topic

doc — Document (ORM)

Next topic

email_lib — Email

This Page

utils — Utilities Module

Date and Time Functions

utils.user_format

User format specified in Control Panel

Examples:

  • dd-mm-yyyy
  • mm-dd-yyyy
  • dd/mm/yyyy
utils.getdate(string_date)
Coverts string date (yyyy-mm-dd) to datetime.date object
utils.add_days(string_date, days)
Adds days to the given string_date
utils.now()
Returns time.strftime(‘%Y-%m-%d %H:%M:%S’)
utils.nowdate()
Returns time.strftime(‘%Y-%m-%d’)
utils.get_first_day(date, d_years=0, d_months=0)
Returns the first day of the month for the date specified by date object Also adds d_years and d_months if specified
utils.get_last_day(dt)
Returns last day of the month using: get_first_day(dt, 0, 1) + datetime.timedelta(-1)
utils.formatdate(dt)
Convers the given string date to user_format

Datatype Conversions

utils.dict_to_str(args, sep='&')
Converts a dictionary to URL
utils.isNull(v)
Returns true if v=’’ or v is None
utils.has_common(l1, l2)
Returns true if there are common elements in lists l1 and l2
utils.flt(s)
Convert to float (ignore commas)
utils.cint(s)
Convert to integer
utils.cstr(s)
Convert to string
utils.str_esc_quote(s)
Escape quotes
utils.replace_newlines(s)
Replace newlines by ‘<br>’
utils.parse_val(v)
Converts to simple datatypes from SQL query results
utils.fmt_money(amount, fmt = '%.2f')
Convert to string with commas for thousands, millions etc

Defaults

utils.get_defaults()
Get dictionary of default values from the Control Panel
utils.set_default(key, val)
Set / add a default value to Control Panel

File (BLOB) Functions

utils.get_file(fname)
Returns result set of ((fieldname, blobcontent, lastmodified),) for a file of name or id fname

Email Functions

utils.validate_email_add(email_str)
Validates the email string
utils.sendmail(recipients, sender='', msg='', subject='[, No Subject], ', parts=[], cc=[], attach=[])
Send an email. For more details see email_lib.sendmail()

Other Functions

utils.getCSVelement(v)

Returns the CSV value of v, For example:

  • apple becomes “apple”
  • hi”there becomes “hi”“there”
utils.generate_hash()
Generates reandom hash for session id
utils.getTraceback()
Returns the traceback of the Exception