terom@50: """ terom@50: Some additional helpers terom@50: """ terom@50: terom@60: import datetime, calendar terom@60: terom@54: import qmsk.web.helpers terom@50: terom@60: import preferences 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_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@62: return self.ctx['prefs'][preferences.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@60: terom@60: def fmt_time (self, time=None) : terom@60: """ terom@60: Format given time, or current time terom@60: """ terom@60: terom@60: # defaults terom@60: if not time : terom@60: time = self.now() terom@55: terom@60: return time.strftime(self.ctx['prefs'][preferences.time_format]) terom@60: terom@60: def fmt_date (self, date=None) : terom@60: """ terom@60: Format given date, or current date terom@60: """ terom@60: terom@60: # defaults terom@60: if not date : terom@60: date = self.now() terom@60: terom@60: return date.strftime(self.ctx['prefs'][preferences.date_format]) terom@60: terom@63: def build_url (self, url, **params) : terom@63: """ terom@63: Build URL with our request object terom@63: """ terom@63: terom@63: return url.build(self.ctx['req'], **params) terom@63: