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