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