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@1039 | 24 | from imiptools.period import Endless |
paul@1039 | 25 | from datetime import timedelta |
paul@1039 | 26 | |
paul@1039 | 27 | # Quota maintenance. |
paul@1039 | 28 | |
paul@1039 | 29 | def check_quota(handler, args): |
paul@1039 | 30 | |
paul@1039 | 31 | """ |
paul@1039 | 32 | Check the current object of the given 'handler' against the applicable |
paul@1039 | 33 | quota. |
paul@1039 | 34 | """ |
paul@1039 | 35 | |
paul@1153 | 36 | _ = handler.get_translator() |
paul@1153 | 37 | |
paul@1039 | 38 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 39 | |
paul@1039 | 40 | # Obtain the journal entries and check the balance. |
paul@1039 | 41 | |
paul@1039 | 42 | journal = handler.get_journal() |
paul@1039 | 43 | entries = journal.get_entries(quota, group) |
paul@1039 | 44 | limits = journal.get_limits(quota) |
paul@1039 | 45 | |
paul@1039 | 46 | # Obtain a limit for the group or any general limit. |
paul@1039 | 47 | # Decline invitations if no limit has been set. |
paul@1039 | 48 | |
paul@1039 | 49 | limit = limits.get(group) or limits.get("*") |
paul@1039 | 50 | if not limit: |
paul@1153 | 51 | return "DECLINED", _("You have no quota allocation for the recipient.") |
paul@1039 | 52 | |
paul@1181 | 53 | # Where the quota is unlimited, accept the invitation. |
paul@1181 | 54 | |
paul@1181 | 55 | if limit == "*": |
paul@1181 | 56 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1181 | 57 | |
paul@1039 | 58 | # Decline events whose durations exceed the balance. |
paul@1039 | 59 | |
paul@1039 | 60 | total = _get_duration(handler) |
paul@1039 | 61 | |
paul@1039 | 62 | if total == Endless(): |
paul@1153 | 63 | return "DECLINED", _("The event period exceeds your quota allocation for the recipient.") |
paul@1039 | 64 | |
paul@1039 | 65 | balance = get_duration(limit) - _get_usage(entries) |
paul@1039 | 66 | |
paul@1039 | 67 | if total > balance: |
paul@1153 | 68 | return "DECLINED", _("The event period exceeds your quota allocation for the recipient.") |
paul@1039 | 69 | else: |
paul@1153 | 70 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1039 | 71 | |
paul@1039 | 72 | def add_to_quota(handler, args): |
paul@1039 | 73 | |
paul@1039 | 74 | """ |
paul@1039 | 75 | Record details of the current object of the given 'handler' in the |
paul@1039 | 76 | applicable quota. |
paul@1039 | 77 | """ |
paul@1039 | 78 | |
paul@1039 | 79 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 80 | |
paul@1039 | 81 | total = _get_duration(handler) |
paul@1048 | 82 | expiry = _get_expiry_time(handler) |
paul@1039 | 83 | |
paul@1050 | 84 | # Reject indefinitely recurring events. |
paul@1050 | 85 | |
paul@1050 | 86 | if total == Endless() or not expiry: |
paul@1050 | 87 | return |
paul@1050 | 88 | |
paul@1059 | 89 | # Update the journal entries. |
paul@1039 | 90 | |
paul@1039 | 91 | journal = handler.get_journal() |
paul@1071 | 92 | entries = journal.get_entries_for_update(quota, group) |
paul@1169 | 93 | handler.update_freebusy(entries, handler.user, False) |
paul@1059 | 94 | journal.set_entries(quota, group, entries) |
paul@1039 | 95 | |
paul@1039 | 96 | def remove_from_quota(handler, args): |
paul@1039 | 97 | |
paul@1039 | 98 | """ |
paul@1039 | 99 | Remove details of the current object of the given 'handler' from the |
paul@1039 | 100 | applicable quota. |
paul@1039 | 101 | """ |
paul@1039 | 102 | |
paul@1039 | 103 | quota, group = _get_quota_and_group(handler, args) |
paul@1039 | 104 | |
paul@1039 | 105 | total = _get_duration(handler) |
paul@1039 | 106 | |
paul@1052 | 107 | # Allow indefinitely recurring events. |
paul@1050 | 108 | |
paul@1050 | 109 | if total == Endless(): |
paul@1052 | 110 | total = None |
paul@1050 | 111 | |
paul@1059 | 112 | # Update the journal entries. |
paul@1039 | 113 | |
paul@1039 | 114 | journal = handler.get_journal() |
paul@1071 | 115 | entries = journal.get_entries_for_update(quota, group) |
paul@1059 | 116 | handler.remove_from_freebusy(entries) |
paul@1059 | 117 | journal.set_entries(quota, group, entries) |
paul@1039 | 118 | |
paul@1039 | 119 | def _get_quota_and_group(handler, args): |
paul@1039 | 120 | |
paul@1039 | 121 | """ |
paul@1039 | 122 | Combine information about the current object from the 'handler' with the |
paul@1039 | 123 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 124 | identity or group involved. |
paul@1039 | 125 | """ |
paul@1039 | 126 | |
paul@1039 | 127 | quota = args and args[0] or handler.user |
paul@1039 | 128 | |
paul@1039 | 129 | # Obtain the identity to whom the quota will apply. |
paul@1039 | 130 | |
paul@1039 | 131 | organiser = get_uri(handler.obj.get_value("ORGANIZER")) |
paul@1039 | 132 | |
paul@1039 | 133 | # Obtain any user group to which the quota will apply instead. |
paul@1039 | 134 | |
paul@1039 | 135 | journal = handler.get_journal() |
paul@1039 | 136 | groups = journal.get_groups(quota) |
paul@1039 | 137 | |
paul@1179 | 138 | return quota, groups.get(organiser) or groups.get("*") or organiser |
paul@1039 | 139 | |
paul@1039 | 140 | def _get_duration(handler): |
paul@1039 | 141 | |
paul@1039 | 142 | "Return the duration of the current object provided by the 'handler'." |
paul@1039 | 143 | |
paul@1050 | 144 | # Reject indefinitely recurring events. |
paul@1050 | 145 | |
paul@1050 | 146 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 147 | return Endless() |
paul@1050 | 148 | |
paul@1050 | 149 | # Otherwise, return a sum of the period durations. |
paul@1039 | 150 | |
paul@1039 | 151 | total = timedelta(0) |
paul@1039 | 152 | |
paul@1050 | 153 | for period in handler.get_periods(handler.obj): |
paul@1039 | 154 | duration = period.get_duration() |
paul@1039 | 155 | |
paul@1039 | 156 | # Decline events whose period durations are endless. |
paul@1039 | 157 | |
paul@1039 | 158 | if duration == Endless(): |
paul@1039 | 159 | return duration |
paul@1039 | 160 | else: |
paul@1039 | 161 | total += duration |
paul@1039 | 162 | |
paul@1039 | 163 | return total |
paul@1039 | 164 | |
paul@1048 | 165 | def _get_expiry_time(handler): |
paul@1048 | 166 | |
paul@1048 | 167 | """ |
paul@1048 | 168 | Return the expiry time for quota purposes of the current object provided by |
paul@1048 | 169 | the 'handler'. |
paul@1048 | 170 | """ |
paul@1048 | 171 | |
paul@1050 | 172 | # Reject indefinitely recurring events. |
paul@1048 | 173 | |
paul@1050 | 174 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 175 | return None |
paul@1050 | 176 | |
paul@1050 | 177 | periods = handler.get_periods(handler.obj) |
paul@1048 | 178 | return periods and to_utc_datetime(periods[-1].get_end_point()) or None |
paul@1048 | 179 | |
paul@1039 | 180 | def _get_usage(entries): |
paul@1039 | 181 | |
paul@1039 | 182 | "Return the usage total according to the given 'entries'." |
paul@1039 | 183 | |
paul@1039 | 184 | total = timedelta(0) |
paul@1059 | 185 | for period in entries: |
paul@1059 | 186 | total += period.get_duration() |
paul@1039 | 187 | return total |
paul@1039 | 188 | |
paul@1039 | 189 | # Collective free/busy maintenance. |
paul@1039 | 190 | |
paul@1039 | 191 | def schedule_across_quota(handler, args): |
paul@1039 | 192 | |
paul@1039 | 193 | """ |
paul@1181 | 194 | Check the current object of the given 'handler' against the individual |
paul@1181 | 195 | schedules managed by the quota. The consolidated schedules are not tested, |
paul@1181 | 196 | nor are the quotas themselves. |
paul@1039 | 197 | """ |
paul@1039 | 198 | |
paul@1153 | 199 | _ = handler.get_translator() |
paul@1153 | 200 | |
paul@1039 | 201 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 202 | |
paul@1166 | 203 | # Check the event periods against the quota's consolidated record of the |
paul@1166 | 204 | # organiser's reservations. |
paul@1039 | 205 | |
paul@1039 | 206 | periods = handler.get_periods(handler.obj) |
paul@1039 | 207 | freebusy = handler.get_journal().get_freebusy(quota, organiser) |
paul@1039 | 208 | scheduled = handler.can_schedule(freebusy, periods) |
paul@1039 | 209 | |
paul@1153 | 210 | if scheduled: |
paul@1153 | 211 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1153 | 212 | else: |
paul@1153 | 213 | return "DECLINED", _("The requested period cannot be scheduled.") |
paul@1039 | 214 | |
paul@1039 | 215 | def add_to_quota_freebusy(handler, args): |
paul@1039 | 216 | |
paul@1039 | 217 | """ |
paul@1039 | 218 | Record details of the current object of the 'handler' in the applicable |
paul@1039 | 219 | free/busy resource. |
paul@1039 | 220 | """ |
paul@1039 | 221 | |
paul@1039 | 222 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 223 | |
paul@1039 | 224 | journal = handler.get_journal() |
paul@1071 | 225 | freebusy = journal.get_freebusy_for_update(quota, organiser) |
paul@1040 | 226 | handler.update_freebusy(freebusy, organiser, True) |
paul@1040 | 227 | journal.set_freebusy(quota, organiser, freebusy) |
paul@1039 | 228 | |
paul@1039 | 229 | def remove_from_quota_freebusy(handler, args): |
paul@1039 | 230 | |
paul@1039 | 231 | """ |
paul@1039 | 232 | Remove details of the current object of the 'handler' from the applicable |
paul@1039 | 233 | free/busy resource. |
paul@1039 | 234 | """ |
paul@1039 | 235 | |
paul@1039 | 236 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 237 | |
paul@1039 | 238 | journal = handler.get_journal() |
paul@1071 | 239 | freebusy = journal.get_freebusy_for_update(quota, organiser) |
paul@1040 | 240 | handler.remove_from_freebusy(freebusy) |
paul@1040 | 241 | journal.set_freebusy(quota, organiser, freebusy) |
paul@1039 | 242 | |
paul@1039 | 243 | def _get_quota_and_identity(handler, args): |
paul@1039 | 244 | |
paul@1039 | 245 | """ |
paul@1039 | 246 | Combine information about the current object from the 'handler' with the |
paul@1039 | 247 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 248 | identity involved. |
paul@1039 | 249 | """ |
paul@1039 | 250 | |
paul@1039 | 251 | quota = args and args[0] or handler.user |
paul@1039 | 252 | |
paul@1039 | 253 | # Obtain the identity for whom the scheduling will apply. |
paul@1039 | 254 | |
paul@1039 | 255 | organiser = get_uri(handler.obj.get_value("ORGANIZER")) |
paul@1039 | 256 | |
paul@1039 | 257 | return quota, organiser |
paul@1039 | 258 | |
paul@1176 | 259 | # Delegation of reservations. |
paul@1176 | 260 | |
paul@1176 | 261 | def schedule_for_delegate(handler, args): |
paul@1176 | 262 | |
paul@1176 | 263 | """ |
paul@1176 | 264 | Check the current object of the given 'handler' against the schedules |
paul@1176 | 265 | managed by the quota, delegating to a specific recipient according to the |
paul@1176 | 266 | given policy. |
paul@1176 | 267 | """ |
paul@1176 | 268 | |
paul@1176 | 269 | _ = handler.get_translator() |
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@1176 | 282 | policy = args and (args[1:] or ["arbitrary"])[0] |
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@1176 | 308 | unavailable = set() |
paul@1176 | 309 | |
paul@1176 | 310 | for period in handler.get_periods(handler.obj): |
paul@1176 | 311 | overlapping = entries.get_overlapping(period) |
paul@1176 | 312 | |
paul@1176 | 313 | # Where scheduling cannot occur, find the busy potential delegates. |
paul@1176 | 314 | |
paul@1176 | 315 | if overlapping: |
paul@1176 | 316 | for p in overlapping: |
paul@1176 | 317 | unavailable.add(p.attendee) |
paul@1176 | 318 | |
paul@1176 | 319 | # Get the remaining, available delegates. |
paul@1176 | 320 | |
paul@1176 | 321 | available = delegates.difference(unavailable) |
paul@1176 | 322 | |
paul@1176 | 323 | # Apply the policy to choose an available delegate. |
paul@1176 | 324 | # NOTE: Currently an arbitrary delegate is chosen if not the recipient. |
paul@1176 | 325 | |
paul@1176 | 326 | if available: |
paul@1176 | 327 | delegate = handler.user in available and handler.user or list(available)[0] |
paul@1176 | 328 | |
paul@1176 | 329 | # Add attendee for delegate, obtaining the original attendee dictionary. |
paul@1176 | 330 | # Modify this user's status to refer to the delegate. |
paul@1176 | 331 | |
paul@1176 | 332 | if delegate != handler.user: |
paul@1176 | 333 | attendee_map = handler.obj.get_value_map("ATTENDEE") |
paul@1176 | 334 | attendee_map[delegate] = {"DELEGATED-FROM" : [handler.user]} |
paul@1176 | 335 | attendee_attr["DELEGATED-TO"] = [delegate] |
paul@1176 | 336 | handler.obj["ATTENDEE"] = attendee_map.items() |
paul@1176 | 337 | |
paul@1176 | 338 | return "DELEGATED", _("The recipient has delegated the requested period.") |
paul@1176 | 339 | else: |
paul@1176 | 340 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1176 | 341 | else: |
paul@1176 | 342 | return "DECLINED", _("The requested period cannot be scheduled.") |
paul@1176 | 343 | |
paul@1040 | 344 | # Locking and unlocking. |
paul@1040 | 345 | |
paul@1040 | 346 | def lock_journal(handler, args): |
paul@1040 | 347 | |
paul@1040 | 348 | "Using the 'handler' and 'args', lock the journal for the quota." |
paul@1040 | 349 | |
paul@1040 | 350 | handler.get_journal().acquire_lock(_get_quota(handler, args)) |
paul@1040 | 351 | |
paul@1040 | 352 | def unlock_journal(handler, args): |
paul@1040 | 353 | |
paul@1040 | 354 | "Using the 'handler' and 'args', unlock the journal for the quota." |
paul@1040 | 355 | |
paul@1040 | 356 | handler.get_journal().release_lock(_get_quota(handler, args)) |
paul@1040 | 357 | |
paul@1040 | 358 | def _get_quota(handler, args): |
paul@1040 | 359 | |
paul@1040 | 360 | "Return the quota using the 'handler' and 'args'." |
paul@1040 | 361 | |
paul@1040 | 362 | return args and args[0] or handler.user |
paul@1040 | 363 | |
paul@1039 | 364 | # Registry of scheduling functions. |
paul@1039 | 365 | |
paul@1039 | 366 | scheduling_functions = { |
paul@1039 | 367 | "check_quota" : check_quota, |
paul@1039 | 368 | "schedule_across_quota" : schedule_across_quota, |
paul@1176 | 369 | "schedule_for_delegate" : schedule_for_delegate, |
paul@1039 | 370 | } |
paul@1039 | 371 | |
paul@1040 | 372 | # Registries of locking and unlocking functions. |
paul@1040 | 373 | |
paul@1040 | 374 | locking_functions = { |
paul@1040 | 375 | "check_quota" : lock_journal, |
paul@1040 | 376 | "schedule_across_quota" : lock_journal, |
paul@1176 | 377 | "schedule_for_delegate" : lock_journal, |
paul@1040 | 378 | } |
paul@1040 | 379 | |
paul@1040 | 380 | unlocking_functions = { |
paul@1040 | 381 | "check_quota" : unlock_journal, |
paul@1040 | 382 | "schedule_across_quota" : unlock_journal, |
paul@1176 | 383 | "schedule_for_delegate" : unlock_journal, |
paul@1040 | 384 | } |
paul@1040 | 385 | |
paul@1039 | 386 | # Registries of listener functions. |
paul@1039 | 387 | |
paul@1039 | 388 | confirmation_functions = { |
paul@1058 | 389 | "check_quota" : add_to_quota, |
paul@1058 | 390 | "schedule_across_quota" : add_to_quota_freebusy, |
paul@1179 | 391 | "schedule_for_delegate" : add_to_quota, |
paul@1039 | 392 | } |
paul@1039 | 393 | |
paul@1039 | 394 | retraction_functions = { |
paul@1058 | 395 | "check_quota" : remove_from_quota, |
paul@1058 | 396 | "schedule_across_quota" : remove_from_quota_freebusy, |
paul@1179 | 397 | "schedule_for_delegate" : remove_from_quota, |
paul@1039 | 398 | } |
paul@1039 | 399 | |
paul@1039 | 400 | # vim: tabstop=4 expandtab shiftwidth=4 |