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