paul@446 | 1 | #!/usr/bin/env python |
paul@446 | 2 | |
paul@446 | 3 | """ |
paul@446 | 4 | A Web interface to an event calendar. |
paul@446 | 5 | |
paul@1062 | 6 | Copyright (C) 2014, 2015, 2016 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@876 | 22 | from datetime import datetime, timedelta |
paul@923 | 23 | from imiptools.data import get_address, get_uri, get_verbose_address, uri_parts |
paul@883 | 24 | from imiptools.dates import format_datetime, get_date, get_datetime, \ |
paul@446 | 25 | get_datetime_item, get_end_of_day, get_start_of_day, \ |
paul@446 | 26 | get_start_of_next_day, get_timestamp, ends_on_same_day, \ |
paul@889 | 27 | to_date, to_timezone |
paul@446 | 28 | from imiptools.period import add_day_start_points, add_empty_days, add_slots, \ |
paul@874 | 29 | get_scale, get_slots, get_spans, partition_by_day, \ |
paul@931 | 30 | remove_end_slot, Period, Point |
paul@928 | 31 | from imipweb.resource import FormUtilities, ResourceClient |
paul@446 | 32 | |
paul@928 | 33 | class CalendarPage(ResourceClient, FormUtilities): |
paul@446 | 34 | |
paul@446 | 35 | "A request handler for the calendar page." |
paul@446 | 36 | |
paul@446 | 37 | # Request logic methods. |
paul@446 | 38 | |
paul@446 | 39 | def handle_newevent(self): |
paul@446 | 40 | |
paul@446 | 41 | """ |
paul@446 | 42 | Handle any new event operation, creating a new event and redirecting to |
paul@446 | 43 | the event page for further activity. |
paul@446 | 44 | """ |
paul@446 | 45 | |
paul@1005 | 46 | _ = self.get_translator() |
paul@1005 | 47 | |
paul@1162 | 48 | # Check the validation token. |
paul@1162 | 49 | |
paul@1162 | 50 | if not self.check_validation_token(): |
paul@1162 | 51 | return False |
paul@1162 | 52 | |
paul@446 | 53 | # Handle a submitted form. |
paul@446 | 54 | |
paul@446 | 55 | args = self.env.get_args() |
paul@446 | 56 | |
paul@778 | 57 | for key in args.keys(): |
paul@778 | 58 | if key.startswith("newevent-"): |
paul@778 | 59 | i = key[len("newevent-"):] |
paul@778 | 60 | break |
paul@778 | 61 | else: |
paul@873 | 62 | return False |
paul@446 | 63 | |
paul@446 | 64 | # Create a new event using the available information. |
paul@446 | 65 | |
paul@446 | 66 | slots = args.get("slot", []) |
paul@446 | 67 | participants = args.get("participants", []) |
paul@778 | 68 | summary = args.get("summary-%s" % i, [None])[0] |
paul@446 | 69 | |
paul@446 | 70 | if not slots: |
paul@873 | 71 | return False |
paul@446 | 72 | |
paul@446 | 73 | # Obtain the user's timezone. |
paul@446 | 74 | |
paul@446 | 75 | tzid = self.get_tzid() |
paul@446 | 76 | |
paul@446 | 77 | # Coalesce the selected slots. |
paul@446 | 78 | |
paul@446 | 79 | slots.sort() |
paul@446 | 80 | coalesced = [] |
paul@446 | 81 | last = None |
paul@446 | 82 | |
paul@446 | 83 | for slot in slots: |
paul@537 | 84 | start, end = (slot.split("-", 1) + [None])[:2] |
paul@446 | 85 | start = get_datetime(start, {"TZID" : tzid}) |
paul@446 | 86 | end = end and get_datetime(end, {"TZID" : tzid}) or get_start_of_next_day(start, tzid) |
paul@446 | 87 | |
paul@446 | 88 | if last: |
paul@446 | 89 | last_start, last_end = last |
paul@446 | 90 | |
paul@446 | 91 | # Merge adjacent dates and datetimes. |
paul@446 | 92 | |
paul@446 | 93 | if start == last_end or \ |
paul@446 | 94 | not isinstance(start, datetime) and \ |
paul@446 | 95 | get_start_of_day(last_end, tzid) == get_start_of_day(start, tzid): |
paul@446 | 96 | |
paul@446 | 97 | last = last_start, end |
paul@446 | 98 | continue |
paul@446 | 99 | |
paul@446 | 100 | # Handle datetimes within dates. |
paul@446 | 101 | # Datetime periods are within single days and are therefore |
paul@446 | 102 | # discarded. |
paul@446 | 103 | |
paul@446 | 104 | elif not isinstance(last_start, datetime) and \ |
paul@446 | 105 | get_start_of_day(start, tzid) == get_start_of_day(last_start, tzid): |
paul@446 | 106 | |
paul@446 | 107 | continue |
paul@446 | 108 | |
paul@446 | 109 | # Add separate dates and datetimes. |
paul@446 | 110 | |
paul@446 | 111 | else: |
paul@446 | 112 | coalesced.append(last) |
paul@446 | 113 | |
paul@446 | 114 | last = start, end |
paul@446 | 115 | |
paul@446 | 116 | if last: |
paul@446 | 117 | coalesced.append(last) |
paul@446 | 118 | |
paul@446 | 119 | # Invent a unique identifier. |
paul@446 | 120 | |
paul@446 | 121 | utcnow = get_timestamp() |
paul@446 | 122 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@446 | 123 | |
paul@446 | 124 | # Create a calendar object and store it as a request. |
paul@446 | 125 | |
paul@446 | 126 | record = [] |
paul@446 | 127 | rwrite = record.append |
paul@446 | 128 | |
paul@446 | 129 | # Define a single occurrence if only one coalesced slot exists. |
paul@446 | 130 | |
paul@446 | 131 | start, end = coalesced[0] |
paul@446 | 132 | start_value, start_attr = get_datetime_item(start, tzid) |
paul@446 | 133 | end_value, end_attr = get_datetime_item(end, tzid) |
paul@794 | 134 | user_attr = self.get_user_attributes() |
paul@446 | 135 | |
paul@446 | 136 | rwrite(("UID", {}, uid)) |
paul@1005 | 137 | rwrite(("SUMMARY", {}, summary or (_("New event at %s") % utcnow))) |
paul@446 | 138 | rwrite(("DTSTAMP", {}, utcnow)) |
paul@446 | 139 | rwrite(("DTSTART", start_attr, start_value)) |
paul@446 | 140 | rwrite(("DTEND", end_attr, end_value)) |
paul@794 | 141 | rwrite(("ORGANIZER", user_attr, self.user)) |
paul@794 | 142 | |
paul@794 | 143 | cn_participants = uri_parts(filter(None, participants)) |
paul@794 | 144 | participants = [] |
paul@446 | 145 | |
paul@794 | 146 | for cn, participant in cn_participants: |
paul@794 | 147 | d = {"RSVP" : "TRUE", "PARTSTAT" : "NEEDS-ACTION"} |
paul@794 | 148 | if cn: |
paul@794 | 149 | d["CN"] = cn |
paul@794 | 150 | rwrite(("ATTENDEE", d, participant)) |
paul@794 | 151 | participants.append(participant) |
paul@446 | 152 | |
paul@446 | 153 | if self.user not in participants: |
paul@794 | 154 | d = {"PARTSTAT" : "ACCEPTED"} |
paul@794 | 155 | d.update(user_attr) |
paul@794 | 156 | rwrite(("ATTENDEE", d, self.user)) |
paul@446 | 157 | |
paul@446 | 158 | # Define additional occurrences if many slots are defined. |
paul@446 | 159 | |
paul@446 | 160 | rdates = [] |
paul@446 | 161 | |
paul@446 | 162 | for start, end in coalesced[1:]: |
paul@446 | 163 | start_value, start_attr = get_datetime_item(start, tzid) |
paul@446 | 164 | end_value, end_attr = get_datetime_item(end, tzid) |
paul@446 | 165 | rdates.append("%s/%s" % (start_value, end_value)) |
paul@446 | 166 | |
paul@446 | 167 | if rdates: |
paul@446 | 168 | rwrite(("RDATE", {"VALUE" : "PERIOD", "TZID" : tzid}, rdates)) |
paul@446 | 169 | |
paul@446 | 170 | node = ("VEVENT", {}, record) |
paul@446 | 171 | |
paul@446 | 172 | self.store.set_event(self.user, uid, None, node=node) |
paul@446 | 173 | self.store.queue_request(self.user, uid) |
paul@446 | 174 | |
paul@446 | 175 | # Redirect to the object (or the first of the objects), where instead of |
paul@446 | 176 | # attendee controls, there will be organiser controls. |
paul@446 | 177 | |
paul@877 | 178 | self.redirect(self.link_to(uid, args=self.get_time_navigation_args())) |
paul@873 | 179 | return True |
paul@446 | 180 | |
paul@881 | 181 | def update_participants(self): |
paul@881 | 182 | |
paul@881 | 183 | "Update the participants used for scheduling purposes." |
paul@881 | 184 | |
paul@881 | 185 | args = self.env.get_args() |
paul@881 | 186 | participants = args.get("participants", []) |
paul@881 | 187 | |
paul@881 | 188 | try: |
paul@881 | 189 | for name, value in args.items(): |
paul@881 | 190 | if name.startswith("remove-participant-"): |
paul@881 | 191 | i = int(name[len("remove-participant-"):]) |
paul@881 | 192 | del participants[i] |
paul@881 | 193 | break |
paul@881 | 194 | except ValueError: |
paul@881 | 195 | pass |
paul@881 | 196 | |
paul@881 | 197 | # Trim empty participants. |
paul@881 | 198 | |
paul@881 | 199 | while participants and not participants[-1].strip(): |
paul@881 | 200 | participants.pop() |
paul@881 | 201 | |
paul@881 | 202 | return participants |
paul@881 | 203 | |
paul@446 | 204 | # Page fragment methods. |
paul@446 | 205 | |
paul@923 | 206 | def show_user_navigation(self): |
paul@923 | 207 | |
paul@923 | 208 | "Show user-specific navigation." |
paul@923 | 209 | |
paul@923 | 210 | page = self.page |
paul@923 | 211 | user_attr = self.get_user_attributes() |
paul@923 | 212 | |
paul@923 | 213 | page.p(id_="user-navigation") |
paul@923 | 214 | page.a(get_verbose_address(self.user, user_attr), href=self.link_to("profile"), class_="username") |
paul@923 | 215 | page.p.close() |
paul@923 | 216 | |
paul@446 | 217 | def show_requests_on_page(self): |
paul@446 | 218 | |
paul@446 | 219 | "Show requests for the current user." |
paul@446 | 220 | |
paul@1005 | 221 | _ = self.get_translator() |
paul@1005 | 222 | |
paul@446 | 223 | page = self.page |
paul@889 | 224 | view_period = self.get_view_period() |
paul@889 | 225 | duration = view_period and view_period.get_duration() or timedelta(1) |
paul@446 | 226 | |
paul@446 | 227 | # NOTE: This list could be more informative, but it is envisaged that |
paul@446 | 228 | # NOTE: the requests would be visited directly anyway. |
paul@446 | 229 | |
paul@446 | 230 | requests = self._get_requests() |
paul@446 | 231 | |
paul@446 | 232 | page.div(id="pending-requests") |
paul@446 | 233 | |
paul@446 | 234 | if requests: |
paul@1008 | 235 | page.p(_("Pending requests:")) |
paul@446 | 236 | |
paul@446 | 237 | page.ul() |
paul@446 | 238 | |
paul@751 | 239 | for uid, recurrenceid, request_type in requests: |
paul@751 | 240 | obj = self._get_object(uid, recurrenceid) |
paul@446 | 241 | if obj: |
paul@889 | 242 | |
paul@889 | 243 | # Provide a link showing the request in context. |
paul@889 | 244 | |
paul@889 | 245 | periods = self.get_periods(obj) |
paul@889 | 246 | if periods: |
paul@889 | 247 | start = to_date(periods[0].get_start()) |
paul@889 | 248 | end = max(to_date(periods[0].get_end()), start + duration) |
paul@889 | 249 | d = {"start" : format_datetime(start), "end" : format_datetime(end)} |
paul@889 | 250 | page.li() |
paul@889 | 251 | page.a(obj.get_value("SUMMARY"), href="%s#request-%s-%s" % (self.link_to(args=d), uid, recurrenceid or "")) |
paul@889 | 252 | page.li.close() |
paul@446 | 253 | |
paul@446 | 254 | page.ul.close() |
paul@446 | 255 | |
paul@446 | 256 | else: |
paul@1005 | 257 | page.p(_("There are no pending requests.")) |
paul@446 | 258 | |
paul@446 | 259 | page.div.close() |
paul@446 | 260 | |
paul@881 | 261 | def show_participants_on_page(self, participants): |
paul@446 | 262 | |
paul@446 | 263 | "Show participants for scheduling purposes." |
paul@446 | 264 | |
paul@1005 | 265 | _ = self.get_translator() |
paul@1005 | 266 | |
paul@446 | 267 | page = self.page |
paul@446 | 268 | |
paul@446 | 269 | # Show any specified participants together with controls to remove and |
paul@446 | 270 | # add participants. |
paul@446 | 271 | |
paul@446 | 272 | page.div(id="participants") |
paul@446 | 273 | |
paul@1005 | 274 | page.p(_("Participants for scheduling:")) |
paul@446 | 275 | |
paul@446 | 276 | for i, participant in enumerate(participants): |
paul@446 | 277 | page.p() |
paul@446 | 278 | page.input(name="participants", type="text", value=participant) |
paul@1005 | 279 | page.input(name="remove-participant-%d" % i, type="submit", value=_("Remove")) |
paul@446 | 280 | page.p.close() |
paul@446 | 281 | |
paul@446 | 282 | page.p() |
paul@446 | 283 | page.input(name="participants", type="text") |
paul@1005 | 284 | page.input(name="add-participant", type="submit", value=_("Add")) |
paul@446 | 285 | page.p.close() |
paul@446 | 286 | |
paul@446 | 287 | page.div.close() |
paul@446 | 288 | |
paul@881 | 289 | def show_calendar_controls(self): |
paul@881 | 290 | |
paul@881 | 291 | """ |
paul@881 | 292 | Show controls for hiding empty days and busy slots in the calendar. |
paul@881 | 293 | |
paul@881 | 294 | The positioning of the controls, paragraph and table are important here: |
paul@881 | 295 | the CSS file describes the relationship between them and the calendar |
paul@881 | 296 | tables. |
paul@881 | 297 | """ |
paul@881 | 298 | |
paul@1005 | 299 | _ = self.get_translator() |
paul@1005 | 300 | |
paul@881 | 301 | page = self.page |
paul@928 | 302 | args = self.env.get_args() |
paul@881 | 303 | |
paul@928 | 304 | self.control("showdays", "checkbox", "show", ("show" in args.get("showdays", [])), id="showdays", accesskey="D") |
paul@928 | 305 | self.control("hidebusy", "checkbox", "hide", ("hide" in args.get("hidebusy", [])), id="hidebusy", accesskey="B") |
paul@881 | 306 | |
paul@883 | 307 | page.p(id_="calendar-controls", class_="controls") |
paul@1005 | 308 | page.span(_("Select days or periods for a new event.")) |
paul@1005 | 309 | page.label(_("Hide busy time periods"), for_="hidebusy", class_="hidebusy enable") |
paul@1005 | 310 | page.label(_("Show busy time periods"), for_="hidebusy", class_="hidebusy disable") |
paul@1005 | 311 | page.label(_("Show empty days"), for_="showdays", class_="showdays disable") |
paul@1005 | 312 | page.label(_("Hide empty days"), for_="showdays", class_="showdays enable") |
paul@1005 | 313 | page.input(name="reset", type="submit", value=_("Clear selections"), id="reset") |
paul@881 | 314 | page.p.close() |
paul@446 | 315 | |
paul@926 | 316 | def show_time_navigation(self, freebusy, view_period): |
paul@876 | 317 | |
paul@876 | 318 | """ |
paul@926 | 319 | Show the calendar navigation links for the schedule defined by |
paul@926 | 320 | 'freebusy' and for the period defined by 'view_period'. |
paul@876 | 321 | """ |
paul@876 | 322 | |
paul@1005 | 323 | _ = self.get_translator() |
paul@1005 | 324 | |
paul@876 | 325 | page = self.page |
paul@889 | 326 | view_start = view_period.get_start() |
paul@889 | 327 | view_end = view_period.get_end() |
paul@889 | 328 | duration = view_period.get_duration() |
paul@876 | 329 | |
paul@1189 | 330 | preceding_events = view_start and freebusy.get_overlapping([Period(None, view_start, self.get_tzid())]) or [] |
paul@1189 | 331 | following_events = view_end and freebusy.get_overlapping([Period(view_end, None, self.get_tzid())]) or [] |
paul@926 | 332 | |
paul@926 | 333 | last_preceding = preceding_events and to_date(preceding_events[-1].get_end()) + timedelta(1) or None |
paul@926 | 334 | first_following = following_events and to_date(following_events[0].get_start()) or None |
paul@926 | 335 | |
paul@881 | 336 | page.p(id_="time-navigation") |
paul@876 | 337 | |
paul@876 | 338 | if view_start: |
paul@930 | 339 | page.input(name="start", type="hidden", value=format_datetime(view_start)) |
paul@930 | 340 | |
paul@926 | 341 | if last_preceding: |
paul@926 | 342 | preceding_start = last_preceding - duration |
paul@1005 | 343 | page.label(_("Show earlier events"), for_="earlier-events", class_="earlier-events") |
paul@926 | 344 | page.input(name="earlier-events", id_="earlier-events", type="submit") |
paul@926 | 345 | page.input(name="earlier-events-start", type="hidden", value=format_datetime(preceding_start)) |
paul@926 | 346 | page.input(name="earlier-events-end", type="hidden", value=format_datetime(last_preceding)) |
paul@926 | 347 | |
paul@889 | 348 | earlier_start = view_start - duration |
paul@1005 | 349 | page.label(_("Show earlier"), for_="earlier", class_="earlier") |
paul@876 | 350 | page.input(name="earlier", id_="earlier", type="submit") |
paul@876 | 351 | page.input(name="earlier-start", type="hidden", value=format_datetime(earlier_start)) |
paul@876 | 352 | page.input(name="earlier-end", type="hidden", value=format_datetime(view_start)) |
paul@926 | 353 | |
paul@876 | 354 | if view_end: |
paul@930 | 355 | page.input(name="end", type="hidden", value=format_datetime(view_end)) |
paul@926 | 356 | |
paul@889 | 357 | later_end = view_end + duration |
paul@1005 | 358 | page.label(_("Show later"), for_="later", class_="later") |
paul@876 | 359 | page.input(name="later", id_="later", type="submit") |
paul@876 | 360 | page.input(name="later-start", type="hidden", value=format_datetime(view_end)) |
paul@876 | 361 | page.input(name="later-end", type="hidden", value=format_datetime(later_end)) |
paul@930 | 362 | |
paul@930 | 363 | if first_following: |
paul@930 | 364 | following_end = first_following + duration |
paul@1005 | 365 | page.label(_("Show later events"), for_="later-events", class_="later-events") |
paul@930 | 366 | page.input(name="later-events", id_="later-events", type="submit") |
paul@930 | 367 | page.input(name="later-events-start", type="hidden", value=format_datetime(first_following)) |
paul@930 | 368 | page.input(name="later-events-end", type="hidden", value=format_datetime(following_end)) |
paul@876 | 369 | |
paul@876 | 370 | page.p.close() |
paul@876 | 371 | |
paul@876 | 372 | def get_time_navigation(self): |
paul@876 | 373 | |
paul@876 | 374 | "Return the start and end dates for the calendar view." |
paul@876 | 375 | |
paul@876 | 376 | for args in [self.env.get_args(), self.env.get_query()]: |
paul@876 | 377 | if args.has_key("earlier"): |
paul@876 | 378 | start_name, end_name = "earlier-start", "earlier-end" |
paul@876 | 379 | break |
paul@926 | 380 | elif args.has_key("earlier-events"): |
paul@926 | 381 | start_name, end_name = "earlier-events-start", "earlier-events-end" |
paul@926 | 382 | break |
paul@876 | 383 | elif args.has_key("later"): |
paul@876 | 384 | start_name, end_name = "later-start", "later-end" |
paul@876 | 385 | break |
paul@926 | 386 | elif args.has_key("later-events"): |
paul@926 | 387 | start_name, end_name = "later-events-start", "later-events-end" |
paul@926 | 388 | break |
paul@876 | 389 | elif args.has_key("start") or args.has_key("end"): |
paul@876 | 390 | start_name, end_name = "start", "end" |
paul@876 | 391 | break |
paul@876 | 392 | else: |
paul@876 | 393 | return None, None |
paul@876 | 394 | |
paul@876 | 395 | view_start = self.get_date_arg(args, start_name) |
paul@876 | 396 | view_end = self.get_date_arg(args, end_name) |
paul@876 | 397 | return view_start, view_end |
paul@876 | 398 | |
paul@877 | 399 | def get_time_navigation_args(self): |
paul@877 | 400 | |
paul@877 | 401 | "Return a dictionary containing start and/or end navigation details." |
paul@877 | 402 | |
paul@889 | 403 | view_period = self.get_view_period() |
paul@889 | 404 | view_start = view_period.get_start() |
paul@889 | 405 | view_end = view_period.get_end() |
paul@877 | 406 | link_args = {} |
paul@877 | 407 | if view_start: |
paul@877 | 408 | link_args["start"] = format_datetime(view_start) |
paul@877 | 409 | if view_end: |
paul@877 | 410 | link_args["end"] = format_datetime(view_end) |
paul@877 | 411 | return link_args |
paul@877 | 412 | |
paul@889 | 413 | def get_view_period(self): |
paul@889 | 414 | |
paul@889 | 415 | "Return the current view period." |
paul@889 | 416 | |
paul@889 | 417 | view_start, view_end = self.get_time_navigation() |
paul@889 | 418 | |
paul@889 | 419 | # Without any explicit limits, impose a reasonable view period. |
paul@889 | 420 | |
paul@889 | 421 | if not (view_start or view_end): |
paul@889 | 422 | view_start = get_date() |
paul@889 | 423 | view_end = get_date(timedelta(7)) |
paul@889 | 424 | |
paul@889 | 425 | return Period(view_start, view_end, self.get_tzid()) |
paul@889 | 426 | |
paul@913 | 427 | def show_view_period(self, view_period): |
paul@913 | 428 | |
paul@913 | 429 | "Show a description of the 'view_period'." |
paul@913 | 430 | |
paul@1005 | 431 | _ = self.get_translator() |
paul@1005 | 432 | |
paul@913 | 433 | page = self.page |
paul@913 | 434 | |
paul@913 | 435 | view_start = view_period.get_start() |
paul@913 | 436 | view_end = view_period.get_end() |
paul@913 | 437 | |
paul@913 | 438 | if not (view_start or view_end): |
paul@913 | 439 | return |
paul@913 | 440 | |
paul@913 | 441 | page.p(class_="view-period") |
paul@913 | 442 | |
paul@913 | 443 | if view_start and view_end: |
paul@1005 | 444 | page.add(_("Showing events from %(start)s until %(end)s") % { |
paul@1005 | 445 | "start" : self.format_date(view_start, "full"), |
paul@1005 | 446 | "end" : self.format_date(view_end, "full")}) |
paul@913 | 447 | elif view_start: |
paul@1005 | 448 | page.add(_("Showing events from %s") % self.format_date(view_start, "full")) |
paul@913 | 449 | elif view_end: |
paul@1005 | 450 | page.add(_("Showing events until %s") % self.format_date(view_end, "full")) |
paul@913 | 451 | |
paul@913 | 452 | page.p.close() |
paul@913 | 453 | |
paul@883 | 454 | def get_period_group_details(self, freebusy, participants, view_period): |
paul@873 | 455 | |
paul@883 | 456 | """ |
paul@883 | 457 | Return details of periods in the given 'freebusy' collection and for the |
paul@883 | 458 | collections of the given 'participants'. |
paul@883 | 459 | """ |
paul@446 | 460 | |
paul@1005 | 461 | _ = self.get_translator() |
paul@1005 | 462 | |
paul@446 | 463 | # Obtain the user's timezone. |
paul@446 | 464 | |
paul@446 | 465 | tzid = self.get_tzid() |
paul@446 | 466 | |
paul@446 | 467 | # Requests are listed and linked to their tentative positions in the |
paul@446 | 468 | # calendar. Other participants are also shown. |
paul@446 | 469 | |
paul@446 | 470 | request_summary = self._get_request_summary() |
paul@446 | 471 | |
paul@446 | 472 | period_groups = [request_summary, freebusy] |
paul@446 | 473 | period_group_types = ["request", "freebusy"] |
paul@1005 | 474 | period_group_sources = [_("Pending requests"), _("Your schedule")] |
paul@446 | 475 | |
paul@446 | 476 | for i, participant in enumerate(participants): |
paul@446 | 477 | period_groups.append(self.store.get_freebusy_for_other(self.user, get_uri(participant))) |
paul@446 | 478 | period_group_types.append("freebusy-part%d" % i) |
paul@446 | 479 | period_group_sources.append(participant) |
paul@446 | 480 | |
paul@446 | 481 | groups = [] |
paul@446 | 482 | group_columns = [] |
paul@446 | 483 | group_types = period_group_types |
paul@446 | 484 | group_sources = period_group_sources |
paul@446 | 485 | all_points = set() |
paul@446 | 486 | |
paul@446 | 487 | # Obtain time point information for each group of periods. |
paul@446 | 488 | |
paul@446 | 489 | for periods in period_groups: |
paul@446 | 490 | |
paul@874 | 491 | # Filter periods outside the given view. |
paul@874 | 492 | |
paul@874 | 493 | if view_period: |
paul@1189 | 494 | periods = periods.get_overlapping([view_period]) |
paul@874 | 495 | |
paul@446 | 496 | # Get the time scale with start and end points. |
paul@446 | 497 | |
paul@884 | 498 | scale = get_scale(periods, tzid, view_period) |
paul@446 | 499 | |
paul@446 | 500 | # Get the time slots for the periods. |
paul@456 | 501 | # Time slots are collections of Point objects with lists of active |
paul@456 | 502 | # periods. |
paul@446 | 503 | |
paul@446 | 504 | slots = get_slots(scale) |
paul@446 | 505 | |
paul@446 | 506 | # Add start of day time points for multi-day periods. |
paul@446 | 507 | |
paul@446 | 508 | add_day_start_points(slots, tzid) |
paul@446 | 509 | |
paul@931 | 510 | # Remove the slot at the end of a view. |
paul@931 | 511 | |
paul@931 | 512 | if view_period: |
paul@931 | 513 | remove_end_slot(slots, view_period) |
paul@931 | 514 | |
paul@446 | 515 | # Record the slots and all time points employed. |
paul@446 | 516 | |
paul@446 | 517 | groups.append(slots) |
paul@456 | 518 | all_points.update([point for point, active in slots]) |
paul@446 | 519 | |
paul@446 | 520 | # Partition the groups into days. |
paul@446 | 521 | |
paul@446 | 522 | days = {} |
paul@446 | 523 | partitioned_groups = [] |
paul@446 | 524 | partitioned_group_types = [] |
paul@446 | 525 | partitioned_group_sources = [] |
paul@446 | 526 | |
paul@446 | 527 | for slots, group_type, group_source in zip(groups, group_types, group_sources): |
paul@446 | 528 | |
paul@446 | 529 | # Propagate time points to all groups of time slots. |
paul@446 | 530 | |
paul@446 | 531 | add_slots(slots, all_points) |
paul@446 | 532 | |
paul@446 | 533 | # Count the number of columns employed by the group. |
paul@446 | 534 | |
paul@446 | 535 | columns = 0 |
paul@446 | 536 | |
paul@446 | 537 | # Partition the time slots by day. |
paul@446 | 538 | |
paul@446 | 539 | partitioned = {} |
paul@446 | 540 | |
paul@446 | 541 | for day, day_slots in partition_by_day(slots).items(): |
paul@446 | 542 | |
paul@446 | 543 | # Construct a list of time intervals within the day. |
paul@446 | 544 | |
paul@446 | 545 | intervals = [] |
paul@449 | 546 | |
paul@449 | 547 | # Convert each partition to a mapping from points to active |
paul@449 | 548 | # periods. |
paul@449 | 549 | |
paul@449 | 550 | partitioned[day] = day_points = {} |
paul@449 | 551 | |
paul@446 | 552 | last = None |
paul@446 | 553 | |
paul@455 | 554 | for point, active in day_slots: |
paul@446 | 555 | columns = max(columns, len(active)) |
paul@455 | 556 | day_points[point] = active |
paul@451 | 557 | |
paul@446 | 558 | if last: |
paul@446 | 559 | intervals.append((last, point)) |
paul@449 | 560 | |
paul@455 | 561 | last = point |
paul@446 | 562 | |
paul@446 | 563 | if last: |
paul@446 | 564 | intervals.append((last, None)) |
paul@446 | 565 | |
paul@446 | 566 | if not days.has_key(day): |
paul@446 | 567 | days[day] = set() |
paul@446 | 568 | |
paul@446 | 569 | # Record the divisions or intervals within each day. |
paul@446 | 570 | |
paul@446 | 571 | days[day].update(intervals) |
paul@446 | 572 | |
paul@446 | 573 | # Only include the requests column if it provides objects. |
paul@446 | 574 | |
paul@446 | 575 | if group_type != "request" or columns: |
paul@869 | 576 | if group_type != "request": |
paul@869 | 577 | columns += 1 |
paul@446 | 578 | group_columns.append(columns) |
paul@446 | 579 | partitioned_groups.append(partitioned) |
paul@446 | 580 | partitioned_group_types.append(group_type) |
paul@446 | 581 | partitioned_group_sources.append(group_source) |
paul@446 | 582 | |
paul@883 | 583 | return days, partitioned_groups, partitioned_group_types, partitioned_group_sources, group_columns |
paul@883 | 584 | |
paul@883 | 585 | # Full page output methods. |
paul@883 | 586 | |
paul@883 | 587 | def show(self): |
paul@883 | 588 | |
paul@883 | 589 | "Show the calendar for the current user." |
paul@883 | 590 | |
paul@1005 | 591 | _ = self.get_translator() |
paul@1005 | 592 | |
paul@1005 | 593 | self.new_page(title=_("Calendar")) |
paul@883 | 594 | page = self.page |
paul@883 | 595 | |
paul@883 | 596 | if self.handle_newevent(): |
paul@883 | 597 | return |
paul@883 | 598 | |
paul@883 | 599 | freebusy = self.store.get_freebusy(self.user) |
paul@883 | 600 | participants = self.update_participants() |
paul@883 | 601 | |
paul@883 | 602 | # Form controls are used in various places on the calendar page. |
paul@883 | 603 | |
paul@883 | 604 | page.form(method="POST") |
paul@1162 | 605 | self.validator() |
paul@923 | 606 | self.show_user_navigation() |
paul@883 | 607 | self.show_requests_on_page() |
paul@883 | 608 | self.show_participants_on_page(participants) |
paul@883 | 609 | |
paul@926 | 610 | # Get the view period and details of events within it and outside it. |
paul@926 | 611 | |
paul@926 | 612 | view_period = self.get_view_period() |
paul@926 | 613 | |
paul@883 | 614 | # Day view: start at the earliest known day and produce days until the |
paul@883 | 615 | # latest known day, with expandable sections of empty days. |
paul@883 | 616 | |
paul@883 | 617 | (days, partitioned_groups, partitioned_group_types, partitioned_group_sources, group_columns) = \ |
paul@883 | 618 | self.get_period_group_details(freebusy, participants, view_period) |
paul@883 | 619 | |
paul@446 | 620 | # Add empty days. |
paul@446 | 621 | |
paul@889 | 622 | add_empty_days(days, self.get_tzid(), view_period.get_start(), view_period.get_end()) |
paul@513 | 623 | |
paul@881 | 624 | # Show controls to change the calendar appearance. |
paul@513 | 625 | |
paul@913 | 626 | self.show_view_period(view_period) |
paul@881 | 627 | self.show_calendar_controls() |
paul@926 | 628 | self.show_time_navigation(freebusy, view_period) |
paul@446 | 629 | |
paul@446 | 630 | # Show the calendar itself. |
paul@446 | 631 | |
paul@772 | 632 | self.show_calendar_days(days, partitioned_groups, partitioned_group_types, partitioned_group_sources, group_columns) |
paul@446 | 633 | |
paul@446 | 634 | # End the form region. |
paul@446 | 635 | |
paul@446 | 636 | page.form.close() |
paul@446 | 637 | |
paul@446 | 638 | # More page fragment methods. |
paul@446 | 639 | |
paul@773 | 640 | def show_calendar_day_controls(self, day): |
paul@446 | 641 | |
paul@773 | 642 | "Show controls for the given 'day' in the calendar." |
paul@446 | 643 | |
paul@446 | 644 | page = self.page |
paul@773 | 645 | daystr, dayid = self._day_value_and_identifier(day) |
paul@446 | 646 | |
paul@446 | 647 | # Generate a dynamic stylesheet to allow day selections to colour |
paul@446 | 648 | # specific days. |
paul@446 | 649 | # NOTE: The style details need to be coordinated with the static |
paul@446 | 650 | # NOTE: stylesheet. |
paul@446 | 651 | |
paul@446 | 652 | page.style(type="text/css") |
paul@446 | 653 | |
paul@773 | 654 | page.add("""\ |
paul@773 | 655 | input.newevent.selector#%s:checked ~ table#region-%s label.day, |
paul@773 | 656 | input.newevent.selector#%s:checked ~ table#region-%s label.timepoint { |
paul@773 | 657 | background-color: #5f4; |
paul@773 | 658 | text-decoration: underline; |
paul@773 | 659 | } |
paul@773 | 660 | """ % (dayid, dayid, dayid, dayid)) |
paul@773 | 661 | |
paul@773 | 662 | page.style.close() |
paul@773 | 663 | |
paul@773 | 664 | # Generate controls to select days. |
paul@773 | 665 | |
paul@773 | 666 | slots = self.env.get_args().get("slot", []) |
paul@773 | 667 | value, identifier = self._day_value_and_identifier(day) |
paul@773 | 668 | self._slot_selector(value, identifier, slots) |
paul@773 | 669 | |
paul@773 | 670 | def show_calendar_interval_controls(self, day, intervals): |
paul@773 | 671 | |
paul@773 | 672 | "Show controls for the intervals provided by 'day' and 'intervals'." |
paul@773 | 673 | |
paul@773 | 674 | page = self.page |
paul@773 | 675 | daystr, dayid = self._day_value_and_identifier(day) |
paul@773 | 676 | |
paul@773 | 677 | # Generate a dynamic stylesheet to allow day selections to colour |
paul@773 | 678 | # specific days. |
paul@773 | 679 | # NOTE: The style details need to be coordinated with the static |
paul@773 | 680 | # NOTE: stylesheet. |
paul@773 | 681 | |
paul@513 | 682 | l = [] |
paul@513 | 683 | |
paul@773 | 684 | for point, endpoint in intervals: |
paul@773 | 685 | timestr, timeid = self._slot_value_and_identifier(point, endpoint) |
paul@513 | 686 | l.append("""\ |
paul@773 | 687 | input.newevent.selector#%s:checked ~ table#region-%s th#region-%s""" % (timeid, dayid, timeid)) |
paul@773 | 688 | |
paul@773 | 689 | page.style(type="text/css") |
paul@513 | 690 | |
paul@513 | 691 | page.add(",\n".join(l)) |
paul@513 | 692 | page.add(""" { |
paul@446 | 693 | background-color: #5f4; |
paul@446 | 694 | text-decoration: underline; |
paul@446 | 695 | } |
paul@513 | 696 | """) |
paul@513 | 697 | |
paul@513 | 698 | page.style.close() |
paul@513 | 699 | |
paul@773 | 700 | # Generate controls to select time periods. |
paul@513 | 701 | |
paul@773 | 702 | slots = self.env.get_args().get("slot", []) |
paul@774 | 703 | last = None |
paul@774 | 704 | |
paul@774 | 705 | # Produce controls for the intervals/slots. Where instants in time are |
paul@774 | 706 | # encountered, they are merged with the following slots, permitting the |
paul@774 | 707 | # selection of contiguous time periods. However, the identifiers |
paul@774 | 708 | # employed by controls corresponding to merged periods will encode the |
paul@774 | 709 | # instant so that labels may reference them conveniently. |
paul@774 | 710 | |
paul@774 | 711 | intervals = list(intervals) |
paul@774 | 712 | intervals.sort() |
paul@774 | 713 | |
paul@773 | 714 | for point, endpoint in intervals: |
paul@774 | 715 | |
paul@774 | 716 | # Merge any previous slot with this one, producing a control. |
paul@774 | 717 | |
paul@774 | 718 | if last: |
paul@774 | 719 | _value, identifier = self._slot_value_and_identifier(last, last) |
paul@774 | 720 | value, _identifier = self._slot_value_and_identifier(last, endpoint) |
paul@774 | 721 | self._slot_selector(value, identifier, slots) |
paul@774 | 722 | |
paul@774 | 723 | # If representing an instant, hold the slot for merging. |
paul@774 | 724 | |
paul@774 | 725 | if endpoint and point.point == endpoint.point: |
paul@774 | 726 | last = point |
paul@774 | 727 | |
paul@774 | 728 | # If not representing an instant, produce a control. |
paul@774 | 729 | |
paul@774 | 730 | else: |
paul@774 | 731 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@774 | 732 | self._slot_selector(value, identifier, slots) |
paul@774 | 733 | last = None |
paul@774 | 734 | |
paul@774 | 735 | # Produce a control for any unmerged slot. |
paul@774 | 736 | |
paul@774 | 737 | if last: |
paul@774 | 738 | _value, identifier = self._slot_value_and_identifier(last, last) |
paul@774 | 739 | value, _identifier = self._slot_value_and_identifier(last, endpoint) |
paul@773 | 740 | self._slot_selector(value, identifier, slots) |
paul@446 | 741 | |
paul@446 | 742 | def show_calendar_participant_headings(self, group_types, group_sources, group_columns): |
paul@446 | 743 | |
paul@446 | 744 | """ |
paul@446 | 745 | Show headings for the participants and other scheduling contributors, |
paul@446 | 746 | defined by 'group_types', 'group_sources' and 'group_columns'. |
paul@446 | 747 | """ |
paul@446 | 748 | |
paul@446 | 749 | page = self.page |
paul@446 | 750 | |
paul@446 | 751 | page.colgroup(span=1, id="columns-timeslot") |
paul@446 | 752 | |
paul@995 | 753 | # Make column groups at least two cells wide. |
paul@995 | 754 | |
paul@446 | 755 | for group_type, columns in zip(group_types, group_columns): |
paul@929 | 756 | page.colgroup(span=max(columns, 2), id="columns-%s" % group_type) |
paul@446 | 757 | |
paul@446 | 758 | page.thead() |
paul@446 | 759 | page.tr() |
paul@446 | 760 | page.th("", class_="emptyheading") |
paul@446 | 761 | |
paul@446 | 762 | for group_type, source, columns in zip(group_types, group_sources, group_columns): |
paul@446 | 763 | page.th(source, |
paul@446 | 764 | class_=(group_type == "request" and "requestheading" or "participantheading"), |
paul@929 | 765 | colspan=max(columns, 2)) |
paul@446 | 766 | |
paul@446 | 767 | page.tr.close() |
paul@446 | 768 | page.thead.close() |
paul@446 | 769 | |
paul@772 | 770 | def show_calendar_days(self, days, partitioned_groups, partitioned_group_types, |
paul@772 | 771 | partitioned_group_sources, group_columns): |
paul@446 | 772 | |
paul@446 | 773 | """ |
paul@446 | 774 | Show calendar days, defined by a collection of 'days', the contributing |
paul@446 | 775 | period information as 'partitioned_groups' (partitioned by day), the |
paul@446 | 776 | 'partitioned_group_types' indicating the kind of contribution involved, |
paul@772 | 777 | the 'partitioned_group_sources' indicating the origin of each group, and |
paul@772 | 778 | the 'group_columns' defining the number of columns in each group. |
paul@446 | 779 | """ |
paul@446 | 780 | |
paul@1005 | 781 | _ = self.get_translator() |
paul@1005 | 782 | |
paul@446 | 783 | page = self.page |
paul@446 | 784 | |
paul@446 | 785 | # Determine the number of columns required. Where participants provide |
paul@446 | 786 | # no columns for events, one still needs to be provided for the |
paul@446 | 787 | # participant itself. |
paul@446 | 788 | |
paul@446 | 789 | all_columns = sum([max(columns, 1) for columns in group_columns]) |
paul@446 | 790 | |
paul@446 | 791 | # Determine the days providing time slots. |
paul@446 | 792 | |
paul@446 | 793 | all_days = days.items() |
paul@446 | 794 | all_days.sort() |
paul@446 | 795 | |
paul@446 | 796 | # Produce a heading and time points for each day. |
paul@446 | 797 | |
paul@778 | 798 | i = 0 |
paul@778 | 799 | |
paul@446 | 800 | for day, intervals in all_days: |
paul@446 | 801 | groups_for_day = [partitioned.get(day) for partitioned in partitioned_groups] |
paul@446 | 802 | is_empty = True |
paul@446 | 803 | |
paul@446 | 804 | for slots in groups_for_day: |
paul@446 | 805 | if not slots: |
paul@446 | 806 | continue |
paul@446 | 807 | |
paul@446 | 808 | for active in slots.values(): |
paul@446 | 809 | if active: |
paul@446 | 810 | is_empty = False |
paul@446 | 811 | break |
paul@446 | 812 | |
paul@768 | 813 | daystr, dayid = self._day_value_and_identifier(day) |
paul@768 | 814 | |
paul@773 | 815 | # Put calendar tables within elements for quicker CSS selection. |
paul@773 | 816 | |
paul@773 | 817 | page.div(class_="calendar") |
paul@773 | 818 | |
paul@773 | 819 | # Show the controls permitting day selection as well as the controls |
paul@773 | 820 | # configuring the new event display. |
paul@773 | 821 | |
paul@773 | 822 | self.show_calendar_day_controls(day) |
paul@773 | 823 | self.show_calendar_interval_controls(day, intervals) |
paul@773 | 824 | |
paul@773 | 825 | # Show an actual table containing the day information. |
paul@773 | 826 | |
paul@772 | 827 | page.table(cellspacing=5, cellpadding=5, class_="calendar %s" % (is_empty and " empty" or ""), id="region-%s" % dayid) |
paul@772 | 828 | |
paul@772 | 829 | page.caption(class_="dayheading container separator") |
paul@446 | 830 | self._day_heading(day) |
paul@772 | 831 | page.caption.close() |
paul@446 | 832 | |
paul@772 | 833 | self.show_calendar_participant_headings(partitioned_group_types, partitioned_group_sources, group_columns) |
paul@772 | 834 | |
paul@772 | 835 | page.tbody(class_="points") |
paul@446 | 836 | self.show_calendar_points(intervals, groups_for_day, partitioned_group_types, group_columns) |
paul@446 | 837 | page.tbody.close() |
paul@446 | 838 | |
paul@772 | 839 | page.table.close() |
paul@772 | 840 | |
paul@773 | 841 | # Show a button for scheduling a new event. |
paul@773 | 842 | |
paul@773 | 843 | page.p(class_="newevent-with-periods") |
paul@1005 | 844 | page.label(_("Summary:")) |
paul@778 | 845 | page.input(name="summary-%d" % i, type="text") |
paul@1005 | 846 | page.input(name="newevent-%d" % i, type="submit", value=_("New event"), accesskey="N") |
paul@773 | 847 | page.p.close() |
paul@773 | 848 | |
paul@876 | 849 | page.p(class_="newevent-with-periods") |
paul@1005 | 850 | page.label(_("Clear selections"), for_="reset", class_="reset") |
paul@876 | 851 | page.p.close() |
paul@876 | 852 | |
paul@773 | 853 | page.div.close() |
paul@773 | 854 | |
paul@778 | 855 | i += 1 |
paul@778 | 856 | |
paul@446 | 857 | def show_calendar_points(self, intervals, groups, group_types, group_columns): |
paul@446 | 858 | |
paul@446 | 859 | """ |
paul@446 | 860 | Show the time 'intervals' along with period information from the given |
paul@446 | 861 | 'groups', having the indicated 'group_types', each with the number of |
paul@446 | 862 | columns given by 'group_columns'. |
paul@446 | 863 | """ |
paul@446 | 864 | |
paul@1005 | 865 | _ = self.get_translator() |
paul@1005 | 866 | |
paul@446 | 867 | page = self.page |
paul@446 | 868 | |
paul@446 | 869 | # Obtain the user's timezone. |
paul@446 | 870 | |
paul@446 | 871 | tzid = self.get_tzid() |
paul@446 | 872 | |
paul@877 | 873 | # Get view information for links. |
paul@877 | 874 | |
paul@877 | 875 | link_args = self.get_time_navigation_args() |
paul@877 | 876 | |
paul@446 | 877 | # Produce a row for each interval. |
paul@446 | 878 | |
paul@446 | 879 | intervals = list(intervals) |
paul@446 | 880 | intervals.sort() |
paul@446 | 881 | |
paul@455 | 882 | for point, endpoint in intervals: |
paul@455 | 883 | continuation = point.point == get_start_of_day(point.point, tzid) |
paul@446 | 884 | |
paul@446 | 885 | # Some rows contain no period details and are marked as such. |
paul@446 | 886 | |
paul@448 | 887 | have_active = False |
paul@448 | 888 | have_active_request = False |
paul@448 | 889 | |
paul@448 | 890 | for slots, group_type in zip(groups, group_types): |
paul@455 | 891 | if slots and slots.get(point): |
paul@448 | 892 | if group_type == "request": |
paul@448 | 893 | have_active_request = True |
paul@448 | 894 | else: |
paul@448 | 895 | have_active = True |
paul@446 | 896 | |
paul@450 | 897 | # Emit properties of the time interval, where post-instant intervals |
paul@450 | 898 | # are also treated as busy. |
paul@450 | 899 | |
paul@446 | 900 | css = " ".join([ |
paul@446 | 901 | "slot", |
paul@455 | 902 | (have_active or point.indicator == Point.REPEATED) and "busy" or \ |
paul@455 | 903 | have_active_request and "suggested" or "empty", |
paul@446 | 904 | continuation and "daystart" or "" |
paul@446 | 905 | ]) |
paul@446 | 906 | |
paul@446 | 907 | page.tr(class_=css) |
paul@774 | 908 | |
paul@774 | 909 | # Produce a time interval heading, spanning two rows if this point |
paul@774 | 910 | # represents an instant. |
paul@774 | 911 | |
paul@455 | 912 | if point.indicator == Point.PRINCIPAL: |
paul@768 | 913 | timestr, timeid = self._slot_value_and_identifier(point, endpoint) |
paul@774 | 914 | page.th(class_="timeslot", id="region-%s" % timeid, |
paul@774 | 915 | rowspan=(endpoint and point.point == endpoint.point and 2 or 1)) |
paul@449 | 916 | self._time_point(point, endpoint) |
paul@774 | 917 | page.th.close() |
paul@446 | 918 | |
paul@446 | 919 | # Obtain slots for the time point from each group. |
paul@446 | 920 | |
paul@446 | 921 | for columns, slots, group_type in zip(group_columns, groups, group_types): |
paul@995 | 922 | |
paul@995 | 923 | # Make column groups at least two cells wide. |
paul@995 | 924 | |
paul@995 | 925 | columns = max(columns, 2) |
paul@455 | 926 | active = slots and slots.get(point) |
paul@446 | 927 | |
paul@446 | 928 | # Where no periods exist for the given time interval, generate |
paul@446 | 929 | # an empty cell. Where a participant provides no periods at all, |
paul@869 | 930 | # one column is provided; otherwise, one more column than the |
paul@869 | 931 | # number required is provided. |
paul@446 | 932 | |
paul@446 | 933 | if not active: |
paul@929 | 934 | self._empty_slot(point, endpoint, max(columns, 2)) |
paul@446 | 935 | continue |
paul@446 | 936 | |
paul@446 | 937 | slots = slots.items() |
paul@446 | 938 | slots.sort() |
paul@446 | 939 | spans = get_spans(slots) |
paul@446 | 940 | |
paul@446 | 941 | empty = 0 |
paul@446 | 942 | |
paul@446 | 943 | # Show a column for each active period. |
paul@446 | 944 | |
paul@458 | 945 | for p in active: |
paul@458 | 946 | |
paul@458 | 947 | # The period can be None, meaning an empty column. |
paul@458 | 948 | |
paul@458 | 949 | if p: |
paul@446 | 950 | |
paul@446 | 951 | # Flush empty slots preceding this one. |
paul@446 | 952 | |
paul@446 | 953 | if empty: |
paul@455 | 954 | self._empty_slot(point, endpoint, empty) |
paul@446 | 955 | empty = 0 |
paul@446 | 956 | |
paul@458 | 957 | key = p.get_key() |
paul@446 | 958 | span = spans[key] |
paul@446 | 959 | |
paul@446 | 960 | # Produce a table cell only at the start of the period |
paul@446 | 961 | # or when continued at the start of a day. |
paul@453 | 962 | # Points defining the ends of instant events should |
paul@453 | 963 | # never define the start of new events. |
paul@446 | 964 | |
paul@546 | 965 | if point.indicator == Point.PRINCIPAL and (point.point == p.get_start() or continuation): |
paul@446 | 966 | |
paul@546 | 967 | has_continued = continuation and point.point != p.get_start() |
paul@546 | 968 | will_continue = not ends_on_same_day(point.point, p.get_end(), tzid) |
paul@458 | 969 | is_organiser = p.organiser == self.user |
paul@446 | 970 | |
paul@446 | 971 | css = " ".join([ |
paul@446 | 972 | "event", |
paul@446 | 973 | has_continued and "continued" or "", |
paul@446 | 974 | will_continue and "continues" or "", |
paul@763 | 975 | p.transp == "ORG" and "only-organising" or is_organiser and "organising" or "attending", |
paul@763 | 976 | self._have_request(p.uid, p.recurrenceid, "COUNTER", True) and "counter" or "", |
paul@446 | 977 | ]) |
paul@446 | 978 | |
paul@446 | 979 | # Only anchor the first cell of events. |
paul@446 | 980 | # Need to only anchor the first period for a recurring |
paul@446 | 981 | # event. |
paul@446 | 982 | |
paul@458 | 983 | html_id = "%s-%s-%s" % (group_type, p.uid, p.recurrenceid or "") |
paul@446 | 984 | |
paul@546 | 985 | if point.point == p.get_start() and html_id not in self.html_ids: |
paul@446 | 986 | page.td(class_=css, rowspan=span, id=html_id) |
paul@446 | 987 | self.html_ids.add(html_id) |
paul@446 | 988 | else: |
paul@446 | 989 | page.td(class_=css, rowspan=span) |
paul@446 | 990 | |
paul@755 | 991 | # Only link to events if they are not being updated |
paul@755 | 992 | # by requests. |
paul@755 | 993 | |
paul@755 | 994 | if not p.summary or \ |
paul@755 | 995 | group_type != "request" and self._have_request(p.uid, p.recurrenceid, None, True): |
paul@755 | 996 | |
paul@1005 | 997 | page.span(p.summary or _("(Participant is busy)")) |
paul@446 | 998 | |
paul@755 | 999 | # Link to requests and events (including ones for |
paul@755 | 1000 | # which counter-proposals exist). |
paul@755 | 1001 | |
paul@777 | 1002 | elif group_type == "request" and self._have_request(p.uid, p.recurrenceid, "COUNTER", True): |
paul@877 | 1003 | d = {"counter" : self._period_identifier(p)} |
paul@877 | 1004 | d.update(link_args) |
paul@877 | 1005 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid, d)) |
paul@777 | 1006 | |
paul@446 | 1007 | else: |
paul@877 | 1008 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid, link_args)) |
paul@446 | 1009 | |
paul@446 | 1010 | page.td.close() |
paul@446 | 1011 | else: |
paul@446 | 1012 | empty += 1 |
paul@446 | 1013 | |
paul@446 | 1014 | # Pad with empty columns. |
paul@446 | 1015 | |
paul@446 | 1016 | empty = columns - len(active) |
paul@446 | 1017 | |
paul@446 | 1018 | if empty: |
paul@904 | 1019 | self._empty_slot(point, endpoint, empty, True) |
paul@446 | 1020 | |
paul@446 | 1021 | page.tr.close() |
paul@446 | 1022 | |
paul@446 | 1023 | def _day_heading(self, day): |
paul@446 | 1024 | |
paul@446 | 1025 | """ |
paul@446 | 1026 | Generate a heading for 'day' of the following form: |
paul@446 | 1027 | |
paul@768 | 1028 | <label class="day" for="day-20150203">Tuesday, 3 February 2015</label> |
paul@446 | 1029 | """ |
paul@446 | 1030 | |
paul@446 | 1031 | page = self.page |
paul@446 | 1032 | value, identifier = self._day_value_and_identifier(day) |
paul@768 | 1033 | page.label(self.format_date(day, "full"), class_="day", for_=identifier) |
paul@446 | 1034 | |
paul@446 | 1035 | def _time_point(self, point, endpoint): |
paul@446 | 1036 | |
paul@446 | 1037 | """ |
paul@446 | 1038 | Generate headings for the 'point' to 'endpoint' period of the following |
paul@446 | 1039 | form: |
paul@446 | 1040 | |
paul@768 | 1041 | <label class="timepoint" for="slot-20150203T090000-20150203T100000">09:00:00 CET</label> |
paul@446 | 1042 | <span class="endpoint">10:00:00 CET</span> |
paul@446 | 1043 | """ |
paul@446 | 1044 | |
paul@446 | 1045 | page = self.page |
paul@446 | 1046 | tzid = self.get_tzid() |
paul@446 | 1047 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@768 | 1048 | page.label(self.format_time(point.point, "long"), class_="timepoint", for_=identifier) |
paul@455 | 1049 | page.span(self.format_time(endpoint and endpoint.point or get_end_of_day(point.point, tzid), "long"), class_="endpoint") |
paul@446 | 1050 | |
paul@446 | 1051 | def _slot_selector(self, value, identifier, slots): |
paul@446 | 1052 | |
paul@446 | 1053 | """ |
paul@446 | 1054 | Provide a timeslot control having the given 'value', employing the |
paul@446 | 1055 | indicated HTML 'identifier', and using the given 'slots' collection |
paul@446 | 1056 | to select any control whose 'value' is in this collection, unless the |
paul@446 | 1057 | "reset" request parameter has been asserted. |
paul@446 | 1058 | """ |
paul@446 | 1059 | |
paul@446 | 1060 | reset = self.env.get_args().has_key("reset") |
paul@446 | 1061 | page = self.page |
paul@446 | 1062 | if not reset and value in slots: |
paul@446 | 1063 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector", checked="checked") |
paul@446 | 1064 | else: |
paul@446 | 1065 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector") |
paul@446 | 1066 | |
paul@904 | 1067 | def _empty_slot(self, point, endpoint, colspan, at_end=False): |
paul@446 | 1068 | |
paul@453 | 1069 | """ |
paul@453 | 1070 | Show an empty slot cell for the given 'point' and 'endpoint', with the |
paul@455 | 1071 | given 'colspan' configuring the cell's appearance. |
paul@453 | 1072 | """ |
paul@446 | 1073 | |
paul@1005 | 1074 | _ = self.get_translator() |
paul@1005 | 1075 | |
paul@446 | 1076 | page = self.page |
paul@904 | 1077 | page.td(class_="empty%s%s" % (point.indicator == Point.PRINCIPAL and " container" or "", at_end and " padding" or ""), colspan=colspan) |
paul@455 | 1078 | if point.indicator == Point.PRINCIPAL: |
paul@453 | 1079 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@1005 | 1080 | page.label(_("Select/deselect period"), class_="newevent popup", for_=identifier) |
paul@453 | 1081 | page.td.close() |
paul@446 | 1082 | |
paul@446 | 1083 | def _day_value_and_identifier(self, day): |
paul@446 | 1084 | |
paul@446 | 1085 | "Return a day value and HTML identifier for the given 'day'." |
paul@446 | 1086 | |
paul@513 | 1087 | value = format_datetime(day) |
paul@446 | 1088 | identifier = "day-%s" % value |
paul@446 | 1089 | return value, identifier |
paul@446 | 1090 | |
paul@446 | 1091 | def _slot_value_and_identifier(self, point, endpoint): |
paul@446 | 1092 | |
paul@446 | 1093 | """ |
paul@446 | 1094 | Return a slot value and HTML identifier for the given 'point' and |
paul@446 | 1095 | 'endpoint'. |
paul@446 | 1096 | """ |
paul@446 | 1097 | |
paul@455 | 1098 | value = "%s-%s" % (format_datetime(point.point), endpoint and format_datetime(endpoint.point) or "") |
paul@446 | 1099 | identifier = "slot-%s" % value |
paul@446 | 1100 | return value, identifier |
paul@446 | 1101 | |
paul@777 | 1102 | def _period_identifier(self, period): |
paul@777 | 1103 | return "%s-%s" % (format_datetime(period.get_start()), format_datetime(period.get_end())) |
paul@777 | 1104 | |
paul@874 | 1105 | def get_date_arg(self, args, name): |
paul@874 | 1106 | values = args.get(name) |
paul@874 | 1107 | if not values: |
paul@874 | 1108 | return None |
paul@874 | 1109 | return get_datetime(values[0], {"VALUE-TYPE" : "DATE"}) |
paul@874 | 1110 | |
paul@446 | 1111 | # vim: tabstop=4 expandtab shiftwidth=4 |