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