terom@50: """ terom@50: Some additional helpers terom@50: """ terom@50: terom@54: import qmsk.web.helpers terom@50: terom@54: import datetime, calendar terom@54: terom@54: class Helpers (qmsk.web.helpers.Helpers) : terom@50: """ terom@54: Our set of helpers, inheriting from base helpers terom@50: """ terom@50: terom@54: def tz_name (self, tz) : terom@54: """ terom@54: Returns a string describing the given timezone terom@54: """ terom@50: terom@54: return str(tz) terom@50: terom@54: def fmt_date (self, date) : terom@54: """ terom@54: Formats a date terom@54: """ terom@54: terom@54: # XXX: hardcoded terom@54: return date.strftime('%Y-%m-%d') terom@54: terom@54: def fmt_month (self, date) : terom@54: """ terom@54: Formats a month terom@54: """ terom@54: terom@54: return date.strftime('%B %Y') terom@54: terom@54: def fmt_weekday (self, wday) : terom@54: """ terom@54: Formats an abbreviated weekday name terom@54: """ terom@54: terom@54: return calendar.day_abbr[wday] terom@54: terom@54: def build_date (self, month, mday) : terom@54: """ terom@54: Returns a datetime.date for the given (month.year, month.month, mday) terom@54: """ terom@54: terom@54: return datetime.date(month.year, month.month, mday) terom@58: terom@58: def now (self) : terom@58: """ terom@58: Build current time terom@58: """ terom@58: terom@58: return self.ctx['timezone'].localize(datetime.datetime.now()) terom@58: terom@58: def today (self) : terom@58: """ terom@58: Build today's date terom@58: """ terom@58: terom@58: return self.now().date() terom@54: terom@54: def is_today (self, date) : terom@54: """ terom@58: Checks if the given date is today terom@54: """ terom@54: terom@54: # construct current date terom@58: return date == self.today() terom@55: terom@58: def is_this_month (self, month) : terom@58: """ terom@58: Checks the given month is the current month terom@58: """ terom@58: terom@58: today = self.today() terom@58: terom@58: return (month.year == today.year and month.month == today.month) terom@58: terom@55: def prev_month_year (self, month) : terom@55: """ terom@55: Returns the year of the month before the given one terom@55: """ terom@55: terom@55: if month.month == 1 : terom@55: return month.year - 1 terom@55: terom@55: else : terom@55: return month.year terom@55: terom@55: def next_month_year (self, month) : terom@55: """ terom@55: Returns the year of the month after the given one terom@55: """ terom@55: terom@55: if month.month == 12 : terom@55: return month.year + 1 terom@55: terom@55: else : terom@55: return month.year terom@55: terom@55: def prev_month (self, month) : terom@55: """ terom@55: Returns the month before the given one terom@55: """ terom@55: terom@55: if month.month == 1 : terom@55: return 12 terom@55: terom@55: else : terom@55: return month.month - 1 terom@55: terom@55: def next_month (self, month) : terom@55: """ terom@55: Returns the month after the given one terom@55: """ terom@55: terom@55: if month.month == 12 : terom@55: return 1 terom@55: terom@55: else : terom@55: return month.month + 1 terom@55: