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