helpers.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 00:52:20 +0200
changeset 131 67f5d2fdca1d
parent 115 751e3fcd11d2
child 132 0e857c4a67de
permissions -rw-r--r--
rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/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
     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
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    10
import preferences, urls, config, version
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
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    26
    
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    27
    def version_link (self) :
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    28
        """
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    29
            Returns a <a href> representing this version of the software
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    30
        """
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    31
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    32
        return version.version_link_hg(config.HGWEB_URL, config.HG_WC_PATH)
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 115
diff changeset
    33
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    34
    def tz_name (self, tz) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    35
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    36
            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
    37
        """
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
    38
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    39
        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
    40
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    41
    def fmt_month (self, date) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    42
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    43
            Formats a month
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    44
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    45
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    46
        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
    47
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    48
    def fmt_weekday (self, wday) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    49
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    50
            Formats an abbreviated weekday name
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    51
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    52
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    53
        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
    54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    55
    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
    56
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    57
            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
    58
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    59
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    60
        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
    61
    
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    62
    def now (self) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    63
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    64
            Build current time
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
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
    67
        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
    68
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    69
    def today (self) :
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
            Build today's date
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
        
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    74
        return self.now().date()
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    75
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    76
    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
    77
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    78
            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
    79
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    80
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    81
        # 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
    82
        return dt.date() == self.today()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    83
    
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    84
    def is_this_month (self, month) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    85
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    86
            Checks the given month is the current month
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    87
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    88
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    89
        today = self.today()
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    90
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    91
        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
    92
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    93
    @staticmethod
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    94
    def _wrap_year (year, month) :
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    95
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    96
            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
    97
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    98
            Returns (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    99
        """
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   100
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   101
        # underflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   102
        if month == 0 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   103
            # wrap to previous year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   104
            return (year - 1, 12)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   105
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   106
        # overflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   107
        elif month == 13 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   108
            # wrap to next year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   109
            return (year + 1, 1)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   110
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   111
        # sane value
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   112
        elif 1 <= month <= 12 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   113
            return (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   114
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   115
        # insane value
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   116
        else :
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   117
            assert False, "invalid year/month: %d/%d" % (year, month)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   118
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   119
    def prev_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   120
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   121
            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
   122
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   123
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   124
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   125
        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
   126
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   127
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   128
        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
   129
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   130
    def next_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   131
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   132
            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
   133
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   134
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   135
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   136
        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
   137
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   138
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   139
        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
   140
    
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   141
    def fmt_time (self, time=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   142
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   143
            Format given time, or current time
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   144
        """
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
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   147
        if not time :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   148
            time = self.now()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   149
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   150
        return time.strftime(self.ctx['prefs'][preferences.time_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   151
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   152
    def fmt_date (self, date=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   153
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   154
            Format given date, or current date
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   155
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   156
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   157
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   158
        if not date :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   159
            date = self.now()
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   160
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   161
        return date.strftime(self.ctx['prefs'][preferences.date_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   162
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
   163
    def url (self, url, **params) :
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   164
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   165
            Build URL with our request object
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   166
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   167
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   168
        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
   169
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
   170
    # 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
   171
    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
   172
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   173
    def utc_timestamp (self, dtz) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   174
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   175
            Build an UTC timestamp from the given datetime
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   176
        """
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   177
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   178
        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
   179
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   180
    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
   181
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   182
            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
   183
        """
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   184
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   185
        return count + skip
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
    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
   188
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   189
            Skip to page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   190
        """
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
        if page :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   193
            return count * page
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
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   196
            return None
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
    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
   199
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   200
            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
   201
        """
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
        if skip > count :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   204
            return skip - count
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
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   207
            return None
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   208
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   209
    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
   210
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   211
            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
   212
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   213
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   214
        return max(values)
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   215
    
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   216
    def select_options (self, key_values, selected_key=None) :
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   217
        """
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   218
            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
   219
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   220
            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
   221
        """
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   222
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   223
        return '\n'.join(
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   224
            '\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
   225
                ' 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
   226
                ' 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
   227
                value
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   228
            ) for key, value in key_values
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   229
        )
107
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   230
    
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   231
    def prev_date (self, date) :
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   232
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   233
            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
   234
        """
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   235
107
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   236
        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
   237
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   238
    def next_date (self, date) :
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   239
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   240
            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
   241
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   242
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   243
        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
   244