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@794 | 23 | from imiptools.data import get_address, get_uri, uri_parts |
paul@446 | 24 | from imiptools.dates import format_datetime, 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@446 | 27 | 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@874 | 31 | Period, Point |
paul@756 | 32 | from imipweb.resource import ResourceClient |
paul@446 | 33 | |
paul@756 | 34 | class CalendarPage(ResourceClient): |
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@446 | 172 | self.redirect(self.link_to(uid)) |
paul@873 | 173 | return True |
paul@446 | 174 | |
paul@446 | 175 | # Page fragment methods. |
paul@446 | 176 | |
paul@446 | 177 | def show_requests_on_page(self): |
paul@446 | 178 | |
paul@446 | 179 | "Show requests for the current user." |
paul@446 | 180 | |
paul@446 | 181 | page = self.page |
paul@446 | 182 | |
paul@446 | 183 | # NOTE: This list could be more informative, but it is envisaged that |
paul@446 | 184 | # NOTE: the requests would be visited directly anyway. |
paul@446 | 185 | |
paul@446 | 186 | requests = self._get_requests() |
paul@446 | 187 | |
paul@446 | 188 | page.div(id="pending-requests") |
paul@446 | 189 | |
paul@446 | 190 | if requests: |
paul@446 | 191 | page.p("Pending requests:") |
paul@446 | 192 | |
paul@446 | 193 | page.ul() |
paul@446 | 194 | |
paul@751 | 195 | for uid, recurrenceid, request_type in requests: |
paul@751 | 196 | obj = self._get_object(uid, recurrenceid) |
paul@446 | 197 | if obj: |
paul@446 | 198 | page.li() |
paul@446 | 199 | page.a(obj.get_value("SUMMARY"), href="#request-%s-%s" % (uid, recurrenceid or "")) |
paul@446 | 200 | page.li.close() |
paul@446 | 201 | |
paul@446 | 202 | page.ul.close() |
paul@446 | 203 | |
paul@446 | 204 | else: |
paul@446 | 205 | page.p("There are no pending requests.") |
paul@446 | 206 | |
paul@446 | 207 | page.div.close() |
paul@446 | 208 | |
paul@446 | 209 | def show_participants_on_page(self): |
paul@446 | 210 | |
paul@446 | 211 | "Show participants for scheduling purposes." |
paul@446 | 212 | |
paul@446 | 213 | page = self.page |
paul@446 | 214 | args = self.env.get_args() |
paul@446 | 215 | participants = args.get("participants", []) |
paul@446 | 216 | |
paul@446 | 217 | try: |
paul@446 | 218 | for name, value in args.items(): |
paul@446 | 219 | if name.startswith("remove-participant-"): |
paul@446 | 220 | i = int(name[len("remove-participant-"):]) |
paul@446 | 221 | del participants[i] |
paul@446 | 222 | break |
paul@446 | 223 | except ValueError: |
paul@446 | 224 | pass |
paul@446 | 225 | |
paul@446 | 226 | # Trim empty participants. |
paul@446 | 227 | |
paul@446 | 228 | while participants and not participants[-1].strip(): |
paul@446 | 229 | participants.pop() |
paul@446 | 230 | |
paul@446 | 231 | # Show any specified participants together with controls to remove and |
paul@446 | 232 | # add participants. |
paul@446 | 233 | |
paul@446 | 234 | page.div(id="participants") |
paul@446 | 235 | |
paul@446 | 236 | page.p("Participants for scheduling:") |
paul@446 | 237 | |
paul@446 | 238 | for i, participant in enumerate(participants): |
paul@446 | 239 | page.p() |
paul@446 | 240 | page.input(name="participants", type="text", value=participant) |
paul@446 | 241 | page.input(name="remove-participant-%d" % i, type="submit", value="Remove") |
paul@446 | 242 | page.p.close() |
paul@446 | 243 | |
paul@446 | 244 | page.p() |
paul@446 | 245 | page.input(name="participants", type="text") |
paul@446 | 246 | page.input(name="add-participant", type="submit", value="Add") |
paul@446 | 247 | page.p.close() |
paul@446 | 248 | |
paul@446 | 249 | page.div.close() |
paul@446 | 250 | |
paul@446 | 251 | return participants |
paul@446 | 252 | |
paul@876 | 253 | def show_time_navigation(self, view_start, view_end, view_period=None): |
paul@876 | 254 | |
paul@876 | 255 | """ |
paul@876 | 256 | Show the calendar navigation links for the period defined by |
paul@876 | 257 | 'view_start' and 'view_end'. |
paul@876 | 258 | """ |
paul@876 | 259 | |
paul@876 | 260 | page = self.page |
paul@876 | 261 | view_period = view_period or timedelta(7) |
paul@876 | 262 | |
paul@876 | 263 | page.p() |
paul@876 | 264 | |
paul@876 | 265 | if view_start: |
paul@876 | 266 | if view_end: |
paul@876 | 267 | earlier_start = view_start - (view_end - view_start) |
paul@876 | 268 | else: |
paul@876 | 269 | earlier_start = view_start - view_period |
paul@876 | 270 | page.label("Show earlier events", for_="earlier", class_="earlier") |
paul@876 | 271 | page.input(name="earlier", id_="earlier", type="submit") |
paul@876 | 272 | page.input(name="earlier-start", type="hidden", value=format_datetime(earlier_start)) |
paul@876 | 273 | page.input(name="earlier-end", type="hidden", value=format_datetime(view_start)) |
paul@876 | 274 | page.input(name="start", type="hidden", value=format_datetime(view_start)) |
paul@876 | 275 | |
paul@876 | 276 | if view_end: |
paul@876 | 277 | if view_start: |
paul@876 | 278 | later_end = view_end + (view_end - view_start) |
paul@876 | 279 | else: |
paul@876 | 280 | later_end = view_end + view_period |
paul@876 | 281 | page.label("Show later events", for_="later", class_="later") |
paul@876 | 282 | page.input(name="later", id_="later", type="submit") |
paul@876 | 283 | page.input(name="later-start", type="hidden", value=format_datetime(view_end)) |
paul@876 | 284 | page.input(name="later-end", type="hidden", value=format_datetime(later_end)) |
paul@876 | 285 | page.input(name="end", type="hidden", value=format_datetime(view_end)) |
paul@876 | 286 | |
paul@876 | 287 | page.p.close() |
paul@876 | 288 | |
paul@876 | 289 | def get_time_navigation(self): |
paul@876 | 290 | |
paul@876 | 291 | "Return the start and end dates for the calendar view." |
paul@876 | 292 | |
paul@876 | 293 | for args in [self.env.get_args(), self.env.get_query()]: |
paul@876 | 294 | if args.has_key("earlier"): |
paul@876 | 295 | start_name, end_name = "earlier-start", "earlier-end" |
paul@876 | 296 | break |
paul@876 | 297 | elif args.has_key("later"): |
paul@876 | 298 | start_name, end_name = "later-start", "later-end" |
paul@876 | 299 | break |
paul@876 | 300 | elif args.has_key("start") or args.has_key("end"): |
paul@876 | 301 | start_name, end_name = "start", "end" |
paul@876 | 302 | break |
paul@876 | 303 | else: |
paul@876 | 304 | return None, None |
paul@876 | 305 | |
paul@876 | 306 | view_start = self.get_date_arg(args, start_name) |
paul@876 | 307 | view_end = self.get_date_arg(args, end_name) |
paul@876 | 308 | return view_start, view_end |
paul@876 | 309 | |
paul@446 | 310 | # Full page output methods. |
paul@446 | 311 | |
paul@446 | 312 | def show(self): |
paul@446 | 313 | |
paul@446 | 314 | "Show the calendar for the current user." |
paul@446 | 315 | |
paul@446 | 316 | self.new_page(title="Calendar") |
paul@446 | 317 | page = self.page |
paul@446 | 318 | |
paul@873 | 319 | if self.handle_newevent(): |
paul@873 | 320 | return |
paul@873 | 321 | |
paul@513 | 322 | freebusy = self.store.get_freebusy(self.user) |
paul@513 | 323 | |
paul@513 | 324 | if not freebusy: |
paul@513 | 325 | page.p("No events scheduled.") |
paul@513 | 326 | return |
paul@513 | 327 | |
paul@446 | 328 | # Form controls are used in various places on the calendar page. |
paul@446 | 329 | |
paul@446 | 330 | page.form(method="POST") |
paul@446 | 331 | |
paul@446 | 332 | self.show_requests_on_page() |
paul@446 | 333 | participants = self.show_participants_on_page() |
paul@446 | 334 | |
paul@446 | 335 | # Obtain the user's timezone. |
paul@446 | 336 | |
paul@446 | 337 | tzid = self.get_tzid() |
paul@446 | 338 | |
paul@446 | 339 | # Day view: start at the earliest known day and produce days until the |
paul@874 | 340 | # latest known day, with expandable sections of empty days. |
paul@874 | 341 | |
paul@876 | 342 | view_start, view_end = self.get_time_navigation() |
paul@874 | 343 | view_period = (view_start or view_end) and Period(view_start, view_end, self.get_tzid()) |
paul@446 | 344 | |
paul@876 | 345 | self.show_time_navigation(view_start, view_end) |
paul@876 | 346 | |
paul@446 | 347 | # Requests are listed and linked to their tentative positions in the |
paul@446 | 348 | # calendar. Other participants are also shown. |
paul@446 | 349 | |
paul@446 | 350 | request_summary = self._get_request_summary() |
paul@446 | 351 | |
paul@446 | 352 | period_groups = [request_summary, freebusy] |
paul@446 | 353 | period_group_types = ["request", "freebusy"] |
paul@446 | 354 | period_group_sources = ["Pending requests", "Your schedule"] |
paul@446 | 355 | |
paul@446 | 356 | for i, participant in enumerate(participants): |
paul@446 | 357 | period_groups.append(self.store.get_freebusy_for_other(self.user, get_uri(participant))) |
paul@446 | 358 | period_group_types.append("freebusy-part%d" % i) |
paul@446 | 359 | period_group_sources.append(participant) |
paul@446 | 360 | |
paul@446 | 361 | groups = [] |
paul@446 | 362 | group_columns = [] |
paul@446 | 363 | group_types = period_group_types |
paul@446 | 364 | group_sources = period_group_sources |
paul@446 | 365 | all_points = set() |
paul@446 | 366 | |
paul@446 | 367 | # Obtain time point information for each group of periods. |
paul@446 | 368 | |
paul@446 | 369 | for periods in period_groups: |
paul@446 | 370 | |
paul@874 | 371 | # Filter periods outside the given view. |
paul@874 | 372 | |
paul@874 | 373 | if view_period: |
paul@874 | 374 | periods = get_overlapping(periods, view_period) |
paul@874 | 375 | |
paul@446 | 376 | # Get the time scale with start and end points. |
paul@446 | 377 | |
paul@529 | 378 | scale = get_scale(periods, tzid) |
paul@446 | 379 | |
paul@446 | 380 | # Get the time slots for the periods. |
paul@456 | 381 | # Time slots are collections of Point objects with lists of active |
paul@456 | 382 | # periods. |
paul@446 | 383 | |
paul@446 | 384 | slots = get_slots(scale) |
paul@446 | 385 | |
paul@446 | 386 | # Add start of day time points for multi-day periods. |
paul@446 | 387 | |
paul@446 | 388 | add_day_start_points(slots, tzid) |
paul@446 | 389 | |
paul@446 | 390 | # Record the slots and all time points employed. |
paul@446 | 391 | |
paul@446 | 392 | groups.append(slots) |
paul@456 | 393 | all_points.update([point for point, active in slots]) |
paul@446 | 394 | |
paul@446 | 395 | # Partition the groups into days. |
paul@446 | 396 | |
paul@446 | 397 | days = {} |
paul@446 | 398 | partitioned_groups = [] |
paul@446 | 399 | partitioned_group_types = [] |
paul@446 | 400 | partitioned_group_sources = [] |
paul@446 | 401 | |
paul@446 | 402 | for slots, group_type, group_source in zip(groups, group_types, group_sources): |
paul@446 | 403 | |
paul@446 | 404 | # Propagate time points to all groups of time slots. |
paul@446 | 405 | |
paul@446 | 406 | add_slots(slots, all_points) |
paul@446 | 407 | |
paul@446 | 408 | # Count the number of columns employed by the group. |
paul@446 | 409 | |
paul@446 | 410 | columns = 0 |
paul@446 | 411 | |
paul@446 | 412 | # Partition the time slots by day. |
paul@446 | 413 | |
paul@446 | 414 | partitioned = {} |
paul@446 | 415 | |
paul@446 | 416 | for day, day_slots in partition_by_day(slots).items(): |
paul@446 | 417 | |
paul@446 | 418 | # Construct a list of time intervals within the day. |
paul@446 | 419 | |
paul@446 | 420 | intervals = [] |
paul@449 | 421 | |
paul@449 | 422 | # Convert each partition to a mapping from points to active |
paul@449 | 423 | # periods. |
paul@449 | 424 | |
paul@449 | 425 | partitioned[day] = day_points = {} |
paul@449 | 426 | |
paul@446 | 427 | last = None |
paul@446 | 428 | |
paul@455 | 429 | for point, active in day_slots: |
paul@446 | 430 | columns = max(columns, len(active)) |
paul@455 | 431 | day_points[point] = active |
paul@451 | 432 | |
paul@446 | 433 | if last: |
paul@446 | 434 | intervals.append((last, point)) |
paul@449 | 435 | |
paul@455 | 436 | last = point |
paul@446 | 437 | |
paul@446 | 438 | if last: |
paul@446 | 439 | intervals.append((last, None)) |
paul@446 | 440 | |
paul@446 | 441 | if not days.has_key(day): |
paul@446 | 442 | days[day] = set() |
paul@446 | 443 | |
paul@446 | 444 | # Record the divisions or intervals within each day. |
paul@446 | 445 | |
paul@446 | 446 | days[day].update(intervals) |
paul@446 | 447 | |
paul@446 | 448 | # Only include the requests column if it provides objects. |
paul@446 | 449 | |
paul@446 | 450 | if group_type != "request" or columns: |
paul@869 | 451 | if group_type != "request": |
paul@869 | 452 | columns += 1 |
paul@446 | 453 | group_columns.append(columns) |
paul@446 | 454 | partitioned_groups.append(partitioned) |
paul@446 | 455 | partitioned_group_types.append(group_type) |
paul@446 | 456 | partitioned_group_sources.append(group_source) |
paul@446 | 457 | |
paul@446 | 458 | # Add empty days. |
paul@446 | 459 | |
paul@876 | 460 | add_empty_days(days, tzid, view_start, view_end) |
paul@513 | 461 | |
paul@513 | 462 | # Show controls for hiding empty days and busy slots. |
paul@513 | 463 | # The positioning of the control, paragraph and table are important here. |
paul@513 | 464 | |
paul@513 | 465 | page.input(name="showdays", type="checkbox", value="show", id="showdays", accesskey="D") |
paul@513 | 466 | page.input(name="hidebusy", type="checkbox", value="hide", id="hidebusy", accesskey="B") |
paul@513 | 467 | |
paul@513 | 468 | page.p(class_="controls") |
paul@876 | 469 | page.span("Select days or periods for a new event.") |
paul@513 | 470 | page.label("Hide busy time periods", for_="hidebusy", class_="hidebusy enable") |
paul@513 | 471 | page.label("Show busy time periods", for_="hidebusy", class_="hidebusy disable") |
paul@513 | 472 | page.label("Show empty days", for_="showdays", class_="showdays disable") |
paul@513 | 473 | page.label("Hide empty days", for_="showdays", class_="showdays enable") |
paul@513 | 474 | page.input(name="reset", type="submit", value="Clear selections", id="reset") |
paul@513 | 475 | page.p.close() |
paul@446 | 476 | |
paul@446 | 477 | # Show the calendar itself. |
paul@446 | 478 | |
paul@772 | 479 | self.show_calendar_days(days, partitioned_groups, partitioned_group_types, partitioned_group_sources, group_columns) |
paul@446 | 480 | |
paul@446 | 481 | # End the form region. |
paul@446 | 482 | |
paul@446 | 483 | page.form.close() |
paul@446 | 484 | |
paul@446 | 485 | # More page fragment methods. |
paul@446 | 486 | |
paul@773 | 487 | def show_calendar_day_controls(self, day): |
paul@446 | 488 | |
paul@773 | 489 | "Show controls for the given 'day' in the calendar." |
paul@446 | 490 | |
paul@446 | 491 | page = self.page |
paul@773 | 492 | daystr, dayid = self._day_value_and_identifier(day) |
paul@446 | 493 | |
paul@446 | 494 | # Generate a dynamic stylesheet to allow day selections to colour |
paul@446 | 495 | # specific days. |
paul@446 | 496 | # NOTE: The style details need to be coordinated with the static |
paul@446 | 497 | # NOTE: stylesheet. |
paul@446 | 498 | |
paul@446 | 499 | page.style(type="text/css") |
paul@446 | 500 | |
paul@773 | 501 | page.add("""\ |
paul@773 | 502 | input.newevent.selector#%s:checked ~ table#region-%s label.day, |
paul@773 | 503 | input.newevent.selector#%s:checked ~ table#region-%s label.timepoint { |
paul@773 | 504 | background-color: #5f4; |
paul@773 | 505 | text-decoration: underline; |
paul@773 | 506 | } |
paul@773 | 507 | """ % (dayid, dayid, dayid, dayid)) |
paul@773 | 508 | |
paul@773 | 509 | page.style.close() |
paul@773 | 510 | |
paul@773 | 511 | # Generate controls to select days. |
paul@773 | 512 | |
paul@773 | 513 | slots = self.env.get_args().get("slot", []) |
paul@773 | 514 | value, identifier = self._day_value_and_identifier(day) |
paul@773 | 515 | self._slot_selector(value, identifier, slots) |
paul@773 | 516 | |
paul@773 | 517 | def show_calendar_interval_controls(self, day, intervals): |
paul@773 | 518 | |
paul@773 | 519 | "Show controls for the intervals provided by 'day' and 'intervals'." |
paul@773 | 520 | |
paul@773 | 521 | page = self.page |
paul@773 | 522 | daystr, dayid = self._day_value_and_identifier(day) |
paul@773 | 523 | |
paul@773 | 524 | # Generate a dynamic stylesheet to allow day selections to colour |
paul@773 | 525 | # specific days. |
paul@773 | 526 | # NOTE: The style details need to be coordinated with the static |
paul@773 | 527 | # NOTE: stylesheet. |
paul@773 | 528 | |
paul@513 | 529 | l = [] |
paul@513 | 530 | |
paul@773 | 531 | for point, endpoint in intervals: |
paul@773 | 532 | timestr, timeid = self._slot_value_and_identifier(point, endpoint) |
paul@513 | 533 | l.append("""\ |
paul@773 | 534 | input.newevent.selector#%s:checked ~ table#region-%s th#region-%s""" % (timeid, dayid, timeid)) |
paul@773 | 535 | |
paul@773 | 536 | page.style(type="text/css") |
paul@513 | 537 | |
paul@513 | 538 | page.add(",\n".join(l)) |
paul@513 | 539 | page.add(""" { |
paul@446 | 540 | background-color: #5f4; |
paul@446 | 541 | text-decoration: underline; |
paul@446 | 542 | } |
paul@513 | 543 | """) |
paul@513 | 544 | |
paul@513 | 545 | page.style.close() |
paul@513 | 546 | |
paul@773 | 547 | # Generate controls to select time periods. |
paul@513 | 548 | |
paul@773 | 549 | slots = self.env.get_args().get("slot", []) |
paul@774 | 550 | last = None |
paul@774 | 551 | |
paul@774 | 552 | # Produce controls for the intervals/slots. Where instants in time are |
paul@774 | 553 | # encountered, they are merged with the following slots, permitting the |
paul@774 | 554 | # selection of contiguous time periods. However, the identifiers |
paul@774 | 555 | # employed by controls corresponding to merged periods will encode the |
paul@774 | 556 | # instant so that labels may reference them conveniently. |
paul@774 | 557 | |
paul@774 | 558 | intervals = list(intervals) |
paul@774 | 559 | intervals.sort() |
paul@774 | 560 | |
paul@773 | 561 | for point, endpoint in intervals: |
paul@774 | 562 | |
paul@774 | 563 | # Merge any previous slot with this one, producing a control. |
paul@774 | 564 | |
paul@774 | 565 | if last: |
paul@774 | 566 | _value, identifier = self._slot_value_and_identifier(last, last) |
paul@774 | 567 | value, _identifier = self._slot_value_and_identifier(last, endpoint) |
paul@774 | 568 | self._slot_selector(value, identifier, slots) |
paul@774 | 569 | |
paul@774 | 570 | # If representing an instant, hold the slot for merging. |
paul@774 | 571 | |
paul@774 | 572 | if endpoint and point.point == endpoint.point: |
paul@774 | 573 | last = point |
paul@774 | 574 | |
paul@774 | 575 | # If not representing an instant, produce a control. |
paul@774 | 576 | |
paul@774 | 577 | else: |
paul@774 | 578 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@774 | 579 | self._slot_selector(value, identifier, slots) |
paul@774 | 580 | last = None |
paul@774 | 581 | |
paul@774 | 582 | # Produce a control for any unmerged slot. |
paul@774 | 583 | |
paul@774 | 584 | if last: |
paul@774 | 585 | _value, identifier = self._slot_value_and_identifier(last, last) |
paul@774 | 586 | value, _identifier = self._slot_value_and_identifier(last, endpoint) |
paul@773 | 587 | self._slot_selector(value, identifier, slots) |
paul@446 | 588 | |
paul@446 | 589 | def show_calendar_participant_headings(self, group_types, group_sources, group_columns): |
paul@446 | 590 | |
paul@446 | 591 | """ |
paul@446 | 592 | Show headings for the participants and other scheduling contributors, |
paul@446 | 593 | defined by 'group_types', 'group_sources' and 'group_columns'. |
paul@446 | 594 | """ |
paul@446 | 595 | |
paul@446 | 596 | page = self.page |
paul@446 | 597 | |
paul@446 | 598 | page.colgroup(span=1, id="columns-timeslot") |
paul@446 | 599 | |
paul@446 | 600 | for group_type, columns in zip(group_types, group_columns): |
paul@446 | 601 | page.colgroup(span=max(columns, 1), id="columns-%s" % group_type) |
paul@446 | 602 | |
paul@446 | 603 | page.thead() |
paul@446 | 604 | page.tr() |
paul@446 | 605 | page.th("", class_="emptyheading") |
paul@446 | 606 | |
paul@446 | 607 | for group_type, source, columns in zip(group_types, group_sources, group_columns): |
paul@446 | 608 | page.th(source, |
paul@446 | 609 | class_=(group_type == "request" and "requestheading" or "participantheading"), |
paul@446 | 610 | colspan=max(columns, 1)) |
paul@446 | 611 | |
paul@446 | 612 | page.tr.close() |
paul@446 | 613 | page.thead.close() |
paul@446 | 614 | |
paul@772 | 615 | def show_calendar_days(self, days, partitioned_groups, partitioned_group_types, |
paul@772 | 616 | partitioned_group_sources, group_columns): |
paul@446 | 617 | |
paul@446 | 618 | """ |
paul@446 | 619 | Show calendar days, defined by a collection of 'days', the contributing |
paul@446 | 620 | period information as 'partitioned_groups' (partitioned by day), the |
paul@446 | 621 | 'partitioned_group_types' indicating the kind of contribution involved, |
paul@772 | 622 | the 'partitioned_group_sources' indicating the origin of each group, and |
paul@772 | 623 | the 'group_columns' defining the number of columns in each group. |
paul@446 | 624 | """ |
paul@446 | 625 | |
paul@446 | 626 | page = self.page |
paul@446 | 627 | |
paul@446 | 628 | # Determine the number of columns required. Where participants provide |
paul@446 | 629 | # no columns for events, one still needs to be provided for the |
paul@446 | 630 | # participant itself. |
paul@446 | 631 | |
paul@446 | 632 | all_columns = sum([max(columns, 1) for columns in group_columns]) |
paul@446 | 633 | |
paul@446 | 634 | # Determine the days providing time slots. |
paul@446 | 635 | |
paul@446 | 636 | all_days = days.items() |
paul@446 | 637 | all_days.sort() |
paul@446 | 638 | |
paul@446 | 639 | # Produce a heading and time points for each day. |
paul@446 | 640 | |
paul@778 | 641 | i = 0 |
paul@778 | 642 | |
paul@446 | 643 | for day, intervals in all_days: |
paul@446 | 644 | groups_for_day = [partitioned.get(day) for partitioned in partitioned_groups] |
paul@446 | 645 | is_empty = True |
paul@446 | 646 | |
paul@446 | 647 | for slots in groups_for_day: |
paul@446 | 648 | if not slots: |
paul@446 | 649 | continue |
paul@446 | 650 | |
paul@446 | 651 | for active in slots.values(): |
paul@446 | 652 | if active: |
paul@446 | 653 | is_empty = False |
paul@446 | 654 | break |
paul@446 | 655 | |
paul@768 | 656 | daystr, dayid = self._day_value_and_identifier(day) |
paul@768 | 657 | |
paul@773 | 658 | # Put calendar tables within elements for quicker CSS selection. |
paul@773 | 659 | |
paul@773 | 660 | page.div(class_="calendar") |
paul@773 | 661 | |
paul@773 | 662 | # Show the controls permitting day selection as well as the controls |
paul@773 | 663 | # configuring the new event display. |
paul@773 | 664 | |
paul@773 | 665 | self.show_calendar_day_controls(day) |
paul@773 | 666 | self.show_calendar_interval_controls(day, intervals) |
paul@773 | 667 | |
paul@773 | 668 | # Show an actual table containing the day information. |
paul@773 | 669 | |
paul@772 | 670 | page.table(cellspacing=5, cellpadding=5, class_="calendar %s" % (is_empty and " empty" or ""), id="region-%s" % dayid) |
paul@772 | 671 | |
paul@772 | 672 | page.caption(class_="dayheading container separator") |
paul@446 | 673 | self._day_heading(day) |
paul@772 | 674 | page.caption.close() |
paul@446 | 675 | |
paul@772 | 676 | self.show_calendar_participant_headings(partitioned_group_types, partitioned_group_sources, group_columns) |
paul@772 | 677 | |
paul@772 | 678 | page.tbody(class_="points") |
paul@446 | 679 | self.show_calendar_points(intervals, groups_for_day, partitioned_group_types, group_columns) |
paul@446 | 680 | page.tbody.close() |
paul@446 | 681 | |
paul@772 | 682 | page.table.close() |
paul@772 | 683 | |
paul@773 | 684 | # Show a button for scheduling a new event. |
paul@773 | 685 | |
paul@773 | 686 | page.p(class_="newevent-with-periods") |
paul@773 | 687 | page.label("Summary:") |
paul@778 | 688 | page.input(name="summary-%d" % i, type="text") |
paul@778 | 689 | page.input(name="newevent-%d" % i, type="submit", value="New event", accesskey="N") |
paul@773 | 690 | page.p.close() |
paul@773 | 691 | |
paul@876 | 692 | page.p(class_="newevent-with-periods") |
paul@876 | 693 | page.label("Clear selections", for_="reset", class_="reset") |
paul@876 | 694 | page.p.close() |
paul@876 | 695 | |
paul@773 | 696 | page.div.close() |
paul@773 | 697 | |
paul@778 | 698 | i += 1 |
paul@778 | 699 | |
paul@446 | 700 | def show_calendar_points(self, intervals, groups, group_types, group_columns): |
paul@446 | 701 | |
paul@446 | 702 | """ |
paul@446 | 703 | Show the time 'intervals' along with period information from the given |
paul@446 | 704 | 'groups', having the indicated 'group_types', each with the number of |
paul@446 | 705 | columns given by 'group_columns'. |
paul@446 | 706 | """ |
paul@446 | 707 | |
paul@446 | 708 | page = self.page |
paul@446 | 709 | |
paul@446 | 710 | # Obtain the user's timezone. |
paul@446 | 711 | |
paul@446 | 712 | tzid = self.get_tzid() |
paul@446 | 713 | |
paul@446 | 714 | # Produce a row for each interval. |
paul@446 | 715 | |
paul@446 | 716 | intervals = list(intervals) |
paul@446 | 717 | intervals.sort() |
paul@446 | 718 | |
paul@455 | 719 | for point, endpoint in intervals: |
paul@455 | 720 | continuation = point.point == get_start_of_day(point.point, tzid) |
paul@446 | 721 | |
paul@446 | 722 | # Some rows contain no period details and are marked as such. |
paul@446 | 723 | |
paul@448 | 724 | have_active = False |
paul@448 | 725 | have_active_request = False |
paul@448 | 726 | |
paul@448 | 727 | for slots, group_type in zip(groups, group_types): |
paul@455 | 728 | if slots and slots.get(point): |
paul@448 | 729 | if group_type == "request": |
paul@448 | 730 | have_active_request = True |
paul@448 | 731 | else: |
paul@448 | 732 | have_active = True |
paul@446 | 733 | |
paul@450 | 734 | # Emit properties of the time interval, where post-instant intervals |
paul@450 | 735 | # are also treated as busy. |
paul@450 | 736 | |
paul@446 | 737 | css = " ".join([ |
paul@446 | 738 | "slot", |
paul@455 | 739 | (have_active or point.indicator == Point.REPEATED) and "busy" or \ |
paul@455 | 740 | have_active_request and "suggested" or "empty", |
paul@446 | 741 | continuation and "daystart" or "" |
paul@446 | 742 | ]) |
paul@446 | 743 | |
paul@446 | 744 | page.tr(class_=css) |
paul@774 | 745 | |
paul@774 | 746 | # Produce a time interval heading, spanning two rows if this point |
paul@774 | 747 | # represents an instant. |
paul@774 | 748 | |
paul@455 | 749 | if point.indicator == Point.PRINCIPAL: |
paul@768 | 750 | timestr, timeid = self._slot_value_and_identifier(point, endpoint) |
paul@774 | 751 | page.th(class_="timeslot", id="region-%s" % timeid, |
paul@774 | 752 | rowspan=(endpoint and point.point == endpoint.point and 2 or 1)) |
paul@449 | 753 | self._time_point(point, endpoint) |
paul@774 | 754 | page.th.close() |
paul@446 | 755 | |
paul@446 | 756 | # Obtain slots for the time point from each group. |
paul@446 | 757 | |
paul@446 | 758 | for columns, slots, group_type in zip(group_columns, groups, group_types): |
paul@455 | 759 | active = slots and slots.get(point) |
paul@446 | 760 | |
paul@446 | 761 | # Where no periods exist for the given time interval, generate |
paul@446 | 762 | # an empty cell. Where a participant provides no periods at all, |
paul@869 | 763 | # one column is provided; otherwise, one more column than the |
paul@869 | 764 | # number required is provided. |
paul@446 | 765 | |
paul@446 | 766 | if not active: |
paul@455 | 767 | self._empty_slot(point, endpoint, max(columns, 1)) |
paul@446 | 768 | continue |
paul@446 | 769 | |
paul@446 | 770 | slots = slots.items() |
paul@446 | 771 | slots.sort() |
paul@446 | 772 | spans = get_spans(slots) |
paul@446 | 773 | |
paul@446 | 774 | empty = 0 |
paul@446 | 775 | |
paul@446 | 776 | # Show a column for each active period. |
paul@446 | 777 | |
paul@458 | 778 | for p in active: |
paul@458 | 779 | |
paul@458 | 780 | # The period can be None, meaning an empty column. |
paul@458 | 781 | |
paul@458 | 782 | if p: |
paul@446 | 783 | |
paul@446 | 784 | # Flush empty slots preceding this one. |
paul@446 | 785 | |
paul@446 | 786 | if empty: |
paul@455 | 787 | self._empty_slot(point, endpoint, empty) |
paul@446 | 788 | empty = 0 |
paul@446 | 789 | |
paul@458 | 790 | key = p.get_key() |
paul@446 | 791 | span = spans[key] |
paul@446 | 792 | |
paul@446 | 793 | # Produce a table cell only at the start of the period |
paul@446 | 794 | # or when continued at the start of a day. |
paul@453 | 795 | # Points defining the ends of instant events should |
paul@453 | 796 | # never define the start of new events. |
paul@446 | 797 | |
paul@546 | 798 | if point.indicator == Point.PRINCIPAL and (point.point == p.get_start() or continuation): |
paul@446 | 799 | |
paul@546 | 800 | has_continued = continuation and point.point != p.get_start() |
paul@546 | 801 | will_continue = not ends_on_same_day(point.point, p.get_end(), tzid) |
paul@458 | 802 | is_organiser = p.organiser == self.user |
paul@446 | 803 | |
paul@446 | 804 | css = " ".join([ |
paul@446 | 805 | "event", |
paul@446 | 806 | has_continued and "continued" or "", |
paul@446 | 807 | will_continue and "continues" or "", |
paul@763 | 808 | p.transp == "ORG" and "only-organising" or is_organiser and "organising" or "attending", |
paul@763 | 809 | self._have_request(p.uid, p.recurrenceid, "COUNTER", True) and "counter" or "", |
paul@446 | 810 | ]) |
paul@446 | 811 | |
paul@446 | 812 | # Only anchor the first cell of events. |
paul@446 | 813 | # Need to only anchor the first period for a recurring |
paul@446 | 814 | # event. |
paul@446 | 815 | |
paul@458 | 816 | html_id = "%s-%s-%s" % (group_type, p.uid, p.recurrenceid or "") |
paul@446 | 817 | |
paul@546 | 818 | if point.point == p.get_start() and html_id not in self.html_ids: |
paul@446 | 819 | page.td(class_=css, rowspan=span, id=html_id) |
paul@446 | 820 | self.html_ids.add(html_id) |
paul@446 | 821 | else: |
paul@446 | 822 | page.td(class_=css, rowspan=span) |
paul@446 | 823 | |
paul@755 | 824 | # Only link to events if they are not being updated |
paul@755 | 825 | # by requests. |
paul@755 | 826 | |
paul@755 | 827 | if not p.summary or \ |
paul@755 | 828 | group_type != "request" and self._have_request(p.uid, p.recurrenceid, None, True): |
paul@755 | 829 | |
paul@755 | 830 | page.span(p.summary or "(Participant is busy)") |
paul@446 | 831 | |
paul@755 | 832 | # Link to requests and events (including ones for |
paul@755 | 833 | # which counter-proposals exist). |
paul@755 | 834 | |
paul@777 | 835 | elif group_type == "request" and self._have_request(p.uid, p.recurrenceid, "COUNTER", True): |
paul@777 | 836 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid, |
paul@777 | 837 | {"counter" : self._period_identifier(p)})) |
paul@777 | 838 | |
paul@446 | 839 | else: |
paul@458 | 840 | page.a(p.summary, href=self.link_to(p.uid, p.recurrenceid)) |
paul@446 | 841 | |
paul@446 | 842 | page.td.close() |
paul@446 | 843 | else: |
paul@446 | 844 | empty += 1 |
paul@446 | 845 | |
paul@446 | 846 | # Pad with empty columns. |
paul@446 | 847 | |
paul@446 | 848 | empty = columns - len(active) |
paul@446 | 849 | |
paul@446 | 850 | if empty: |
paul@455 | 851 | self._empty_slot(point, endpoint, empty) |
paul@446 | 852 | |
paul@446 | 853 | page.tr.close() |
paul@446 | 854 | |
paul@446 | 855 | def _day_heading(self, day): |
paul@446 | 856 | |
paul@446 | 857 | """ |
paul@446 | 858 | Generate a heading for 'day' of the following form: |
paul@446 | 859 | |
paul@768 | 860 | <label class="day" for="day-20150203">Tuesday, 3 February 2015</label> |
paul@446 | 861 | """ |
paul@446 | 862 | |
paul@446 | 863 | page = self.page |
paul@446 | 864 | value, identifier = self._day_value_and_identifier(day) |
paul@768 | 865 | page.label(self.format_date(day, "full"), class_="day", for_=identifier) |
paul@446 | 866 | |
paul@446 | 867 | def _time_point(self, point, endpoint): |
paul@446 | 868 | |
paul@446 | 869 | """ |
paul@446 | 870 | Generate headings for the 'point' to 'endpoint' period of the following |
paul@446 | 871 | form: |
paul@446 | 872 | |
paul@768 | 873 | <label class="timepoint" for="slot-20150203T090000-20150203T100000">09:00:00 CET</label> |
paul@446 | 874 | <span class="endpoint">10:00:00 CET</span> |
paul@446 | 875 | """ |
paul@446 | 876 | |
paul@446 | 877 | page = self.page |
paul@446 | 878 | tzid = self.get_tzid() |
paul@446 | 879 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@768 | 880 | page.label(self.format_time(point.point, "long"), class_="timepoint", for_=identifier) |
paul@455 | 881 | page.span(self.format_time(endpoint and endpoint.point or get_end_of_day(point.point, tzid), "long"), class_="endpoint") |
paul@446 | 882 | |
paul@446 | 883 | def _slot_selector(self, value, identifier, slots): |
paul@446 | 884 | |
paul@446 | 885 | """ |
paul@446 | 886 | Provide a timeslot control having the given 'value', employing the |
paul@446 | 887 | indicated HTML 'identifier', and using the given 'slots' collection |
paul@446 | 888 | to select any control whose 'value' is in this collection, unless the |
paul@446 | 889 | "reset" request parameter has been asserted. |
paul@446 | 890 | """ |
paul@446 | 891 | |
paul@446 | 892 | reset = self.env.get_args().has_key("reset") |
paul@446 | 893 | page = self.page |
paul@446 | 894 | if not reset and value in slots: |
paul@446 | 895 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector", checked="checked") |
paul@446 | 896 | else: |
paul@446 | 897 | page.input(name="slot", type="checkbox", value=value, id=identifier, class_="newevent selector") |
paul@446 | 898 | |
paul@455 | 899 | def _empty_slot(self, point, endpoint, colspan): |
paul@446 | 900 | |
paul@453 | 901 | """ |
paul@453 | 902 | Show an empty slot cell for the given 'point' and 'endpoint', with the |
paul@455 | 903 | given 'colspan' configuring the cell's appearance. |
paul@453 | 904 | """ |
paul@446 | 905 | |
paul@446 | 906 | page = self.page |
paul@455 | 907 | page.td(class_="empty%s" % (point.indicator == Point.PRINCIPAL and " container" or ""), colspan=colspan) |
paul@455 | 908 | if point.indicator == Point.PRINCIPAL: |
paul@453 | 909 | value, identifier = self._slot_value_and_identifier(point, endpoint) |
paul@453 | 910 | page.label("Select/deselect period", class_="newevent popup", for_=identifier) |
paul@453 | 911 | page.td.close() |
paul@446 | 912 | |
paul@446 | 913 | def _day_value_and_identifier(self, day): |
paul@446 | 914 | |
paul@446 | 915 | "Return a day value and HTML identifier for the given 'day'." |
paul@446 | 916 | |
paul@513 | 917 | value = format_datetime(day) |
paul@446 | 918 | identifier = "day-%s" % value |
paul@446 | 919 | return value, identifier |
paul@446 | 920 | |
paul@446 | 921 | def _slot_value_and_identifier(self, point, endpoint): |
paul@446 | 922 | |
paul@446 | 923 | """ |
paul@446 | 924 | Return a slot value and HTML identifier for the given 'point' and |
paul@446 | 925 | 'endpoint'. |
paul@446 | 926 | """ |
paul@446 | 927 | |
paul@455 | 928 | value = "%s-%s" % (format_datetime(point.point), endpoint and format_datetime(endpoint.point) or "") |
paul@446 | 929 | identifier = "slot-%s" % value |
paul@446 | 930 | return value, identifier |
paul@446 | 931 | |
paul@777 | 932 | def _period_identifier(self, period): |
paul@777 | 933 | return "%s-%s" % (format_datetime(period.get_start()), format_datetime(period.get_end())) |
paul@777 | 934 | |
paul@874 | 935 | def get_date_arg(self, args, name): |
paul@874 | 936 | values = args.get(name) |
paul@874 | 937 | if not values: |
paul@874 | 938 | return None |
paul@874 | 939 | return get_datetime(values[0], {"VALUE-TYPE" : "DATE"}) |
paul@874 | 940 | |
paul@446 | 941 | # vim: tabstop=4 expandtab shiftwidth=4 |