paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | Common resource functionality for Web calendar clients. |
paul@446 | 5 | |
paul@446 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@446 | 7 | |
paul@446 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@446 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@446 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@446 | 11 | version. |
paul@446 | 12 | |
paul@446 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@446 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@446 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@446 | 16 | details. |
paul@446 | 17 | |
paul@446 | 18 | You should have received a copy of the GNU General Public License along with |
paul@446 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@446 | 20 | """ |
paul@446 | 21 | |
paul@764 | 22 | from datetime import datetime, timedelta |
paul@756 | 23 | from imiptools.client import Client, ClientForObject |
paul@622 | 24 | from imiptools.data import get_uri, uri_values |
paul@764 | 25 | from imiptools.dates import format_datetime, get_recurrence_start_point, to_date |
paul@678 | 26 | from imiptools.period import remove_period, remove_affected_period |
paul@446 | 27 | from imipweb.env import CGIEnvironment |
paul@446 | 28 | import babel.dates |
paul@446 | 29 | import imip_store |
paul@446 | 30 | import markup |
paul@764 | 31 | import pytz |
paul@446 | 32 | |
paul@756 | 33 | class Resource: |
paul@446 | 34 | |
paul@756 | 35 | "A Web application resource." |
paul@446 | 36 | |
paul@446 | 37 | def __init__(self, resource=None): |
paul@756 | 38 | |
paul@756 | 39 | """ |
paul@756 | 40 | Initialise a resource, allowing it to share the environment of any given |
paul@756 | 41 | existing 'resource'. |
paul@756 | 42 | """ |
paul@756 | 43 | |
paul@446 | 44 | self.encoding = "utf-8" |
paul@446 | 45 | self.env = CGIEnvironment(self.encoding) |
paul@446 | 46 | |
paul@756 | 47 | self.objects = {} |
paul@446 | 48 | self.locale = None |
paul@446 | 49 | self.requests = None |
paul@446 | 50 | |
paul@446 | 51 | self.out = resource and resource.out or self.env.get_output() |
paul@446 | 52 | self.page = resource and resource.page or markup.page() |
paul@446 | 53 | self.html_ids = None |
paul@446 | 54 | |
paul@446 | 55 | # Presentation methods. |
paul@446 | 56 | |
paul@446 | 57 | def new_page(self, title): |
paul@446 | 58 | self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) |
paul@446 | 59 | self.html_ids = set() |
paul@446 | 60 | |
paul@446 | 61 | def status(self, code, message): |
paul@446 | 62 | self.header("Status", "%s %s" % (code, message)) |
paul@446 | 63 | |
paul@446 | 64 | def header(self, header, value): |
paul@446 | 65 | print >>self.out, "%s: %s" % (header, value) |
paul@446 | 66 | |
paul@446 | 67 | def no_user(self): |
paul@446 | 68 | self.status(403, "Forbidden") |
paul@446 | 69 | self.new_page(title="Forbidden") |
paul@446 | 70 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@446 | 71 | |
paul@446 | 72 | def no_page(self): |
paul@446 | 73 | self.status(404, "Not Found") |
paul@446 | 74 | self.new_page(title="Not Found") |
paul@446 | 75 | self.page.p("No page is provided at the given address.") |
paul@446 | 76 | |
paul@446 | 77 | def redirect(self, url): |
paul@446 | 78 | self.status(302, "Redirect") |
paul@446 | 79 | self.header("Location", url) |
paul@446 | 80 | self.new_page(title="Redirect") |
paul@446 | 81 | self.page.p("Redirecting to: %s" % url) |
paul@446 | 82 | |
paul@763 | 83 | def link_to(self, uid, recurrenceid=None): |
paul@755 | 84 | |
paul@755 | 85 | """ |
paul@763 | 86 | Return a link to an object with the given 'uid' and 'recurrenceid'. |
paul@763 | 87 | See get_identifiers for the decoding of such links. |
paul@755 | 88 | """ |
paul@755 | 89 | |
paul@755 | 90 | path = [uid] |
paul@446 | 91 | if recurrenceid: |
paul@755 | 92 | path.append(recurrenceid) |
paul@755 | 93 | return self.env.new_url("/".join(path)) |
paul@446 | 94 | |
paul@623 | 95 | # Control naming helpers. |
paul@446 | 96 | |
paul@622 | 97 | def element_identifier(self, name, index=None): |
paul@446 | 98 | return index is not None and "%s-%d" % (name, index) or name |
paul@446 | 99 | |
paul@622 | 100 | def element_name(self, name, suffix, index=None): |
paul@446 | 101 | return index is not None and "%s-%s" % (name, suffix) or name |
paul@446 | 102 | |
paul@623 | 103 | def element_enable(self, index=None): |
paul@623 | 104 | return index is not None and str(index) or "enable" |
paul@623 | 105 | |
paul@623 | 106 | # Access to objects. |
paul@623 | 107 | |
paul@755 | 108 | def get_identifiers(self, path_info): |
paul@755 | 109 | |
paul@755 | 110 | """ |
paul@755 | 111 | Return identifiers provided by 'path_info', potentially encoded by |
paul@755 | 112 | 'link_to'. |
paul@755 | 113 | """ |
paul@755 | 114 | |
paul@446 | 115 | parts = path_info.lstrip("/").split("/") |
paul@755 | 116 | |
paul@755 | 117 | # UID only. |
paul@755 | 118 | |
paul@446 | 119 | if len(parts) == 1: |
paul@763 | 120 | return parts[0], None |
paul@755 | 121 | |
paul@763 | 122 | # UID and RECURRENCE-ID. |
paul@755 | 123 | |
paul@446 | 124 | else: |
paul@763 | 125 | return parts[:2] |
paul@446 | 126 | |
paul@755 | 127 | def _get_object(self, uid, recurrenceid=None, section=None): |
paul@755 | 128 | if self.objects.has_key((uid, recurrenceid, section)): |
paul@755 | 129 | return self.objects[(uid, recurrenceid, section)] |
paul@446 | 130 | |
paul@755 | 131 | obj = self.objects[(uid, recurrenceid, section)] = self.get_stored_object(uid, recurrenceid, section) |
paul@446 | 132 | return obj |
paul@446 | 133 | |
paul@446 | 134 | def _get_recurrences(self, uid): |
paul@446 | 135 | return self.store.get_recurrences(self.user, uid) |
paul@446 | 136 | |
paul@694 | 137 | def _get_active_recurrences(self, uid): |
paul@694 | 138 | return self.store.get_active_recurrences(self.user, uid) |
paul@694 | 139 | |
paul@446 | 140 | def _get_requests(self): |
paul@446 | 141 | if self.requests is None: |
paul@699 | 142 | self.requests = self.store.get_requests(self.user) |
paul@446 | 143 | return self.requests |
paul@446 | 144 | |
paul@755 | 145 | def _have_request(self, uid, recurrenceid=None, type=None, strict=False): |
paul@755 | 146 | return self.store.have_request(self._get_requests(), uid, recurrenceid, type, strict) |
paul@751 | 147 | |
paul@446 | 148 | def _get_request_summary(self): |
paul@630 | 149 | |
paul@630 | 150 | "Return a list of periods comprising the request summary." |
paul@630 | 151 | |
paul@446 | 152 | summary = [] |
paul@630 | 153 | |
paul@751 | 154 | for uid, recurrenceid, request_type in self._get_requests(): |
paul@606 | 155 | obj = self.get_stored_object(uid, recurrenceid) |
paul@446 | 156 | if obj: |
paul@694 | 157 | recurrenceids = self._get_active_recurrences(uid) |
paul@446 | 158 | |
paul@630 | 159 | # Obtain only active periods, not those replaced by redefined |
paul@678 | 160 | # recurrences, converting to free/busy periods. |
paul@446 | 161 | |
paul@630 | 162 | for p in obj.get_active_periods(recurrenceids, self.get_tzid(), self.get_window_end()): |
paul@678 | 163 | summary.append(obj.get_freebusy_period(p)) |
paul@446 | 164 | |
paul@446 | 165 | return summary |
paul@446 | 166 | |
paul@446 | 167 | # Preference methods. |
paul@446 | 168 | |
paul@446 | 169 | def get_user_locale(self): |
paul@446 | 170 | if not self.locale: |
paul@446 | 171 | self.locale = self.get_preferences().get("LANG", "en") |
paul@446 | 172 | return self.locale |
paul@446 | 173 | |
paul@446 | 174 | # Prettyprinting of dates and times. |
paul@446 | 175 | |
paul@446 | 176 | def format_date(self, dt, format): |
paul@446 | 177 | return self._format_datetime(babel.dates.format_date, dt, format) |
paul@446 | 178 | |
paul@446 | 179 | def format_time(self, dt, format): |
paul@446 | 180 | return self._format_datetime(babel.dates.format_time, dt, format) |
paul@446 | 181 | |
paul@446 | 182 | def format_datetime(self, dt, format): |
paul@446 | 183 | return self._format_datetime( |
paul@446 | 184 | isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, |
paul@446 | 185 | dt, format) |
paul@446 | 186 | |
paul@446 | 187 | def _format_datetime(self, fn, dt, format): |
paul@446 | 188 | return fn(dt, format=format, locale=self.get_user_locale()) |
paul@446 | 189 | |
paul@446 | 190 | # Data management methods. |
paul@446 | 191 | |
paul@446 | 192 | def remove_request(self, uid, recurrenceid=None): |
paul@446 | 193 | return self.store.dequeue_request(self.user, uid, recurrenceid) |
paul@446 | 194 | |
paul@446 | 195 | def remove_event(self, uid, recurrenceid=None): |
paul@446 | 196 | return self.store.remove_event(self.user, uid, recurrenceid) |
paul@446 | 197 | |
paul@756 | 198 | class ResourceClient(Resource, Client): |
paul@446 | 199 | |
paul@756 | 200 | "A Web application resource and calendar client." |
paul@446 | 201 | |
paul@756 | 202 | def __init__(self, resource=None): |
paul@756 | 203 | Resource.__init__(self, resource) |
paul@756 | 204 | user = self.env.get_user() |
paul@756 | 205 | Client.__init__(self, user and get_uri(user) or None) |
paul@446 | 206 | |
paul@756 | 207 | class ResourceClientForObject(Resource, ClientForObject): |
paul@447 | 208 | |
paul@756 | 209 | "A Web application resource and calendar client for a specific object." |
paul@672 | 210 | |
paul@756 | 211 | def __init__(self, resource=None): |
paul@756 | 212 | Resource.__init__(self, resource) |
paul@756 | 213 | user = self.env.get_user() |
paul@756 | 214 | ClientForObject.__init__(self, None, user and get_uri(user) or None) |
paul@446 | 215 | |
paul@764 | 216 | class FormUtilities: |
paul@764 | 217 | |
paul@764 | 218 | "Utility methods." |
paul@764 | 219 | |
paul@764 | 220 | def control(self, name, type, value, selected=False, **kw): |
paul@764 | 221 | |
paul@764 | 222 | """ |
paul@764 | 223 | Show a control with the given 'name', 'type' and 'value', with |
paul@764 | 224 | 'selected' indicating whether it should be selected (checked or |
paul@764 | 225 | equivalent), and with keyword arguments setting other properties. |
paul@764 | 226 | """ |
paul@764 | 227 | |
paul@764 | 228 | page = self.page |
paul@764 | 229 | if selected: |
paul@764 | 230 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@764 | 231 | else: |
paul@764 | 232 | page.input(name=name, type=type, value=value, **kw) |
paul@764 | 233 | |
paul@764 | 234 | def menu(self, name, default, items, class_="", index=None): |
paul@764 | 235 | |
paul@764 | 236 | """ |
paul@764 | 237 | Show a select menu having the given 'name', set to the given 'default', |
paul@764 | 238 | providing the given (value, label) 'items', and employing the given CSS |
paul@764 | 239 | 'class_' if specified. |
paul@764 | 240 | """ |
paul@764 | 241 | |
paul@764 | 242 | page = self.page |
paul@764 | 243 | values = self.env.get_args().get(name, [default]) |
paul@764 | 244 | if index is not None: |
paul@764 | 245 | values = values[index:] |
paul@764 | 246 | values = values and values[0:1] or [default] |
paul@764 | 247 | |
paul@764 | 248 | page.select(name=name, class_=class_) |
paul@764 | 249 | for v, label in items: |
paul@764 | 250 | if v is None: |
paul@764 | 251 | continue |
paul@764 | 252 | if v in values: |
paul@764 | 253 | page.option(label, value=v, selected="selected") |
paul@764 | 254 | else: |
paul@764 | 255 | page.option(label, value=v) |
paul@764 | 256 | page.select.close() |
paul@764 | 257 | |
paul@764 | 258 | def date_controls(self, name, default, index=None, show_tzid=True, read_only=False): |
paul@764 | 259 | |
paul@764 | 260 | """ |
paul@764 | 261 | Show date controls for a field with the given 'name' and 'default' form |
paul@764 | 262 | date value. |
paul@764 | 263 | |
paul@764 | 264 | If 'index' is specified, default field values will be overridden by the |
paul@764 | 265 | element from a collection of existing form values with the specified |
paul@764 | 266 | index; otherwise, field values will be overridden by a single form |
paul@764 | 267 | value. |
paul@764 | 268 | |
paul@764 | 269 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@764 | 270 | provided. |
paul@764 | 271 | |
paul@764 | 272 | If 'read_only' is set to a true value, the controls will be hidden and |
paul@764 | 273 | labels will be employed instead. |
paul@764 | 274 | """ |
paul@764 | 275 | |
paul@764 | 276 | page = self.page |
paul@764 | 277 | |
paul@764 | 278 | # Show dates for up to one week around the current date. |
paul@764 | 279 | |
paul@764 | 280 | dt = default.as_datetime() |
paul@764 | 281 | if not dt: |
paul@764 | 282 | dt = date.today() |
paul@764 | 283 | |
paul@764 | 284 | base = to_date(dt) |
paul@764 | 285 | |
paul@764 | 286 | # Show a date label with a hidden field if read-only. |
paul@764 | 287 | |
paul@764 | 288 | if read_only: |
paul@764 | 289 | self.control("%s-date" % name, "hidden", format_datetime(base)) |
paul@764 | 290 | page.span(self.format_date(base, "long")) |
paul@764 | 291 | |
paul@764 | 292 | # Show dates for up to one week around the current date. |
paul@764 | 293 | # NOTE: Support paging to other dates. |
paul@764 | 294 | |
paul@764 | 295 | else: |
paul@764 | 296 | items = [] |
paul@764 | 297 | for i in range(-7, 8): |
paul@764 | 298 | d = base + timedelta(i) |
paul@764 | 299 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@764 | 300 | self.menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@764 | 301 | |
paul@764 | 302 | # Show time details. |
paul@764 | 303 | |
paul@764 | 304 | page.span(class_="time enabled") |
paul@764 | 305 | |
paul@764 | 306 | if read_only: |
paul@764 | 307 | page.span("%s:%s:%s" % (default.get_hour(), default.get_minute(), default.get_second())) |
paul@764 | 308 | self.control("%s-hour" % name, "hidden", default.get_hour()) |
paul@764 | 309 | self.control("%s-minute" % name, "hidden", default.get_minute()) |
paul@764 | 310 | self.control("%s-second" % name, "hidden", default.get_second()) |
paul@764 | 311 | else: |
paul@764 | 312 | self.control("%s-hour" % name, "text", default.get_hour(), maxlength=2, size=2) |
paul@764 | 313 | page.add(":") |
paul@764 | 314 | self.control("%s-minute" % name, "text", default.get_minute(), maxlength=2, size=2) |
paul@764 | 315 | page.add(":") |
paul@764 | 316 | self.control("%s-second" % name, "text", default.get_second(), maxlength=2, size=2) |
paul@764 | 317 | |
paul@764 | 318 | # Show time zone details. |
paul@764 | 319 | |
paul@764 | 320 | if show_tzid: |
paul@764 | 321 | page.add(" ") |
paul@764 | 322 | tzid = default.get_tzid() or self.get_tzid() |
paul@764 | 323 | |
paul@764 | 324 | # Show a label if read-only or a menu otherwise. |
paul@764 | 325 | |
paul@764 | 326 | if read_only: |
paul@764 | 327 | self.control("%s-tzid" % name, "hidden", tzid) |
paul@764 | 328 | page.span(tzid) |
paul@764 | 329 | else: |
paul@764 | 330 | self.timezone_menu("%s-tzid" % name, tzid, index) |
paul@764 | 331 | |
paul@764 | 332 | page.span.close() |
paul@764 | 333 | |
paul@764 | 334 | def timezone_menu(self, name, default, index=None): |
paul@764 | 335 | |
paul@764 | 336 | """ |
paul@764 | 337 | Show timezone controls using a menu with the given 'name', set to the |
paul@764 | 338 | given 'default' unless a field of the given 'name' provides a value. |
paul@764 | 339 | """ |
paul@764 | 340 | |
paul@764 | 341 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@764 | 342 | self.menu(name, default, entries, index=index) |
paul@764 | 343 | |
paul@446 | 344 | # vim: tabstop=4 expandtab shiftwidth=4 |