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