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