helpers.py
author Tero Marttila <terom@fixme.fi>
Sat, 14 Feb 2009 17:59:13 +0200
changeset 124 4bc4de14f006
parent 115 751e3fcd11d2
child 131 67f5d2fdca1d
permissions -rw-r--r--
remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
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
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
     5
import datetime
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
     6
import calendar as _calendar
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
     7
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
     8
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
     9
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    10
import preferences, urls, config
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    11
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    12
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
    13
    """
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    14
        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
    15
    """
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
    16
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    17
    # set contructor...
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    18
    set = set
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    19
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    20
    # reference to calendar instance
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    21
    calendar = _calendar.Calendar()
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    22
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    23
    # list of (month_num, month_name) for the months in the year
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    24
    months = list(enumerate(_calendar.month_name))[1:]
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    25
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    26
    def tz_name (self, tz) :
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
            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
    29
        """
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
    30
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    31
        return self.now().strftime(config.TIMEZONE_FMT)
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
    32
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    33
    def fmt_month (self, date) :
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
            Formats a month
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
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    38
        return date.strftime(config.MONTH_FMT)
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    39
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    40
    def fmt_weekday (self, wday) :
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
            Formats an abbreviated weekday name
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    43
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    44
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    45
        return _calendar.day_abbr[wday]
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    46
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    47
    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
    48
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    49
            Returns a datetime.datetime for the given (month.year, month.month, mday)
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    50
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    51
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    52
        return self.ctx['prefs'][preferences.timezone].localize(datetime.datetime(month.year, month.month, mday))
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    53
    
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    54
    def now (self) :
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
            Build current time
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    57
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    58
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
    59
        return self.ctx['prefs'][preferences.timezone].localize(datetime.datetime.now())
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    60
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    61
    def today (self) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    62
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    63
            Build today's date
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    64
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    65
        
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    66
        return self.now().date()
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    67
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    68
    def is_today (self, dt) :
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    69
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    70
            Checks if the given datetime.datetime is today
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    71
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    72
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    73
        # compare with current date
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    74
        return dt.date() == self.today()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    75
    
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    76
    def is_this_month (self, month) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    77
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    78
            Checks the given month is the current month
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    79
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    80
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    81
        today = self.today()
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    82
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    83
        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
    84
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    85
    @staticmethod
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    86
    def _wrap_year (year, month) :
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    87
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    88
            Wraps month to between [1, 12], spilling overflow/underflow by to year.
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    89
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    90
            Returns (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    91
        """
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    92
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    93
        # underflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    94
        if month == 0 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    95
            # wrap to previous year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    96
            return (year - 1, 12)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    97
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    98
        # overflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    99
        elif month == 13 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   100
            # wrap to next year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   101
            return (year + 1, 1)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   102
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   103
        # sane value
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   104
        elif 1 <= month <= 12 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   105
            return (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   106
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   107
        # insane value
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   108
        else :
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   109
            assert False, "invalid year/month: %d/%d" % (year, month)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   110
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   111
    def prev_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   112
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   113
            Returns the month preceding the given one (as a datetime.datetime)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   114
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   115
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   116
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   117
        y, m = self._wrap_year(month.year, month.month - 1)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   118
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   119
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   120
        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   121
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   122
    def next_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   123
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   124
            Returns the month following the given one (as a datetime.datetime)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   125
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   126
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   127
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   128
        y, m = self._wrap_year(month.year, month.month + 1)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   129
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   130
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   131
        return datetime.datetime(year=y, month=m, day=1, tzinfo=month.tzinfo)
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   132
    
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   133
    def fmt_time (self, time=None) :
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
            Format given time, or current time
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   136
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   137
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   138
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   139
        if not time :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   140
            time = self.now()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   141
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   142
        return time.strftime(self.ctx['prefs'][preferences.time_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   143
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   144
    def fmt_date (self, date=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   145
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   146
            Format given date, or current date
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   147
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   148
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   149
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   150
        if not date :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   151
            date = self.now()
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   152
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   153
        return date.strftime(self.ctx['prefs'][preferences.date_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   154
114
d4848d807fd1 center calendar list, add prev/next date to channel_link, add view-last-x-lines form to channel_last/channel
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   155
    def url (self, url, **params) :
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   156
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   157
            Build URL with our request object
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   158
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   159
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   160
        return url.build(self.ctx['req'], **params)
114
d4848d807fd1 center calendar list, add prev/next date to channel_link, add view-last-x-lines form to channel_last/channel
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   161
d4848d807fd1 center calendar list, add prev/next date to channel_link, add view-last-x-lines form to channel_last/channel
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   162
    # old name
d4848d807fd1 center calendar list, add prev/next date to channel_link, add view-last-x-lines form to channel_last/channel
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   163
    build_url = url
d4848d807fd1 center calendar list, add prev/next date to channel_link, add view-last-x-lines form to channel_last/channel
Tero Marttila <terom@fixme.fi>
parents: 113
diff changeset
   164
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   165
    def utc_timestamp (self, dtz) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   166
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   167
            Build an UTC timestamp from the given datetime
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   168
        """
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   169
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   170
        return urls.types['ts'].build(dtz)
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   171
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   172
    def skip_next (self, count, skip) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   173
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   174
            Return skip offset for next page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   175
        """
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   176
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   177
        return count + skip
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   178
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   179
    def skip_page (self, count, page) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   180
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   181
            Skip to page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   182
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   183
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   184
        if page :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   185
            return count * page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   186
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   187
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   188
            return None
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   189
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   190
    def skip_prev (self, count, skip) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   191
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   192
            Return skip offset for previous page, None for first page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   193
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   194
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   195
        if skip > count :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   196
            return skip - count
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   197
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   198
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   199
            return None
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   200
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   201
    def max (self, *values) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   202
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   203
            Returns the largest of the given values
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   204
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   205
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   206
        return max(values)
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   207
    
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   208
    def select_options (self, key_values, selected_key=None) :
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   209
        """
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   210
            Render a series of <option> tags for <select>.
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   211
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   212
            The given key_values is an iterable of (key, value) pairs, key may be None if it's the same as value.
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   213
        """
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   214
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   215
        return '\n'.join(
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   216
            '\t<option%s%s>%s</option>' % (
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   217
                ' value="%s"' % key if key is not None else '',
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   218
                ' selected="selected"' if (key if key is not None else value) == selected_key else '',
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   219
                value
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   220
            ) for key, value in key_values
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   221
        )
107
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   222
    
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   223
    def prev_date (self, date) :
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   224
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   225
            Returns the previous date for the given datetime-date
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   226
        """
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   227
107
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   228
        return datetime.datetime(date.year, date.month, date.day, tzinfo=date.tzinfo) - datetime.timedelta(days=1)
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   229
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   230
    def next_date (self, date) :
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   231
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   232
            Returns the previous date for the given datetime-date
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   233
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   234
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   235
        return datetime.datetime(date.year, date.month, date.day, tzinfo=date.tzinfo) + datetime.timedelta(days=1)
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   236