paul@69 | 1 | #!/usr/bin/env python |
paul@69 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | A Web interface to a user's calendar. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@146 | 22 | # Edit this path to refer to the location of the imiptools libraries, if |
paul@146 | 23 | # necessary. |
paul@146 | 24 | |
paul@146 | 25 | LIBRARY_PATH = "/var/lib/imip-agent" |
paul@146 | 26 | |
paul@232 | 27 | from datetime import date, datetime, timedelta |
paul@149 | 28 | import babel.dates |
paul@149 | 29 | import cgi, os, sys |
paul@69 | 30 | |
paul@146 | 31 | sys.path.append(LIBRARY_PATH) |
paul@69 | 32 | |
paul@213 | 33 | from imiptools.content import Handler |
paul@309 | 34 | from imiptools.data import get_address, get_uri, make_freebusy, Object, to_part, \ |
paul@309 | 35 | uri_dict, uri_item, uri_items, uri_values |
paul@286 | 36 | from imiptools.dates import format_datetime, format_time, get_date, get_datetime, \ |
paul@291 | 37 | get_datetime_item, get_default_timezone, \ |
paul@241 | 38 | get_end_of_day, get_start_of_day, get_start_of_next_day, \ |
paul@274 | 39 | get_timestamp, ends_on_same_day, to_timezone |
paul@83 | 40 | from imiptools.mail import Messenger |
paul@279 | 41 | from imiptools.period import add_day_start_points, add_empty_days, add_slots, \ |
paul@279 | 42 | convert_periods, get_freebusy_details, \ |
paul@162 | 43 | get_scale, have_conflict, get_slots, get_spans, \ |
paul@305 | 44 | partition_by_day, remove_from_freebusy, update_freebusy, \ |
paul@305 | 45 | _update_freebusy |
paul@147 | 46 | from imiptools.profile import Preferences |
paul@213 | 47 | import imip_store |
paul@69 | 48 | import markup |
paul@69 | 49 | |
paul@69 | 50 | getenv = os.environ.get |
paul@69 | 51 | setenv = os.environ.__setitem__ |
paul@69 | 52 | |
paul@69 | 53 | class CGIEnvironment: |
paul@69 | 54 | |
paul@69 | 55 | "A CGI-compatible environment." |
paul@69 | 56 | |
paul@212 | 57 | def __init__(self, charset=None): |
paul@212 | 58 | self.charset = charset |
paul@69 | 59 | self.args = None |
paul@69 | 60 | self.method = None |
paul@69 | 61 | self.path = None |
paul@69 | 62 | self.path_info = None |
paul@69 | 63 | self.user = None |
paul@69 | 64 | |
paul@69 | 65 | def get_args(self): |
paul@69 | 66 | if self.args is None: |
paul@69 | 67 | if self.get_method() != "POST": |
paul@69 | 68 | setenv("QUERY_STRING", "") |
paul@212 | 69 | args = cgi.parse(keep_blank_values=True) |
paul@212 | 70 | |
paul@212 | 71 | if not self.charset: |
paul@212 | 72 | self.args = args |
paul@212 | 73 | else: |
paul@212 | 74 | self.args = {} |
paul@212 | 75 | for key, values in args.items(): |
paul@212 | 76 | self.args[key] = [unicode(value, self.charset) for value in values] |
paul@212 | 77 | |
paul@69 | 78 | return self.args |
paul@69 | 79 | |
paul@69 | 80 | def get_method(self): |
paul@69 | 81 | if self.method is None: |
paul@69 | 82 | self.method = getenv("REQUEST_METHOD") or "GET" |
paul@69 | 83 | return self.method |
paul@69 | 84 | |
paul@69 | 85 | def get_path(self): |
paul@69 | 86 | if self.path is None: |
paul@69 | 87 | self.path = getenv("SCRIPT_NAME") or "" |
paul@69 | 88 | return self.path |
paul@69 | 89 | |
paul@69 | 90 | def get_path_info(self): |
paul@69 | 91 | if self.path_info is None: |
paul@69 | 92 | self.path_info = getenv("PATH_INFO") or "" |
paul@69 | 93 | return self.path_info |
paul@69 | 94 | |
paul@69 | 95 | def get_user(self): |
paul@69 | 96 | if self.user is None: |
paul@69 | 97 | self.user = getenv("REMOTE_USER") or "" |
paul@69 | 98 | return self.user |
paul@69 | 99 | |
paul@69 | 100 | def get_output(self): |
paul@69 | 101 | return sys.stdout |
paul@69 | 102 | |
paul@69 | 103 | def get_url(self): |
paul@69 | 104 | path = self.get_path() |
paul@69 | 105 | path_info = self.get_path_info() |
paul@69 | 106 | return "%s%s" % (path.rstrip("/"), path_info) |
paul@69 | 107 | |
paul@154 | 108 | def new_url(self, path_info): |
paul@154 | 109 | path = self.get_path() |
paul@154 | 110 | return "%s/%s" % (path.rstrip("/"), path_info.lstrip("/")) |
paul@154 | 111 | |
paul@305 | 112 | class Common: |
paul@305 | 113 | |
paul@305 | 114 | "Common handler and manager methods." |
paul@305 | 115 | |
paul@305 | 116 | def __init__(self, user): |
paul@305 | 117 | self.user = user |
paul@305 | 118 | self.preferences = None |
paul@305 | 119 | |
paul@305 | 120 | def get_preferences(self): |
paul@305 | 121 | if not self.preferences: |
paul@305 | 122 | self.preferences = Preferences(self.user) |
paul@305 | 123 | return self.preferences |
paul@305 | 124 | |
paul@305 | 125 | def get_tzid(self): |
paul@305 | 126 | prefs = self.get_preferences() |
paul@305 | 127 | return prefs.get("TZID") or get_default_timezone() |
paul@305 | 128 | |
paul@305 | 129 | class ManagerHandler(Handler, Common): |
paul@79 | 130 | |
paul@121 | 131 | """ |
paul@121 | 132 | A content handler for use by the manager, as opposed to operating within the |
paul@121 | 133 | mail processing pipeline. |
paul@121 | 134 | """ |
paul@79 | 135 | |
paul@121 | 136 | def __init__(self, obj, user, messenger): |
paul@224 | 137 | Handler.__init__(self, messenger=messenger) |
paul@305 | 138 | Common.__init__(self, user) |
paul@305 | 139 | |
paul@224 | 140 | self.set_object(obj) |
paul@82 | 141 | |
paul@79 | 142 | # Communication methods. |
paul@79 | 143 | |
paul@253 | 144 | def send_message(self, method, sender, for_organiser): |
paul@79 | 145 | |
paul@79 | 146 | """ |
paul@207 | 147 | Create a full calendar object employing the given 'method', and send it |
paul@253 | 148 | to the appropriate recipients, also sending a copy to the 'sender'. The |
paul@253 | 149 | 'for_organiser' value indicates whether the organiser is sending this |
paul@253 | 150 | message. |
paul@79 | 151 | """ |
paul@79 | 152 | |
paul@219 | 153 | parts = [self.obj.to_part(method)] |
paul@207 | 154 | |
paul@260 | 155 | # As organiser, send an invitation to attendees, excluding oneself if |
paul@260 | 156 | # also attending. The updated event will be saved by the outgoing |
paul@260 | 157 | # handler. |
paul@260 | 158 | |
paul@309 | 159 | organiser = uri_values(self.obj.get_value("ORGANIZER")) |
paul@309 | 160 | attendees = uri_values(self.obj.get_values("ATTENDEE")) |
paul@308 | 161 | |
paul@253 | 162 | if for_organiser: |
paul@308 | 163 | recipients = [get_address(attendee) for attendee in attendees if attendee != self.user] |
paul@207 | 164 | else: |
paul@308 | 165 | recipients = [get_address(organiser)] |
paul@207 | 166 | |
paul@219 | 167 | # Bundle free/busy information if appropriate. |
paul@219 | 168 | |
paul@219 | 169 | preferences = Preferences(self.user) |
paul@219 | 170 | |
paul@219 | 171 | if preferences.get("freebusy_sharing") == "share" and \ |
paul@219 | 172 | preferences.get("freebusy_bundling") == "always": |
paul@219 | 173 | |
paul@222 | 174 | # Invent a unique identifier. |
paul@222 | 175 | |
paul@222 | 176 | utcnow = get_timestamp() |
paul@222 | 177 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@222 | 178 | |
paul@222 | 179 | freebusy = self.store.get_freebusy(self.user) |
paul@305 | 180 | |
paul@305 | 181 | # Replace the non-updated free/busy details for this event with |
paul@305 | 182 | # newer details (since the outgoing handler updates this user's |
paul@305 | 183 | # free/busy details). |
paul@305 | 184 | |
paul@305 | 185 | tzid = self.get_tzid() |
paul@305 | 186 | |
paul@305 | 187 | _update_freebusy(freebusy, self.obj.get_periods_for_freebusy(tzid), |
paul@305 | 188 | self.obj.get_value("TRANSP") or "OPAQUE", self.obj.get_value("UID")) |
paul@305 | 189 | |
paul@292 | 190 | user_attr = self.messenger and self.messenger.sender != get_address(self.user) and \ |
paul@292 | 191 | {"SENT-BY" : get_uri(self.messenger.sender)} or {} |
paul@292 | 192 | |
paul@292 | 193 | parts.append(to_part("PUBLISH", [ |
paul@292 | 194 | make_freebusy(freebusy, uid, self.user, user_attr) |
paul@292 | 195 | ])) |
paul@219 | 196 | |
paul@219 | 197 | message = self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) |
paul@207 | 198 | self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) |
paul@79 | 199 | |
paul@79 | 200 | # Action methods. |
paul@79 | 201 | |
paul@266 | 202 | def process_received_request(self, update=False): |
paul@79 | 203 | |
paul@79 | 204 | """ |
paul@266 | 205 | Process the current request for the given 'user'. Return whether any |
paul@79 | 206 | action was taken. |
paul@155 | 207 | |
paul@155 | 208 | If 'update' is given, the sequence number will be incremented in order |
paul@155 | 209 | to override any previous response. |
paul@79 | 210 | """ |
paul@79 | 211 | |
paul@266 | 212 | # Reply only on behalf of this user. |
paul@79 | 213 | |
paul@309 | 214 | for attendee, attendee_attr in uri_items(self.obj.get_items("ATTENDEE")): |
paul@79 | 215 | |
paul@79 | 216 | if attendee == self.user: |
paul@266 | 217 | if attendee_attr.has_key("RSVP"): |
paul@266 | 218 | del attendee_attr["RSVP"] |
paul@128 | 219 | if self.messenger and self.messenger.sender != get_address(attendee): |
paul@128 | 220 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@213 | 221 | self.obj["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@273 | 222 | |
paul@158 | 223 | self.update_dtstamp() |
paul@273 | 224 | self.set_sequence(update) |
paul@155 | 225 | |
paul@253 | 226 | self.send_message("REPLY", get_address(attendee), for_organiser=False) |
paul@79 | 227 | |
paul@79 | 228 | return True |
paul@79 | 229 | |
paul@79 | 230 | return False |
paul@79 | 231 | |
paul@308 | 232 | def process_created_request(self, method, update=False, removed=None): |
paul@207 | 233 | |
paul@207 | 234 | """ |
paul@207 | 235 | Process the current request for the given 'user', sending a created |
paul@255 | 236 | request of the given 'method' to attendees. Return whether any action |
paul@255 | 237 | was taken. |
paul@207 | 238 | |
paul@207 | 239 | If 'update' is given, the sequence number will be incremented in order |
paul@207 | 240 | to override any previous message. |
paul@308 | 241 | |
paul@308 | 242 | If 'removed' is specified, a list of participants to be removed is |
paul@308 | 243 | provided. |
paul@207 | 244 | """ |
paul@207 | 245 | |
paul@309 | 246 | organiser, organiser_attr = uri_item(self.obj.get_item("ORGANIZER")) |
paul@213 | 247 | |
paul@213 | 248 | if self.messenger and self.messenger.sender != get_address(organiser): |
paul@213 | 249 | organiser_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@273 | 250 | |
paul@311 | 251 | to_cancel = [] |
paul@311 | 252 | |
paul@308 | 253 | if removed: |
paul@311 | 254 | attendees = uri_items(self.obj.get_items("ATTENDEE")) |
paul@311 | 255 | remaining = [] |
paul@311 | 256 | |
paul@311 | 257 | for attendee, attendee_attr in attendees: |
paul@311 | 258 | if attendee in removed: |
paul@311 | 259 | to_cancel.append((attendee, attendee_attr)) |
paul@311 | 260 | else: |
paul@311 | 261 | remaining.append((attendee, attendee_attr)) |
paul@311 | 262 | |
paul@311 | 263 | self.obj["ATTENDEE"] = remaining |
paul@308 | 264 | |
paul@207 | 265 | self.update_dtstamp() |
paul@273 | 266 | self.set_sequence(update) |
paul@207 | 267 | |
paul@308 | 268 | self.send_message(method, get_address(organiser), for_organiser=True) |
paul@308 | 269 | |
paul@311 | 270 | # When cancelling, replace the attendees with those for whom the event |
paul@311 | 271 | # is now cancelled. |
paul@311 | 272 | |
paul@311 | 273 | if to_cancel: |
paul@311 | 274 | self.obj["ATTENDEE"] = to_cancel |
paul@311 | 275 | self.send_message("CANCEL", get_address(organiser), for_organiser=True) |
paul@311 | 276 | |
paul@311 | 277 | # Just in case more work is done with this event, the attendees are |
paul@311 | 278 | # now restored. |
paul@311 | 279 | |
paul@311 | 280 | self.obj["ATTENDEE"] = remaining |
paul@311 | 281 | |
paul@207 | 282 | return True |
paul@207 | 283 | |
paul@305 | 284 | class Manager(Common): |
paul@69 | 285 | |
paul@69 | 286 | "A simple manager application." |
paul@69 | 287 | |
paul@82 | 288 | def __init__(self, messenger=None): |
paul@82 | 289 | self.messenger = messenger or Messenger() |
paul@212 | 290 | self.encoding = "utf-8" |
paul@212 | 291 | self.env = CGIEnvironment(self.encoding) |
paul@212 | 292 | |
paul@69 | 293 | user = self.env.get_user() |
paul@305 | 294 | Common.__init__(self, user and get_uri(user) or None) |
paul@305 | 295 | |
paul@149 | 296 | self.locale = None |
paul@121 | 297 | self.requests = None |
paul@121 | 298 | |
paul@69 | 299 | self.out = self.env.get_output() |
paul@69 | 300 | self.page = markup.page() |
paul@69 | 301 | |
paul@77 | 302 | self.store = imip_store.FileStore() |
paul@162 | 303 | self.objects = {} |
paul@77 | 304 | |
paul@77 | 305 | try: |
paul@77 | 306 | self.publisher = imip_store.FilePublisher() |
paul@77 | 307 | except OSError: |
paul@77 | 308 | self.publisher = None |
paul@77 | 309 | |
paul@121 | 310 | def _get_uid(self, path_info): |
paul@121 | 311 | return path_info.lstrip("/").split("/", 1)[0] |
paul@121 | 312 | |
paul@117 | 313 | def _get_object(self, uid): |
paul@162 | 314 | if self.objects.has_key(uid): |
paul@162 | 315 | return self.objects[uid] |
paul@162 | 316 | |
paul@301 | 317 | fragment = uid and self.store.get_event(self.user, uid) or None |
paul@213 | 318 | obj = self.objects[uid] = fragment and Object(fragment) |
paul@121 | 319 | return obj |
paul@121 | 320 | |
paul@121 | 321 | def _get_requests(self): |
paul@121 | 322 | if self.requests is None: |
paul@121 | 323 | self.requests = self.store.get_requests(self.user) |
paul@121 | 324 | return self.requests |
paul@117 | 325 | |
paul@162 | 326 | def _get_request_summary(self): |
paul@162 | 327 | summary = [] |
paul@162 | 328 | for uid in self._get_requests(): |
paul@162 | 329 | obj = self._get_object(uid) |
paul@162 | 330 | if obj: |
paul@162 | 331 | summary.append(( |
paul@213 | 332 | obj.get_value("DTSTART"), |
paul@213 | 333 | obj.get_value("DTEND"), |
paul@162 | 334 | uid |
paul@162 | 335 | )) |
paul@162 | 336 | return summary |
paul@162 | 337 | |
paul@147 | 338 | # Preference methods. |
paul@147 | 339 | |
paul@149 | 340 | def get_user_locale(self): |
paul@149 | 341 | if not self.locale: |
paul@149 | 342 | self.locale = self.get_preferences().get("LANG", "C") |
paul@149 | 343 | return self.locale |
paul@147 | 344 | |
paul@162 | 345 | # Prettyprinting of dates and times. |
paul@162 | 346 | |
paul@149 | 347 | def format_date(self, dt, format): |
paul@149 | 348 | return self._format_datetime(babel.dates.format_date, dt, format) |
paul@149 | 349 | |
paul@149 | 350 | def format_time(self, dt, format): |
paul@149 | 351 | return self._format_datetime(babel.dates.format_time, dt, format) |
paul@149 | 352 | |
paul@149 | 353 | def format_datetime(self, dt, format): |
paul@232 | 354 | return self._format_datetime( |
paul@232 | 355 | isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, |
paul@232 | 356 | dt, format) |
paul@232 | 357 | |
paul@149 | 358 | def _format_datetime(self, fn, dt, format): |
paul@149 | 359 | return fn(dt, format=format, locale=self.get_user_locale()) |
paul@149 | 360 | |
paul@78 | 361 | # Data management methods. |
paul@78 | 362 | |
paul@78 | 363 | def remove_request(self, uid): |
paul@105 | 364 | return self.store.dequeue_request(self.user, uid) |
paul@78 | 365 | |
paul@234 | 366 | def remove_event(self, uid): |
paul@234 | 367 | return self.store.remove_event(self.user, uid) |
paul@234 | 368 | |
paul@296 | 369 | def update_freebusy(self, uid, obj): |
paul@296 | 370 | tzid = self.get_tzid() |
paul@296 | 371 | freebusy = self.store.get_freebusy(self.user) |
paul@296 | 372 | update_freebusy(freebusy, self.user, obj.get_periods_for_freebusy(tzid), |
paul@296 | 373 | obj.get_value("TRANSP"), uid, self.store) |
paul@296 | 374 | |
paul@296 | 375 | def remove_from_freebusy(self, uid): |
paul@296 | 376 | freebusy = self.store.get_freebusy(self.user) |
paul@296 | 377 | remove_from_freebusy(freebusy, self.user, uid, self.store) |
paul@296 | 378 | |
paul@78 | 379 | # Presentation methods. |
paul@78 | 380 | |
paul@69 | 381 | def new_page(self, title): |
paul@192 | 382 | self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) |
paul@69 | 383 | |
paul@69 | 384 | def status(self, code, message): |
paul@123 | 385 | self.header("Status", "%s %s" % (code, message)) |
paul@123 | 386 | |
paul@123 | 387 | def header(self, header, value): |
paul@123 | 388 | print >>self.out, "%s: %s" % (header, value) |
paul@69 | 389 | |
paul@69 | 390 | def no_user(self): |
paul@69 | 391 | self.status(403, "Forbidden") |
paul@69 | 392 | self.new_page(title="Forbidden") |
paul@69 | 393 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@69 | 394 | |
paul@70 | 395 | def no_page(self): |
paul@70 | 396 | self.status(404, "Not Found") |
paul@70 | 397 | self.new_page(title="Not Found") |
paul@70 | 398 | self.page.p("No page is provided at the given address.") |
paul@70 | 399 | |
paul@123 | 400 | def redirect(self, url): |
paul@123 | 401 | self.status(302, "Redirect") |
paul@123 | 402 | self.header("Location", url) |
paul@123 | 403 | self.new_page(title="Redirect") |
paul@123 | 404 | self.page.p("Redirecting to: %s" % url) |
paul@123 | 405 | |
paul@246 | 406 | # Request logic methods. |
paul@121 | 407 | |
paul@202 | 408 | def handle_newevent(self): |
paul@202 | 409 | |
paul@207 | 410 | """ |
paul@207 | 411 | Handle any new event operation, creating a new event and redirecting to |
paul@207 | 412 | the event page for further activity. |
paul@207 | 413 | """ |
paul@202 | 414 | |
paul@202 | 415 | # Handle a submitted form. |
paul@202 | 416 | |
paul@202 | 417 | args = self.env.get_args() |
paul@202 | 418 | |
paul@202 | 419 | if not args.has_key("newevent"): |
paul@202 | 420 | return |
paul@202 | 421 | |
paul@202 | 422 | # Create a new event using the available information. |
paul@202 | 423 | |
paul@236 | 424 | slots = args.get("slot", []) |
paul@202 | 425 | participants = args.get("participants", []) |
paul@202 | 426 | |
paul@236 | 427 | if not slots: |
paul@202 | 428 | return |
paul@202 | 429 | |
paul@273 | 430 | # Obtain the user's timezone. |
paul@273 | 431 | |
paul@273 | 432 | tzid = self.get_tzid() |
paul@273 | 433 | |
paul@236 | 434 | # Coalesce the selected slots. |
paul@236 | 435 | |
paul@236 | 436 | slots.sort() |
paul@236 | 437 | coalesced = [] |
paul@236 | 438 | last = None |
paul@236 | 439 | |
paul@236 | 440 | for slot in slots: |
paul@236 | 441 | start, end = slot.split("-") |
paul@273 | 442 | start = get_datetime(start, {"TZID" : tzid}) |
paul@273 | 443 | end = end and get_datetime(end, {"TZID" : tzid}) or get_start_of_next_day(start, tzid) |
paul@248 | 444 | |
paul@236 | 445 | if last: |
paul@248 | 446 | last_start, last_end = last |
paul@248 | 447 | |
paul@248 | 448 | # Merge adjacent dates and datetimes. |
paul@248 | 449 | |
paul@273 | 450 | if start == last_end or get_start_of_day(last_end, tzid) == get_start_of_day(start, tzid): |
paul@248 | 451 | last = last_start, end |
paul@236 | 452 | continue |
paul@248 | 453 | |
paul@248 | 454 | # Handle datetimes within dates. |
paul@248 | 455 | # Datetime periods are within single days and are therefore |
paul@248 | 456 | # discarded. |
paul@248 | 457 | |
paul@273 | 458 | elif get_start_of_day(start, tzid) == get_start_of_day(last_start, tzid): |
paul@248 | 459 | continue |
paul@248 | 460 | |
paul@248 | 461 | # Add separate dates and datetimes. |
paul@248 | 462 | |
paul@236 | 463 | else: |
paul@236 | 464 | coalesced.append(last) |
paul@248 | 465 | |
paul@236 | 466 | last = start, end |
paul@236 | 467 | |
paul@236 | 468 | if last: |
paul@236 | 469 | coalesced.append(last) |
paul@202 | 470 | |
paul@202 | 471 | # Invent a unique identifier. |
paul@202 | 472 | |
paul@222 | 473 | utcnow = get_timestamp() |
paul@202 | 474 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@202 | 475 | |
paul@236 | 476 | # Define a single occurrence if only one coalesced slot exists. |
paul@236 | 477 | # Otherwise, many occurrences are defined. |
paul@202 | 478 | |
paul@236 | 479 | for i, (start, end) in enumerate(coalesced): |
paul@236 | 480 | this_uid = "%s-%s" % (uid, i) |
paul@236 | 481 | |
paul@252 | 482 | start_value, start_attr = get_datetime_item(start, tzid) |
paul@252 | 483 | end_value, end_attr = get_datetime_item(end, tzid) |
paul@239 | 484 | |
paul@236 | 485 | # Create a calendar object and store it as a request. |
paul@236 | 486 | |
paul@236 | 487 | record = [] |
paul@236 | 488 | rwrite = record.append |
paul@202 | 489 | |
paul@236 | 490 | rwrite(("UID", {}, this_uid)) |
paul@236 | 491 | rwrite(("SUMMARY", {}, "New event at %s" % utcnow)) |
paul@236 | 492 | rwrite(("DTSTAMP", {}, utcnow)) |
paul@239 | 493 | rwrite(("DTSTART", start_attr, start_value)) |
paul@239 | 494 | rwrite(("DTEND", end_attr, end_value)) |
paul@236 | 495 | rwrite(("ORGANIZER", {}, self.user)) |
paul@202 | 496 | |
paul@236 | 497 | for participant in participants: |
paul@236 | 498 | if not participant: |
paul@236 | 499 | continue |
paul@236 | 500 | participant = get_uri(participant) |
paul@253 | 501 | rwrite(("ATTENDEE", {"RSVP" : "TRUE", "PARTSTAT" : "NEEDS-ACTION"}, participant)) |
paul@202 | 502 | |
paul@236 | 503 | obj = ("VEVENT", {}, record) |
paul@236 | 504 | |
paul@236 | 505 | self.store.set_event(self.user, this_uid, obj) |
paul@236 | 506 | self.store.queue_request(self.user, this_uid) |
paul@202 | 507 | |
paul@236 | 508 | # Redirect to the object (or the first of the objects), where instead of |
paul@236 | 509 | # attendee controls, there will be organiser controls. |
paul@236 | 510 | |
paul@236 | 511 | self.redirect(self.env.new_url("%s-0" % uid)) |
paul@202 | 512 | |
paul@286 | 513 | def handle_request(self, uid, obj): |
paul@121 | 514 | |
paul@299 | 515 | """ |
paul@299 | 516 | Handle actions involving the given 'uid' and 'obj' object, returning an |
paul@299 | 517 | error if one occurred, or None if the request was successfully handled. |
paul@299 | 518 | """ |
paul@121 | 519 | |
paul@121 | 520 | # Handle a submitted form. |
paul@121 | 521 | |
paul@121 | 522 | args = self.env.get_args() |
paul@299 | 523 | |
paul@299 | 524 | # Get the possible actions. |
paul@299 | 525 | |
paul@299 | 526 | reply = args.has_key("reply") |
paul@299 | 527 | discard = args.has_key("discard") |
paul@299 | 528 | invite = args.has_key("invite") |
paul@299 | 529 | cancel = args.has_key("cancel") |
paul@299 | 530 | save = args.has_key("save") |
paul@299 | 531 | |
paul@299 | 532 | have_action = reply or discard or invite or cancel or save |
paul@299 | 533 | |
paul@299 | 534 | if not have_action: |
paul@299 | 535 | return ["action"] |
paul@121 | 536 | |
paul@212 | 537 | # Update the object. |
paul@212 | 538 | |
paul@212 | 539 | if args.has_key("summary"): |
paul@213 | 540 | obj["SUMMARY"] = [(args["summary"][0], {})] |
paul@212 | 541 | |
paul@309 | 542 | organisers = uri_dict(obj.get_value_map("ORGANIZER")) |
paul@309 | 543 | attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@308 | 544 | |
paul@257 | 545 | if args.has_key("partstat"): |
paul@286 | 546 | for d in attendees, organisers: |
paul@286 | 547 | if d.has_key(self.user): |
paul@286 | 548 | d[self.user]["PARTSTAT"] = args["partstat"][0] |
paul@286 | 549 | if d[self.user].has_key("RSVP"): |
paul@286 | 550 | del d[self.user]["RSVP"] |
paul@286 | 551 | |
paul@309 | 552 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@286 | 553 | |
paul@308 | 554 | # Obtain any participants to be removed. |
paul@308 | 555 | |
paul@308 | 556 | removed = args.get("remove") |
paul@308 | 557 | |
paul@286 | 558 | # Obtain the user's timezone and process datetime values. |
paul@286 | 559 | |
paul@286 | 560 | update = False |
paul@286 | 561 | |
paul@286 | 562 | if is_organiser: |
paul@300 | 563 | dtend_enabled = args.get("dtend-control", [None])[0] == "enable" |
paul@300 | 564 | dttimes_enabled = args.get("dttimes-control", [None])[0] == "enable" |
paul@300 | 565 | |
paul@300 | 566 | t = self.handle_date_controls("dtstart", dttimes_enabled) |
paul@286 | 567 | if t: |
paul@290 | 568 | dtstart, attr = t |
paul@300 | 569 | update = self.set_datetime_in_object(dtstart, attr.get("TZID"), "DTSTART", obj) or update |
paul@286 | 570 | else: |
paul@299 | 571 | return ["dtstart"] |
paul@290 | 572 | |
paul@290 | 573 | # Handle specified end datetimes. |
paul@290 | 574 | |
paul@300 | 575 | if dtend_enabled: |
paul@300 | 576 | t = self.handle_date_controls("dtend", dttimes_enabled) |
paul@290 | 577 | if t: |
paul@290 | 578 | dtend, attr = t |
paul@290 | 579 | |
paul@290 | 580 | # Convert end dates to iCalendar "next day" dates. |
paul@286 | 581 | |
paul@290 | 582 | if not isinstance(dtend, datetime): |
paul@290 | 583 | dtend += timedelta(1) |
paul@300 | 584 | update = self.set_datetime_in_object(dtend, attr.get("TZID"), "DTEND", obj) or update |
paul@290 | 585 | else: |
paul@299 | 586 | return ["dtend"] |
paul@290 | 587 | |
paul@299 | 588 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@299 | 589 | # handled by making the event occupy the rest of the day. |
paul@290 | 590 | |
paul@286 | 591 | else: |
paul@299 | 592 | dtend = dtstart + timedelta(1) |
paul@290 | 593 | if isinstance(dtstart, datetime): |
paul@299 | 594 | dtend = get_start_of_day(dtend, attr["TZID"]) |
paul@300 | 595 | update = self.set_datetime_in_object(dtend, attr.get("TZID"), "DTEND", obj) or update |
paul@286 | 596 | |
paul@290 | 597 | if dtstart >= dtend: |
paul@299 | 598 | return ["dtstart", "dtend"] |
paul@257 | 599 | |
paul@212 | 600 | # Process any action. |
paul@212 | 601 | |
paul@299 | 602 | handled = True |
paul@121 | 603 | |
paul@266 | 604 | if reply or invite or cancel: |
paul@121 | 605 | |
paul@212 | 606 | handler = ManagerHandler(obj, self.user, self.messenger) |
paul@121 | 607 | |
paul@212 | 608 | # Process the object and remove it from the list of requests. |
paul@121 | 609 | |
paul@266 | 610 | if reply and handler.process_received_request(update) or \ |
paul@308 | 611 | is_organiser and (invite or cancel) and \ |
paul@308 | 612 | handler.process_created_request(invite and "REQUEST" or "CANCEL", update, removed): |
paul@121 | 613 | |
paul@121 | 614 | self.remove_request(uid) |
paul@121 | 615 | |
paul@257 | 616 | # Save single user events. |
paul@121 | 617 | |
paul@257 | 618 | elif save: |
paul@257 | 619 | self.store.set_event(self.user, uid, obj.to_node()) |
paul@296 | 620 | self.update_freebusy(uid, obj) |
paul@257 | 621 | self.remove_request(uid) |
paul@121 | 622 | |
paul@257 | 623 | # Remove the request and the object. |
paul@257 | 624 | |
paul@257 | 625 | elif discard: |
paul@296 | 626 | self.remove_from_freebusy(uid) |
paul@234 | 627 | self.remove_event(uid) |
paul@121 | 628 | self.remove_request(uid) |
paul@121 | 629 | |
paul@121 | 630 | else: |
paul@123 | 631 | handled = False |
paul@121 | 632 | |
paul@212 | 633 | # Upon handling an action, redirect to the main page. |
paul@212 | 634 | |
paul@123 | 635 | if handled: |
paul@123 | 636 | self.redirect(self.env.get_path()) |
paul@123 | 637 | |
paul@299 | 638 | return None |
paul@121 | 639 | |
paul@300 | 640 | def handle_date_controls(self, name, with_time=True): |
paul@155 | 641 | |
paul@155 | 642 | """ |
paul@286 | 643 | Handle date control information for fields starting with 'name', |
paul@290 | 644 | returning a (datetime, attr) tuple or None if the fields cannot be used |
paul@286 | 645 | to construct a datetime object. |
paul@155 | 646 | """ |
paul@155 | 647 | |
paul@286 | 648 | args = self.env.get_args() |
paul@286 | 649 | |
paul@286 | 650 | if args.has_key("%s-date" % name): |
paul@286 | 651 | date = args["%s-date" % name][0] |
paul@300 | 652 | |
paul@300 | 653 | if with_time: |
paul@300 | 654 | hour = args.get("%s-hour" % name, [None])[0] |
paul@300 | 655 | minute = args.get("%s-minute" % name, [None])[0] |
paul@300 | 656 | second = args.get("%s-second" % name, [None])[0] |
paul@300 | 657 | tzid = args.get("%s-tzid" % name, [self.get_tzid()])[0] |
paul@286 | 658 | |
paul@300 | 659 | time = (hour or minute or second) and "T%s%s%s" % (hour, minute, second) or "" |
paul@300 | 660 | value = "%s%s" % (date, time) |
paul@300 | 661 | attr = {"TZID" : tzid, "VALUE" : "DATE-TIME"} |
paul@300 | 662 | dt = get_datetime(value, attr) |
paul@300 | 663 | else: |
paul@300 | 664 | attr = {"VALUE" : "DATE"} |
paul@300 | 665 | dt = get_datetime(date) |
paul@300 | 666 | |
paul@286 | 667 | if dt: |
paul@290 | 668 | return dt, attr |
paul@286 | 669 | |
paul@286 | 670 | return None |
paul@286 | 671 | |
paul@286 | 672 | def set_datetime_in_object(self, dt, tzid, property, obj): |
paul@286 | 673 | |
paul@286 | 674 | """ |
paul@286 | 675 | Set 'dt' and 'tzid' for the given 'property' in 'obj', returning whether |
paul@286 | 676 | an update has occurred. |
paul@286 | 677 | """ |
paul@286 | 678 | |
paul@286 | 679 | if dt: |
paul@286 | 680 | old_value = obj.get_value(property) |
paul@286 | 681 | obj[property] = [get_datetime_item(dt, tzid)] |
paul@286 | 682 | return format_datetime(dt) != old_value |
paul@286 | 683 | |
paul@286 | 684 | return False |
paul@286 | 685 | |
paul@286 | 686 | # Page fragment methods. |
paul@286 | 687 | |
paul@286 | 688 | def show_request_controls(self, obj): |
paul@286 | 689 | |
paul@286 | 690 | "Show form controls for a request concerning 'obj'." |
paul@286 | 691 | |
paul@212 | 692 | page = self.page |
paul@212 | 693 | |
paul@309 | 694 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@207 | 695 | |
paul@309 | 696 | attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@253 | 697 | is_attendee = attendees.has_key(self.user) |
paul@253 | 698 | attendee_attr = attendees.get(self.user) |
paul@121 | 699 | |
paul@276 | 700 | is_request = obj.get_value("UID") in self._get_requests() |
paul@276 | 701 | |
paul@257 | 702 | have_other_attendees = len(attendees) > (is_attendee and 1 or 0) |
paul@257 | 703 | |
paul@257 | 704 | # Show appropriate options depending on the role of the user. |
paul@257 | 705 | |
paul@257 | 706 | if is_attendee and not is_organiser: |
paul@286 | 707 | page.p("An action is required for this request:") |
paul@253 | 708 | |
paul@255 | 709 | page.p() |
paul@266 | 710 | page.input(name="reply", type="submit", value="Reply") |
paul@255 | 711 | page.add(" ") |
paul@255 | 712 | page.input(name="discard", type="submit", value="Discard") |
paul@255 | 713 | page.p.close() |
paul@207 | 714 | |
paul@255 | 715 | if is_organiser: |
paul@257 | 716 | if have_other_attendees: |
paul@286 | 717 | page.p("As organiser, you can perform the following:") |
paul@255 | 718 | |
paul@257 | 719 | page.p() |
paul@257 | 720 | page.input(name="invite", type="submit", value="Invite") |
paul@257 | 721 | page.add(" ") |
paul@276 | 722 | if is_request: |
paul@276 | 723 | page.input(name="discard", type="submit", value="Discard") |
paul@276 | 724 | else: |
paul@276 | 725 | page.input(name="cancel", type="submit", value="Cancel") |
paul@257 | 726 | page.p.close() |
paul@257 | 727 | else: |
paul@257 | 728 | page.p() |
paul@257 | 729 | page.input(name="save", type="submit", value="Save") |
paul@276 | 730 | page.add(" ") |
paul@276 | 731 | page.input(name="discard", type="submit", value="Discard") |
paul@257 | 732 | page.p.close() |
paul@207 | 733 | |
paul@287 | 734 | property_items = [ |
paul@287 | 735 | ("SUMMARY", "Summary"), |
paul@287 | 736 | ("DTSTART", "Start"), |
paul@287 | 737 | ("DTEND", "End"), |
paul@287 | 738 | ("ORGANIZER", "Organiser"), |
paul@287 | 739 | ("ATTENDEE", "Attendee"), |
paul@287 | 740 | ] |
paul@210 | 741 | |
paul@257 | 742 | partstat_items = [ |
paul@257 | 743 | ("NEEDS-ACTION", "Not confirmed"), |
paul@257 | 744 | ("ACCEPTED", "Attending"), |
paul@259 | 745 | ("TENTATIVE", "Tentatively attending"), |
paul@257 | 746 | ("DECLINED", "Not attending"), |
paul@277 | 747 | ("DELEGATED", "Delegated"), |
paul@257 | 748 | ] |
paul@257 | 749 | |
paul@299 | 750 | def show_object_on_page(self, uid, obj, error=None): |
paul@121 | 751 | |
paul@121 | 752 | """ |
paul@121 | 753 | Show the calendar object with the given 'uid' and representation 'obj' |
paul@299 | 754 | on the current page. If 'error' is given, show a suitable message. |
paul@121 | 755 | """ |
paul@121 | 756 | |
paul@210 | 757 | page = self.page |
paul@212 | 758 | page.form(method="POST") |
paul@210 | 759 | |
paul@154 | 760 | # Obtain the user's timezone. |
paul@154 | 761 | |
paul@244 | 762 | tzid = self.get_tzid() |
paul@121 | 763 | |
paul@290 | 764 | # Provide controls to change the displayed object. |
paul@290 | 765 | |
paul@290 | 766 | args = self.env.get_args() |
paul@300 | 767 | dtend_control = args.get("dtend-control", [None])[0] |
paul@300 | 768 | dttimes_control = args.get("dttimes-control", [None])[0] |
paul@300 | 769 | with_time = dttimes_control == "enable" |
paul@290 | 770 | |
paul@300 | 771 | t = self.handle_date_controls("dtstart", with_time) |
paul@290 | 772 | if t: |
paul@290 | 773 | dtstart, dtstart_attr = t |
paul@290 | 774 | else: |
paul@290 | 775 | dtstart, dtstart_attr = obj.get_datetime_item("DTSTART") |
paul@290 | 776 | |
paul@297 | 777 | if dtend_control == "enable": |
paul@300 | 778 | t = self.handle_date_controls("dtend", with_time) |
paul@290 | 779 | if t: |
paul@290 | 780 | dtend, dtend_attr = t |
paul@297 | 781 | else: |
paul@297 | 782 | dtend, dtend_attr = None, {} |
paul@297 | 783 | elif dtend_control == "disable": |
paul@297 | 784 | dtend, dtend_attr = None, {} |
paul@297 | 785 | else: |
paul@290 | 786 | dtend, dtend_attr = obj.get_datetime_item("DTEND") |
paul@290 | 787 | |
paul@290 | 788 | # Change end dates to refer to the actual dates, not the iCalendar |
paul@290 | 789 | # "next day" dates. |
paul@290 | 790 | |
paul@290 | 791 | if dtend and not isinstance(dtend, datetime): |
paul@290 | 792 | dtend -= timedelta(1) |
paul@290 | 793 | |
paul@297 | 794 | # Show the end datetime controls if already active or if an object needs |
paul@297 | 795 | # them. |
paul@297 | 796 | |
paul@300 | 797 | dtend_enabled = dtend_control == "enable" or isinstance(dtend, datetime) or dtstart != dtend |
paul@300 | 798 | dttimes_enabled = dttimes_control == "enable" or isinstance(dtstart, datetime) or isinstance(dtend, datetime) |
paul@297 | 799 | |
paul@300 | 800 | if dtend_enabled: |
paul@290 | 801 | page.input(name="dtend-control", type="radio", value="enable", id="dtend-enable", checked="checked") |
paul@290 | 802 | page.input(name="dtend-control", type="radio", value="disable", id="dtend-disable") |
paul@290 | 803 | else: |
paul@290 | 804 | page.input(name="dtend-control", type="radio", value="enable", id="dtend-enable") |
paul@290 | 805 | page.input(name="dtend-control", type="radio", value="disable", id="dtend-disable", checked="checked") |
paul@290 | 806 | |
paul@300 | 807 | if dttimes_enabled: |
paul@300 | 808 | page.input(name="dttimes-control", type="radio", value="enable", id="dttimes-enable", checked="checked") |
paul@300 | 809 | page.input(name="dttimes-control", type="radio", value="disable", id="dttimes-disable") |
paul@300 | 810 | else: |
paul@300 | 811 | page.input(name="dttimes-control", type="radio", value="enable", id="dttimes-enable") |
paul@300 | 812 | page.input(name="dttimes-control", type="radio", value="disable", id="dttimes-disable", checked="checked") |
paul@300 | 813 | |
paul@121 | 814 | # Provide a summary of the object. |
paul@121 | 815 | |
paul@230 | 816 | page.table(class_="object", cellspacing=5, cellpadding=5) |
paul@212 | 817 | page.thead() |
paul@212 | 818 | page.tr() |
paul@286 | 819 | page.th("Event", class_="mainheading", colspan=2) |
paul@212 | 820 | page.tr.close() |
paul@212 | 821 | page.thead.close() |
paul@212 | 822 | page.tbody() |
paul@121 | 823 | |
paul@309 | 824 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@269 | 825 | |
paul@287 | 826 | for name, label in self.property_items: |
paul@210 | 827 | page.tr() |
paul@210 | 828 | |
paul@210 | 829 | # Handle datetimes specially. |
paul@210 | 830 | |
paul@147 | 831 | if name in ["DTSTART", "DTEND"]: |
paul@299 | 832 | field = name.lower() |
paul@290 | 833 | |
paul@299 | 834 | page.th(label, class_="objectheading %s%s" % (field, error and field in error and " error" or "")) |
paul@290 | 835 | |
paul@297 | 836 | # Obtain the datetime. |
paul@297 | 837 | |
paul@290 | 838 | if name == "DTSTART": |
paul@290 | 839 | dt, attr, event_tzid = dtstart, dtstart_attr, dtstart_attr.get("TZID", tzid) |
paul@297 | 840 | |
paul@297 | 841 | # Where no end datetime exists, use the start datetime as the |
paul@297 | 842 | # basis of any potential datetime specified if dt-control is |
paul@297 | 843 | # set. |
paul@297 | 844 | |
paul@290 | 845 | else: |
paul@293 | 846 | dt, attr, event_tzid = dtend or dtstart, dtend_attr or dtstart_attr, (dtend_attr or dtstart_attr).get("TZID", tzid) |
paul@293 | 847 | |
paul@300 | 848 | # Show controls for editing as organiser. |
paul@286 | 849 | |
paul@286 | 850 | if is_organiser: |
paul@300 | 851 | value = format_datetime(dt) |
paul@300 | 852 | |
paul@299 | 853 | page.td(class_="objectvalue %s" % field) |
paul@290 | 854 | if name == "DTEND": |
paul@300 | 855 | page.div(class_="dt disabled") |
paul@290 | 856 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@290 | 857 | page.div.close() |
paul@290 | 858 | |
paul@300 | 859 | page.div(class_="dt enabled") |
paul@299 | 860 | self._show_date_controls(field, value, attr, tzid) |
paul@300 | 861 | if name == "DTSTART": |
paul@300 | 862 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@300 | 863 | page.label("Specify dates only", for_="dttimes-disable", class_="time enabled disable") |
paul@300 | 864 | elif name == "DTEND": |
paul@290 | 865 | page.label("End on same day", for_="dtend-disable", class_="disable") |
paul@290 | 866 | page.div.close() |
paul@290 | 867 | |
paul@286 | 868 | page.td.close() |
paul@300 | 869 | |
paul@300 | 870 | # Show a label as attendee. |
paul@300 | 871 | |
paul@286 | 872 | else: |
paul@300 | 873 | page.td(self.format_datetime(dt, "full")) |
paul@286 | 874 | |
paul@210 | 875 | page.tr.close() |
paul@210 | 876 | |
paul@212 | 877 | # Handle the summary specially. |
paul@212 | 878 | |
paul@212 | 879 | elif name == "SUMMARY": |
paul@290 | 880 | value = args.get("summary", [obj.get_value(name)])[0] |
paul@290 | 881 | |
paul@212 | 882 | page.th(label, class_="objectheading") |
paul@286 | 883 | page.td() |
paul@269 | 884 | if is_organiser: |
paul@269 | 885 | page.input(name="summary", type="text", value=value, size=80) |
paul@269 | 886 | else: |
paul@269 | 887 | page.add(value) |
paul@212 | 888 | page.td.close() |
paul@212 | 889 | page.tr.close() |
paul@212 | 890 | |
paul@210 | 891 | # Handle potentially many values. |
paul@210 | 892 | |
paul@147 | 893 | else: |
paul@213 | 894 | items = obj.get_items(name) |
paul@233 | 895 | if not items: |
paul@233 | 896 | continue |
paul@233 | 897 | |
paul@210 | 898 | page.th(label, class_="objectheading", rowspan=len(items)) |
paul@210 | 899 | |
paul@210 | 900 | first = True |
paul@210 | 901 | |
paul@308 | 902 | for i, (value, attr) in enumerate(items): |
paul@210 | 903 | if not first: |
paul@210 | 904 | page.tr() |
paul@210 | 905 | else: |
paul@210 | 906 | first = False |
paul@121 | 907 | |
paul@277 | 908 | if name in ("ATTENDEE", "ORGANIZER"): |
paul@309 | 909 | value = get_uri(value) |
paul@309 | 910 | |
paul@265 | 911 | page.td(class_="objectattribute") |
paul@265 | 912 | page.add(value) |
paul@286 | 913 | page.add(" ") |
paul@210 | 914 | |
paul@210 | 915 | partstat = attr.get("PARTSTAT") |
paul@286 | 916 | if value == self.user and (not is_organiser or name == "ORGANIZER"): |
paul@257 | 917 | self._show_menu("partstat", partstat, self.partstat_items) |
paul@265 | 918 | else: |
paul@286 | 919 | page.span(dict(self.partstat_items).get(partstat, ""), class_="partstat") |
paul@308 | 920 | |
paul@308 | 921 | if is_organiser and name == "ATTENDEE": |
paul@308 | 922 | page.input(name="remove", type="checkbox", value=value, id="remove-%d" % i, class_="remove") |
paul@308 | 923 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@308 | 924 | page.label("Uninvited", for_="remove-%d" % i, class_="removed") |
paul@308 | 925 | |
paul@265 | 926 | else: |
paul@286 | 927 | page.td(class_="objectattribute") |
paul@265 | 928 | page.add(value) |
paul@210 | 929 | |
paul@210 | 930 | page.td.close() |
paul@210 | 931 | page.tr.close() |
paul@210 | 932 | |
paul@212 | 933 | page.tbody.close() |
paul@210 | 934 | page.table.close() |
paul@121 | 935 | |
paul@307 | 936 | self.show_conflicting_events(uid, obj) |
paul@307 | 937 | self.show_request_controls(obj) |
paul@307 | 938 | |
paul@307 | 939 | page.form.close() |
paul@307 | 940 | |
paul@307 | 941 | def show_conflicting_events(self, uid, obj): |
paul@307 | 942 | |
paul@307 | 943 | """ |
paul@307 | 944 | Show conflicting events for the object having the given 'uid' and |
paul@307 | 945 | representation 'obj'. |
paul@307 | 946 | """ |
paul@307 | 947 | |
paul@307 | 948 | page = self.page |
paul@307 | 949 | |
paul@307 | 950 | # Obtain the user's timezone. |
paul@307 | 951 | |
paul@307 | 952 | tzid = self.get_tzid() |
paul@307 | 953 | |
paul@213 | 954 | dtstart = format_datetime(obj.get_utc_datetime("DTSTART")) |
paul@213 | 955 | dtend = format_datetime(obj.get_utc_datetime("DTEND")) |
paul@121 | 956 | |
paul@121 | 957 | # Indicate whether there are conflicting events. |
paul@121 | 958 | |
paul@121 | 959 | freebusy = self.store.get_freebusy(self.user) |
paul@121 | 960 | |
paul@121 | 961 | if freebusy: |
paul@121 | 962 | |
paul@121 | 963 | # Obtain any time zone details from the suggested event. |
paul@121 | 964 | |
paul@213 | 965 | _dtstart, attr = obj.get_item("DTSTART") |
paul@154 | 966 | tzid = attr.get("TZID", tzid) |
paul@121 | 967 | |
paul@121 | 968 | # Show any conflicts. |
paul@121 | 969 | |
paul@302 | 970 | conflicts = [t for t in have_conflict(freebusy, [(dtstart, dtend)], True) if t[2] != uid] |
paul@154 | 971 | |
paul@302 | 972 | if conflicts: |
paul@302 | 973 | page.p("This event conflicts with others:") |
paul@154 | 974 | |
paul@302 | 975 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@302 | 976 | page.thead() |
paul@302 | 977 | page.tr() |
paul@302 | 978 | page.th("Event") |
paul@302 | 979 | page.th("Start") |
paul@302 | 980 | page.th("End") |
paul@302 | 981 | page.tr.close() |
paul@302 | 982 | page.thead.close() |
paul@302 | 983 | page.tbody() |
paul@302 | 984 | |
paul@302 | 985 | for t in conflicts: |
paul@302 | 986 | start, end, found_uid = t[:3] |
paul@302 | 987 | |
paul@302 | 988 | # Provide details of any conflicting event. |
paul@302 | 989 | |
paul@302 | 990 | start = self.format_datetime(to_timezone(get_datetime(start), tzid), "long") |
paul@302 | 991 | end = self.format_datetime(to_timezone(get_datetime(end), tzid), "long") |
paul@302 | 992 | |
paul@302 | 993 | page.tr() |
paul@154 | 994 | |
paul@154 | 995 | # Show the event summary for the conflicting event. |
paul@154 | 996 | |
paul@302 | 997 | page.td() |
paul@302 | 998 | |
paul@154 | 999 | found_obj = self._get_object(found_uid) |
paul@154 | 1000 | if found_obj: |
paul@213 | 1001 | page.a(found_obj.get_value("SUMMARY"), href=self.env.new_url(found_uid)) |
paul@302 | 1002 | else: |
paul@302 | 1003 | page.add("No details available") |
paul@302 | 1004 | |
paul@302 | 1005 | page.td.close() |
paul@302 | 1006 | |
paul@302 | 1007 | page.td(start) |
paul@302 | 1008 | page.td(end) |
paul@302 | 1009 | |
paul@302 | 1010 | page.tr.close() |
paul@302 | 1011 | |
paul@302 | 1012 | page.tbody.close() |
paul@302 | 1013 | page.table.close() |
paul@121 | 1014 | |
paul@121 | 1015 | def show_requests_on_page(self): |
paul@69 | 1016 | |
paul@69 | 1017 | "Show requests for the current user." |
paul@69 | 1018 | |
paul@69 | 1019 | # NOTE: This list could be more informative, but it is envisaged that |
paul@69 | 1020 | # NOTE: the requests would be visited directly anyway. |
paul@69 | 1021 | |
paul@121 | 1022 | requests = self._get_requests() |
paul@70 | 1023 | |
paul@185 | 1024 | self.page.div(id="pending-requests") |
paul@185 | 1025 | |
paul@80 | 1026 | if requests: |
paul@114 | 1027 | self.page.p("Pending requests:") |
paul@114 | 1028 | |
paul@80 | 1029 | self.page.ul() |
paul@69 | 1030 | |
paul@80 | 1031 | for request in requests: |
paul@165 | 1032 | obj = self._get_object(request) |
paul@165 | 1033 | if obj: |
paul@165 | 1034 | self.page.li() |
paul@213 | 1035 | self.page.a(obj.get_value("SUMMARY"), href="#request-%s" % request) |
paul@165 | 1036 | self.page.li.close() |
paul@80 | 1037 | |
paul@80 | 1038 | self.page.ul.close() |
paul@80 | 1039 | |
paul@80 | 1040 | else: |
paul@80 | 1041 | self.page.p("There are no pending requests.") |
paul@69 | 1042 | |
paul@185 | 1043 | self.page.div.close() |
paul@185 | 1044 | |
paul@185 | 1045 | def show_participants_on_page(self): |
paul@185 | 1046 | |
paul@185 | 1047 | "Show participants for scheduling purposes." |
paul@185 | 1048 | |
paul@185 | 1049 | args = self.env.get_args() |
paul@185 | 1050 | participants = args.get("participants", []) |
paul@185 | 1051 | |
paul@185 | 1052 | try: |
paul@185 | 1053 | for name, value in args.items(): |
paul@185 | 1054 | if name.startswith("remove-participant-"): |
paul@185 | 1055 | i = int(name[len("remove-participant-"):]) |
paul@185 | 1056 | del participants[i] |
paul@185 | 1057 | break |
paul@185 | 1058 | except ValueError: |
paul@185 | 1059 | pass |
paul@185 | 1060 | |
paul@185 | 1061 | # Trim empty participants. |
paul@185 | 1062 | |
paul@185 | 1063 | while participants and not participants[-1].strip(): |
paul@185 | 1064 | participants.pop() |
paul@185 | 1065 | |
paul@185 | 1066 | # Show any specified participants together with controls to remove and |
paul@185 | 1067 | # add participants. |
paul@185 | 1068 | |
paul@185 | 1069 | self.page.div(id="participants") |
paul@185 | 1070 | |
paul@185 | 1071 | self.page.p("Participants for scheduling:") |
paul@185 | 1072 | |
paul@185 | 1073 | for i, participant in enumerate(participants): |
paul@185 | 1074 | self.page.p() |
paul@185 | 1075 | self.page.input(name="participants", type="text", value=participant) |
paul@185 | 1076 | self.page.input(name="remove-participant-%d" % i, type="submit", value="Remove") |
paul@185 | 1077 | self.page.p.close() |
paul@185 | 1078 | |
paul@185 | 1079 | self.page.p() |
paul@185 | 1080 | self.page.input(name="participants", type="text") |
paul@185 | 1081 | self.page.input(name="add-participant", type="submit", value="Add") |
paul@185 | 1082 | self.page.p.close() |
paul@185 | 1083 | |
paul@185 | 1084 | self.page.div.close() |
paul@185 | 1085 | |
paul@185 | 1086 | return participants |
paul@185 | 1087 | |
paul@121 | 1088 | # Full page output methods. |
paul@70 | 1089 | |
paul@121 | 1090 | def show_object(self, path_info): |
paul@70 | 1091 | |
paul@121 | 1092 | "Show an object request using the given 'path_info' for the current user." |
paul@70 | 1093 | |
paul@121 | 1094 | uid = self._get_uid(path_info) |
paul@121 | 1095 | obj = self._get_object(uid) |
paul@121 | 1096 | |
paul@121 | 1097 | if not obj: |
paul@70 | 1098 | return False |
paul@70 | 1099 | |
paul@299 | 1100 | error = self.handle_request(uid, obj) |
paul@77 | 1101 | |
paul@299 | 1102 | if not error: |
paul@123 | 1103 | return True |
paul@73 | 1104 | |
paul@123 | 1105 | self.new_page(title="Event") |
paul@299 | 1106 | self.show_object_on_page(uid, obj, error) |
paul@73 | 1107 | |
paul@70 | 1108 | return True |
paul@70 | 1109 | |
paul@114 | 1110 | def show_calendar(self): |
paul@114 | 1111 | |
paul@114 | 1112 | "Show the calendar for the current user." |
paul@114 | 1113 | |
paul@202 | 1114 | handled = self.handle_newevent() |
paul@202 | 1115 | |
paul@114 | 1116 | self.new_page(title="Calendar") |
paul@162 | 1117 | page = self.page |
paul@162 | 1118 | |
paul@196 | 1119 | # Form controls are used in various places on the calendar page. |
paul@196 | 1120 | |
paul@196 | 1121 | page.form(method="POST") |
paul@196 | 1122 | |
paul@121 | 1123 | self.show_requests_on_page() |
paul@185 | 1124 | participants = self.show_participants_on_page() |
paul@114 | 1125 | |
paul@196 | 1126 | # Show a button for scheduling a new event. |
paul@196 | 1127 | |
paul@230 | 1128 | page.p(class_="controls") |
paul@196 | 1129 | page.input(name="newevent", type="submit", value="New event", id="newevent") |
paul@258 | 1130 | page.input(name="reset", type="submit", value="Clear selections", id="reset") |
paul@196 | 1131 | page.p.close() |
paul@196 | 1132 | |
paul@280 | 1133 | # Show controls for hiding empty days and busy slots. |
paul@203 | 1134 | # The positioning of the control, paragraph and table are important here. |
paul@203 | 1135 | |
paul@288 | 1136 | page.input(name="showdays", type="checkbox", value="show", id="showdays", accesskey="D") |
paul@282 | 1137 | page.input(name="hidebusy", type="checkbox", value="hide", id="hidebusy", accesskey="B") |
paul@203 | 1138 | |
paul@230 | 1139 | page.p(class_="controls") |
paul@237 | 1140 | page.label("Hide busy time periods", for_="hidebusy", class_="hidebusy enable") |
paul@237 | 1141 | page.label("Show busy time periods", for_="hidebusy", class_="hidebusy disable") |
paul@288 | 1142 | page.label("Show empty days", for_="showdays", class_="showdays disable") |
paul@288 | 1143 | page.label("Hide empty days", for_="showdays", class_="showdays enable") |
paul@203 | 1144 | page.p.close() |
paul@203 | 1145 | |
paul@114 | 1146 | freebusy = self.store.get_freebusy(self.user) |
paul@114 | 1147 | |
paul@114 | 1148 | if not freebusy: |
paul@114 | 1149 | page.p("No events scheduled.") |
paul@114 | 1150 | return |
paul@114 | 1151 | |
paul@154 | 1152 | # Obtain the user's timezone. |
paul@147 | 1153 | |
paul@244 | 1154 | tzid = self.get_tzid() |
paul@147 | 1155 | |
paul@114 | 1156 | # Day view: start at the earliest known day and produce days until the |
paul@114 | 1157 | # latest known day, perhaps with expandable sections of empty days. |
paul@114 | 1158 | |
paul@114 | 1159 | # Month view: start at the earliest known month and produce months until |
paul@114 | 1160 | # the latest known month, perhaps with expandable sections of empty |
paul@114 | 1161 | # months. |
paul@114 | 1162 | |
paul@114 | 1163 | # Details of users to invite to new events could be superimposed on the |
paul@114 | 1164 | # calendar. |
paul@114 | 1165 | |
paul@185 | 1166 | # Requests are listed and linked to their tentative positions in the |
paul@185 | 1167 | # calendar. Other participants are also shown. |
paul@185 | 1168 | |
paul@185 | 1169 | request_summary = self._get_request_summary() |
paul@185 | 1170 | |
paul@185 | 1171 | period_groups = [request_summary, freebusy] |
paul@185 | 1172 | period_group_types = ["request", "freebusy"] |
paul@185 | 1173 | period_group_sources = ["Pending requests", "Your schedule"] |
paul@185 | 1174 | |
paul@187 | 1175 | for i, participant in enumerate(participants): |
paul@185 | 1176 | period_groups.append(self.store.get_freebusy_for_other(self.user, get_uri(participant))) |
paul@187 | 1177 | period_group_types.append("freebusy-part%d" % i) |
paul@185 | 1178 | period_group_sources.append(participant) |
paul@114 | 1179 | |
paul@162 | 1180 | groups = [] |
paul@162 | 1181 | group_columns = [] |
paul@185 | 1182 | group_types = period_group_types |
paul@185 | 1183 | group_sources = period_group_sources |
paul@162 | 1184 | all_points = set() |
paul@162 | 1185 | |
paul@162 | 1186 | # Obtain time point information for each group of periods. |
paul@162 | 1187 | |
paul@185 | 1188 | for periods in period_groups: |
paul@162 | 1189 | periods = convert_periods(periods, tzid) |
paul@162 | 1190 | |
paul@162 | 1191 | # Get the time scale with start and end points. |
paul@162 | 1192 | |
paul@162 | 1193 | scale = get_scale(periods) |
paul@162 | 1194 | |
paul@162 | 1195 | # Get the time slots for the periods. |
paul@162 | 1196 | |
paul@162 | 1197 | slots = get_slots(scale) |
paul@162 | 1198 | |
paul@162 | 1199 | # Add start of day time points for multi-day periods. |
paul@162 | 1200 | |
paul@244 | 1201 | add_day_start_points(slots, tzid) |
paul@162 | 1202 | |
paul@162 | 1203 | # Record the slots and all time points employed. |
paul@162 | 1204 | |
paul@162 | 1205 | groups.append(slots) |
paul@201 | 1206 | all_points.update([point for point, active in slots]) |
paul@162 | 1207 | |
paul@162 | 1208 | # Partition the groups into days. |
paul@162 | 1209 | |
paul@162 | 1210 | days = {} |
paul@162 | 1211 | partitioned_groups = [] |
paul@171 | 1212 | partitioned_group_types = [] |
paul@185 | 1213 | partitioned_group_sources = [] |
paul@162 | 1214 | |
paul@185 | 1215 | for slots, group_type, group_source in zip(groups, group_types, group_sources): |
paul@162 | 1216 | |
paul@162 | 1217 | # Propagate time points to all groups of time slots. |
paul@162 | 1218 | |
paul@162 | 1219 | add_slots(slots, all_points) |
paul@162 | 1220 | |
paul@162 | 1221 | # Count the number of columns employed by the group. |
paul@162 | 1222 | |
paul@162 | 1223 | columns = 0 |
paul@162 | 1224 | |
paul@162 | 1225 | # Partition the time slots by day. |
paul@162 | 1226 | |
paul@162 | 1227 | partitioned = {} |
paul@162 | 1228 | |
paul@162 | 1229 | for day, day_slots in partition_by_day(slots).items(): |
paul@201 | 1230 | intervals = [] |
paul@201 | 1231 | last = None |
paul@201 | 1232 | |
paul@201 | 1233 | for point, active in day_slots: |
paul@201 | 1234 | columns = max(columns, len(active)) |
paul@201 | 1235 | if last: |
paul@201 | 1236 | intervals.append((last, point)) |
paul@201 | 1237 | last = point |
paul@201 | 1238 | |
paul@201 | 1239 | if last: |
paul@201 | 1240 | intervals.append((last, None)) |
paul@162 | 1241 | |
paul@162 | 1242 | if not days.has_key(day): |
paul@162 | 1243 | days[day] = set() |
paul@162 | 1244 | |
paul@162 | 1245 | # Convert each partition to a mapping from points to active |
paul@162 | 1246 | # periods. |
paul@162 | 1247 | |
paul@201 | 1248 | partitioned[day] = dict(day_slots) |
paul@201 | 1249 | |
paul@201 | 1250 | # Record the divisions or intervals within each day. |
paul@201 | 1251 | |
paul@201 | 1252 | days[day].update(intervals) |
paul@162 | 1253 | |
paul@194 | 1254 | if group_type != "request" or columns: |
paul@194 | 1255 | group_columns.append(columns) |
paul@194 | 1256 | partitioned_groups.append(partitioned) |
paul@194 | 1257 | partitioned_group_types.append(group_type) |
paul@194 | 1258 | partitioned_group_sources.append(group_source) |
paul@114 | 1259 | |
paul@279 | 1260 | # Add empty days. |
paul@279 | 1261 | |
paul@283 | 1262 | add_empty_days(days, tzid) |
paul@279 | 1263 | |
paul@279 | 1264 | # Show the controls permitting day selection. |
paul@279 | 1265 | |
paul@243 | 1266 | self.show_calendar_day_controls(days) |
paul@243 | 1267 | |
paul@279 | 1268 | # Show the calendar itself. |
paul@279 | 1269 | |
paul@230 | 1270 | page.table(cellspacing=5, cellpadding=5, class_="calendar") |
paul@188 | 1271 | self.show_calendar_participant_headings(partitioned_group_types, partitioned_group_sources, group_columns) |
paul@171 | 1272 | self.show_calendar_days(days, partitioned_groups, partitioned_group_types, group_columns) |
paul@162 | 1273 | page.table.close() |
paul@114 | 1274 | |
paul@196 | 1275 | # End the form region. |
paul@196 | 1276 | |
paul@196 | 1277 | page.form.close() |
paul@196 | 1278 | |
paul@246 | 1279 | # More page fragment methods. |
paul@246 | 1280 | |
paul@243 | 1281 | def show_calendar_day_controls(self, days): |
paul@243 | 1282 | |
paul@243 | 1283 | "Show controls for the given 'days' in the calendar." |
paul@243 | 1284 | |
paul@243 | 1285 | page = self.page |
paul@243 | 1286 | slots = self.env.get_args().get("slot", []) |
paul@243 | 1287 | |
paul@243 | 1288 | for day in days: |
paul@243 | 1289 | value, identifier = self._day_value_and_identifier(day) |
paul@243 | 1290 | self._slot_selector(value, identifier, slots) |
paul@243 | 1291 | |
paul@243 | 1292 | # Generate a dynamic stylesheet to allow day selections to colour |
paul@243 | 1293 | # specific days. |
paul@243 | 1294 | # NOTE: The style details need to be coordinated with the static |
paul@243 | 1295 | # NOTE: stylesheet. |
paul@243 | 1296 | |
paul@243 | 1297 | page.style(type="text/css") |
paul@243 | 1298 | |
paul@243 | 1299 | for day in days: |
paul@243 | 1300 | daystr = format_datetime(day) |
paul@243 | 1301 | page.add("""\ |
paul@249 | 1302 | input.newevent.selector#day-%s-:checked ~ table label.day.day-%s, |
paul@249 | 1303 | input.newevent.selector#day-%s-:checked ~ table label.timepoint.day-%s { |
paul@243 | 1304 | background-color: #5f4; |
paul@243 | 1305 | text-decoration: underline; |
paul@243 | 1306 | } |
paul@243 | 1307 | """ % (daystr, daystr, daystr, daystr)) |
paul@243 | 1308 | |
paul@243 | 1309 | page.style.close() |
paul@243 | 1310 | |
paul@188 | 1311 | def show_calendar_participant_headings(self, group_types, group_sources, group_columns): |
paul@186 | 1312 | |
paul@186 | 1313 | """ |
paul@186 | 1314 | Show headings for the participants and other scheduling contributors, |
paul@188 | 1315 | defined by 'group_types', 'group_sources' and 'group_columns'. |
paul@186 | 1316 | """ |
paul@186 | 1317 | |
paul@185 | 1318 | page = self.page |
paul@185 | 1319 | |
paul@188 | 1320 | page.colgroup(span=1, id="columns-timeslot") |
paul@186 | 1321 | |
paul@188 | 1322 | for group_type, columns in zip(group_types, group_columns): |
paul@191 | 1323 | page.colgroup(span=max(columns, 1), id="columns-%s" % group_type) |
paul@186 | 1324 | |
paul@185 | 1325 | page.thead() |
paul@185 | 1326 | page.tr() |
paul@185 | 1327 | page.th("", class_="emptyheading") |
paul@185 | 1328 | |
paul@193 | 1329 | for group_type, source, columns in zip(group_types, group_sources, group_columns): |
paul@193 | 1330 | page.th(source, |
paul@193 | 1331 | class_=(group_type == "request" and "requestheading" or "participantheading"), |
paul@193 | 1332 | colspan=max(columns, 1)) |
paul@185 | 1333 | |
paul@185 | 1334 | page.tr.close() |
paul@185 | 1335 | page.thead.close() |
paul@185 | 1336 | |
paul@171 | 1337 | def show_calendar_days(self, days, partitioned_groups, partitioned_group_types, group_columns): |
paul@186 | 1338 | |
paul@186 | 1339 | """ |
paul@186 | 1340 | Show calendar days, defined by a collection of 'days', the contributing |
paul@186 | 1341 | period information as 'partitioned_groups' (partitioned by day), the |
paul@186 | 1342 | 'partitioned_group_types' indicating the kind of contribution involved, |
paul@186 | 1343 | and the 'group_columns' defining the number of columns in each group. |
paul@186 | 1344 | """ |
paul@186 | 1345 | |
paul@162 | 1346 | page = self.page |
paul@162 | 1347 | |
paul@191 | 1348 | # Determine the number of columns required. Where participants provide |
paul@191 | 1349 | # no columns for events, one still needs to be provided for the |
paul@191 | 1350 | # participant itself. |
paul@147 | 1351 | |
paul@191 | 1352 | all_columns = sum([max(columns, 1) for columns in group_columns]) |
paul@191 | 1353 | |
paul@191 | 1354 | # Determine the days providing time slots. |
paul@191 | 1355 | |
paul@162 | 1356 | all_days = days.items() |
paul@162 | 1357 | all_days.sort() |
paul@162 | 1358 | |
paul@162 | 1359 | # Produce a heading and time points for each day. |
paul@162 | 1360 | |
paul@201 | 1361 | for day, intervals in all_days: |
paul@279 | 1362 | groups_for_day = [partitioned.get(day) for partitioned in partitioned_groups] |
paul@279 | 1363 | is_empty = True |
paul@279 | 1364 | |
paul@279 | 1365 | for slots in groups_for_day: |
paul@279 | 1366 | if not slots: |
paul@279 | 1367 | continue |
paul@279 | 1368 | |
paul@279 | 1369 | for active in slots.values(): |
paul@279 | 1370 | if active: |
paul@279 | 1371 | is_empty = False |
paul@279 | 1372 | break |
paul@279 | 1373 | |
paul@282 | 1374 | page.thead(class_="separator%s" % (is_empty and " empty" or "")) |
paul@282 | 1375 | page.tr() |
paul@243 | 1376 | page.th(class_="dayheading container", colspan=all_columns+1) |
paul@239 | 1377 | self._day_heading(day) |
paul@114 | 1378 | page.th.close() |
paul@153 | 1379 | page.tr.close() |
paul@186 | 1380 | page.thead.close() |
paul@114 | 1381 | |
paul@282 | 1382 | page.tbody(class_="points%s" % (is_empty and " empty" or "")) |
paul@280 | 1383 | self.show_calendar_points(intervals, groups_for_day, partitioned_group_types, group_columns) |
paul@186 | 1384 | page.tbody.close() |
paul@185 | 1385 | |
paul@280 | 1386 | def show_calendar_points(self, intervals, groups, group_types, group_columns): |
paul@186 | 1387 | |
paul@186 | 1388 | """ |
paul@201 | 1389 | Show the time 'intervals' along with period information from the given |
paul@186 | 1390 | 'groups', having the indicated 'group_types', each with the number of |
paul@186 | 1391 | columns given by 'group_columns'. |
paul@186 | 1392 | """ |
paul@186 | 1393 | |
paul@162 | 1394 | page = self.page |
paul@162 | 1395 | |
paul@244 | 1396 | # Obtain the user's timezone. |
paul@244 | 1397 | |
paul@244 | 1398 | tzid = self.get_tzid() |
paul@244 | 1399 | |
paul@203 | 1400 | # Produce a row for each interval. |
paul@162 | 1401 | |
paul@201 | 1402 | intervals = list(intervals) |
paul@201 | 1403 | intervals.sort() |
paul@162 | 1404 | |
paul@201 | 1405 | for point, endpoint in intervals: |
paul@244 | 1406 | continuation = point == get_start_of_day(point, tzid) |
paul@153 | 1407 | |
paul@203 | 1408 | # Some rows contain no period details and are marked as such. |
paul@203 | 1409 | |
paul@283 | 1410 | have_active = reduce(lambda x, y: x or y, [slots and slots.get(point) for slots in groups], None) |
paul@203 | 1411 | |
paul@203 | 1412 | css = " ".join( |
paul@203 | 1413 | ["slot"] + |
paul@231 | 1414 | (have_active and ["busy"] or ["empty"]) + |
paul@203 | 1415 | (continuation and ["daystart"] or []) |
paul@203 | 1416 | ) |
paul@203 | 1417 | |
paul@203 | 1418 | page.tr(class_=css) |
paul@162 | 1419 | page.th(class_="timeslot") |
paul@201 | 1420 | self._time_point(point, endpoint) |
paul@162 | 1421 | page.th.close() |
paul@162 | 1422 | |
paul@162 | 1423 | # Obtain slots for the time point from each group. |
paul@162 | 1424 | |
paul@171 | 1425 | for columns, slots, group_type in zip(group_columns, groups, group_types): |
paul@162 | 1426 | active = slots and slots.get(point) |
paul@162 | 1427 | |
paul@191 | 1428 | # Where no periods exist for the given time interval, generate |
paul@191 | 1429 | # an empty cell. Where a participant provides no periods at all, |
paul@191 | 1430 | # the colspan is adjusted to be 1, not 0. |
paul@191 | 1431 | |
paul@162 | 1432 | if not active: |
paul@196 | 1433 | page.td(class_="empty container", colspan=max(columns, 1)) |
paul@201 | 1434 | self._empty_slot(point, endpoint) |
paul@196 | 1435 | page.td.close() |
paul@162 | 1436 | continue |
paul@162 | 1437 | |
paul@162 | 1438 | slots = slots.items() |
paul@162 | 1439 | slots.sort() |
paul@162 | 1440 | spans = get_spans(slots) |
paul@162 | 1441 | |
paul@278 | 1442 | empty = 0 |
paul@278 | 1443 | |
paul@162 | 1444 | # Show a column for each active period. |
paul@117 | 1445 | |
paul@153 | 1446 | for t in active: |
paul@185 | 1447 | if t and len(t) >= 2: |
paul@278 | 1448 | |
paul@278 | 1449 | # Flush empty slots preceding this one. |
paul@278 | 1450 | |
paul@278 | 1451 | if empty: |
paul@278 | 1452 | page.td(class_="empty container", colspan=empty) |
paul@278 | 1453 | self._empty_slot(point, endpoint) |
paul@278 | 1454 | page.td.close() |
paul@278 | 1455 | empty = 0 |
paul@278 | 1456 | |
paul@185 | 1457 | start, end, uid, key = get_freebusy_details(t) |
paul@185 | 1458 | span = spans[key] |
paul@171 | 1459 | |
paul@171 | 1460 | # Produce a table cell only at the start of the period |
paul@171 | 1461 | # or when continued at the start of a day. |
paul@171 | 1462 | |
paul@153 | 1463 | if point == start or continuation: |
paul@153 | 1464 | |
paul@275 | 1465 | obj = self._get_object(uid) |
paul@275 | 1466 | |
paul@195 | 1467 | has_continued = continuation and point != start |
paul@244 | 1468 | will_continue = not ends_on_same_day(point, end, tzid) |
paul@309 | 1469 | is_organiser = obj and get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@275 | 1470 | |
paul@195 | 1471 | css = " ".join( |
paul@195 | 1472 | ["event"] + |
paul@195 | 1473 | (has_continued and ["continued"] or []) + |
paul@275 | 1474 | (will_continue and ["continues"] or []) + |
paul@275 | 1475 | (is_organiser and ["organising"] or ["attending"]) |
paul@195 | 1476 | ) |
paul@195 | 1477 | |
paul@189 | 1478 | # Only anchor the first cell of events. |
paul@189 | 1479 | |
paul@189 | 1480 | if point == start: |
paul@195 | 1481 | page.td(class_=css, rowspan=span, id="%s-%s" % (group_type, uid)) |
paul@189 | 1482 | else: |
paul@195 | 1483 | page.td(class_=css, rowspan=span) |
paul@171 | 1484 | |
paul@185 | 1485 | if not obj: |
paul@291 | 1486 | page.span("(Participant is busy)") |
paul@185 | 1487 | else: |
paul@213 | 1488 | summary = obj.get_value("SUMMARY") |
paul@171 | 1489 | |
paul@171 | 1490 | # Only link to events if they are not being |
paul@171 | 1491 | # updated by requests. |
paul@171 | 1492 | |
paul@171 | 1493 | if uid in self._get_requests() and group_type != "request": |
paul@189 | 1494 | page.span(summary) |
paul@164 | 1495 | else: |
paul@171 | 1496 | href = "%s/%s" % (self.env.get_url().rstrip("/"), uid) |
paul@189 | 1497 | page.a(summary, href=href) |
paul@171 | 1498 | |
paul@153 | 1499 | page.td.close() |
paul@153 | 1500 | else: |
paul@278 | 1501 | empty += 1 |
paul@114 | 1502 | |
paul@166 | 1503 | # Pad with empty columns. |
paul@166 | 1504 | |
paul@278 | 1505 | empty = columns - len(active) |
paul@278 | 1506 | |
paul@278 | 1507 | if empty: |
paul@278 | 1508 | page.td(class_="empty container", colspan=empty) |
paul@201 | 1509 | self._empty_slot(point, endpoint) |
paul@196 | 1510 | page.td.close() |
paul@166 | 1511 | |
paul@162 | 1512 | page.tr.close() |
paul@114 | 1513 | |
paul@239 | 1514 | def _day_heading(self, day): |
paul@243 | 1515 | |
paul@243 | 1516 | """ |
paul@243 | 1517 | Generate a heading for 'day' of the following form: |
paul@243 | 1518 | |
paul@243 | 1519 | <label class="day day-20150203" for="day-20150203">Tuesday, 3 February 2015</label> |
paul@243 | 1520 | """ |
paul@243 | 1521 | |
paul@239 | 1522 | page = self.page |
paul@243 | 1523 | daystr = format_datetime(day) |
paul@239 | 1524 | value, identifier = self._day_value_and_identifier(day) |
paul@243 | 1525 | page.label(self.format_date(day, "full"), class_="day day-%s" % daystr, for_=identifier) |
paul@239 | 1526 | |
paul@201 | 1527 | def _time_point(self, point, endpoint): |
paul@243 | 1528 | |
paul@243 | 1529 | """ |
paul@243 | 1530 | Generate headings for the 'point' to 'endpoint' period of the following |
paul@243 | 1531 | form: |
paul@243 | 1532 | |
paul@243 | 1533 | <label class="timepoint day-20150203" for="slot-20150203T090000-20150203T100000">09:00:00 CET</label> |
paul@243 | 1534 | <span class="endpoint">10:00:00 CET</span> |
paul@243 | 1535 | """ |
paul@243 | 1536 | |
paul@201 | 1537 | page = self.page |
paul@244 | 1538 | tzid = self.get_tzid() |
paul@243 | 1539 | daystr = format_datetime(point.date()) |
paul@201 | 1540 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@238 | 1541 | slots = self.env.get_args().get("slot", []) |
paul@239 | 1542 | self._slot_selector(value, identifier, slots) |
paul@243 | 1543 | page.label(self.format_time(point, "long"), class_="timepoint day-%s" % daystr, for_=identifier) |
paul@244 | 1544 | page.span(self.format_time(endpoint or get_end_of_day(point, tzid), "long"), class_="endpoint") |
paul@239 | 1545 | |
paul@239 | 1546 | def _slot_selector(self, value, identifier, slots): |
paul@258 | 1547 | reset = self.env.get_args().has_key("reset") |
paul@239 | 1548 | page = self.page |
paul@258 | 1549 | if not reset and value in slots: |
paul@249 | 1550 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector", checked="checked") |
paul@202 | 1551 | else: |
paul@249 | 1552 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector") |
paul@201 | 1553 | |
paul@201 | 1554 | def _empty_slot(self, point, endpoint): |
paul@197 | 1555 | page = self.page |
paul@201 | 1556 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@236 | 1557 | page.label("Select/deselect period", class_="newevent popup", for_=identifier) |
paul@196 | 1558 | |
paul@239 | 1559 | def _day_value_and_identifier(self, day): |
paul@239 | 1560 | value = "%s-" % format_datetime(day) |
paul@239 | 1561 | identifier = "day-%s" % value |
paul@239 | 1562 | return value, identifier |
paul@239 | 1563 | |
paul@201 | 1564 | def _slot_value_and_identifier(self, point, endpoint): |
paul@202 | 1565 | value = "%s-%s" % (format_datetime(point), endpoint and format_datetime(endpoint) or "") |
paul@201 | 1566 | identifier = "slot-%s" % value |
paul@201 | 1567 | return value, identifier |
paul@196 | 1568 | |
paul@286 | 1569 | def _show_menu(self, name, default, items): |
paul@257 | 1570 | page = self.page |
paul@286 | 1571 | values = self.env.get_args().get(name, [default]) |
paul@257 | 1572 | page.select(name=name) |
paul@257 | 1573 | for v, label in items: |
paul@257 | 1574 | if v in values: |
paul@257 | 1575 | page.option(label, value=v, selected="selected") |
paul@257 | 1576 | else: |
paul@257 | 1577 | page.option(label, value=v) |
paul@257 | 1578 | page.select.close() |
paul@257 | 1579 | |
paul@286 | 1580 | def _show_date_controls(self, name, default, attr, tzid): |
paul@286 | 1581 | |
paul@286 | 1582 | """ |
paul@286 | 1583 | Show date controls for a field with the given 'name' and 'default' value |
paul@286 | 1584 | and 'attr', with the given 'tzid' being used if no other time regime |
paul@286 | 1585 | information is provided. |
paul@286 | 1586 | """ |
paul@286 | 1587 | |
paul@286 | 1588 | page = self.page |
paul@286 | 1589 | args = self.env.get_args() |
paul@286 | 1590 | |
paul@286 | 1591 | event_tzid = attr.get("TZID", tzid) |
paul@286 | 1592 | dt = get_datetime(default, attr) |
paul@286 | 1593 | |
paul@286 | 1594 | # Show dates for up to one week around the current date. |
paul@286 | 1595 | |
paul@286 | 1596 | base = get_date(dt) |
paul@286 | 1597 | items = [] |
paul@286 | 1598 | for i in range(-7, 8): |
paul@286 | 1599 | d = base + timedelta(i) |
paul@286 | 1600 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@286 | 1601 | |
paul@286 | 1602 | self._show_menu("%s-date" % name, format_datetime(base), items) |
paul@286 | 1603 | |
paul@286 | 1604 | # Show time details. |
paul@286 | 1605 | |
paul@300 | 1606 | dt_time = isinstance(dt, datetime) and dt or None |
paul@300 | 1607 | hour = args.get("%s-hour" % name, "%02d" % (dt_time and dt_time.hour or 0)) |
paul@300 | 1608 | minute = args.get("%s-minute" % name, "%02d" % (dt_time and dt_time.minute or 0)) |
paul@300 | 1609 | second = args.get("%s-second" % name, "%02d" % (dt_time and dt_time.second or 0)) |
paul@300 | 1610 | |
paul@300 | 1611 | page.span(class_="time enabled") |
paul@300 | 1612 | page.input(name="%s-hour" % name, type="text", value=hour, maxlength=2, size=2) |
paul@300 | 1613 | page.add(":") |
paul@300 | 1614 | page.input(name="%s-minute" % name, type="text", value=minute, maxlength=2, size=2) |
paul@300 | 1615 | page.add(":") |
paul@300 | 1616 | page.input(name="%s-second" % name, type="text", value=second, maxlength=2, size=2) |
paul@300 | 1617 | page.add(" ") |
paul@300 | 1618 | self._show_menu("%s-tzid" % name, event_tzid, |
paul@300 | 1619 | [(event_tzid, event_tzid)] + ( |
paul@300 | 1620 | event_tzid != tzid and [(tzid, tzid)] or [] |
paul@300 | 1621 | )) |
paul@300 | 1622 | page.span.close() |
paul@286 | 1623 | |
paul@246 | 1624 | # Incoming HTTP request direction. |
paul@246 | 1625 | |
paul@69 | 1626 | def select_action(self): |
paul@69 | 1627 | |
paul@69 | 1628 | "Select the desired action and show the result." |
paul@69 | 1629 | |
paul@121 | 1630 | path_info = self.env.get_path_info().strip("/") |
paul@121 | 1631 | |
paul@69 | 1632 | if not path_info: |
paul@114 | 1633 | self.show_calendar() |
paul@121 | 1634 | elif self.show_object(path_info): |
paul@70 | 1635 | pass |
paul@70 | 1636 | else: |
paul@70 | 1637 | self.no_page() |
paul@69 | 1638 | |
paul@82 | 1639 | def __call__(self): |
paul@69 | 1640 | |
paul@69 | 1641 | "Interpret a request and show an appropriate response." |
paul@69 | 1642 | |
paul@69 | 1643 | if not self.user: |
paul@69 | 1644 | self.no_user() |
paul@69 | 1645 | else: |
paul@69 | 1646 | self.select_action() |
paul@69 | 1647 | |
paul@70 | 1648 | # Write the headers and actual content. |
paul@70 | 1649 | |
paul@69 | 1650 | print >>self.out, "Content-Type: text/html; charset=%s" % self.encoding |
paul@69 | 1651 | print >>self.out |
paul@69 | 1652 | self.out.write(unicode(self.page).encode(self.encoding)) |
paul@69 | 1653 | |
paul@69 | 1654 | if __name__ == "__main__": |
paul@128 | 1655 | Manager()() |
paul@69 | 1656 | |
paul@69 | 1657 | # vim: tabstop=4 expandtab shiftwidth=4 |