paul@1039 | 1 | #!/usr/bin/env python |
paul@1039 | 2 | |
paul@1039 | 3 | """ |
paul@1039 | 4 | Quota-related scheduling functionality. |
paul@1039 | 5 | |
paul@1039 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1039 | 7 | |
paul@1039 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1039 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1039 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1039 | 11 | version. |
paul@1039 | 12 | |
paul@1039 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1039 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1039 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1039 | 16 | details. |
paul@1039 | 17 | |
paul@1039 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1039 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1039 | 20 | """ |
paul@1039 | 21 | |
paul@1059 | 22 | from imiptools.dates import get_duration, to_utc_datetime |
paul@1176 | 23 | from imiptools.data import get_uri, uri_dict |
paul@1185 | 24 | from imiptools.handlers.scheduling.common import get_scheduling_conflicts, \ |
paul@1185 | 25 | standard_responses |
paul@1039 | 26 | from imiptools.period import Endless |
paul@1039 | 27 | from datetime import timedelta |
paul@1039 | 28 | |
paul@1039 | 29 | # Quota maintenance. |
paul@1039 | 30 | |
paul@1039 | 31 | def check_quota(handler, args): |
paul@1039 | 32 | |
paul@1039 | 33 | """ |
paul@1039 | 34 | Check the current object of the given 'handler' against the applicable |
paul@1039 | 35 | quota. |
paul@1039 | 36 | """ |
paul@1039 | 37 | |
paul@1153 | 38 | _ = handler.get_translator() |
paul@1153 | 39 | |
paul@1039 | 40 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 41 | |
paul@1039 | 42 | # Obtain the journal entries and check the balance. |
paul@1039 | 43 | |
paul@1039 | 44 | journal = handler.get_journal() |
paul@1039 | 45 | entries = journal.get_entries(quota, group) |
paul@1039 | 46 | limits = journal.get_limits(quota) |
paul@1039 | 47 | |
paul@1039 | 48 | # Obtain a limit for the group or any general limit. |
paul@1039 | 49 | # Decline invitations if no limit has been set. |
paul@1039 | 50 | |
paul@1039 | 51 | limit = limits.get(group) or limits.get("*") |
paul@1039 | 52 | if not limit: |
paul@1153 | 53 | return "DECLINED", _("You have no quota allocation for the recipient.") |
paul@1039 | 54 | |
paul@1185 | 55 | # Decline endless events even for unlimited quotas. |
paul@1185 | 56 | # NOTE: Such events could be supported in a similar way to those supported |
paul@1185 | 57 | # NOTE: for each user. |
paul@1185 | 58 | |
paul@1185 | 59 | total = _get_duration(handler) |
paul@1185 | 60 | |
paul@1185 | 61 | if total == Endless(): |
paul@1185 | 62 | return "DECLINED", _("The event period exceeds your quota allocation for the recipient.") |
paul@1185 | 63 | |
paul@1181 | 64 | # Where the quota is unlimited, accept the invitation. |
paul@1181 | 65 | |
paul@1181 | 66 | if limit == "*": |
paul@1181 | 67 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1181 | 68 | |
paul@1039 | 69 | # Decline events whose durations exceed the balance. |
paul@1039 | 70 | |
paul@1039 | 71 | balance = get_duration(limit) - _get_usage(entries) |
paul@1039 | 72 | |
paul@1039 | 73 | if total > balance: |
paul@1153 | 74 | return "DECLINED", _("The event period exceeds your quota allocation for the recipient.") |
paul@1039 | 75 | else: |
paul@1153 | 76 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1039 | 77 | |
paul@1039 | 78 | def add_to_quota(handler, args): |
paul@1039 | 79 | |
paul@1039 | 80 | """ |
paul@1039 | 81 | Record details of the current object of the given 'handler' in the |
paul@1039 | 82 | applicable quota. |
paul@1039 | 83 | """ |
paul@1039 | 84 | |
paul@1039 | 85 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 86 | |
paul@1039 | 87 | total = _get_duration(handler) |
paul@1048 | 88 | expiry = _get_expiry_time(handler) |
paul@1039 | 89 | |
paul@1185 | 90 | # Ignore indefinitely recurring events. |
paul@1050 | 91 | |
paul@1050 | 92 | if total == Endless() or not expiry: |
paul@1050 | 93 | return |
paul@1050 | 94 | |
paul@1059 | 95 | # Update the journal entries. |
paul@1039 | 96 | |
paul@1039 | 97 | journal = handler.get_journal() |
paul@1071 | 98 | entries = journal.get_entries_for_update(quota, group) |
paul@1169 | 99 | handler.update_freebusy(entries, handler.user, False) |
paul@1059 | 100 | journal.set_entries(quota, group, entries) |
paul@1039 | 101 | |
paul@1039 | 102 | def remove_from_quota(handler, args): |
paul@1039 | 103 | |
paul@1039 | 104 | """ |
paul@1039 | 105 | Remove details of the current object of the given 'handler' from the |
paul@1039 | 106 | applicable quota. |
paul@1039 | 107 | """ |
paul@1039 | 108 | |
paul@1039 | 109 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 110 | |
paul@1039 | 111 | total = _get_duration(handler) |
paul@1185 | 112 | expiry = _get_expiry_time(handler) |
paul@1050 | 113 | |
paul@1185 | 114 | # Ignore indefinitely recurring events. |
paul@1185 | 115 | |
paul@1185 | 116 | if total == Endless() or not expiry: |
paul@1185 | 117 | return |
paul@1050 | 118 | |
paul@1059 | 119 | # Update the journal entries. |
paul@1039 | 120 | |
paul@1039 | 121 | journal = handler.get_journal() |
paul@1071 | 122 | entries = journal.get_entries_for_update(quota, group) |
paul@1059 | 123 | handler.remove_from_freebusy(entries) |
paul@1059 | 124 | journal.set_entries(quota, group, entries) |
paul@1039 | 125 | |
paul@1039 | 126 | def _get_quota_and_group(handler, args): |
paul@1039 | 127 | |
paul@1039 | 128 | """ |
paul@1039 | 129 | Combine information about the current object from the 'handler' with the |
paul@1039 | 130 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 131 | identity or group involved. |
paul@1039 | 132 | """ |
paul@1039 | 133 | |
paul@1039 | 134 | quota = args and args[0] or handler.user |
paul@1039 | 135 | |
paul@1039 | 136 | # Obtain the identity to whom the quota will apply. |
paul@1039 | 137 | |
paul@1039 | 138 | organiser = get_uri(handler.obj.get_value("ORGANIZER")) |
paul@1039 | 139 | |
paul@1039 | 140 | # Obtain any user group to which the quota will apply instead. |
paul@1039 | 141 | |
paul@1039 | 142 | journal = handler.get_journal() |
paul@1039 | 143 | groups = journal.get_groups(quota) |
paul@1039 | 144 | |
paul@1179 | 145 | return quota, groups.get(organiser) or groups.get("*") or organiser |
paul@1039 | 146 | |
paul@1039 | 147 | def _get_duration(handler): |
paul@1039 | 148 | |
paul@1039 | 149 | "Return the duration of the current object provided by the 'handler'." |
paul@1039 | 150 | |
paul@1050 | 151 | # Reject indefinitely recurring events. |
paul@1050 | 152 | |
paul@1050 | 153 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 154 | return Endless() |
paul@1050 | 155 | |
paul@1050 | 156 | # Otherwise, return a sum of the period durations. |
paul@1039 | 157 | |
paul@1039 | 158 | total = timedelta(0) |
paul@1039 | 159 | |
paul@1050 | 160 | for period in handler.get_periods(handler.obj): |
paul@1039 | 161 | duration = period.get_duration() |
paul@1039 | 162 | |
paul@1039 | 163 | # Decline events whose period durations are endless. |
paul@1039 | 164 | |
paul@1039 | 165 | if duration == Endless(): |
paul@1039 | 166 | return duration |
paul@1039 | 167 | else: |
paul@1039 | 168 | total += duration |
paul@1039 | 169 | |
paul@1039 | 170 | return total |
paul@1039 | 171 | |
paul@1048 | 172 | def _get_expiry_time(handler): |
paul@1048 | 173 | |
paul@1048 | 174 | """ |
paul@1048 | 175 | Return the expiry time for quota purposes of the current object provided by |
paul@1048 | 176 | the 'handler'. |
paul@1048 | 177 | """ |
paul@1048 | 178 | |
paul@1050 | 179 | # Reject indefinitely recurring events. |
paul@1048 | 180 | |
paul@1050 | 181 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 182 | return None |
paul@1050 | 183 | |
paul@1050 | 184 | periods = handler.get_periods(handler.obj) |
paul@1048 | 185 | return periods and to_utc_datetime(periods[-1].get_end_point()) or None |
paul@1048 | 186 | |
paul@1039 | 187 | def _get_usage(entries): |
paul@1039 | 188 | |
paul@1039 | 189 | "Return the usage total according to the given 'entries'." |
paul@1039 | 190 | |
paul@1039 | 191 | total = timedelta(0) |
paul@1059 | 192 | for period in entries: |
paul@1059 | 193 | total += period.get_duration() |
paul@1039 | 194 | return total |
paul@1039 | 195 | |
paul@1039 | 196 | # Collective free/busy maintenance. |
paul@1039 | 197 | |
paul@1039 | 198 | def schedule_across_quota(handler, args): |
paul@1039 | 199 | |
paul@1039 | 200 | """ |
paul@1181 | 201 | Check the current object of the given 'handler' against the individual |
paul@1181 | 202 | schedules managed by the quota. The consolidated schedules are not tested, |
paul@1181 | 203 | nor are the quotas themselves. |
paul@1039 | 204 | """ |
paul@1039 | 205 | |
paul@1039 | 206 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 207 | |
paul@1166 | 208 | # Check the event periods against the quota's consolidated record of the |
paul@1166 | 209 | # organiser's reservations. |
paul@1039 | 210 | |
paul@1039 | 211 | periods = handler.get_periods(handler.obj) |
paul@1039 | 212 | freebusy = handler.get_journal().get_freebusy(quota, organiser) |
paul@1039 | 213 | scheduled = handler.can_schedule(freebusy, periods) |
paul@1039 | 214 | |
paul@1185 | 215 | return standard_responses(handler, scheduled and "ACCEPTED" or "DECLINED") |
paul@1039 | 216 | |
paul@1039 | 217 | def add_to_quota_freebusy(handler, args): |
paul@1039 | 218 | |
paul@1039 | 219 | """ |
paul@1039 | 220 | Record details of the current object of the 'handler' in the applicable |
paul@1039 | 221 | free/busy resource. |
paul@1039 | 222 | """ |
paul@1039 | 223 | |
paul@1039 | 224 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 225 | |
paul@1039 | 226 | journal = handler.get_journal() |
paul@1071 | 227 | freebusy = journal.get_freebusy_for_update(quota, organiser) |
paul@1040 | 228 | handler.update_freebusy(freebusy, organiser, True) |
paul@1040 | 229 | journal.set_freebusy(quota, organiser, freebusy) |
paul@1039 | 230 | |
paul@1039 | 231 | def remove_from_quota_freebusy(handler, args): |
paul@1039 | 232 | |
paul@1039 | 233 | """ |
paul@1039 | 234 | Remove details of the current object of the 'handler' from the applicable |
paul@1039 | 235 | free/busy resource. |
paul@1039 | 236 | """ |
paul@1039 | 237 | |
paul@1039 | 238 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 239 | |
paul@1039 | 240 | journal = handler.get_journal() |
paul@1071 | 241 | freebusy = journal.get_freebusy_for_update(quota, organiser) |
paul@1040 | 242 | handler.remove_from_freebusy(freebusy) |
paul@1040 | 243 | journal.set_freebusy(quota, organiser, freebusy) |
paul@1039 | 244 | |
paul@1039 | 245 | def _get_quota_and_identity(handler, args): |
paul@1039 | 246 | |
paul@1039 | 247 | """ |
paul@1039 | 248 | Combine information about the current object from the 'handler' with the |
paul@1039 | 249 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 250 | identity involved. |
paul@1039 | 251 | """ |
paul@1039 | 252 | |
paul@1039 | 253 | quota = args and args[0] or handler.user |
paul@1039 | 254 | |
paul@1039 | 255 | # Obtain the identity for whom the scheduling will apply. |
paul@1039 | 256 | |
paul@1039 | 257 | organiser = get_uri(handler.obj.get_value("ORGANIZER")) |
paul@1039 | 258 | |
paul@1039 | 259 | return quota, organiser |
paul@1039 | 260 | |
paul@1176 | 261 | # Delegation of reservations. |
paul@1176 | 262 | |
paul@1176 | 263 | def schedule_for_delegate(handler, args): |
paul@1176 | 264 | |
paul@1176 | 265 | """ |
paul@1176 | 266 | Check the current object of the given 'handler' against the schedules |
paul@1176 | 267 | managed by the quota, delegating to a specific recipient according to the |
paul@1185 | 268 | given policies. |
paul@1176 | 269 | """ |
paul@1176 | 270 | |
paul@1181 | 271 | # First check the quota and decline any request that would exceed the quota. |
paul@1181 | 272 | |
paul@1181 | 273 | scheduled = check_quota(handler, args) |
paul@1181 | 274 | response, description = scheduled or ("DECLINED", None) |
paul@1181 | 275 | |
paul@1181 | 276 | if response == "DECLINED": |
paul@1181 | 277 | return response, description |
paul@1181 | 278 | |
paul@1181 | 279 | # Obtain the quota and organiser group details to evaluate delegation. |
paul@1181 | 280 | |
paul@1176 | 281 | quota, group = _get_quota_and_group(handler, args) |
paul@1185 | 282 | policies = args and args[1:] or ["available"] |
paul@1176 | 283 | |
paul@1176 | 284 | # Determine the status of the recipient. |
paul@1176 | 285 | |
paul@1176 | 286 | attendee_map = uri_dict(handler.obj.get_value_map("ATTENDEE")) |
paul@1176 | 287 | attendee_attr = attendee_map[handler.user] |
paul@1176 | 288 | |
paul@1176 | 289 | # Prevent delegation by a delegate. |
paul@1176 | 290 | |
paul@1176 | 291 | if attendee_attr.get("DELEGATED-FROM"): |
paul@1176 | 292 | delegates = set([handler.user]) |
paul@1176 | 293 | |
paul@1176 | 294 | # Obtain the delegate pool for the quota. |
paul@1176 | 295 | |
paul@1176 | 296 | else: |
paul@1176 | 297 | delegates = handler.get_journal().get_delegates(quota) |
paul@1176 | 298 | |
paul@1176 | 299 | # Obtain the remaining delegates not already involved in the event. |
paul@1176 | 300 | |
paul@1176 | 301 | delegates = set(delegates).difference(attendee_map) |
paul@1176 | 302 | delegates.add(handler.user) |
paul@1176 | 303 | |
paul@1176 | 304 | # Get the quota's schedule for the requested periods and identify |
paul@1176 | 305 | # unavailable delegates. |
paul@1176 | 306 | |
paul@1176 | 307 | entries = handler.get_journal().get_entries(quota, group) |
paul@1185 | 308 | conflicts = get_scheduling_conflicts(handler, entries, delegates, attendee=True) |
paul@1176 | 309 | |
paul@1185 | 310 | # Get the delegates in order of increasing unavailability (or decreasing |
paul@1185 | 311 | # availability). |
paul@1176 | 312 | |
paul@1185 | 313 | unavailability = conflicts.items() |
paul@1185 | 314 | |
paul@1185 | 315 | # Apply the policies to choose a suitable delegate. |
paul@1176 | 316 | |
paul@1185 | 317 | if "most-available" in policies: |
paul@1185 | 318 | unavailability.sort(key=lambda t: t[1]) |
paul@1185 | 319 | available = [delegate for (delegate, commitments) in unavailability] |
paul@1185 | 320 | delegate = available and available[0] |
paul@1176 | 321 | |
paul@1185 | 322 | # The default is to select completely available delegates. |
paul@1176 | 323 | |
paul@1185 | 324 | else: |
paul@1185 | 325 | available = [delegate for (delegate, commitments) in unavailability if not commitments] |
paul@1185 | 326 | delegate = available and (handler.user in available and handler.user or available[0]) |
paul@1176 | 327 | |
paul@1185 | 328 | # Only accept or delegate if a suitably available delegate is found. |
paul@1176 | 329 | |
paul@1185 | 330 | if delegate: |
paul@1176 | 331 | |
paul@1176 | 332 | # Add attendee for delegate, obtaining the original attendee dictionary. |
paul@1176 | 333 | # Modify this user's status to refer to the delegate. |
paul@1176 | 334 | |
paul@1176 | 335 | if delegate != handler.user: |
paul@1176 | 336 | attendee_map = handler.obj.get_value_map("ATTENDEE") |
paul@1176 | 337 | attendee_map[delegate] = {"DELEGATED-FROM" : [handler.user]} |
paul@1176 | 338 | attendee_attr["DELEGATED-TO"] = [delegate] |
paul@1176 | 339 | handler.obj["ATTENDEE"] = attendee_map.items() |
paul@1176 | 340 | |
paul@1185 | 341 | response = "DELEGATED" |
paul@1176 | 342 | else: |
paul@1185 | 343 | response = "ACCEPTED" |
paul@1176 | 344 | else: |
paul@1185 | 345 | response = "DECLINED" |
paul@1185 | 346 | |
paul@1185 | 347 | return standard_responses(handler, response) |
paul@1176 | 348 | |
paul@1040 | 349 | # Locking and unlocking. |
paul@1040 | 350 | |
paul@1040 | 351 | def lock_journal(handler, args): |
paul@1040 | 352 | |
paul@1040 | 353 | "Using the 'handler' and 'args', lock the journal for the quota." |
paul@1040 | 354 | |
paul@1040 | 355 | handler.get_journal().acquire_lock(_get_quota(handler, args)) |
paul@1040 | 356 | |
paul@1040 | 357 | def unlock_journal(handler, args): |
paul@1040 | 358 | |
paul@1040 | 359 | "Using the 'handler' and 'args', unlock the journal for the quota." |
paul@1040 | 360 | |
paul@1040 | 361 | handler.get_journal().release_lock(_get_quota(handler, args)) |
paul@1040 | 362 | |
paul@1040 | 363 | def _get_quota(handler, args): |
paul@1040 | 364 | |
paul@1040 | 365 | "Return the quota using the 'handler' and 'args'." |
paul@1040 | 366 | |
paul@1040 | 367 | return args and args[0] or handler.user |
paul@1040 | 368 | |
paul@1039 | 369 | # Registry of scheduling functions. |
paul@1039 | 370 | |
paul@1039 | 371 | scheduling_functions = { |
paul@1039 | 372 | "check_quota" : check_quota, |
paul@1039 | 373 | "schedule_across_quota" : schedule_across_quota, |
paul@1176 | 374 | "schedule_for_delegate" : schedule_for_delegate, |
paul@1039 | 375 | } |
paul@1039 | 376 | |
paul@1040 | 377 | # Registries of locking and unlocking functions. |
paul@1040 | 378 | |
paul@1040 | 379 | locking_functions = { |
paul@1040 | 380 | "check_quota" : lock_journal, |
paul@1040 | 381 | "schedule_across_quota" : lock_journal, |
paul@1176 | 382 | "schedule_for_delegate" : lock_journal, |
paul@1040 | 383 | } |
paul@1040 | 384 | |
paul@1040 | 385 | unlocking_functions = { |
paul@1040 | 386 | "check_quota" : unlock_journal, |
paul@1040 | 387 | "schedule_across_quota" : unlock_journal, |
paul@1176 | 388 | "schedule_for_delegate" : unlock_journal, |
paul@1040 | 389 | } |
paul@1040 | 390 | |
paul@1039 | 391 | # Registries of listener functions. |
paul@1039 | 392 | |
paul@1039 | 393 | confirmation_functions = { |
paul@1058 | 394 | "check_quota" : add_to_quota, |
paul@1058 | 395 | "schedule_across_quota" : add_to_quota_freebusy, |
paul@1179 | 396 | "schedule_for_delegate" : add_to_quota, |
paul@1039 | 397 | } |
paul@1039 | 398 | |
paul@1039 | 399 | retraction_functions = { |
paul@1058 | 400 | "check_quota" : remove_from_quota, |
paul@1058 | 401 | "schedule_across_quota" : remove_from_quota_freebusy, |
paul@1179 | 402 | "schedule_for_delegate" : remove_from_quota, |
paul@1039 | 403 | } |
paul@1039 | 404 | |
paul@1039 | 405 | # vim: tabstop=4 expandtab shiftwidth=4 |