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@202 | 27 | from datetime import datetime |
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@153 | 33 | from imiptools.content import Handler, get_address, \ |
paul@210 | 34 | get_item, get_items, get_uri, get_utc_datetime, \ |
paul@210 | 35 | get_value, get_value_map, get_values, \ |
paul@210 | 36 | parse_object, to_part |
paul@153 | 37 | from imiptools.dates import format_datetime, get_datetime, get_start_of_day, \ |
paul@202 | 38 | get_end_of_day, ends_on_same_day, to_timezone |
paul@83 | 39 | from imiptools.mail import Messenger |
paul@162 | 40 | from imiptools.period import add_day_start_points, add_slots, convert_periods, \ |
paul@185 | 41 | get_freebusy_details, \ |
paul@162 | 42 | get_scale, have_conflict, get_slots, get_spans, \ |
paul@162 | 43 | partition_by_day |
paul@147 | 44 | from imiptools.profile import Preferences |
paul@77 | 45 | from vCalendar import to_node |
paul@69 | 46 | import markup |
paul@69 | 47 | import imip_store |
paul@69 | 48 | |
paul@69 | 49 | getenv = os.environ.get |
paul@69 | 50 | setenv = os.environ.__setitem__ |
paul@69 | 51 | |
paul@69 | 52 | class CGIEnvironment: |
paul@69 | 53 | |
paul@69 | 54 | "A CGI-compatible environment." |
paul@69 | 55 | |
paul@69 | 56 | def __init__(self): |
paul@69 | 57 | self.args = None |
paul@69 | 58 | self.method = None |
paul@69 | 59 | self.path = None |
paul@69 | 60 | self.path_info = None |
paul@69 | 61 | self.user = None |
paul@69 | 62 | |
paul@69 | 63 | def get_args(self): |
paul@69 | 64 | if self.args is None: |
paul@69 | 65 | if self.get_method() != "POST": |
paul@69 | 66 | setenv("QUERY_STRING", "") |
paul@69 | 67 | self.args = cgi.parse(keep_blank_values=True) |
paul@69 | 68 | return self.args |
paul@69 | 69 | |
paul@69 | 70 | def get_method(self): |
paul@69 | 71 | if self.method is None: |
paul@69 | 72 | self.method = getenv("REQUEST_METHOD") or "GET" |
paul@69 | 73 | return self.method |
paul@69 | 74 | |
paul@69 | 75 | def get_path(self): |
paul@69 | 76 | if self.path is None: |
paul@69 | 77 | self.path = getenv("SCRIPT_NAME") or "" |
paul@69 | 78 | return self.path |
paul@69 | 79 | |
paul@69 | 80 | def get_path_info(self): |
paul@69 | 81 | if self.path_info is None: |
paul@69 | 82 | self.path_info = getenv("PATH_INFO") or "" |
paul@69 | 83 | return self.path_info |
paul@69 | 84 | |
paul@69 | 85 | def get_user(self): |
paul@69 | 86 | if self.user is None: |
paul@69 | 87 | self.user = getenv("REMOTE_USER") or "" |
paul@69 | 88 | return self.user |
paul@69 | 89 | |
paul@69 | 90 | def get_output(self): |
paul@69 | 91 | return sys.stdout |
paul@69 | 92 | |
paul@69 | 93 | def get_url(self): |
paul@69 | 94 | path = self.get_path() |
paul@69 | 95 | path_info = self.get_path_info() |
paul@69 | 96 | return "%s%s" % (path.rstrip("/"), path_info) |
paul@69 | 97 | |
paul@154 | 98 | def new_url(self, path_info): |
paul@154 | 99 | path = self.get_path() |
paul@154 | 100 | return "%s/%s" % (path.rstrip("/"), path_info.lstrip("/")) |
paul@154 | 101 | |
paul@79 | 102 | class ManagerHandler(Handler): |
paul@79 | 103 | |
paul@121 | 104 | """ |
paul@121 | 105 | A content handler for use by the manager, as opposed to operating within the |
paul@121 | 106 | mail processing pipeline. |
paul@121 | 107 | """ |
paul@79 | 108 | |
paul@121 | 109 | def __init__(self, obj, user, messenger): |
paul@121 | 110 | details, details_attr = obj.values()[0] |
paul@79 | 111 | Handler.__init__(self, details) |
paul@121 | 112 | self.obj = obj |
paul@79 | 113 | self.user = user |
paul@82 | 114 | self.messenger = messenger |
paul@82 | 115 | |
paul@207 | 116 | self.organiser = self.get_value("ORGANIZER") |
paul@207 | 117 | self.attendees = self.get_values("ATTENDEE") |
paul@79 | 118 | |
paul@79 | 119 | # Communication methods. |
paul@79 | 120 | |
paul@207 | 121 | def send_message(self, method, sender): |
paul@79 | 122 | |
paul@79 | 123 | """ |
paul@207 | 124 | Create a full calendar object employing the given 'method', and send it |
paul@207 | 125 | to the appropriate recipients, also sending a copy to the 'sender'. |
paul@79 | 126 | """ |
paul@79 | 127 | |
paul@121 | 128 | node = to_node(self.obj) |
paul@207 | 129 | part = to_part(method, [node]) |
paul@207 | 130 | |
paul@207 | 131 | if self.user == self.organiser: |
paul@207 | 132 | recipients = map(get_address, self.attendees) |
paul@207 | 133 | else: |
paul@207 | 134 | recipients = [get_address(self.organiser)] |
paul@207 | 135 | |
paul@207 | 136 | message = self.messenger.make_outgoing_message([part], recipients, outgoing_bcc=sender) |
paul@207 | 137 | self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) |
paul@79 | 138 | |
paul@79 | 139 | # Action methods. |
paul@79 | 140 | |
paul@207 | 141 | def process_received_request(self, accept, update=False): |
paul@79 | 142 | |
paul@79 | 143 | """ |
paul@79 | 144 | Process the current request for the given 'user', accepting any request |
paul@79 | 145 | when 'accept' is true, declining requests otherwise. Return whether any |
paul@79 | 146 | action was taken. |
paul@155 | 147 | |
paul@155 | 148 | If 'update' is given, the sequence number will be incremented in order |
paul@155 | 149 | to override any previous response. |
paul@79 | 150 | """ |
paul@79 | 151 | |
paul@79 | 152 | # When accepting or declining, do so only on behalf of this user, |
paul@79 | 153 | # preserving any other attributes set as an attendee. |
paul@79 | 154 | |
paul@79 | 155 | for attendee, attendee_attr in self.get_items("ATTENDEE"): |
paul@79 | 156 | |
paul@79 | 157 | if attendee == self.user: |
paul@79 | 158 | attendee_attr["PARTSTAT"] = accept and "ACCEPTED" or "DECLINED" |
paul@128 | 159 | if self.messenger and self.messenger.sender != get_address(attendee): |
paul@128 | 160 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@79 | 161 | self.details["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@155 | 162 | if update: |
paul@155 | 163 | sequence = self.get_value("SEQUENCE") or "0" |
paul@155 | 164 | self.details["SEQUENCE"] = [(str(int(sequence) + 1), {})] |
paul@158 | 165 | self.update_dtstamp() |
paul@155 | 166 | |
paul@207 | 167 | self.send_message("REPLY", get_address(attendee)) |
paul@79 | 168 | |
paul@79 | 169 | return True |
paul@79 | 170 | |
paul@79 | 171 | return False |
paul@79 | 172 | |
paul@207 | 173 | def process_created_request(self, update=False): |
paul@207 | 174 | |
paul@207 | 175 | """ |
paul@207 | 176 | Process the current request for the given 'user', sending a created |
paul@207 | 177 | request to attendees. Return whether any action was taken. |
paul@207 | 178 | |
paul@207 | 179 | If 'update' is given, the sequence number will be incremented in order |
paul@207 | 180 | to override any previous message. |
paul@207 | 181 | """ |
paul@207 | 182 | |
paul@207 | 183 | if update: |
paul@207 | 184 | sequence = self.get_value("SEQUENCE") or "0" |
paul@207 | 185 | self.details["SEQUENCE"] = [(str(int(sequence) + 1), {})] |
paul@207 | 186 | self.update_dtstamp() |
paul@207 | 187 | |
paul@207 | 188 | self.send_message("REQUEST", get_address(self.organiser)) |
paul@207 | 189 | |
paul@207 | 190 | return True |
paul@207 | 191 | |
paul@69 | 192 | class Manager: |
paul@69 | 193 | |
paul@69 | 194 | "A simple manager application." |
paul@69 | 195 | |
paul@82 | 196 | def __init__(self, messenger=None): |
paul@82 | 197 | self.messenger = messenger or Messenger() |
paul@82 | 198 | |
paul@69 | 199 | self.env = CGIEnvironment() |
paul@69 | 200 | user = self.env.get_user() |
paul@77 | 201 | self.user = user and get_uri(user) or None |
paul@147 | 202 | self.preferences = None |
paul@149 | 203 | self.locale = None |
paul@121 | 204 | self.requests = None |
paul@121 | 205 | |
paul@69 | 206 | self.out = self.env.get_output() |
paul@69 | 207 | self.page = markup.page() |
paul@69 | 208 | self.encoding = "utf-8" |
paul@69 | 209 | |
paul@77 | 210 | self.store = imip_store.FileStore() |
paul@162 | 211 | self.objects = {} |
paul@77 | 212 | |
paul@77 | 213 | try: |
paul@77 | 214 | self.publisher = imip_store.FilePublisher() |
paul@77 | 215 | except OSError: |
paul@77 | 216 | self.publisher = None |
paul@77 | 217 | |
paul@121 | 218 | def _get_uid(self, path_info): |
paul@121 | 219 | return path_info.lstrip("/").split("/", 1)[0] |
paul@121 | 220 | |
paul@117 | 221 | def _get_object(self, uid): |
paul@162 | 222 | if self.objects.has_key(uid): |
paul@162 | 223 | return self.objects[uid] |
paul@162 | 224 | |
paul@117 | 225 | f = uid and self.store.get_event(self.user, uid) or None |
paul@117 | 226 | |
paul@117 | 227 | if not f: |
paul@117 | 228 | return None |
paul@117 | 229 | |
paul@162 | 230 | self.objects[uid] = obj = parse_object(f, "utf-8") |
paul@117 | 231 | |
paul@117 | 232 | if not obj: |
paul@117 | 233 | return None |
paul@117 | 234 | |
paul@121 | 235 | return obj |
paul@121 | 236 | |
paul@121 | 237 | def _get_details(self, obj): |
paul@121 | 238 | details, details_attr = obj.values()[0] |
paul@121 | 239 | return details |
paul@121 | 240 | |
paul@121 | 241 | def _get_requests(self): |
paul@121 | 242 | if self.requests is None: |
paul@121 | 243 | self.requests = self.store.get_requests(self.user) |
paul@121 | 244 | return self.requests |
paul@117 | 245 | |
paul@162 | 246 | def _get_request_summary(self): |
paul@162 | 247 | summary = [] |
paul@162 | 248 | for uid in self._get_requests(): |
paul@162 | 249 | obj = self._get_object(uid) |
paul@162 | 250 | if obj: |
paul@162 | 251 | details = self._get_details(obj) |
paul@162 | 252 | summary.append(( |
paul@162 | 253 | get_value(details, "DTSTART"), |
paul@162 | 254 | get_value(details, "DTEND"), |
paul@162 | 255 | uid |
paul@162 | 256 | )) |
paul@162 | 257 | return summary |
paul@162 | 258 | |
paul@147 | 259 | # Preference methods. |
paul@147 | 260 | |
paul@149 | 261 | def get_user_locale(self): |
paul@149 | 262 | if not self.locale: |
paul@149 | 263 | self.locale = self.get_preferences().get("LANG", "C") |
paul@149 | 264 | return self.locale |
paul@147 | 265 | |
paul@147 | 266 | def get_preferences(self): |
paul@147 | 267 | if not self.preferences: |
paul@147 | 268 | self.preferences = Preferences(self.user) |
paul@147 | 269 | return self.preferences |
paul@147 | 270 | |
paul@162 | 271 | # Prettyprinting of dates and times. |
paul@162 | 272 | |
paul@149 | 273 | def format_date(self, dt, format): |
paul@149 | 274 | return self._format_datetime(babel.dates.format_date, dt, format) |
paul@149 | 275 | |
paul@149 | 276 | def format_time(self, dt, format): |
paul@149 | 277 | return self._format_datetime(babel.dates.format_time, dt, format) |
paul@149 | 278 | |
paul@149 | 279 | def format_datetime(self, dt, format): |
paul@149 | 280 | return self._format_datetime(babel.dates.format_datetime, dt, format) |
paul@149 | 281 | |
paul@149 | 282 | def _format_datetime(self, fn, dt, format): |
paul@149 | 283 | return fn(dt, format=format, locale=self.get_user_locale()) |
paul@149 | 284 | |
paul@78 | 285 | # Data management methods. |
paul@78 | 286 | |
paul@78 | 287 | def remove_request(self, uid): |
paul@105 | 288 | return self.store.dequeue_request(self.user, uid) |
paul@78 | 289 | |
paul@78 | 290 | # Presentation methods. |
paul@78 | 291 | |
paul@69 | 292 | def new_page(self, title): |
paul@192 | 293 | self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) |
paul@69 | 294 | |
paul@69 | 295 | def status(self, code, message): |
paul@123 | 296 | self.header("Status", "%s %s" % (code, message)) |
paul@123 | 297 | |
paul@123 | 298 | def header(self, header, value): |
paul@123 | 299 | print >>self.out, "%s: %s" % (header, value) |
paul@69 | 300 | |
paul@69 | 301 | def no_user(self): |
paul@69 | 302 | self.status(403, "Forbidden") |
paul@69 | 303 | self.new_page(title="Forbidden") |
paul@69 | 304 | self.page.p("You are not logged in and thus cannot access scheduling requests.") |
paul@69 | 305 | |
paul@70 | 306 | def no_page(self): |
paul@70 | 307 | self.status(404, "Not Found") |
paul@70 | 308 | self.new_page(title="Not Found") |
paul@70 | 309 | self.page.p("No page is provided at the given address.") |
paul@70 | 310 | |
paul@123 | 311 | def redirect(self, url): |
paul@123 | 312 | self.status(302, "Redirect") |
paul@123 | 313 | self.header("Location", url) |
paul@123 | 314 | self.new_page(title="Redirect") |
paul@123 | 315 | self.page.p("Redirecting to: %s" % url) |
paul@123 | 316 | |
paul@121 | 317 | # Request logic and page fragment methods. |
paul@121 | 318 | |
paul@202 | 319 | def handle_newevent(self): |
paul@202 | 320 | |
paul@207 | 321 | """ |
paul@207 | 322 | Handle any new event operation, creating a new event and redirecting to |
paul@207 | 323 | the event page for further activity. |
paul@207 | 324 | """ |
paul@202 | 325 | |
paul@202 | 326 | # Handle a submitted form. |
paul@202 | 327 | |
paul@202 | 328 | args = self.env.get_args() |
paul@202 | 329 | |
paul@202 | 330 | if not args.has_key("newevent"): |
paul@202 | 331 | return |
paul@202 | 332 | |
paul@202 | 333 | # Create a new event using the available information. |
paul@202 | 334 | |
paul@202 | 335 | slot = args.get("slot", [None])[0] |
paul@202 | 336 | participants = args.get("participants", []) |
paul@202 | 337 | |
paul@202 | 338 | if not slot: |
paul@202 | 339 | return |
paul@202 | 340 | |
paul@202 | 341 | start, end = slot.split("-") |
paul@202 | 342 | |
paul@202 | 343 | # Obtain the user's timezone. |
paul@202 | 344 | |
paul@202 | 345 | prefs = self.get_preferences() |
paul@202 | 346 | tzid = prefs.get("TZID", "UTC") |
paul@202 | 347 | |
paul@202 | 348 | # Invent a unique identifier. |
paul@202 | 349 | |
paul@202 | 350 | utcnow = format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@202 | 351 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@202 | 352 | |
paul@202 | 353 | # Create a calendar object and store it as a request. |
paul@202 | 354 | |
paul@202 | 355 | record = [] |
paul@202 | 356 | rwrite = record.append |
paul@202 | 357 | |
paul@202 | 358 | rwrite(("UID", {}, uid)) |
paul@202 | 359 | rwrite(("SUMMARY", {}, "New event at %s" % utcnow)) |
paul@202 | 360 | rwrite(("DTSTAMP", {}, utcnow)) |
paul@207 | 361 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME", "TZID" : tzid}, start)) |
paul@207 | 362 | rwrite(("DTEND", {"VALUE" : "DATE-TIME", "TZID" : tzid}, end or |
paul@202 | 363 | format_datetime(get_end_of_day(get_datetime(start, {"TZID" : tzid}))) |
paul@202 | 364 | )) |
paul@202 | 365 | rwrite(("ORGANIZER", {}, self.user)) |
paul@202 | 366 | |
paul@202 | 367 | for participant in participants: |
paul@202 | 368 | if not participant: |
paul@202 | 369 | continue |
paul@202 | 370 | participant = get_uri(participant) |
paul@202 | 371 | if participant != self.user: |
paul@202 | 372 | rwrite(("ATTENDEE", {}, participant)) |
paul@202 | 373 | |
paul@202 | 374 | obj = ("VEVENT", {}, record) |
paul@202 | 375 | |
paul@202 | 376 | self.store.set_event(self.user, uid, obj) |
paul@202 | 377 | self.store.queue_request(self.user, uid) |
paul@202 | 378 | |
paul@202 | 379 | # Redirect to the object, where instead of attendee controls, |
paul@202 | 380 | # there will be organiser controls. |
paul@202 | 381 | |
paul@202 | 382 | self.redirect(self.env.new_url(uid)) |
paul@202 | 383 | |
paul@155 | 384 | def handle_request(self, uid, request, queued): |
paul@121 | 385 | |
paul@155 | 386 | """ |
paul@155 | 387 | Handle actions involving the given 'uid' and 'request' object, where |
paul@155 | 388 | 'queued' indicates that the object has not yet been handled. |
paul@155 | 389 | """ |
paul@121 | 390 | |
paul@121 | 391 | # Handle a submitted form. |
paul@121 | 392 | |
paul@121 | 393 | args = self.env.get_args() |
paul@123 | 394 | handled = True |
paul@121 | 395 | |
paul@121 | 396 | accept = args.has_key("accept") |
paul@121 | 397 | decline = args.has_key("decline") |
paul@207 | 398 | invite = args.has_key("invite") |
paul@155 | 399 | update = not queued and args.has_key("update") |
paul@121 | 400 | |
paul@207 | 401 | if accept or decline or invite: |
paul@121 | 402 | |
paul@121 | 403 | handler = ManagerHandler(request, self.user, self.messenger) |
paul@121 | 404 | |
paul@207 | 405 | # Process the request and remove it from the list. |
paul@121 | 406 | |
paul@207 | 407 | if (accept or decline) and handler.process_received_request(accept, update) or \ |
paul@207 | 408 | invite and handler.process_created_request(update): |
paul@121 | 409 | |
paul@121 | 410 | self.remove_request(uid) |
paul@121 | 411 | |
paul@207 | 412 | elif args.has_key("discard"): |
paul@121 | 413 | |
paul@121 | 414 | # Remove the request from the list. |
paul@121 | 415 | |
paul@121 | 416 | self.remove_request(uid) |
paul@121 | 417 | |
paul@121 | 418 | else: |
paul@123 | 419 | handled = False |
paul@121 | 420 | |
paul@123 | 421 | if handled: |
paul@123 | 422 | self.redirect(self.env.get_path()) |
paul@123 | 423 | |
paul@123 | 424 | return handled |
paul@121 | 425 | |
paul@155 | 426 | def show_request_form(self, obj, needs_action): |
paul@155 | 427 | |
paul@155 | 428 | """ |
paul@155 | 429 | Show a form for a request concerning 'obj', indicating whether action is |
paul@155 | 430 | needed if 'needs_action' is specified as a true value. |
paul@155 | 431 | """ |
paul@155 | 432 | |
paul@155 | 433 | details = self._get_details(obj) |
paul@155 | 434 | |
paul@207 | 435 | is_organiser = get_value(details, "ORGANIZER") == self.user |
paul@207 | 436 | |
paul@207 | 437 | if not is_organiser: |
paul@207 | 438 | attendees = get_value_map(details, "ATTENDEE") |
paul@207 | 439 | attendee_attr = attendees.get(self.user) |
paul@121 | 440 | |
paul@207 | 441 | if attendee_attr: |
paul@207 | 442 | partstat = attendee_attr.get("PARTSTAT") |
paul@207 | 443 | if partstat == "ACCEPTED": |
paul@207 | 444 | self.page.p("This request has been accepted.") |
paul@207 | 445 | elif partstat == "DECLINED": |
paul@207 | 446 | self.page.p("This request has been declined.") |
paul@207 | 447 | else: |
paul@207 | 448 | self.page.p("This request has not yet been dealt with.") |
paul@121 | 449 | |
paul@155 | 450 | if needs_action: |
paul@155 | 451 | self.page.p("An action is required for this request:") |
paul@155 | 452 | else: |
paul@155 | 453 | self.page.p("This request can be updated as follows:") |
paul@155 | 454 | |
paul@121 | 455 | self.page.form(method="POST") |
paul@121 | 456 | self.page.p() |
paul@207 | 457 | |
paul@207 | 458 | # Show appropriate options depending on the role of the user. |
paul@207 | 459 | |
paul@207 | 460 | if is_organiser: |
paul@207 | 461 | self.page.input(name="invite", type="submit", value="Invite") |
paul@207 | 462 | else: |
paul@207 | 463 | self.page.input(name="accept", type="submit", value="Accept") |
paul@207 | 464 | self.page.add(" ") |
paul@207 | 465 | self.page.input(name="decline", type="submit", value="Decline") |
paul@207 | 466 | |
paul@121 | 467 | self.page.add(" ") |
paul@207 | 468 | self.page.input(name="discard", type="submit", value="Discard") |
paul@207 | 469 | |
paul@207 | 470 | # Updated objects need to have details updated upon sending. |
paul@207 | 471 | |
paul@155 | 472 | if not needs_action: |
paul@155 | 473 | self.page.input(name="update", type="hidden", value="true") |
paul@207 | 474 | |
paul@121 | 475 | self.page.p.close() |
paul@121 | 476 | self.page.form.close() |
paul@121 | 477 | |
paul@210 | 478 | object_labels = { |
paul@210 | 479 | "SUMMARY" : "Summary", |
paul@210 | 480 | "DTSTART" : "Start", |
paul@210 | 481 | "DTEND" : "End", |
paul@210 | 482 | "ORGANIZER" : "Organiser", |
paul@210 | 483 | "ATTENDEE" : "Attendee", |
paul@210 | 484 | } |
paul@210 | 485 | |
paul@121 | 486 | def show_object_on_page(self, uid, obj): |
paul@121 | 487 | |
paul@121 | 488 | """ |
paul@121 | 489 | Show the calendar object with the given 'uid' and representation 'obj' |
paul@121 | 490 | on the current page. |
paul@121 | 491 | """ |
paul@121 | 492 | |
paul@210 | 493 | page = self.page |
paul@210 | 494 | |
paul@154 | 495 | # Obtain the user's timezone. |
paul@154 | 496 | |
paul@154 | 497 | prefs = self.get_preferences() |
paul@154 | 498 | tzid = prefs.get("TZID", "UTC") |
paul@121 | 499 | |
paul@121 | 500 | # Provide a summary of the object. |
paul@121 | 501 | |
paul@154 | 502 | details = self._get_details(obj) |
paul@154 | 503 | |
paul@210 | 504 | page.table(cellspacing=5, cellpadding=5) |
paul@121 | 505 | |
paul@121 | 506 | for name in ["SUMMARY", "DTSTART", "DTEND", "ORGANIZER", "ATTENDEE"]: |
paul@210 | 507 | page.tr() |
paul@210 | 508 | |
paul@210 | 509 | label = self.object_labels.get(name, name) |
paul@210 | 510 | |
paul@210 | 511 | # Handle datetimes specially. |
paul@210 | 512 | |
paul@147 | 513 | if name in ["DTSTART", "DTEND"]: |
paul@147 | 514 | value, attr = get_item(details, name) |
paul@154 | 515 | tzid = attr.get("TZID", tzid) |
paul@149 | 516 | value = self.format_datetime(to_timezone(get_datetime(value), tzid), "full") |
paul@210 | 517 | page.th(label, class_="objectheading") |
paul@210 | 518 | page.td(value) |
paul@210 | 519 | page.tr.close() |
paul@210 | 520 | |
paul@210 | 521 | # Handle potentially many values. |
paul@210 | 522 | |
paul@147 | 523 | else: |
paul@210 | 524 | items = get_items(details, name) |
paul@210 | 525 | page.th(label, class_="objectheading", rowspan=len(items)) |
paul@210 | 526 | |
paul@210 | 527 | first = True |
paul@210 | 528 | |
paul@210 | 529 | for value, attr in items: |
paul@210 | 530 | if not first: |
paul@210 | 531 | page.tr() |
paul@210 | 532 | else: |
paul@210 | 533 | first = False |
paul@121 | 534 | |
paul@210 | 535 | page.td() |
paul@210 | 536 | page.add(value) |
paul@210 | 537 | |
paul@210 | 538 | if name == "ATTENDEE": |
paul@210 | 539 | partstat = attr.get("PARTSTAT") |
paul@210 | 540 | if partstat: |
paul@210 | 541 | page.add(" (%s)" % partstat) |
paul@210 | 542 | |
paul@210 | 543 | page.td.close() |
paul@210 | 544 | page.tr.close() |
paul@210 | 545 | |
paul@210 | 546 | page.table.close() |
paul@121 | 547 | |
paul@121 | 548 | dtstart = format_datetime(get_utc_datetime(details, "DTSTART")) |
paul@121 | 549 | dtend = format_datetime(get_utc_datetime(details, "DTEND")) |
paul@121 | 550 | |
paul@121 | 551 | # Indicate whether there are conflicting events. |
paul@121 | 552 | |
paul@121 | 553 | freebusy = self.store.get_freebusy(self.user) |
paul@121 | 554 | |
paul@121 | 555 | if freebusy: |
paul@121 | 556 | |
paul@121 | 557 | # Obtain any time zone details from the suggested event. |
paul@121 | 558 | |
paul@121 | 559 | _dtstart, attr = get_item(details, "DTSTART") |
paul@154 | 560 | tzid = attr.get("TZID", tzid) |
paul@121 | 561 | |
paul@121 | 562 | # Show any conflicts. |
paul@121 | 563 | |
paul@121 | 564 | for t in have_conflict(freebusy, [(dtstart, dtend)], True): |
paul@121 | 565 | start, end, found_uid = t[:3] |
paul@154 | 566 | |
paul@154 | 567 | # Provide details of any conflicting event. |
paul@154 | 568 | |
paul@121 | 569 | if uid != found_uid: |
paul@149 | 570 | start = self.format_datetime(to_timezone(get_datetime(start), tzid), "full") |
paul@149 | 571 | end = self.format_datetime(to_timezone(get_datetime(end), tzid), "full") |
paul@210 | 572 | page.p("Event conflicts with another from %s to %s: " % (start, end)) |
paul@154 | 573 | |
paul@154 | 574 | # Show the event summary for the conflicting event. |
paul@154 | 575 | |
paul@154 | 576 | found_obj = self._get_object(found_uid) |
paul@154 | 577 | if found_obj: |
paul@154 | 578 | found_details = self._get_details(found_obj) |
paul@210 | 579 | page.a(get_value(found_details, "SUMMARY"), href=self.env.new_url(found_uid)) |
paul@121 | 580 | |
paul@121 | 581 | def show_requests_on_page(self): |
paul@69 | 582 | |
paul@69 | 583 | "Show requests for the current user." |
paul@69 | 584 | |
paul@69 | 585 | # NOTE: This list could be more informative, but it is envisaged that |
paul@69 | 586 | # NOTE: the requests would be visited directly anyway. |
paul@69 | 587 | |
paul@121 | 588 | requests = self._get_requests() |
paul@70 | 589 | |
paul@185 | 590 | self.page.div(id="pending-requests") |
paul@185 | 591 | |
paul@80 | 592 | if requests: |
paul@114 | 593 | self.page.p("Pending requests:") |
paul@114 | 594 | |
paul@80 | 595 | self.page.ul() |
paul@69 | 596 | |
paul@80 | 597 | for request in requests: |
paul@165 | 598 | obj = self._get_object(request) |
paul@165 | 599 | if obj: |
paul@165 | 600 | details = self._get_details(obj) |
paul@165 | 601 | self.page.li() |
paul@171 | 602 | self.page.a(get_value(details, "SUMMARY"), href="#request-%s" % request) |
paul@165 | 603 | self.page.li.close() |
paul@80 | 604 | |
paul@80 | 605 | self.page.ul.close() |
paul@80 | 606 | |
paul@80 | 607 | else: |
paul@80 | 608 | self.page.p("There are no pending requests.") |
paul@69 | 609 | |
paul@185 | 610 | self.page.div.close() |
paul@185 | 611 | |
paul@185 | 612 | def show_participants_on_page(self): |
paul@185 | 613 | |
paul@185 | 614 | "Show participants for scheduling purposes." |
paul@185 | 615 | |
paul@185 | 616 | args = self.env.get_args() |
paul@185 | 617 | participants = args.get("participants", []) |
paul@185 | 618 | |
paul@185 | 619 | try: |
paul@185 | 620 | for name, value in args.items(): |
paul@185 | 621 | if name.startswith("remove-participant-"): |
paul@185 | 622 | i = int(name[len("remove-participant-"):]) |
paul@185 | 623 | del participants[i] |
paul@185 | 624 | break |
paul@185 | 625 | except ValueError: |
paul@185 | 626 | pass |
paul@185 | 627 | |
paul@185 | 628 | # Trim empty participants. |
paul@185 | 629 | |
paul@185 | 630 | while participants and not participants[-1].strip(): |
paul@185 | 631 | participants.pop() |
paul@185 | 632 | |
paul@185 | 633 | # Show any specified participants together with controls to remove and |
paul@185 | 634 | # add participants. |
paul@185 | 635 | |
paul@185 | 636 | self.page.div(id="participants") |
paul@185 | 637 | |
paul@185 | 638 | self.page.p("Participants for scheduling:") |
paul@185 | 639 | |
paul@185 | 640 | for i, participant in enumerate(participants): |
paul@185 | 641 | self.page.p() |
paul@185 | 642 | self.page.input(name="participants", type="text", value=participant) |
paul@185 | 643 | self.page.input(name="remove-participant-%d" % i, type="submit", value="Remove") |
paul@185 | 644 | self.page.p.close() |
paul@185 | 645 | |
paul@185 | 646 | self.page.p() |
paul@185 | 647 | self.page.input(name="participants", type="text") |
paul@185 | 648 | self.page.input(name="add-participant", type="submit", value="Add") |
paul@185 | 649 | self.page.p.close() |
paul@185 | 650 | |
paul@185 | 651 | self.page.div.close() |
paul@185 | 652 | |
paul@185 | 653 | return participants |
paul@185 | 654 | |
paul@121 | 655 | # Full page output methods. |
paul@70 | 656 | |
paul@121 | 657 | def show_object(self, path_info): |
paul@70 | 658 | |
paul@121 | 659 | "Show an object request using the given 'path_info' for the current user." |
paul@70 | 660 | |
paul@121 | 661 | uid = self._get_uid(path_info) |
paul@121 | 662 | obj = self._get_object(uid) |
paul@121 | 663 | |
paul@121 | 664 | if not obj: |
paul@70 | 665 | return False |
paul@70 | 666 | |
paul@123 | 667 | is_request = uid in self._get_requests() |
paul@155 | 668 | handled = self.handle_request(uid, obj, is_request) |
paul@77 | 669 | |
paul@123 | 670 | if handled: |
paul@123 | 671 | return True |
paul@73 | 672 | |
paul@123 | 673 | self.new_page(title="Event") |
paul@79 | 674 | |
paul@121 | 675 | self.show_object_on_page(uid, obj) |
paul@73 | 676 | |
paul@155 | 677 | self.show_request_form(obj, is_request and not handled) |
paul@73 | 678 | |
paul@70 | 679 | return True |
paul@70 | 680 | |
paul@114 | 681 | def show_calendar(self): |
paul@114 | 682 | |
paul@114 | 683 | "Show the calendar for the current user." |
paul@114 | 684 | |
paul@202 | 685 | handled = self.handle_newevent() |
paul@202 | 686 | |
paul@114 | 687 | self.new_page(title="Calendar") |
paul@162 | 688 | page = self.page |
paul@162 | 689 | |
paul@196 | 690 | # Form controls are used in various places on the calendar page. |
paul@196 | 691 | |
paul@196 | 692 | page.form(method="POST") |
paul@196 | 693 | |
paul@121 | 694 | self.show_requests_on_page() |
paul@185 | 695 | participants = self.show_participants_on_page() |
paul@114 | 696 | |
paul@196 | 697 | # Show a button for scheduling a new event. |
paul@196 | 698 | |
paul@196 | 699 | page.p() |
paul@196 | 700 | page.input(name="newevent", type="submit", value="New event", id="newevent") |
paul@196 | 701 | page.p.close() |
paul@196 | 702 | |
paul@203 | 703 | # Show a control for hiding empty slots. |
paul@203 | 704 | # The positioning of the control, paragraph and table are important here. |
paul@203 | 705 | |
paul@203 | 706 | page.input(name="hideslots", type="checkbox", value="hide", id="hideslots") |
paul@203 | 707 | |
paul@203 | 708 | page.p() |
paul@203 | 709 | page.label("Hide unused time periods", for_="hideslots", class_="enable") |
paul@203 | 710 | page.label("Show unused time periods", for_="hideslots", class_="disable") |
paul@203 | 711 | page.p.close() |
paul@203 | 712 | |
paul@114 | 713 | freebusy = self.store.get_freebusy(self.user) |
paul@114 | 714 | |
paul@114 | 715 | if not freebusy: |
paul@114 | 716 | page.p("No events scheduled.") |
paul@114 | 717 | return |
paul@114 | 718 | |
paul@154 | 719 | # Obtain the user's timezone. |
paul@147 | 720 | |
paul@147 | 721 | prefs = self.get_preferences() |
paul@153 | 722 | tzid = prefs.get("TZID", "UTC") |
paul@147 | 723 | |
paul@114 | 724 | # Day view: start at the earliest known day and produce days until the |
paul@114 | 725 | # latest known day, perhaps with expandable sections of empty days. |
paul@114 | 726 | |
paul@114 | 727 | # Month view: start at the earliest known month and produce months until |
paul@114 | 728 | # the latest known month, perhaps with expandable sections of empty |
paul@114 | 729 | # months. |
paul@114 | 730 | |
paul@114 | 731 | # Details of users to invite to new events could be superimposed on the |
paul@114 | 732 | # calendar. |
paul@114 | 733 | |
paul@185 | 734 | # Requests are listed and linked to their tentative positions in the |
paul@185 | 735 | # calendar. Other participants are also shown. |
paul@185 | 736 | |
paul@185 | 737 | request_summary = self._get_request_summary() |
paul@185 | 738 | |
paul@185 | 739 | period_groups = [request_summary, freebusy] |
paul@185 | 740 | period_group_types = ["request", "freebusy"] |
paul@185 | 741 | period_group_sources = ["Pending requests", "Your schedule"] |
paul@185 | 742 | |
paul@187 | 743 | for i, participant in enumerate(participants): |
paul@185 | 744 | period_groups.append(self.store.get_freebusy_for_other(self.user, get_uri(participant))) |
paul@187 | 745 | period_group_types.append("freebusy-part%d" % i) |
paul@185 | 746 | period_group_sources.append(participant) |
paul@114 | 747 | |
paul@162 | 748 | groups = [] |
paul@162 | 749 | group_columns = [] |
paul@185 | 750 | group_types = period_group_types |
paul@185 | 751 | group_sources = period_group_sources |
paul@162 | 752 | all_points = set() |
paul@162 | 753 | |
paul@162 | 754 | # Obtain time point information for each group of periods. |
paul@162 | 755 | |
paul@185 | 756 | for periods in period_groups: |
paul@162 | 757 | periods = convert_periods(periods, tzid) |
paul@162 | 758 | |
paul@162 | 759 | # Get the time scale with start and end points. |
paul@162 | 760 | |
paul@162 | 761 | scale = get_scale(periods) |
paul@162 | 762 | |
paul@162 | 763 | # Get the time slots for the periods. |
paul@162 | 764 | |
paul@162 | 765 | slots = get_slots(scale) |
paul@162 | 766 | |
paul@162 | 767 | # Add start of day time points for multi-day periods. |
paul@162 | 768 | |
paul@162 | 769 | add_day_start_points(slots) |
paul@162 | 770 | |
paul@162 | 771 | # Record the slots and all time points employed. |
paul@162 | 772 | |
paul@162 | 773 | groups.append(slots) |
paul@201 | 774 | all_points.update([point for point, active in slots]) |
paul@162 | 775 | |
paul@162 | 776 | # Partition the groups into days. |
paul@162 | 777 | |
paul@162 | 778 | days = {} |
paul@162 | 779 | partitioned_groups = [] |
paul@171 | 780 | partitioned_group_types = [] |
paul@185 | 781 | partitioned_group_sources = [] |
paul@162 | 782 | |
paul@185 | 783 | for slots, group_type, group_source in zip(groups, group_types, group_sources): |
paul@162 | 784 | |
paul@162 | 785 | # Propagate time points to all groups of time slots. |
paul@162 | 786 | |
paul@162 | 787 | add_slots(slots, all_points) |
paul@162 | 788 | |
paul@162 | 789 | # Count the number of columns employed by the group. |
paul@162 | 790 | |
paul@162 | 791 | columns = 0 |
paul@162 | 792 | |
paul@162 | 793 | # Partition the time slots by day. |
paul@162 | 794 | |
paul@162 | 795 | partitioned = {} |
paul@162 | 796 | |
paul@162 | 797 | for day, day_slots in partition_by_day(slots).items(): |
paul@201 | 798 | intervals = [] |
paul@201 | 799 | last = None |
paul@201 | 800 | |
paul@201 | 801 | for point, active in day_slots: |
paul@201 | 802 | columns = max(columns, len(active)) |
paul@201 | 803 | if last: |
paul@201 | 804 | intervals.append((last, point)) |
paul@201 | 805 | last = point |
paul@201 | 806 | |
paul@201 | 807 | if last: |
paul@201 | 808 | intervals.append((last, None)) |
paul@162 | 809 | |
paul@162 | 810 | if not days.has_key(day): |
paul@162 | 811 | days[day] = set() |
paul@162 | 812 | |
paul@162 | 813 | # Convert each partition to a mapping from points to active |
paul@162 | 814 | # periods. |
paul@162 | 815 | |
paul@201 | 816 | partitioned[day] = dict(day_slots) |
paul@201 | 817 | |
paul@201 | 818 | # Record the divisions or intervals within each day. |
paul@201 | 819 | |
paul@201 | 820 | days[day].update(intervals) |
paul@162 | 821 | |
paul@194 | 822 | if group_type != "request" or columns: |
paul@194 | 823 | group_columns.append(columns) |
paul@194 | 824 | partitioned_groups.append(partitioned) |
paul@194 | 825 | partitioned_group_types.append(group_type) |
paul@194 | 826 | partitioned_group_sources.append(group_source) |
paul@114 | 827 | |
paul@188 | 828 | page.table(cellspacing=5, cellpadding=5, id="calendar") |
paul@188 | 829 | self.show_calendar_participant_headings(partitioned_group_types, partitioned_group_sources, group_columns) |
paul@171 | 830 | self.show_calendar_days(days, partitioned_groups, partitioned_group_types, group_columns) |
paul@162 | 831 | page.table.close() |
paul@114 | 832 | |
paul@196 | 833 | # End the form region. |
paul@196 | 834 | |
paul@196 | 835 | page.form.close() |
paul@196 | 836 | |
paul@188 | 837 | def show_calendar_participant_headings(self, group_types, group_sources, group_columns): |
paul@186 | 838 | |
paul@186 | 839 | """ |
paul@186 | 840 | Show headings for the participants and other scheduling contributors, |
paul@188 | 841 | defined by 'group_types', 'group_sources' and 'group_columns'. |
paul@186 | 842 | """ |
paul@186 | 843 | |
paul@185 | 844 | page = self.page |
paul@185 | 845 | |
paul@188 | 846 | page.colgroup(span=1, id="columns-timeslot") |
paul@186 | 847 | |
paul@188 | 848 | for group_type, columns in zip(group_types, group_columns): |
paul@191 | 849 | page.colgroup(span=max(columns, 1), id="columns-%s" % group_type) |
paul@186 | 850 | |
paul@185 | 851 | page.thead() |
paul@185 | 852 | page.tr() |
paul@185 | 853 | page.th("", class_="emptyheading") |
paul@185 | 854 | |
paul@193 | 855 | for group_type, source, columns in zip(group_types, group_sources, group_columns): |
paul@193 | 856 | page.th(source, |
paul@193 | 857 | class_=(group_type == "request" and "requestheading" or "participantheading"), |
paul@193 | 858 | colspan=max(columns, 1)) |
paul@185 | 859 | |
paul@185 | 860 | page.tr.close() |
paul@185 | 861 | page.thead.close() |
paul@185 | 862 | |
paul@171 | 863 | def show_calendar_days(self, days, partitioned_groups, partitioned_group_types, group_columns): |
paul@186 | 864 | |
paul@186 | 865 | """ |
paul@186 | 866 | Show calendar days, defined by a collection of 'days', the contributing |
paul@186 | 867 | period information as 'partitioned_groups' (partitioned by day), the |
paul@186 | 868 | 'partitioned_group_types' indicating the kind of contribution involved, |
paul@186 | 869 | and the 'group_columns' defining the number of columns in each group. |
paul@186 | 870 | """ |
paul@186 | 871 | |
paul@162 | 872 | page = self.page |
paul@162 | 873 | |
paul@191 | 874 | # Determine the number of columns required. Where participants provide |
paul@191 | 875 | # no columns for events, one still needs to be provided for the |
paul@191 | 876 | # participant itself. |
paul@147 | 877 | |
paul@191 | 878 | all_columns = sum([max(columns, 1) for columns in group_columns]) |
paul@191 | 879 | |
paul@191 | 880 | # Determine the days providing time slots. |
paul@191 | 881 | |
paul@162 | 882 | all_days = days.items() |
paul@162 | 883 | all_days.sort() |
paul@162 | 884 | |
paul@162 | 885 | # Produce a heading and time points for each day. |
paul@162 | 886 | |
paul@201 | 887 | for day, intervals in all_days: |
paul@186 | 888 | page.thead() |
paul@114 | 889 | page.tr() |
paul@171 | 890 | page.th(class_="dayheading", colspan=all_columns+1) |
paul@153 | 891 | page.add(self.format_date(day, "full")) |
paul@114 | 892 | page.th.close() |
paul@153 | 893 | page.tr.close() |
paul@186 | 894 | page.thead.close() |
paul@114 | 895 | |
paul@162 | 896 | groups_for_day = [partitioned.get(day) for partitioned in partitioned_groups] |
paul@162 | 897 | |
paul@186 | 898 | page.tbody() |
paul@201 | 899 | self.show_calendar_points(intervals, groups_for_day, partitioned_group_types, group_columns) |
paul@186 | 900 | page.tbody.close() |
paul@185 | 901 | |
paul@201 | 902 | def show_calendar_points(self, intervals, groups, group_types, group_columns): |
paul@186 | 903 | |
paul@186 | 904 | """ |
paul@201 | 905 | Show the time 'intervals' along with period information from the given |
paul@186 | 906 | 'groups', having the indicated 'group_types', each with the number of |
paul@186 | 907 | columns given by 'group_columns'. |
paul@186 | 908 | """ |
paul@186 | 909 | |
paul@162 | 910 | page = self.page |
paul@162 | 911 | |
paul@203 | 912 | # Produce a row for each interval. |
paul@162 | 913 | |
paul@201 | 914 | intervals = list(intervals) |
paul@201 | 915 | intervals.sort() |
paul@162 | 916 | |
paul@201 | 917 | for point, endpoint in intervals: |
paul@162 | 918 | continuation = point == get_start_of_day(point) |
paul@153 | 919 | |
paul@203 | 920 | # Some rows contain no period details and are marked as such. |
paul@203 | 921 | |
paul@203 | 922 | have_active = reduce(lambda x, y: x or y, [slots.get(point) for slots in groups], None) |
paul@203 | 923 | |
paul@203 | 924 | css = " ".join( |
paul@203 | 925 | ["slot"] + |
paul@203 | 926 | (not have_active and ["empty"] or []) + |
paul@203 | 927 | (continuation and ["daystart"] or []) |
paul@203 | 928 | ) |
paul@203 | 929 | |
paul@203 | 930 | page.tr(class_=css) |
paul@162 | 931 | page.th(class_="timeslot") |
paul@201 | 932 | self._time_point(point, endpoint) |
paul@162 | 933 | page.th.close() |
paul@162 | 934 | |
paul@162 | 935 | # Obtain slots for the time point from each group. |
paul@162 | 936 | |
paul@171 | 937 | for columns, slots, group_type in zip(group_columns, groups, group_types): |
paul@162 | 938 | active = slots and slots.get(point) |
paul@162 | 939 | |
paul@191 | 940 | # Where no periods exist for the given time interval, generate |
paul@191 | 941 | # an empty cell. Where a participant provides no periods at all, |
paul@191 | 942 | # the colspan is adjusted to be 1, not 0. |
paul@191 | 943 | |
paul@162 | 944 | if not active: |
paul@196 | 945 | page.td(class_="empty container", colspan=max(columns, 1)) |
paul@201 | 946 | self._empty_slot(point, endpoint) |
paul@196 | 947 | page.td.close() |
paul@162 | 948 | continue |
paul@162 | 949 | |
paul@162 | 950 | slots = slots.items() |
paul@162 | 951 | slots.sort() |
paul@162 | 952 | spans = get_spans(slots) |
paul@162 | 953 | |
paul@162 | 954 | # Show a column for each active period. |
paul@117 | 955 | |
paul@153 | 956 | for t in active: |
paul@185 | 957 | if t and len(t) >= 2: |
paul@185 | 958 | start, end, uid, key = get_freebusy_details(t) |
paul@185 | 959 | span = spans[key] |
paul@171 | 960 | |
paul@171 | 961 | # Produce a table cell only at the start of the period |
paul@171 | 962 | # or when continued at the start of a day. |
paul@171 | 963 | |
paul@153 | 964 | if point == start or continuation: |
paul@153 | 965 | |
paul@195 | 966 | has_continued = continuation and point != start |
paul@195 | 967 | will_continue = not ends_on_same_day(point, end) |
paul@195 | 968 | css = " ".join( |
paul@195 | 969 | ["event"] + |
paul@195 | 970 | (has_continued and ["continued"] or []) + |
paul@195 | 971 | (will_continue and ["continues"] or []) |
paul@195 | 972 | ) |
paul@195 | 973 | |
paul@189 | 974 | # Only anchor the first cell of events. |
paul@189 | 975 | |
paul@189 | 976 | if point == start: |
paul@195 | 977 | page.td(class_=css, rowspan=span, id="%s-%s" % (group_type, uid)) |
paul@189 | 978 | else: |
paul@195 | 979 | page.td(class_=css, rowspan=span) |
paul@171 | 980 | |
paul@153 | 981 | obj = self._get_object(uid) |
paul@185 | 982 | |
paul@185 | 983 | if not obj: |
paul@185 | 984 | page.span("") |
paul@185 | 985 | else: |
paul@153 | 986 | details = self._get_details(obj) |
paul@164 | 987 | summary = get_value(details, "SUMMARY") |
paul@171 | 988 | |
paul@171 | 989 | # Only link to events if they are not being |
paul@171 | 990 | # updated by requests. |
paul@171 | 991 | |
paul@171 | 992 | if uid in self._get_requests() and group_type != "request": |
paul@189 | 993 | page.span(summary) |
paul@164 | 994 | else: |
paul@171 | 995 | href = "%s/%s" % (self.env.get_url().rstrip("/"), uid) |
paul@189 | 996 | page.a(summary, href=href) |
paul@171 | 997 | |
paul@153 | 998 | page.td.close() |
paul@153 | 999 | else: |
paul@196 | 1000 | page.td(class_="empty container") |
paul@201 | 1001 | self._empty_slot(point, endpoint) |
paul@196 | 1002 | page.td.close() |
paul@114 | 1003 | |
paul@166 | 1004 | # Pad with empty columns. |
paul@166 | 1005 | |
paul@166 | 1006 | i = columns - len(active) |
paul@166 | 1007 | while i > 0: |
paul@166 | 1008 | i -= 1 |
paul@196 | 1009 | page.td(class_="empty container") |
paul@201 | 1010 | self._empty_slot(point, endpoint) |
paul@196 | 1011 | page.td.close() |
paul@166 | 1012 | |
paul@162 | 1013 | page.tr.close() |
paul@114 | 1014 | |
paul@201 | 1015 | def _time_point(self, point, endpoint): |
paul@201 | 1016 | page = self.page |
paul@201 | 1017 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@202 | 1018 | slot = self.env.get_args().get("slot", [None])[0] |
paul@202 | 1019 | if slot == value: |
paul@202 | 1020 | page.input(name="slot", type="radio", value=value, id=identifier, class_="newevent", checked="checked") |
paul@202 | 1021 | else: |
paul@202 | 1022 | page.input(name="slot", type="radio", value=value, id=identifier, class_="newevent") |
paul@201 | 1023 | page.label(self.format_time(point, "long"), class_="timepoint", for_=identifier) |
paul@201 | 1024 | |
paul@201 | 1025 | def _empty_slot(self, point, endpoint): |
paul@197 | 1026 | page = self.page |
paul@201 | 1027 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@201 | 1028 | page.label("Make a new event in this period", class_="newevent popup", for_=identifier) |
paul@196 | 1029 | |
paul@201 | 1030 | def _slot_value_and_identifier(self, point, endpoint): |
paul@202 | 1031 | value = "%s-%s" % (format_datetime(point), endpoint and format_datetime(endpoint) or "") |
paul@201 | 1032 | identifier = "slot-%s" % value |
paul@201 | 1033 | return value, identifier |
paul@196 | 1034 | |
paul@69 | 1035 | def select_action(self): |
paul@69 | 1036 | |
paul@69 | 1037 | "Select the desired action and show the result." |
paul@69 | 1038 | |
paul@121 | 1039 | path_info = self.env.get_path_info().strip("/") |
paul@121 | 1040 | |
paul@69 | 1041 | if not path_info: |
paul@114 | 1042 | self.show_calendar() |
paul@121 | 1043 | elif self.show_object(path_info): |
paul@70 | 1044 | pass |
paul@70 | 1045 | else: |
paul@70 | 1046 | self.no_page() |
paul@69 | 1047 | |
paul@82 | 1048 | def __call__(self): |
paul@69 | 1049 | |
paul@69 | 1050 | "Interpret a request and show an appropriate response." |
paul@69 | 1051 | |
paul@69 | 1052 | if not self.user: |
paul@69 | 1053 | self.no_user() |
paul@69 | 1054 | else: |
paul@69 | 1055 | self.select_action() |
paul@69 | 1056 | |
paul@70 | 1057 | # Write the headers and actual content. |
paul@70 | 1058 | |
paul@69 | 1059 | print >>self.out, "Content-Type: text/html; charset=%s" % self.encoding |
paul@69 | 1060 | print >>self.out |
paul@69 | 1061 | self.out.write(unicode(self.page).encode(self.encoding)) |
paul@69 | 1062 | |
paul@69 | 1063 | if __name__ == "__main__": |
paul@128 | 1064 | Manager()() |
paul@69 | 1065 | |
paul@69 | 1066 | # vim: tabstop=4 expandtab shiftwidth=4 |