helpers.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 07:06:49 +0200
changeset 60 759369a79527
parent 58 ce028d356e1f
child 62 e7ca94b94a4e
permissions -rw-r--r--
improve form styling
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Some additional helpers
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
     5
import datetime, calendar
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
     6
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     7
import qmsk.web.helpers
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
     9
import preferences
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    10
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    11
class Helpers (qmsk.web.helpers.Helpers) :
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    13
        Our set of helpers, inheriting from base helpers
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    """
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    16
    def tz_name (self, tz) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    17
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    18
            Returns a string describing the given timezone
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    19
        """
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    21
        return str(tz)
50
f13cf27a360b implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    23
    def fmt_month (self, date) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    24
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    25
            Formats a month
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    26
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    27
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    28
        return date.strftime('%B %Y')
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    29
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    30
    def fmt_weekday (self, wday) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    31
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    32
            Formats an abbreviated weekday name
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    33
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    34
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    35
        return calendar.day_abbr[wday]
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    36
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    37
    def build_date (self, month, mday) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    38
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    39
            Returns a datetime.date for the given (month.year, month.month, mday)
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    40
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    41
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    42
        return datetime.date(month.year, month.month, mday)
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    43
    
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    44
    def now (self) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    45
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    46
            Build current time
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    47
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    48
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    49
        return self.ctx['timezone'].localize(datetime.datetime.now())
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    50
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    51
    def today (self) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    52
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    53
            Build today's date
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    54
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    55
        
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    56
        return self.now().date()
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    57
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    58
    def is_today (self, date) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    59
        """
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    60
            Checks if the given date is today
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    61
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    62
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    63
        # construct current date
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    64
        return date == self.today()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    65
    
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    66
    def is_this_month (self, month) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    67
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    68
            Checks the given month is the current month
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    69
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    70
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    71
        today = self.today()
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    72
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    73
        return (month.year == today.year and month.month == today.month)
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    74
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    75
    def prev_month_year (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    76
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    77
            Returns the year of the month before the given one
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    78
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    79
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    80
        if month.month == 1 :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    81
            return month.year - 1
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    82
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    83
        else :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    84
            return month.year
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    85
            
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    86
    def next_month_year (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    87
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    88
            Returns the year of the month after the given one
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    89
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    90
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    91
        if month.month == 12 :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    92
            return month.year + 1
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    93
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    94
        else :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    95
            return month.year
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    96
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    97
    def prev_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    98
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    99
            Returns the month before the given one
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   100
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   101
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   102
        if month.month == 1 :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   103
            return 12
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   104
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   105
        else :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   106
            return month.month - 1
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   107
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   108
    def next_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   109
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   110
            Returns the month after the given one
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   111
        """
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   112
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   113
        if month.month == 12 :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   114
            return 1
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   115
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   116
        else :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   117
            return month.month + 1
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   118
    
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   119
    def fmt_time (self, time=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   120
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   121
            Format given time, or current time
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   122
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   123
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   124
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   125
        if not time :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   126
            time = self.now()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   127
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   128
        return time.strftime(self.ctx['prefs'][preferences.time_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   129
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   130
    def fmt_date (self, date=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   131
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   132
            Format given date, or current date
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   133
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   134
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   135
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   136
        if not date :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   137
            date = self.now()
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   138
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   139
        return date.strftime(self.ctx['prefs'][preferences.date_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   140