"""
Some additional helpers
"""
import datetime, calendar
import qmsk.web.helpers
import preferences, urls, config
class Helpers (qmsk.web.helpers.Helpers) :
"""
Our set of helpers, inheriting from base helpers
"""
def tz_name (self, tz) :
"""
Returns a string describing the given timezone
"""
return self.now().strftime(config.TIMEZONE_FMT)
def fmt_month (self, date) :
"""
Formats a month
"""
return date.strftime(config.MONTH_FMT)
def fmt_weekday (self, wday) :
"""
Formats an abbreviated weekday name
"""
return calendar.day_abbr[wday]
def build_date (self, month, mday) :
"""
Returns a datetime.date for the given (month.year, month.month, mday)
"""
return datetime.date(month.year, month.month, mday)
def now (self) :
"""
Build current time
"""
return self.ctx['prefs'][preferences.timezone].localize(datetime.datetime.now())
def today (self) :
"""
Build today's date
"""
return self.now().date()
def is_today (self, date) :
"""
Checks if the given date is today
"""
# construct current date
return date == self.today()
def is_this_month (self, month) :
"""
Checks the given month is the current month
"""
today = self.today()
return (month.year == today.year and month.month == today.month)
def prev_month_year (self, month) :
"""
Returns the year of the month before the given one
"""
if month.month == 1 :
return month.year - 1
else :
return month.year
def next_month_year (self, month) :
"""
Returns the year of the month after the given one
"""
if month.month == 12 :
return month.year + 1
else :
return month.year
def prev_month (self, month) :
"""
Returns the month before the given one
"""
if month.month == 1 :
return 12
else :
return month.month - 1
def next_month (self, month) :
"""
Returns the month after the given one
"""
if month.month == 12 :
return 1
else :
return month.month + 1
def fmt_time (self, time=None) :
"""
Format given time, or current time
"""
# defaults
if not time :
time = self.now()
return time.strftime(self.ctx['prefs'][preferences.time_format])
def fmt_date (self, date=None) :
"""
Format given date, or current date
"""
# defaults
if not date :
date = self.now()
return date.strftime(self.ctx['prefs'][preferences.date_format])
def build_url (self, url, **params) :
"""
Build URL with our request object
"""
return url.build(self.ctx['req'], **params)
def utc_timestamp (self, dtz) :
"""
Build an UTC timestamp from the given datetime
"""
return urls.types['ts'].build(dtz)
def skip_next (self, count, skip) :
"""
Return skip offset for next page
"""
return count + skip
def skip_page (self, count, page) :
"""
Skip to page
"""
if page :
return count * page
else :
return None
def skip_prev (self, count, skip) :
"""
Return skip offset for previous page, None for first page
"""
if skip > count :
return skip - count
else :
return None
def max (self, *values) :
"""
Returns the largest of the given values
"""
return max(values)
def select_options (self, key_values, selected_key) :
"""
Render a series of <option> tags for <select>
"""
return '\n'.join(
'\t<option%s%s>%s</option>' % (
' value="%s"' % key if key != value else '',
' selected="selected"' if key == selected_key else '',
value
) for key, value in key_values
)