helpers.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 02:09:14 +0200
changeset 134 fbccc1648d79
parent 132 0e857c4a67de
permissions -rw-r--r--
improved error handling for CGI/FastCGI
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:]
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
    25
    
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
    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
    27
        """
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
            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
    29
        """
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
        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
    32
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    33
    def tz_name (self, tz) :
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
            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
    36
        """
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
    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 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
    39
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    40
    def fmt_month (self, date) :
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 a month
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
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    45
        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
    46
        
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    47
    def fmt_weekday (self, wday) :
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    48
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    49
            Formats an abbreviated weekday name
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
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
    52
        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
    53
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    54
    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
    55
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    56
            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
    57
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    58
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    59
        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
    60
    
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    61
    def now (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 current time
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
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
    66
        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
    67
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    68
    def today (self) :
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
            Build today's date
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    71
        """
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 self.now().date()
54
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    74
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    75
    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
    76
        """
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    77
            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
    78
        """
b65a95eb9f6b implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    79
115
751e3fcd11d2 have dates in URLs be partial timestamps - fix datetime-timezone-comparison mess
Tero Marttila <terom@fixme.fi>
parents: 114
diff changeset
    80
        # 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
    81
        return dt.date() == self.today()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    82
    
58
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    83
    def is_this_month (self, month) :
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    84
        """
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    85
            Checks the given month is the current month
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    86
        """
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
        today = self.today()
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    89
ce028d356e1f special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    90
        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
    91
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    92
    @staticmethod
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    93
    def _wrap_year (year, month) :
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    94
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    95
            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
    96
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    97
            Returns (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
    98
        """
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
        # underflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   101
        if month == 0 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   102
            # wrap to previous year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   103
            return (year - 1, 12)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   104
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   105
        # overflow?
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   106
        elif month == 13 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   107
            # wrap to next year
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   108
            return (year + 1, 1)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   109
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   110
        # sane value
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   111
        elif 1 <= month <= 12 :
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   112
            return (year, month)
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   113
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   114
        # insane value
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   115
        else :
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   116
            assert False, "invalid year/month: %d/%d" % (year, month)
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   117
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   118
    def prev_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   119
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   120
            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
   121
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   122
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   123
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   124
        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
   125
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   126
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   127
        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
   128
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   129
    def next_month (self, month) :
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   130
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   131
            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
   132
        """
112
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   133
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   134
        # previous month
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   135
        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
   136
        
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   137
        # build datetime
090192b64d7e add three calendars to the channel_calendar view
Tero Marttila <terom@fixme.fi>
parents: 107
diff changeset
   138
        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
   139
    
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   140
    def fmt_time (self, time=None) :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   141
        """
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   142
            Format given time, or current time
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
        
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   145
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   146
        if not time :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   147
            time = self.now()
55
5667d2bbdc50 improve calendar further
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   148
60
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   149
        return time.strftime(self.ctx['prefs'][preferences.time_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   150
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   151
    def fmt_date (self, date=None) :
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
            Format given date, or current date
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   154
        """
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
        # defaults
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   157
        if not date :
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   158
            date = self.now()
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   159
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   160
        return date.strftime(self.ctx['prefs'][preferences.date_format])
759369a79527 improve form styling
Tero Marttila <terom@fixme.fi>
parents: 58
diff changeset
   161
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
   162
    def url (self, url, **params) :
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   163
        """
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   164
            Build URL with our request object
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   165
        """
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
        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
   168
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
    # 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
   170
    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
   171
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   172
    def utc_timestamp (self, dtz) :
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   173
        """
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   174
            Build an UTC timestamp from the given datetime
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   175
        """
63
416560b82116 rudimentary search
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   176
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   177
        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
   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_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
   180
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   181
            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
   182
        """
72
5ade0288f2ec implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   183
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   184
        return count + skip
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   185
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   186
    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
   187
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   188
            Skip to page
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
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   191
        if page :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   192
            return count * 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
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   195
            return None
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   196
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   197
    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
   198
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   199
            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
   200
        """
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
        if skip > count :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   203
            return skip - count
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
        else :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   206
            return None
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   207
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   208
    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
   209
        """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   210
            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
   211
        """
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
        return max(values)
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   214
    
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   215
    def select_options (self, key_values, selected_key=None) :
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   216
        """
113
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   217
            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
   218
9fc0eb751b6a add <form> for month/year to calendar header
Tero Marttila <terom@fixme.fi>
parents: 112
diff changeset
   219
            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
   220
        """
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   221
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   222
        return '\n'.join(
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   223
            '\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
   224
                ' 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
   225
                ' 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
   226
                value
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   227
            ) for key, value in key_values
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   228
        )
107
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 prev_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
        """
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   234
107
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
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   237
    def next_date (self, date) :
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   238
        """
67f48e288102 add prev/next date links to channel_date pagination
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   239
            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
   240
        """
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
        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
   243