author | Tero Marttila <terom@fixme.fi> |
Wed, 11 Feb 2009 03:05:11 +0200 | |
changeset 97 | 6165f1ba458d |
parent 79 | 43ac75054d5c |
child 107 | 67f48e288102 |
permissions | -rw-r--r-- |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
Some additional helpers |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
60 | 5 |
import datetime, calendar |
6 |
||
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
7 |
import qmsk.web.helpers |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
|
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
9 |
import preferences, urls, config |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
10 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
11 |
class Helpers (qmsk.web.helpers.Helpers) : |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
""" |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
13 |
Our set of helpers, inheriting from base helpers |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
16 |
def tz_name (self, tz) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
17 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
18 |
Returns a string describing the given timezone |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
19 |
""" |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
|
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
21 |
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
|
22 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
23 |
def fmt_month (self, date) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
24 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
25 |
Formats a month |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
26 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
27 |
|
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
28 |
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
|
29 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
30 |
def fmt_weekday (self, wday) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
31 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
32 |
Formats an abbreviated weekday name |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
33 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
34 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
35 |
return calendar.day_abbr[wday] |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
36 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
37 |
def build_date (self, month, mday) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
38 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
39 |
Returns a datetime.date for the given (month.year, month.month, mday) |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
40 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
41 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
42 |
return datetime.date(month.year, month.month, mday) |
58
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
43 |
|
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
44 |
def now (self) : |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
45 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
46 |
Build current time |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
47 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
48 |
|
62
e7ca94b94a4e
add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
49 |
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
|
50 |
|
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
51 |
def today (self) : |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
52 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
53 |
Build today's date |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
54 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
55 |
|
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
56 |
return self.now().date() |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
57 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
58 |
def is_today (self, date) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
59 |
""" |
58
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
60 |
Checks if the given date is today |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
61 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
62 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
63 |
# construct current date |
58
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
64 |
return date == self.today() |
55 | 65 |
|
58
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
66 |
def is_this_month (self, month) : |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
67 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
68 |
Checks the given month is the current month |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
69 |
""" |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
70 |
|
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
71 |
today = self.today() |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
72 |
|
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
73 |
return (month.year == today.year and month.month == today.month) |
ce028d356e1f
special-case current month in calendar
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
74 |
|
55 | 75 |
def prev_month_year (self, month) : |
76 |
""" |
|
77 |
Returns the year of the month before the given one |
|
78 |
""" |
|
79 |
||
80 |
if month.month == 1 : |
|
81 |
return month.year - 1 |
|
82 |
||
83 |
else : |
|
84 |
return month.year |
|
85 |
||
86 |
def next_month_year (self, month) : |
|
87 |
""" |
|
88 |
Returns the year of the month after the given one |
|
89 |
""" |
|
90 |
||
91 |
if month.month == 12 : |
|
92 |
return month.year + 1 |
|
93 |
||
94 |
else : |
|
95 |
return month.year |
|
96 |
||
97 |
def prev_month (self, month) : |
|
98 |
""" |
|
99 |
Returns the month before the given one |
|
100 |
""" |
|
101 |
||
102 |
if month.month == 1 : |
|
103 |
return 12 |
|
104 |
||
105 |
else : |
|
106 |
return month.month - 1 |
|
107 |
||
108 |
def next_month (self, month) : |
|
109 |
""" |
|
110 |
Returns the month after the given one |
|
111 |
""" |
|
112 |
||
113 |
if month.month == 12 : |
|
114 |
return 1 |
|
115 |
||
116 |
else : |
|
117 |
return month.month + 1 |
|
60 | 118 |
|
119 |
def fmt_time (self, time=None) : |
|
120 |
""" |
|
121 |
Format given time, or current time |
|
122 |
""" |
|
123 |
||
124 |
# defaults |
|
125 |
if not time : |
|
126 |
time = self.now() |
|
55 | 127 |
|
60 | 128 |
return time.strftime(self.ctx['prefs'][preferences.time_format]) |
129 |
||
130 |
def fmt_date (self, date=None) : |
|
131 |
""" |
|
132 |
Format given date, or current date |
|
133 |
""" |
|
134 |
||
135 |
# defaults |
|
136 |
if not date : |
|
137 |
date = self.now() |
|
138 |
||
139 |
return date.strftime(self.ctx['prefs'][preferences.date_format]) |
|
140 |
||
63 | 141 |
def build_url (self, url, **params) : |
142 |
""" |
|
143 |
Build URL with our request object |
|
144 |
""" |
|
145 |
||
146 |
return url.build(self.ctx['req'], **params) |
|
72
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
147 |
|
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
148 |
def utc_timestamp (self, dtz) : |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
149 |
""" |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
150 |
Build an UTC timestamp from the given datetime |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
151 |
""" |
63 | 152 |
|
72
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
153 |
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
|
154 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
155 |
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
|
156 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
157 |
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
|
158 |
""" |
72
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
71
diff
changeset
|
159 |
|
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
160 |
return count + skip |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
161 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
162 |
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
|
163 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
164 |
Skip to page |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
165 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
166 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
167 |
if page : |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
168 |
return count * page |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
169 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
170 |
else : |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
171 |
return None |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
172 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
173 |
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
|
174 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
175 |
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
|
176 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
177 |
|
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
178 |
if skip > count : |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
179 |
return skip - count |
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 |
else : |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
182 |
return None |
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 |
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
|
185 |
""" |
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
186 |
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
|
187 |
""" |
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 |
return max(values) |
79 | 190 |
|
191 |
def select_options (self, key_values, selected_key) : |
|
192 |
""" |
|
193 |
Render a series of <option> tags for <select> |
|
194 |
""" |
|
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
195 |
|
79 | 196 |
return '\n'.join( |
197 |
'\t<option%s%s>%s</option>' % ( |
|
198 |
' value="%s"' % key if key != value else '', |
|
199 |
' selected="selected"' if key == selected_key else '', |
|
200 |
value |
|
201 |
) for key, value in key_values |
|
202 |
) |
|
203 |