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