paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | A Web interface to a calendar event. |
paul@446 | 5 | |
paul@446 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@446 | 7 | |
paul@446 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@446 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@446 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@446 | 11 | version. |
paul@446 | 12 | |
paul@446 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@446 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@446 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@446 | 16 | details. |
paul@446 | 17 | |
paul@446 | 18 | You should have received a copy of the GNU General Public License along with |
paul@446 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@446 | 20 | """ |
paul@446 | 21 | |
paul@446 | 22 | from datetime import datetime, timedelta |
paul@473 | 23 | from imiptools.client import update_attendees, update_participation |
paul@446 | 24 | from imiptools.data import get_uri, uri_dict, uri_values |
paul@446 | 25 | from imiptools.dates import format_datetime, to_date, get_datetime, \ |
paul@446 | 26 | get_datetime_item, get_period_item, \ |
paul@446 | 27 | get_start_of_day, to_timezone |
paul@446 | 28 | from imiptools.mail import Messenger |
paul@446 | 29 | from imiptools.period import have_conflict |
paul@446 | 30 | from imipweb.handler import ManagerHandler |
paul@446 | 31 | from imipweb.resource import Resource |
paul@446 | 32 | import pytz |
paul@446 | 33 | |
paul@446 | 34 | class EventPage(Resource): |
paul@446 | 35 | |
paul@446 | 36 | "A request handler for the event page." |
paul@446 | 37 | |
paul@446 | 38 | def __init__(self, resource=None, messenger=None): |
paul@446 | 39 | Resource.__init__(self, resource) |
paul@446 | 40 | self.messenger = messenger or Messenger() |
paul@446 | 41 | |
paul@474 | 42 | # Various property values and labels. |
paul@474 | 43 | |
paul@474 | 44 | property_items = [ |
paul@474 | 45 | ("SUMMARY", "Summary"), |
paul@474 | 46 | ("DTSTART", "Start"), |
paul@474 | 47 | ("DTEND", "End"), |
paul@474 | 48 | ("ORGANIZER", "Organiser"), |
paul@474 | 49 | ("ATTENDEE", "Attendee"), |
paul@474 | 50 | ] |
paul@474 | 51 | |
paul@474 | 52 | partstat_items = [ |
paul@474 | 53 | ("NEEDS-ACTION", "Not confirmed"), |
paul@474 | 54 | ("ACCEPTED", "Attending"), |
paul@474 | 55 | ("TENTATIVE", "Tentatively attending"), |
paul@474 | 56 | ("DECLINED", "Not attending"), |
paul@474 | 57 | ("DELEGATED", "Delegated"), |
paul@474 | 58 | (None, "Not indicated"), |
paul@474 | 59 | ] |
paul@474 | 60 | |
paul@446 | 61 | # Request logic methods. |
paul@446 | 62 | |
paul@446 | 63 | def handle_request(self, uid, recurrenceid, obj): |
paul@446 | 64 | |
paul@446 | 65 | """ |
paul@446 | 66 | Handle actions involving the given 'uid', 'recurrenceid', and 'obj' as |
paul@446 | 67 | the object's representation, returning an error if one occurred, or None |
paul@446 | 68 | if the request was successfully handled. |
paul@446 | 69 | """ |
paul@446 | 70 | |
paul@446 | 71 | # Handle a submitted form. |
paul@446 | 72 | |
paul@446 | 73 | args = self.env.get_args() |
paul@446 | 74 | |
paul@446 | 75 | # Get the possible actions. |
paul@446 | 76 | |
paul@446 | 77 | reply = args.has_key("reply") |
paul@446 | 78 | discard = args.has_key("discard") |
paul@446 | 79 | invite = args.has_key("invite") |
paul@446 | 80 | cancel = args.has_key("cancel") |
paul@446 | 81 | save = args.has_key("save") |
paul@446 | 82 | ignore = args.has_key("ignore") |
paul@446 | 83 | |
paul@446 | 84 | have_action = reply or discard or invite or cancel or save or ignore |
paul@446 | 85 | |
paul@446 | 86 | if not have_action: |
paul@446 | 87 | return ["action"] |
paul@446 | 88 | |
paul@446 | 89 | # If ignoring the object, return to the calendar. |
paul@446 | 90 | |
paul@446 | 91 | if ignore: |
paul@446 | 92 | self.redirect(self.env.get_path()) |
paul@446 | 93 | return None |
paul@446 | 94 | |
paul@446 | 95 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@446 | 96 | |
paul@446 | 97 | # Obtain the user's timezone and process datetime values. |
paul@446 | 98 | |
paul@446 | 99 | update = False |
paul@472 | 100 | periods = None |
paul@446 | 101 | |
paul@446 | 102 | if is_organiser: |
paul@446 | 103 | periods, errors = self.handle_all_period_controls() |
paul@446 | 104 | if errors: |
paul@446 | 105 | return errors |
paul@472 | 106 | |
paul@472 | 107 | # Update the object. |
paul@472 | 108 | |
paul@472 | 109 | if reply or invite or cancel or save: |
paul@472 | 110 | |
paul@473 | 111 | # Update principal event details if organiser. |
paul@473 | 112 | |
paul@473 | 113 | if is_organiser: |
paul@473 | 114 | |
paul@473 | 115 | # Update time periods (main and recurring). |
paul@473 | 116 | |
paul@473 | 117 | if periods: |
paul@473 | 118 | self.set_period_in_object(obj, periods[0]) |
paul@473 | 119 | self.set_periods_in_object(obj, periods[1:]) |
paul@472 | 120 | |
paul@473 | 121 | # Update summary. |
paul@473 | 122 | |
paul@473 | 123 | if args.has_key("summary"): |
paul@473 | 124 | obj["SUMMARY"] = [(args["summary"][0], {})] |
paul@446 | 125 | |
paul@473 | 126 | # Obtain any participants and those to be removed. |
paul@472 | 127 | |
paul@478 | 128 | attendees = self.get_attendees() |
paul@473 | 129 | removed = args.get("remove") |
paul@473 | 130 | to_cancel = update_attendees(obj, attendees, removed) |
paul@472 | 131 | |
paul@472 | 132 | # Update attendee participation. |
paul@472 | 133 | |
paul@472 | 134 | if args.has_key("partstat"): |
paul@473 | 135 | update_participation(obj, self.user, args["partstat"][0]) |
paul@446 | 136 | |
paul@446 | 137 | # Process any action. |
paul@446 | 138 | |
paul@446 | 139 | handled = True |
paul@446 | 140 | |
paul@446 | 141 | if reply or invite or cancel: |
paul@446 | 142 | |
paul@446 | 143 | handler = ManagerHandler(obj, self.user, self.messenger) |
paul@446 | 144 | |
paul@446 | 145 | # Process the object and remove it from the list of requests. |
paul@446 | 146 | |
paul@473 | 147 | if reply and handler.process_received_request(update): |
paul@473 | 148 | self.remove_request(uid, recurrenceid) |
paul@473 | 149 | |
paul@473 | 150 | elif is_organiser and (invite or cancel): |
paul@446 | 151 | |
paul@473 | 152 | if handler.process_created_request( |
paul@473 | 153 | invite and "REQUEST" or "CANCEL", update, to_cancel): |
paul@473 | 154 | |
paul@473 | 155 | self.remove_request(uid, recurrenceid) |
paul@446 | 156 | |
paul@446 | 157 | # Save single user events. |
paul@446 | 158 | |
paul@446 | 159 | elif save: |
paul@446 | 160 | self.store.set_event(self.user, uid, recurrenceid, node=obj.to_node()) |
paul@446 | 161 | self.update_freebusy(uid, recurrenceid, obj) |
paul@446 | 162 | self.remove_request(uid, recurrenceid) |
paul@446 | 163 | |
paul@446 | 164 | # Remove the request and the object. |
paul@446 | 165 | |
paul@446 | 166 | elif discard: |
paul@446 | 167 | self.remove_from_freebusy(uid, recurrenceid) |
paul@446 | 168 | self.remove_event(uid, recurrenceid) |
paul@446 | 169 | self.remove_request(uid, recurrenceid) |
paul@446 | 170 | |
paul@446 | 171 | else: |
paul@446 | 172 | handled = False |
paul@446 | 173 | |
paul@446 | 174 | # Upon handling an action, redirect to the main page. |
paul@446 | 175 | |
paul@446 | 176 | if handled: |
paul@446 | 177 | self.redirect(self.env.get_path()) |
paul@446 | 178 | |
paul@446 | 179 | return None |
paul@446 | 180 | |
paul@446 | 181 | def handle_all_period_controls(self): |
paul@446 | 182 | |
paul@446 | 183 | """ |
paul@446 | 184 | Handle datetime controls for a particular period, where 'index' may be |
paul@446 | 185 | used to indicate a recurring period, or the main start and end datetimes |
paul@446 | 186 | are handled. |
paul@446 | 187 | """ |
paul@446 | 188 | |
paul@446 | 189 | args = self.env.get_args() |
paul@446 | 190 | |
paul@446 | 191 | periods = [] |
paul@446 | 192 | |
paul@446 | 193 | # Get the main period details. |
paul@446 | 194 | |
paul@446 | 195 | dtend_enabled = args.get("dtend-control", [None])[0] |
paul@446 | 196 | dttimes_enabled = args.get("dttimes-control", [None])[0] |
paul@446 | 197 | start_values = self.get_date_control_values("dtstart") |
paul@446 | 198 | end_values = self.get_date_control_values("dtend") |
paul@446 | 199 | |
paul@446 | 200 | period, errors = self.handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled) |
paul@446 | 201 | |
paul@446 | 202 | if errors: |
paul@446 | 203 | return None, errors |
paul@446 | 204 | |
paul@446 | 205 | periods.append(period) |
paul@446 | 206 | |
paul@446 | 207 | # Get the recurring period details. |
paul@446 | 208 | |
paul@446 | 209 | all_dtend_enabled = args.get("dtend-control-recur", []) |
paul@446 | 210 | all_dttimes_enabled = args.get("dttimes-control-recur", []) |
paul@446 | 211 | all_start_values = self.get_date_control_values("dtstart-recur", multiple=True) |
paul@461 | 212 | all_end_values = self.get_date_control_values("dtend-recur", multiple=True, tzid_name="dtstart-recur") |
paul@446 | 213 | |
paul@446 | 214 | for index, (start_values, end_values, dtend_enabled, dttimes_enabled) in \ |
paul@446 | 215 | enumerate(map(None, all_start_values, all_end_values, all_dtend_enabled, all_dttimes_enabled)): |
paul@446 | 216 | |
paul@446 | 217 | dtend_enabled = str(index) in all_dtend_enabled |
paul@446 | 218 | dttimes_enabled = str(index) in all_dttimes_enabled |
paul@446 | 219 | period, errors = self.handle_period_controls(start_values, end_values, dtend_enabled, dttimes_enabled) |
paul@446 | 220 | |
paul@446 | 221 | if errors: |
paul@446 | 222 | return None, errors |
paul@446 | 223 | |
paul@446 | 224 | periods.append(period) |
paul@446 | 225 | |
paul@446 | 226 | return periods, None |
paul@446 | 227 | |
paul@446 | 228 | def handle_period_controls(self, start_values, end_values, dtend_enabled, dttimes_enabled): |
paul@446 | 229 | |
paul@446 | 230 | """ |
paul@446 | 231 | Handle datetime controls for a particular period, described by the given |
paul@446 | 232 | 'start_values' and 'end_values', with 'dtend_enabled' and |
paul@446 | 233 | 'dttimes_enabled' affecting the usage of the provided values. |
paul@446 | 234 | """ |
paul@446 | 235 | |
paul@446 | 236 | t = self.handle_date_control_values(start_values, dttimes_enabled) |
paul@446 | 237 | if t: |
paul@446 | 238 | dtstart, dtstart_attr = t |
paul@446 | 239 | else: |
paul@446 | 240 | return None, ["dtstart"] |
paul@446 | 241 | |
paul@446 | 242 | # Handle specified end datetimes. |
paul@446 | 243 | |
paul@446 | 244 | if dtend_enabled: |
paul@446 | 245 | t = self.handle_date_control_values(end_values, dttimes_enabled) |
paul@446 | 246 | if t: |
paul@446 | 247 | dtend, dtend_attr = t |
paul@446 | 248 | |
paul@446 | 249 | # Convert end dates to iCalendar "next day" dates. |
paul@446 | 250 | |
paul@446 | 251 | if not isinstance(dtend, datetime): |
paul@446 | 252 | dtend += timedelta(1) |
paul@446 | 253 | else: |
paul@446 | 254 | return None, ["dtend"] |
paul@446 | 255 | |
paul@446 | 256 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@446 | 257 | # handled by making the event occupy the rest of the day. |
paul@446 | 258 | |
paul@446 | 259 | else: |
paul@446 | 260 | dtend = dtstart + timedelta(1) |
paul@446 | 261 | dtend_attr = dtstart_attr |
paul@446 | 262 | |
paul@446 | 263 | if isinstance(dtstart, datetime): |
paul@446 | 264 | dtend = get_start_of_day(dtend, attr["TZID"]) |
paul@446 | 265 | |
paul@454 | 266 | if dtstart > dtend: |
paul@446 | 267 | return None, ["dtstart", "dtend"] |
paul@446 | 268 | |
paul@446 | 269 | return ((dtstart, dtstart_attr), (dtend, dtend_attr)), None |
paul@446 | 270 | |
paul@446 | 271 | def handle_date_control_values(self, values, with_time=True): |
paul@446 | 272 | |
paul@446 | 273 | """ |
paul@446 | 274 | Handle date control information for the given 'values', returning a |
paul@446 | 275 | (datetime, attr) tuple, or None if the fields cannot be used to |
paul@446 | 276 | construct a datetime object. |
paul@446 | 277 | """ |
paul@446 | 278 | |
paul@446 | 279 | if not values or not values["date"]: |
paul@446 | 280 | return None |
paul@446 | 281 | elif with_time: |
paul@446 | 282 | value = "%s%s" % (values["date"], values["time"]) |
paul@446 | 283 | attr = {"TZID" : values["tzid"], "VALUE" : "DATE-TIME"} |
paul@446 | 284 | dt = get_datetime(value, attr) |
paul@446 | 285 | else: |
paul@446 | 286 | attr = {"VALUE" : "DATE"} |
paul@446 | 287 | dt = get_datetime(values["date"]) |
paul@446 | 288 | |
paul@446 | 289 | if dt: |
paul@446 | 290 | return dt, attr |
paul@446 | 291 | |
paul@446 | 292 | return None |
paul@446 | 293 | |
paul@461 | 294 | def get_date_control_values(self, name, multiple=False, tzid_name=None): |
paul@446 | 295 | |
paul@446 | 296 | """ |
paul@446 | 297 | Return a dictionary containing date, time and tzid entries for fields |
paul@461 | 298 | starting with 'name'. If 'multiple' is set to a true value, many |
paul@461 | 299 | dictionaries will be returned corresponding to a collection of |
paul@461 | 300 | datetimes. If 'tzid_name' is specified, the time zone information will |
paul@461 | 301 | be acquired from a field starting with 'tzid_name' instead of 'name'. |
paul@446 | 302 | """ |
paul@446 | 303 | |
paul@446 | 304 | args = self.env.get_args() |
paul@446 | 305 | |
paul@446 | 306 | dates = args.get("%s-date" % name, []) |
paul@446 | 307 | hours = args.get("%s-hour" % name, []) |
paul@446 | 308 | minutes = args.get("%s-minute" % name, []) |
paul@446 | 309 | seconds = args.get("%s-second" % name, []) |
paul@461 | 310 | tzids = args.get("%s-tzid" % (tzid_name or name), []) |
paul@446 | 311 | |
paul@446 | 312 | # Handle absent values by employing None values. |
paul@446 | 313 | |
paul@446 | 314 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@446 | 315 | if not field_values and not multiple: |
paul@446 | 316 | field_values = [(None, None, None, None, None)] |
paul@446 | 317 | |
paul@446 | 318 | all_values = [] |
paul@446 | 319 | |
paul@446 | 320 | for date, hour, minute, second, tzid in field_values: |
paul@446 | 321 | |
paul@446 | 322 | # Construct a usable dictionary of values. |
paul@446 | 323 | |
paul@446 | 324 | time = (hour or minute or second) and \ |
paul@446 | 325 | "T%s%s%s" % ( |
paul@446 | 326 | (hour or "").rjust(2, "0")[:2], |
paul@446 | 327 | (minute or "").rjust(2, "0")[:2], |
paul@446 | 328 | (second or "").rjust(2, "0")[:2] |
paul@446 | 329 | ) or "" |
paul@446 | 330 | |
paul@446 | 331 | value = { |
paul@446 | 332 | "date" : date, |
paul@446 | 333 | "time" : time, |
paul@446 | 334 | "tzid" : tzid or self.get_tzid() |
paul@446 | 335 | } |
paul@446 | 336 | |
paul@446 | 337 | # Return a single value or append to a collection of all values. |
paul@446 | 338 | |
paul@446 | 339 | if not multiple: |
paul@446 | 340 | return value |
paul@446 | 341 | else: |
paul@446 | 342 | all_values.append(value) |
paul@446 | 343 | |
paul@446 | 344 | return all_values |
paul@446 | 345 | |
paul@446 | 346 | def set_period_in_object(self, obj, period): |
paul@446 | 347 | |
paul@446 | 348 | "Set in the given 'obj' the given 'period' as the main start and end." |
paul@446 | 349 | |
paul@446 | 350 | (dtstart, dtstart_attr), (dtend, dtend_attr) = period |
paul@446 | 351 | |
paul@457 | 352 | result = self.set_datetime_in_object(dtstart, dtstart_attr.get("TZID"), "DTSTART", obj) |
paul@457 | 353 | result = self.set_datetime_in_object(dtend, dtend_attr.get("TZID"), "DTEND", obj) or result |
paul@457 | 354 | return result |
paul@446 | 355 | |
paul@446 | 356 | def set_periods_in_object(self, obj, periods): |
paul@446 | 357 | |
paul@446 | 358 | "Set in the given 'obj' the given 'periods'." |
paul@446 | 359 | |
paul@446 | 360 | update = False |
paul@446 | 361 | |
paul@446 | 362 | old_values = obj.get_values("RDATE") |
paul@446 | 363 | new_rdates = [] |
paul@446 | 364 | |
paul@446 | 365 | if obj.has_key("RDATE"): |
paul@446 | 366 | del obj["RDATE"] |
paul@446 | 367 | |
paul@446 | 368 | for period in periods: |
paul@446 | 369 | (dtstart, dtstart_attr), (dtend, dtend_attr) = period |
paul@446 | 370 | tzid = dtstart_attr.get("TZID") or dtend_attr.get("TZID") |
paul@446 | 371 | new_rdates.append(get_period_item(dtstart, dtend, tzid)) |
paul@446 | 372 | |
paul@446 | 373 | obj["RDATE"] = new_rdates |
paul@446 | 374 | |
paul@446 | 375 | # NOTE: To do: calculate the update status. |
paul@446 | 376 | return update |
paul@446 | 377 | |
paul@446 | 378 | def set_datetime_in_object(self, dt, tzid, property, obj): |
paul@446 | 379 | |
paul@446 | 380 | """ |
paul@446 | 381 | Set 'dt' and 'tzid' for the given 'property' in 'obj', returning whether |
paul@446 | 382 | an update has occurred. |
paul@446 | 383 | """ |
paul@446 | 384 | |
paul@446 | 385 | if dt: |
paul@446 | 386 | old_value = obj.get_value(property) |
paul@446 | 387 | obj[property] = [get_datetime_item(dt, tzid)] |
paul@446 | 388 | return format_datetime(dt) != old_value |
paul@446 | 389 | |
paul@446 | 390 | return False |
paul@446 | 391 | |
paul@446 | 392 | def get_event_period(self, obj): |
paul@446 | 393 | |
paul@446 | 394 | """ |
paul@446 | 395 | Return (dtstart, dtstart attributes), (dtend, dtend attributes) for |
paul@446 | 396 | 'obj'. |
paul@446 | 397 | """ |
paul@446 | 398 | |
paul@446 | 399 | dtstart, dtstart_attr = obj.get_datetime_item("DTSTART") |
paul@446 | 400 | if obj.has_key("DTEND"): |
paul@446 | 401 | dtend, dtend_attr = obj.get_datetime_item("DTEND") |
paul@446 | 402 | elif obj.has_key("DURATION"): |
paul@446 | 403 | duration = obj.get_duration("DURATION") |
paul@446 | 404 | dtend = dtstart + duration |
paul@446 | 405 | dtend_attr = dtstart_attr |
paul@446 | 406 | else: |
paul@446 | 407 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@446 | 408 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@446 | 409 | |
paul@478 | 410 | def get_attendees(self): |
paul@478 | 411 | |
paul@478 | 412 | """ |
paul@478 | 413 | Return attendees from the request, normalised for iCalendar purposes, |
paul@478 | 414 | and without duplicates. |
paul@478 | 415 | """ |
paul@478 | 416 | |
paul@478 | 417 | args = self.env.get_args() |
paul@478 | 418 | |
paul@478 | 419 | attendees = args.get("attendee", []) |
paul@478 | 420 | unique_attendees = set() |
paul@478 | 421 | ordered_attendees = [] |
paul@478 | 422 | |
paul@478 | 423 | for attendee in attendees: |
paul@478 | 424 | attendee = get_uri(attendee) |
paul@478 | 425 | if attendee not in unique_attendees: |
paul@478 | 426 | unique_attendees.add(attendee) |
paul@478 | 427 | ordered_attendees.append(attendee) |
paul@478 | 428 | |
paul@478 | 429 | return ordered_attendees |
paul@478 | 430 | |
paul@478 | 431 | def update_attendees(self, obj): |
paul@477 | 432 | |
paul@477 | 433 | "Add or remove attendees. This does not affect the stored object." |
paul@477 | 434 | |
paul@477 | 435 | args = self.env.get_args() |
paul@477 | 436 | |
paul@478 | 437 | attendees = self.get_attendees() |
paul@477 | 438 | |
paul@477 | 439 | if args.has_key("add"): |
paul@477 | 440 | attendees.append("") |
paul@477 | 441 | |
paul@477 | 442 | if args.has_key("remove"): |
paul@477 | 443 | removed_attendee = args["remove"][0] |
paul@477 | 444 | if removed_attendee in attendees: |
paul@477 | 445 | attendees.remove(removed_attendee) |
paul@477 | 446 | |
paul@477 | 447 | return attendees |
paul@477 | 448 | |
paul@446 | 449 | # Page fragment methods. |
paul@446 | 450 | |
paul@446 | 451 | def show_request_controls(self, obj): |
paul@446 | 452 | |
paul@446 | 453 | "Show form controls for a request concerning 'obj'." |
paul@446 | 454 | |
paul@446 | 455 | page = self.page |
paul@446 | 456 | args = self.env.get_args() |
paul@446 | 457 | |
paul@446 | 458 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@446 | 459 | |
paul@446 | 460 | attendees = uri_values((obj.get_values("ATTENDEE") or []) + filter(None, args.get("attendee", []))) |
paul@446 | 461 | is_attendee = self.user in attendees |
paul@446 | 462 | |
paul@446 | 463 | is_request = (obj.get_value("UID"), obj.get_value("RECURRENCE-ID")) in self._get_requests() |
paul@446 | 464 | |
paul@446 | 465 | have_other_attendees = len(attendees) > (is_attendee and 1 or 0) |
paul@446 | 466 | |
paul@446 | 467 | # Show appropriate options depending on the role of the user. |
paul@446 | 468 | |
paul@446 | 469 | if is_attendee and not is_organiser: |
paul@446 | 470 | page.p("An action is required for this request:") |
paul@446 | 471 | |
paul@446 | 472 | page.p() |
paul@446 | 473 | page.input(name="reply", type="submit", value="Send reply") |
paul@446 | 474 | page.add(" ") |
paul@446 | 475 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 476 | page.add(" ") |
paul@446 | 477 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 478 | page.p.close() |
paul@446 | 479 | |
paul@446 | 480 | if is_organiser: |
paul@446 | 481 | page.p("As organiser, you can perform the following:") |
paul@446 | 482 | |
paul@446 | 483 | if have_other_attendees: |
paul@446 | 484 | page.p() |
paul@446 | 485 | page.input(name="invite", type="submit", value="Invite/notify attendees") |
paul@446 | 486 | page.add(" ") |
paul@446 | 487 | if is_request: |
paul@446 | 488 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 489 | else: |
paul@446 | 490 | page.input(name="cancel", type="submit", value="Cancel event") |
paul@446 | 491 | page.add(" ") |
paul@446 | 492 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 493 | page.p.close() |
paul@446 | 494 | else: |
paul@446 | 495 | page.p() |
paul@446 | 496 | page.input(name="save", type="submit", value="Save event") |
paul@446 | 497 | page.add(" ") |
paul@446 | 498 | page.input(name="discard", type="submit", value="Discard event") |
paul@446 | 499 | page.add(" ") |
paul@446 | 500 | page.input(name="ignore", type="submit", value="Do nothing for now") |
paul@446 | 501 | page.p.close() |
paul@446 | 502 | |
paul@446 | 503 | def show_object_on_page(self, uid, obj, error=None): |
paul@446 | 504 | |
paul@446 | 505 | """ |
paul@446 | 506 | Show the calendar object with the given 'uid' and representation 'obj' |
paul@446 | 507 | on the current page. If 'error' is given, show a suitable message. |
paul@446 | 508 | """ |
paul@446 | 509 | |
paul@446 | 510 | page = self.page |
paul@446 | 511 | page.form(method="POST") |
paul@446 | 512 | |
paul@446 | 513 | page.input(name="editing", type="hidden", value="true") |
paul@446 | 514 | |
paul@446 | 515 | args = self.env.get_args() |
paul@446 | 516 | |
paul@446 | 517 | # Obtain the user's timezone. |
paul@446 | 518 | |
paul@446 | 519 | tzid = self.get_tzid() |
paul@446 | 520 | |
paul@446 | 521 | # Obtain basic event information, showing any necessary editing controls. |
paul@446 | 522 | |
paul@446 | 523 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@473 | 524 | initial_load = not args.has_key("editing") |
paul@446 | 525 | |
paul@475 | 526 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@478 | 527 | attendees = is_organiser and self.update_attendees(obj) or \ |
paul@475 | 528 | (initial_load or not is_organiser) and existing_attendees or [] |
paul@446 | 529 | |
paul@446 | 530 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_event_period(obj) |
paul@446 | 531 | self.show_object_datetime_controls(dtstart, dtend) |
paul@446 | 532 | |
paul@446 | 533 | # Provide a summary of the object. |
paul@446 | 534 | |
paul@446 | 535 | page.table(class_="object", cellspacing=5, cellpadding=5) |
paul@446 | 536 | page.thead() |
paul@446 | 537 | page.tr() |
paul@446 | 538 | page.th("Event", class_="mainheading", colspan=2) |
paul@446 | 539 | page.tr.close() |
paul@446 | 540 | page.thead.close() |
paul@446 | 541 | page.tbody() |
paul@446 | 542 | |
paul@446 | 543 | for name, label in self.property_items: |
paul@446 | 544 | field = name.lower() |
paul@446 | 545 | |
paul@446 | 546 | items = obj.get_items(name) or [] |
paul@446 | 547 | rowspan = len(items) |
paul@446 | 548 | |
paul@446 | 549 | if name == "ATTENDEE": |
paul@473 | 550 | rowspan = len(attendees) + 1 # for the add button |
paul@446 | 551 | elif not items: |
paul@446 | 552 | continue |
paul@446 | 553 | |
paul@446 | 554 | page.tr() |
paul@446 | 555 | page.th(label, class_="objectheading %s%s" % (field, error and field in error and " error" or ""), rowspan=rowspan) |
paul@446 | 556 | |
paul@446 | 557 | # Handle datetimes specially. |
paul@446 | 558 | |
paul@446 | 559 | if name in ["DTSTART", "DTEND"]: |
paul@446 | 560 | |
paul@446 | 561 | # Obtain the datetime. |
paul@446 | 562 | |
paul@446 | 563 | if name == "DTSTART": |
paul@446 | 564 | dt, attr = dtstart, dtstart_attr |
paul@446 | 565 | |
paul@446 | 566 | # Where no end datetime exists, use the start datetime as the |
paul@446 | 567 | # basis of any potential datetime specified if dt-control is |
paul@446 | 568 | # set. |
paul@446 | 569 | |
paul@446 | 570 | else: |
paul@446 | 571 | dt, attr = dtend or dtstart, dtend_attr or dtstart_attr |
paul@446 | 572 | |
paul@446 | 573 | self.show_datetime_controls(obj, dt, attr, name == "DTSTART") |
paul@446 | 574 | |
paul@446 | 575 | page.tr.close() |
paul@446 | 576 | |
paul@446 | 577 | # Handle the summary specially. |
paul@446 | 578 | |
paul@446 | 579 | elif name == "SUMMARY": |
paul@446 | 580 | value = args.get("summary", [obj.get_value(name)])[0] |
paul@446 | 581 | |
paul@446 | 582 | page.td() |
paul@446 | 583 | if is_organiser: |
paul@446 | 584 | page.input(name="summary", type="text", value=value, size=80) |
paul@446 | 585 | else: |
paul@446 | 586 | page.add(value) |
paul@446 | 587 | page.td.close() |
paul@446 | 588 | page.tr.close() |
paul@446 | 589 | |
paul@473 | 590 | # Handle attendees specially. |
paul@473 | 591 | |
paul@473 | 592 | elif name == "ATTENDEE": |
paul@473 | 593 | attendee_map = dict(items) |
paul@473 | 594 | first = True |
paul@473 | 595 | |
paul@473 | 596 | for i, value in enumerate(attendees): |
paul@473 | 597 | if not first: |
paul@473 | 598 | page.tr() |
paul@473 | 599 | else: |
paul@473 | 600 | first = False |
paul@473 | 601 | |
paul@479 | 602 | # Obtain details of attendees to supply attributes. |
paul@473 | 603 | |
paul@479 | 604 | self.show_attendee(obj, i, value, attendee_map.get(value)) |
paul@473 | 605 | page.tr.close() |
paul@473 | 606 | |
paul@473 | 607 | # Allow more attendees to be specified. |
paul@473 | 608 | |
paul@473 | 609 | if is_organiser: |
paul@473 | 610 | i = len(attendees) |
paul@473 | 611 | |
paul@473 | 612 | if not first: |
paul@473 | 613 | page.tr() |
paul@473 | 614 | |
paul@473 | 615 | page.td() |
paul@473 | 616 | page.input(name="add", type="submit", value="add", id="add-%d" % i, class_="add") |
paul@473 | 617 | page.label("Add attendee", for_="add-%d" % i, class_="add") |
paul@473 | 618 | page.td.close() |
paul@473 | 619 | page.tr.close() |
paul@473 | 620 | |
paul@473 | 621 | # Handle potentially many values of other kinds. |
paul@446 | 622 | |
paul@446 | 623 | else: |
paul@446 | 624 | first = True |
paul@446 | 625 | |
paul@446 | 626 | for i, (value, attr) in enumerate(items): |
paul@446 | 627 | if not first: |
paul@446 | 628 | page.tr() |
paul@446 | 629 | else: |
paul@446 | 630 | first = False |
paul@446 | 631 | |
paul@473 | 632 | page.td(class_="objectvalue") |
paul@473 | 633 | page.add(value) |
paul@446 | 634 | page.td.close() |
paul@446 | 635 | page.tr.close() |
paul@446 | 636 | |
paul@446 | 637 | page.tbody.close() |
paul@446 | 638 | page.table.close() |
paul@446 | 639 | |
paul@446 | 640 | self.show_recurrences(obj) |
paul@446 | 641 | self.show_conflicting_events(uid, obj) |
paul@446 | 642 | self.show_request_controls(obj) |
paul@446 | 643 | |
paul@446 | 644 | page.form.close() |
paul@446 | 645 | |
paul@479 | 646 | def show_attendee(self, obj, i, attendee, attendee_attr): |
paul@479 | 647 | |
paul@479 | 648 | """ |
paul@479 | 649 | For the given object 'obj', show the attendee in position 'i' with the |
paul@479 | 650 | given 'attendee' value, having 'attendee_attr' as any stored attributes. |
paul@479 | 651 | """ |
paul@479 | 652 | |
paul@479 | 653 | page = self.page |
paul@479 | 654 | args = self.env.get_args() |
paul@479 | 655 | |
paul@479 | 656 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@479 | 657 | existing = attendee_attr is not None |
paul@479 | 658 | partstat = attendee_attr and attendee_attr.get("PARTSTAT") |
paul@479 | 659 | |
paul@479 | 660 | page.td(class_="objectvalue") |
paul@479 | 661 | |
paul@479 | 662 | # Show a form control as organiser for new attendees. |
paul@479 | 663 | |
paul@479 | 664 | if is_organiser and not existing: |
paul@479 | 665 | page.input(name="attendee", type="value", value=attendee, size="40") |
paul@479 | 666 | else: |
paul@479 | 667 | page.input(name="attendee", type="hidden", value=attendee) |
paul@479 | 668 | page.add(attendee) |
paul@479 | 669 | page.add(" ") |
paul@479 | 670 | |
paul@479 | 671 | # Show participation status, editable for the current user. |
paul@479 | 672 | |
paul@479 | 673 | if attendee == self.user: |
paul@479 | 674 | self._show_menu("partstat", partstat, self.partstat_items, "partstat") |
paul@479 | 675 | |
paul@479 | 676 | # Allow the participation indicator to act as a submit |
paul@479 | 677 | # button in order to refresh the page and show a control for |
paul@479 | 678 | # the current user, if indicated. |
paul@479 | 679 | |
paul@480 | 680 | elif is_organiser and not existing: |
paul@479 | 681 | page.input(name="partstat-refresh", type="submit", value="refresh", id="partstat-%d" % i, class_="refresh") |
paul@479 | 682 | page.label(dict(self.partstat_items).get(partstat, ""), for_="partstat-%s" % i, class_="partstat") |
paul@479 | 683 | else: |
paul@479 | 684 | page.span(dict(self.partstat_items).get(partstat, ""), class_="partstat") |
paul@479 | 685 | |
paul@479 | 686 | # Permit organisers to remove attendees. |
paul@479 | 687 | |
paul@479 | 688 | if is_organiser: |
paul@479 | 689 | |
paul@479 | 690 | # Permit the removal of newly-added attendees. |
paul@479 | 691 | |
paul@479 | 692 | remove_type = (existing and attendee != self.user) and "checkbox" or "submit" |
paul@479 | 693 | |
paul@479 | 694 | self._control("remove", remove_type, attendee, attendee in args.get("remove", []), id="remove-%d" % i, class_="remove") |
paul@479 | 695 | |
paul@479 | 696 | page.label("Remove", for_="remove-%d" % i, class_="remove") |
paul@479 | 697 | page.label("Uninvited", for_="remove-%d" % i, class_="removed") |
paul@479 | 698 | |
paul@479 | 699 | page.td.close() |
paul@479 | 700 | |
paul@446 | 701 | def show_recurrences(self, obj): |
paul@446 | 702 | |
paul@446 | 703 | "Show recurrences for the object having the given representation 'obj'." |
paul@446 | 704 | |
paul@446 | 705 | page = self.page |
paul@446 | 706 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@446 | 707 | |
paul@446 | 708 | # Obtain any parent object if this object is a specific recurrence. |
paul@446 | 709 | |
paul@446 | 710 | uid = obj.get_value("UID") |
paul@446 | 711 | recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) |
paul@446 | 712 | |
paul@446 | 713 | if recurrenceid: |
paul@446 | 714 | obj = self._get_object(uid) |
paul@446 | 715 | if not obj: |
paul@446 | 716 | return |
paul@446 | 717 | |
paul@446 | 718 | page.p("This event modifies a recurring event.") |
paul@446 | 719 | |
paul@446 | 720 | # Obtain the periods associated with the event in the user's time zone. |
paul@446 | 721 | |
paul@458 | 722 | periods = obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@446 | 723 | recurrenceids = self._get_recurrences(uid) |
paul@446 | 724 | |
paul@446 | 725 | if len(periods) == 1: |
paul@446 | 726 | return |
paul@446 | 727 | |
paul@446 | 728 | if is_organiser: |
paul@446 | 729 | page.p("This event recurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@446 | 730 | else: |
paul@446 | 731 | page.p("This event occurs on the following occasions within the next %d days:" % self.get_window_size()) |
paul@446 | 732 | |
paul@446 | 733 | # Determine whether any periods are explicitly created or are part of a |
paul@446 | 734 | # rule. |
paul@446 | 735 | |
paul@458 | 736 | explicit_periods = filter(lambda p: p.origin != "RRULE", periods) |
paul@446 | 737 | |
paul@446 | 738 | # Show each recurrence in a separate table if editable. |
paul@446 | 739 | |
paul@446 | 740 | if is_organiser and explicit_periods: |
paul@446 | 741 | |
paul@458 | 742 | for index, p in enumerate(periods[1:]): |
paul@446 | 743 | |
paul@446 | 744 | # Isolate the controls from neighbouring tables. |
paul@446 | 745 | |
paul@446 | 746 | page.div() |
paul@446 | 747 | |
paul@458 | 748 | self.show_object_datetime_controls(p.start, p.end, index) |
paul@446 | 749 | |
paul@446 | 750 | # NOTE: Need to customise the TH classes according to errors and |
paul@446 | 751 | # NOTE: index information. |
paul@446 | 752 | |
paul@446 | 753 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@446 | 754 | page.caption("Occurrence") |
paul@446 | 755 | page.tbody() |
paul@446 | 756 | page.tr() |
paul@446 | 757 | page.th("Start", class_="objectheading start") |
paul@458 | 758 | self.show_recurrence_controls(obj, index, p.start, p.end, p.origin, recurrenceid, recurrenceids, True) |
paul@446 | 759 | page.tr.close() |
paul@446 | 760 | page.tr() |
paul@446 | 761 | page.th("End", class_="objectheading end") |
paul@458 | 762 | self.show_recurrence_controls(obj, index, p.start, p.end, p.origin, recurrenceid, recurrenceids, False) |
paul@446 | 763 | page.tr.close() |
paul@446 | 764 | page.tbody.close() |
paul@446 | 765 | page.table.close() |
paul@446 | 766 | |
paul@446 | 767 | page.div.close() |
paul@446 | 768 | |
paul@446 | 769 | # Otherwise, use a compact single table. |
paul@446 | 770 | |
paul@446 | 771 | else: |
paul@446 | 772 | page.table(cellspacing=5, cellpadding=5, class_="recurrence") |
paul@446 | 773 | page.caption("Occurrences") |
paul@446 | 774 | page.thead() |
paul@446 | 775 | page.tr() |
paul@446 | 776 | page.th("Start", class_="objectheading start") |
paul@446 | 777 | page.th("End", class_="objectheading end") |
paul@446 | 778 | page.tr.close() |
paul@446 | 779 | page.thead.close() |
paul@446 | 780 | page.tbody() |
paul@446 | 781 | |
paul@446 | 782 | # Show only subsequent periods if organiser, since the principal |
paul@446 | 783 | # period will be the start and end datetimes. |
paul@446 | 784 | |
paul@458 | 785 | for index, p in enumerate(is_organiser and periods[1:] or periods): |
paul@446 | 786 | page.tr() |
paul@458 | 787 | self.show_recurrence_controls(obj, index, p.start, p.end, p.origin, recurrenceid, recurrenceids, True) |
paul@458 | 788 | self.show_recurrence_controls(obj, index, p.start, p.end, p.origin, recurrenceid, recurrenceids, False) |
paul@446 | 789 | page.tr.close() |
paul@446 | 790 | page.tbody.close() |
paul@446 | 791 | page.table.close() |
paul@446 | 792 | |
paul@446 | 793 | def show_conflicting_events(self, uid, obj): |
paul@446 | 794 | |
paul@446 | 795 | """ |
paul@446 | 796 | Show conflicting events for the object having the given 'uid' and |
paul@446 | 797 | representation 'obj'. |
paul@446 | 798 | """ |
paul@446 | 799 | |
paul@446 | 800 | page = self.page |
paul@446 | 801 | |
paul@446 | 802 | # Obtain the user's timezone. |
paul@446 | 803 | |
paul@446 | 804 | tzid = self.get_tzid() |
paul@446 | 805 | periods = obj.get_periods_for_freebusy(self.get_tzid(), self.get_window_end()) |
paul@446 | 806 | |
paul@446 | 807 | # Indicate whether there are conflicting events. |
paul@446 | 808 | |
paul@446 | 809 | freebusy = self.store.get_freebusy(self.user) |
paul@446 | 810 | |
paul@446 | 811 | if freebusy: |
paul@446 | 812 | |
paul@446 | 813 | # Obtain any time zone details from the suggested event. |
paul@446 | 814 | |
paul@446 | 815 | _dtstart, attr = obj.get_item("DTSTART") |
paul@446 | 816 | tzid = attr.get("TZID", tzid) |
paul@446 | 817 | |
paul@446 | 818 | # Show any conflicts. |
paul@446 | 819 | |
paul@458 | 820 | conflicts = list([p for p in have_conflict(freebusy, periods, True) if p.uid != uid]) |
paul@458 | 821 | conflicts.sort() |
paul@446 | 822 | |
paul@446 | 823 | if conflicts: |
paul@446 | 824 | page.p("This event conflicts with others:") |
paul@446 | 825 | |
paul@446 | 826 | page.table(cellspacing=5, cellpadding=5, class_="conflicts") |
paul@446 | 827 | page.thead() |
paul@446 | 828 | page.tr() |
paul@446 | 829 | page.th("Event") |
paul@446 | 830 | page.th("Start") |
paul@446 | 831 | page.th("End") |
paul@446 | 832 | page.tr.close() |
paul@446 | 833 | page.thead.close() |
paul@446 | 834 | page.tbody() |
paul@446 | 835 | |
paul@458 | 836 | for p in conflicts: |
paul@446 | 837 | |
paul@446 | 838 | # Provide details of any conflicting event. |
paul@446 | 839 | |
paul@458 | 840 | start = self.format_datetime(to_timezone(get_datetime(p.start), tzid), "long") |
paul@458 | 841 | end = self.format_datetime(to_timezone(get_datetime(p.end), tzid), "long") |
paul@446 | 842 | |
paul@446 | 843 | page.tr() |
paul@446 | 844 | |
paul@446 | 845 | # Show the event summary for the conflicting event. |
paul@446 | 846 | |
paul@446 | 847 | page.td() |
paul@458 | 848 | page.a(p.summary, href=self.link_to(p.uid)) |
paul@446 | 849 | page.td.close() |
paul@446 | 850 | |
paul@446 | 851 | page.td(start) |
paul@446 | 852 | page.td(end) |
paul@446 | 853 | |
paul@446 | 854 | page.tr.close() |
paul@446 | 855 | |
paul@446 | 856 | page.tbody.close() |
paul@446 | 857 | page.table.close() |
paul@446 | 858 | |
paul@474 | 859 | # Generation of controls within page fragments. |
paul@474 | 860 | |
paul@474 | 861 | def show_object_datetime_controls(self, start, end, index=None): |
paul@474 | 862 | |
paul@474 | 863 | """ |
paul@474 | 864 | Show datetime-related controls if already active or if an object needs |
paul@474 | 865 | them for the given 'start' to 'end' period. The given 'index' is used to |
paul@474 | 866 | parameterise individual controls for dynamic manipulation. |
paul@474 | 867 | """ |
paul@474 | 868 | |
paul@474 | 869 | page = self.page |
paul@474 | 870 | args = self.env.get_args() |
paul@474 | 871 | sn = self._suffixed_name |
paul@474 | 872 | ssn = self._simple_suffixed_name |
paul@474 | 873 | |
paul@474 | 874 | # Add a dynamic stylesheet to permit the controls to modify the display. |
paul@474 | 875 | # NOTE: The style details need to be coordinated with the static |
paul@474 | 876 | # NOTE: stylesheet. |
paul@474 | 877 | |
paul@474 | 878 | if index is not None: |
paul@474 | 879 | page.style(type="text/css") |
paul@474 | 880 | |
paul@474 | 881 | # Unlike the rules for object properties, these affect recurrence |
paul@474 | 882 | # properties. |
paul@474 | 883 | |
paul@474 | 884 | page.add("""\ |
paul@474 | 885 | input#dttimes-enable-%(index)d, |
paul@474 | 886 | input#dtend-enable-%(index)d, |
paul@474 | 887 | input#dttimes-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue .time.enabled, |
paul@474 | 888 | input#dttimes-enable-%(index)d:checked ~ .recurrence td.objectvalue .time.disabled, |
paul@474 | 889 | input#dtend-enable-%(index)d:not(:checked) ~ .recurrence td.objectvalue.dtend .dt.enabled, |
paul@474 | 890 | input#dtend-enable-%(index)d:checked ~ .recurrence td.objectvalue.dtend .dt.disabled { |
paul@474 | 891 | display: none; |
paul@474 | 892 | }""" % {"index" : index}) |
paul@474 | 893 | |
paul@474 | 894 | page.style.close() |
paul@474 | 895 | |
paul@474 | 896 | dtend_control = args.get(ssn("dtend-control", "recur", index), []) |
paul@474 | 897 | dttimes_control = args.get(ssn("dttimes-control", "recur", index), []) |
paul@474 | 898 | |
paul@474 | 899 | dtend_enabled = index is not None and str(index) in dtend_control or index is None and dtend_control |
paul@474 | 900 | dttimes_enabled = index is not None and str(index) in dttimes_control or index is None and dttimes_control |
paul@474 | 901 | |
paul@474 | 902 | initial_load = not args.has_key("editing") |
paul@474 | 903 | |
paul@474 | 904 | dtend_enabled = dtend_enabled or initial_load and (isinstance(end, datetime) or start != end - timedelta(1)) |
paul@474 | 905 | dttimes_enabled = dttimes_enabled or initial_load and (isinstance(start, datetime) or isinstance(end, datetime)) |
paul@474 | 906 | |
paul@474 | 907 | self._control( |
paul@474 | 908 | ssn("dtend-control", "recur", index), "checkbox", |
paul@474 | 909 | index is not None and str(index) or "enable", dtend_enabled, |
paul@474 | 910 | id=sn("dtend-enable", index) |
paul@474 | 911 | ) |
paul@474 | 912 | |
paul@474 | 913 | self._control( |
paul@474 | 914 | ssn("dttimes-control", "recur", index), "checkbox", |
paul@474 | 915 | index is not None and str(index) or "enable", dttimes_enabled, |
paul@474 | 916 | id=sn("dttimes-enable", index) |
paul@474 | 917 | ) |
paul@474 | 918 | |
paul@474 | 919 | def show_datetime_controls(self, obj, dt, attr, show_start): |
paul@474 | 920 | |
paul@474 | 921 | """ |
paul@474 | 922 | Show datetime details from the given 'obj' for the datetime 'dt' and |
paul@474 | 923 | attributes 'attr', showing start details if 'show_start' is set |
paul@474 | 924 | to a true value. Details will appear as controls for organisers and |
paul@474 | 925 | labels for attendees. |
paul@474 | 926 | """ |
paul@474 | 927 | |
paul@474 | 928 | page = self.page |
paul@474 | 929 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@474 | 930 | |
paul@474 | 931 | # Change end dates to refer to the actual dates, not the iCalendar |
paul@474 | 932 | # "next day" dates. |
paul@474 | 933 | |
paul@474 | 934 | if not show_start and not isinstance(dt, datetime): |
paul@474 | 935 | dt -= timedelta(1) |
paul@474 | 936 | |
paul@474 | 937 | # Show controls for editing as organiser. |
paul@474 | 938 | |
paul@474 | 939 | if is_organiser: |
paul@474 | 940 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 941 | |
paul@474 | 942 | if show_start: |
paul@474 | 943 | page.div(class_="dt enabled") |
paul@474 | 944 | self._show_date_controls("dtstart", dt, attr.get("TZID")) |
paul@474 | 945 | page.br() |
paul@474 | 946 | page.label("Specify times", for_="dttimes-enable", class_="time disabled enable") |
paul@474 | 947 | page.label("Specify dates only", for_="dttimes-enable", class_="time enabled disable") |
paul@474 | 948 | page.div.close() |
paul@474 | 949 | |
paul@474 | 950 | else: |
paul@474 | 951 | page.div(class_="dt disabled") |
paul@474 | 952 | page.label("Specify end date", for_="dtend-enable", class_="enable") |
paul@474 | 953 | page.div.close() |
paul@474 | 954 | page.div(class_="dt enabled") |
paul@474 | 955 | self._show_date_controls("dtend", dt, attr.get("TZID")) |
paul@474 | 956 | page.br() |
paul@474 | 957 | page.label("End on same day", for_="dtend-enable", class_="disable") |
paul@474 | 958 | page.div.close() |
paul@474 | 959 | |
paul@474 | 960 | page.td.close() |
paul@474 | 961 | |
paul@474 | 962 | # Show a label as attendee. |
paul@474 | 963 | |
paul@474 | 964 | else: |
paul@474 | 965 | page.td(self.format_datetime(dt, "full")) |
paul@474 | 966 | |
paul@474 | 967 | def show_recurrence_controls(self, obj, index, start, end, origin, recurrenceid, recurrenceids, show_start): |
paul@474 | 968 | |
paul@474 | 969 | """ |
paul@474 | 970 | Show datetime details from the given 'obj' for the recurrence having the |
paul@474 | 971 | given 'index', with the recurrence period described by the datetimes |
paul@474 | 972 | 'start' and 'end', indicating the 'origin' of the period from the event |
paul@474 | 973 | details, employing any 'recurrenceid' and 'recurrenceids' for the object |
paul@474 | 974 | to configure the displayed information. |
paul@474 | 975 | |
paul@474 | 976 | If 'show_start' is set to a true value, the start details will be shown; |
paul@474 | 977 | otherwise, the end details will be shown. |
paul@474 | 978 | """ |
paul@474 | 979 | |
paul@474 | 980 | page = self.page |
paul@474 | 981 | sn = self._suffixed_name |
paul@474 | 982 | ssn = self._simple_suffixed_name |
paul@474 | 983 | |
paul@474 | 984 | is_organiser = get_uri(obj.get_value("ORGANIZER")) == self.user |
paul@474 | 985 | |
paul@474 | 986 | # Change end dates to refer to the actual dates, not the iCalendar |
paul@474 | 987 | # "next day" dates. |
paul@474 | 988 | |
paul@474 | 989 | if not isinstance(end, datetime): |
paul@474 | 990 | end -= timedelta(1) |
paul@474 | 991 | |
paul@474 | 992 | start_utc = format_datetime(to_timezone(start, "UTC")) |
paul@474 | 993 | replaced = recurrenceids and start_utc in recurrenceids and "replaced" or "" |
paul@474 | 994 | css = " ".join([ |
paul@474 | 995 | replaced, |
paul@474 | 996 | recurrenceid and start_utc == recurrenceid and "affected" or "" |
paul@474 | 997 | ]) |
paul@474 | 998 | |
paul@474 | 999 | # Show controls for editing as organiser. |
paul@474 | 1000 | |
paul@474 | 1001 | if is_organiser and not replaced and origin != "RRULE": |
paul@474 | 1002 | page.td(class_="objectvalue dt%s" % (show_start and "start" or "end")) |
paul@474 | 1003 | |
paul@474 | 1004 | if show_start: |
paul@474 | 1005 | page.div(class_="dt enabled") |
paul@474 | 1006 | self._show_date_controls(ssn("dtstart", "recur", index), start, index=index) |
paul@474 | 1007 | page.br() |
paul@474 | 1008 | page.label("Specify times", for_=sn("dttimes-enable", index), class_="time disabled enable") |
paul@474 | 1009 | page.label("Specify dates only", for_=sn("dttimes-enable", index), class_="time enabled disable") |
paul@474 | 1010 | page.div.close() |
paul@474 | 1011 | |
paul@474 | 1012 | else: |
paul@474 | 1013 | page.div(class_="dt disabled") |
paul@474 | 1014 | page.label("Specify end date", for_=sn("dtend-enable", index), class_="enable") |
paul@474 | 1015 | page.div.close() |
paul@474 | 1016 | page.div(class_="dt enabled") |
paul@474 | 1017 | self._show_date_controls(ssn("dtend", "recur", index), end, index=index, show_tzid=False) |
paul@474 | 1018 | page.br() |
paul@474 | 1019 | page.label("End on same day", for_=sn("dtend-enable", index), class_="disable") |
paul@474 | 1020 | page.div.close() |
paul@474 | 1021 | |
paul@474 | 1022 | page.td.close() |
paul@474 | 1023 | |
paul@474 | 1024 | # Show label as attendee. |
paul@474 | 1025 | |
paul@474 | 1026 | else: |
paul@474 | 1027 | page.td(self.format_datetime(show_start and start or end, "long"), class_=css) |
paul@474 | 1028 | |
paul@446 | 1029 | # Full page output methods. |
paul@446 | 1030 | |
paul@446 | 1031 | def show(self, path_info): |
paul@446 | 1032 | |
paul@446 | 1033 | "Show an object request using the given 'path_info' for the current user." |
paul@446 | 1034 | |
paul@446 | 1035 | uid, recurrenceid = self._get_identifiers(path_info) |
paul@446 | 1036 | obj = self._get_object(uid, recurrenceid) |
paul@446 | 1037 | |
paul@446 | 1038 | if not obj: |
paul@446 | 1039 | return False |
paul@446 | 1040 | |
paul@446 | 1041 | error = self.handle_request(uid, recurrenceid, obj) |
paul@446 | 1042 | |
paul@446 | 1043 | if not error: |
paul@446 | 1044 | return True |
paul@446 | 1045 | |
paul@446 | 1046 | self.new_page(title="Event") |
paul@446 | 1047 | self.show_object_on_page(uid, obj, error) |
paul@446 | 1048 | |
paul@446 | 1049 | return True |
paul@446 | 1050 | |
paul@446 | 1051 | # Utility methods. |
paul@446 | 1052 | |
paul@474 | 1053 | def _control(self, name, type, value, selected, **kw): |
paul@474 | 1054 | |
paul@474 | 1055 | """ |
paul@474 | 1056 | Show a control with the given 'name', 'type' and 'value', with |
paul@474 | 1057 | 'selected' indicating whether it should be selected (checked or |
paul@474 | 1058 | equivalent), and with keyword arguments setting other properties. |
paul@474 | 1059 | """ |
paul@474 | 1060 | |
paul@474 | 1061 | page = self.page |
paul@474 | 1062 | if selected: |
paul@474 | 1063 | page.input(name=name, type=type, value=value, checked=selected, **kw) |
paul@474 | 1064 | else: |
paul@474 | 1065 | page.input(name=name, type=type, value=value, **kw) |
paul@474 | 1066 | |
paul@446 | 1067 | def _show_menu(self, name, default, items, class_="", index=None): |
paul@446 | 1068 | |
paul@446 | 1069 | """ |
paul@446 | 1070 | Show a select menu having the given 'name', set to the given 'default', |
paul@446 | 1071 | providing the given (value, label) 'items', and employing the given CSS |
paul@446 | 1072 | 'class_' if specified. |
paul@446 | 1073 | """ |
paul@446 | 1074 | |
paul@446 | 1075 | page = self.page |
paul@446 | 1076 | values = self.env.get_args().get(name, [default]) |
paul@446 | 1077 | if index is not None: |
paul@446 | 1078 | values = values[index:] |
paul@446 | 1079 | values = values and values[0:1] or [default] |
paul@446 | 1080 | |
paul@446 | 1081 | page.select(name=name, class_=class_) |
paul@446 | 1082 | for v, label in items: |
paul@446 | 1083 | if v is None: |
paul@446 | 1084 | continue |
paul@446 | 1085 | if v in values: |
paul@446 | 1086 | page.option(label, value=v, selected="selected") |
paul@446 | 1087 | else: |
paul@446 | 1088 | page.option(label, value=v) |
paul@446 | 1089 | page.select.close() |
paul@446 | 1090 | |
paul@461 | 1091 | def _show_date_controls(self, name, default, tzid=None, index=None, show_tzid=True): |
paul@446 | 1092 | |
paul@446 | 1093 | """ |
paul@446 | 1094 | Show date controls for a field with the given 'name' and 'default' value |
paul@461 | 1095 | and 'tzid'. If 'index' is specified, default field values will be |
paul@461 | 1096 | overridden by the element from a collection of existing form values with |
paul@461 | 1097 | the specified index; otherwise, field values will be overridden by a |
paul@461 | 1098 | single form value. |
paul@461 | 1099 | |
paul@461 | 1100 | If 'show_tzid' is set to a false value, the time zone menu will not be |
paul@461 | 1101 | provided. |
paul@446 | 1102 | """ |
paul@446 | 1103 | |
paul@446 | 1104 | page = self.page |
paul@446 | 1105 | args = self.env.get_args() |
paul@446 | 1106 | |
paul@446 | 1107 | # Show dates for up to one week around the current date. |
paul@446 | 1108 | |
paul@446 | 1109 | base = to_date(default) |
paul@446 | 1110 | items = [] |
paul@446 | 1111 | for i in range(-7, 8): |
paul@446 | 1112 | d = base + timedelta(i) |
paul@446 | 1113 | items.append((format_datetime(d), self.format_date(d, "full"))) |
paul@446 | 1114 | |
paul@446 | 1115 | self._show_menu("%s-date" % name, format_datetime(base), items, index=index) |
paul@446 | 1116 | |
paul@446 | 1117 | # Show time details. |
paul@446 | 1118 | |
paul@446 | 1119 | default_time = isinstance(default, datetime) and default or None |
paul@446 | 1120 | |
paul@446 | 1121 | hour = args.get("%s-hour" % name, [])[index or 0:] |
paul@446 | 1122 | hour = hour and hour[0] or "%02d" % (default_time and default_time.hour or 0) |
paul@446 | 1123 | minute = args.get("%s-minute" % name, [])[index or 0:] |
paul@446 | 1124 | minute = minute and minute[0] or "%02d" % (default_time and default_time.minute or 0) |
paul@446 | 1125 | second = args.get("%s-second" % name, [])[index or 0:] |
paul@446 | 1126 | second = second and second[0] or "%02d" % (default_time and default_time.second or 0) |
paul@446 | 1127 | |
paul@446 | 1128 | page.span(class_="time enabled") |
paul@446 | 1129 | page.input(name="%s-hour" % name, type="text", value=hour, maxlength=2, size=2) |
paul@446 | 1130 | page.add(":") |
paul@446 | 1131 | page.input(name="%s-minute" % name, type="text", value=minute, maxlength=2, size=2) |
paul@446 | 1132 | page.add(":") |
paul@446 | 1133 | page.input(name="%s-second" % name, type="text", value=second, maxlength=2, size=2) |
paul@461 | 1134 | if show_tzid: |
paul@461 | 1135 | tzid = tzid or self.get_tzid() |
paul@461 | 1136 | page.add(" ") |
paul@461 | 1137 | self._show_timezone_menu("%s-tzid" % name, tzid, index) |
paul@446 | 1138 | page.span.close() |
paul@446 | 1139 | |
paul@446 | 1140 | def _show_timezone_menu(self, name, default, index=None): |
paul@446 | 1141 | |
paul@446 | 1142 | """ |
paul@446 | 1143 | Show timezone controls using a menu with the given 'name', set to the |
paul@446 | 1144 | given 'default' unless a field of the given 'name' provides a value. |
paul@446 | 1145 | """ |
paul@446 | 1146 | |
paul@446 | 1147 | entries = [(tzid, tzid) for tzid in pytz.all_timezones] |
paul@446 | 1148 | self._show_menu(name, default, entries, index=index) |
paul@446 | 1149 | |
paul@446 | 1150 | # vim: tabstop=4 expandtab shiftwidth=4 |