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