paul@1039 | 1 | #!/usr/bin/env python |
paul@1039 | 2 | |
paul@1039 | 3 | """ |
paul@1039 | 4 | Quota-related scheduling functionality. |
paul@1039 | 5 | |
paul@1232 | 6 | Copyright (C) 2016, 2017 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@1185 | 23 | from imiptools.handlers.scheduling.common import get_scheduling_conflicts, \ |
paul@1185 | 24 | standard_responses |
paul@1039 | 25 | from imiptools.period import Endless |
paul@1039 | 26 | from datetime import timedelta |
paul@1039 | 27 | |
paul@1039 | 28 | # Quota maintenance. |
paul@1039 | 29 | |
paul@1039 | 30 | def check_quota(handler, args): |
paul@1039 | 31 | |
paul@1039 | 32 | """ |
paul@1039 | 33 | Check the current object of the given 'handler' against the applicable |
paul@1039 | 34 | quota. |
paul@1039 | 35 | """ |
paul@1039 | 36 | |
paul@1153 | 37 | _ = handler.get_translator() |
paul@1153 | 38 | |
paul@1039 | 39 | # Obtain the journal entries and check the balance. |
paul@1039 | 40 | |
paul@1039 | 41 | journal = handler.get_journal() |
paul@1338 | 42 | if not journal: |
paul@1338 | 43 | return "DECLINED", _("You have no quota allocation for the recipient.") |
paul@1338 | 44 | |
paul@1338 | 45 | quota, group = _get_quota_and_group(handler, args) |
paul@1338 | 46 | |
paul@1039 | 47 | entries = journal.get_entries(quota, group) |
paul@1039 | 48 | limits = journal.get_limits(quota) |
paul@1039 | 49 | |
paul@1039 | 50 | # Obtain a limit for the group or any general limit. |
paul@1039 | 51 | # Decline invitations if no limit has been set. |
paul@1039 | 52 | |
paul@1039 | 53 | limit = limits.get(group) or limits.get("*") |
paul@1039 | 54 | if not limit: |
paul@1153 | 55 | return "DECLINED", _("You have no quota allocation for the recipient.") |
paul@1039 | 56 | |
paul@1196 | 57 | # Where the quota is unlimited, accept the invitation. |
paul@1196 | 58 | |
paul@1196 | 59 | if limit == "*": |
paul@1196 | 60 | return "ACCEPTED", _("The recipient has scheduled the requested period.") |
paul@1196 | 61 | |
paul@1196 | 62 | # Decline endless events for limited quotas. |
paul@1185 | 63 | |
paul@1185 | 64 | total = _get_duration(handler) |
paul@1185 | 65 | |
paul@1185 | 66 | if total == Endless(): |
paul@1185 | 67 | return "DECLINED", _("The event period exceeds your quota allocation for the recipient.") |
paul@1185 | 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@1338 | 85 | if not handler.get_journal(): |
paul@1338 | 86 | return |
paul@1338 | 87 | |
paul@1039 | 88 | quota, group = _get_quota_and_group(handler, args) |
paul@1199 | 89 | _add_to_quota(handler, quota, group, handler.user, False) |
paul@1196 | 90 | |
paul@1039 | 91 | def remove_from_quota(handler, args): |
paul@1039 | 92 | |
paul@1039 | 93 | """ |
paul@1039 | 94 | Remove details of the current object of the given 'handler' from the |
paul@1039 | 95 | applicable quota. |
paul@1039 | 96 | """ |
paul@1039 | 97 | |
paul@1338 | 98 | if not handler.get_journal(): |
paul@1338 | 99 | return |
paul@1338 | 100 | |
paul@1039 | 101 | quota, group = _get_quota_and_group(handler, args) |
paul@1199 | 102 | _remove_from_quota(handler, quota, group, handler.user) |
paul@1196 | 103 | |
paul@1199 | 104 | def update_event(handler, args): |
paul@1196 | 105 | |
paul@1196 | 106 | "Update a stored version of the current object of the given 'handler'." |
paul@1196 | 107 | |
paul@1338 | 108 | journal = handler.get_journal() |
paul@1338 | 109 | if not journal: |
paul@1338 | 110 | return |
paul@1338 | 111 | |
paul@1196 | 112 | quota, group = _get_quota_and_group(handler, args) |
paul@1196 | 113 | |
paul@1196 | 114 | # Where an existing version of the object exists, merge the recipient's |
paul@1196 | 115 | # attendance information. |
paul@1196 | 116 | |
paul@1232 | 117 | obj = journal.get_event(quota, handler.uid, handler.recurrenceid) |
paul@1196 | 118 | if not obj: |
paul@1196 | 119 | obj = handler.obj |
paul@1196 | 120 | |
paul@1196 | 121 | # Set attendance. |
paul@1196 | 122 | |
paul@1336 | 123 | attendee_map = obj.get_uri_map("ATTENDEE") |
paul@1196 | 124 | attendee_map[handler.user]["PARTSTAT"] = "ACCEPTED" |
paul@1196 | 125 | obj["ATTENDEE"] = attendee_map.items() |
paul@1196 | 126 | |
paul@1196 | 127 | # Record the object so that recurrences can be generated. |
paul@1196 | 128 | |
paul@1196 | 129 | journal.set_event(quota, handler.uid, handler.recurrenceid, obj.to_node()) |
paul@1196 | 130 | |
paul@1199 | 131 | def remove_event(handler, args): |
paul@1196 | 132 | |
paul@1196 | 133 | "Remove a stored version of the current object of the given 'handler'." |
paul@1196 | 134 | |
paul@1338 | 135 | journal = handler.get_journal() |
paul@1338 | 136 | if not journal: |
paul@1338 | 137 | return |
paul@1338 | 138 | |
paul@1196 | 139 | quota, group = _get_quota_and_group(handler, args) |
paul@1196 | 140 | |
paul@1196 | 141 | # Where an existing version of the object exists, remove the recipient's |
paul@1196 | 142 | # attendance information. |
paul@1196 | 143 | |
paul@1232 | 144 | obj = journal.get_event(quota, handler.uid, handler.recurrenceid) |
paul@1196 | 145 | if not obj: |
paul@1196 | 146 | return |
paul@1196 | 147 | |
paul@1336 | 148 | attendee_map = obj.get_uri_map("ATTENDEE") |
paul@1196 | 149 | delegates = journal.get_delegates(quota) |
paul@1196 | 150 | |
paul@1196 | 151 | # Determine whether any of the delegates are still involved. |
paul@1196 | 152 | |
paul@1196 | 153 | attendees = set(delegates).intersection(attendee_map.keys()) |
paul@1196 | 154 | if handler.user in attendees: |
paul@1196 | 155 | attendees.remove(handler.user) |
paul@1196 | 156 | |
paul@1196 | 157 | # Remove event details where no delegates will be involved. |
paul@1196 | 158 | |
paul@1196 | 159 | if not attendees: |
paul@1196 | 160 | journal.remove_event(quota, handler.uid, handler.recurrenceid) |
paul@1196 | 161 | return |
paul@1196 | 162 | |
paul@1196 | 163 | del attendee_map[handler.user] |
paul@1196 | 164 | obj["ATTENDEE"] = attendee_map.items() |
paul@1196 | 165 | |
paul@1196 | 166 | # Record the object so that recurrences can be generated. |
paul@1196 | 167 | |
paul@1196 | 168 | journal.set_event(quota, handler.uid, handler.recurrenceid, obj.to_node()) |
paul@1196 | 169 | |
paul@1039 | 170 | def _get_quota_and_group(handler, args): |
paul@1039 | 171 | |
paul@1039 | 172 | """ |
paul@1039 | 173 | Combine information about the current object from the 'handler' with the |
paul@1039 | 174 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 175 | identity or group involved. |
paul@1039 | 176 | """ |
paul@1039 | 177 | |
paul@1338 | 178 | journal = handler.get_journal() |
paul@1338 | 179 | assert journal |
paul@1338 | 180 | |
paul@1039 | 181 | quota = args and args[0] or handler.user |
paul@1039 | 182 | |
paul@1039 | 183 | # Obtain the identity to whom the quota will apply. |
paul@1039 | 184 | |
paul@1336 | 185 | organiser = handler.obj.get_uri("ORGANIZER") |
paul@1039 | 186 | |
paul@1039 | 187 | # Obtain any user group to which the quota will apply instead. |
paul@1039 | 188 | |
paul@1039 | 189 | groups = journal.get_groups(quota) |
paul@1039 | 190 | |
paul@1179 | 191 | return quota, groups.get(organiser) or groups.get("*") or organiser |
paul@1039 | 192 | |
paul@1039 | 193 | def _get_duration(handler): |
paul@1039 | 194 | |
paul@1039 | 195 | "Return the duration of the current object provided by the 'handler'." |
paul@1039 | 196 | |
paul@1050 | 197 | # Reject indefinitely recurring events. |
paul@1050 | 198 | |
paul@1050 | 199 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 200 | return Endless() |
paul@1050 | 201 | |
paul@1050 | 202 | # Otherwise, return a sum of the period durations. |
paul@1039 | 203 | |
paul@1039 | 204 | total = timedelta(0) |
paul@1039 | 205 | |
paul@1238 | 206 | for period in handler.get_periods(handler.obj, future_only=True): |
paul@1039 | 207 | duration = period.get_duration() |
paul@1039 | 208 | |
paul@1039 | 209 | # Decline events whose period durations are endless. |
paul@1039 | 210 | |
paul@1039 | 211 | if duration == Endless(): |
paul@1039 | 212 | return duration |
paul@1039 | 213 | else: |
paul@1039 | 214 | total += duration |
paul@1039 | 215 | |
paul@1039 | 216 | return total |
paul@1039 | 217 | |
paul@1048 | 218 | def _get_expiry_time(handler): |
paul@1048 | 219 | |
paul@1048 | 220 | """ |
paul@1048 | 221 | Return the expiry time for quota purposes of the current object provided by |
paul@1048 | 222 | the 'handler'. |
paul@1048 | 223 | """ |
paul@1048 | 224 | |
paul@1050 | 225 | # Reject indefinitely recurring events. |
paul@1048 | 226 | |
paul@1050 | 227 | if handler.obj.possibly_recurring_indefinitely(): |
paul@1050 | 228 | return None |
paul@1050 | 229 | |
paul@1238 | 230 | periods = handler.get_periods(handler.obj, future_only=True) |
paul@1048 | 231 | return periods and to_utc_datetime(periods[-1].get_end_point()) or None |
paul@1048 | 232 | |
paul@1039 | 233 | def _get_usage(entries): |
paul@1039 | 234 | |
paul@1039 | 235 | "Return the usage total according to the given 'entries'." |
paul@1039 | 236 | |
paul@1039 | 237 | total = timedelta(0) |
paul@1059 | 238 | for period in entries: |
paul@1059 | 239 | total += period.get_duration() |
paul@1039 | 240 | return total |
paul@1039 | 241 | |
paul@1199 | 242 | def _add_to_quota(handler, quota, user, participant, is_organiser): |
paul@1199 | 243 | |
paul@1199 | 244 | """ |
paul@1199 | 245 | Record details of the current object of the 'handler' in the applicable |
paul@1199 | 246 | free/busy resource. |
paul@1199 | 247 | """ |
paul@1199 | 248 | |
paul@1199 | 249 | journal = handler.get_journal() |
paul@1338 | 250 | assert journal |
paul@1338 | 251 | |
paul@1199 | 252 | freebusy = journal.get_entries_for_update(quota, user) |
paul@1199 | 253 | handler.update_freebusy(freebusy, participant, is_organiser) |
paul@1199 | 254 | |
paul@1199 | 255 | # Remove original recurrence details replaced by additional |
paul@1199 | 256 | # recurrences, as well as obsolete additional recurrences. |
paul@1199 | 257 | |
paul@1199 | 258 | handler.remove_freebusy_for_recurrences(freebusy, journal.get_recurrences(quota, handler.uid)) |
paul@1199 | 259 | |
paul@1199 | 260 | # Update free/busy provider information if the event may recur indefinitely. |
paul@1199 | 261 | |
paul@1199 | 262 | if handler.possibly_recurring_indefinitely(): |
paul@1199 | 263 | journal.append_freebusy_provider(quota, handler.obj) |
paul@1199 | 264 | |
paul@1199 | 265 | journal.set_entries(quota, user, freebusy) |
paul@1199 | 266 | |
paul@1199 | 267 | def _remove_from_quota(handler, quota, user, participant): |
paul@1199 | 268 | |
paul@1199 | 269 | """ |
paul@1199 | 270 | Remove details of the current object of the 'handler' from the applicable |
paul@1199 | 271 | free/busy resource. |
paul@1199 | 272 | """ |
paul@1199 | 273 | |
paul@1199 | 274 | journal = handler.get_journal() |
paul@1338 | 275 | assert journal |
paul@1338 | 276 | |
paul@1199 | 277 | freebusy = journal.get_entries_for_update(quota, user) |
paul@1199 | 278 | |
paul@1199 | 279 | # Remove only the entries associated with this recipient. |
paul@1199 | 280 | |
paul@1231 | 281 | handler.remove_from_freebusy(freebusy, participant) |
paul@1199 | 282 | |
paul@1199 | 283 | # Update free/busy provider information if the event may recur indefinitely. |
paul@1199 | 284 | |
paul@1199 | 285 | if handler.possibly_recurring_indefinitely(): |
paul@1199 | 286 | journal.remove_freebusy_provider(quota, handler.obj) |
paul@1199 | 287 | |
paul@1199 | 288 | journal.set_entries(quota, user, freebusy) |
paul@1199 | 289 | |
paul@1039 | 290 | # Collective free/busy maintenance. |
paul@1039 | 291 | |
paul@1039 | 292 | def schedule_across_quota(handler, args): |
paul@1039 | 293 | |
paul@1039 | 294 | """ |
paul@1181 | 295 | Check the current object of the given 'handler' against the individual |
paul@1181 | 296 | schedules managed by the quota. The consolidated schedules are not tested, |
paul@1181 | 297 | nor are the quotas themselves. |
paul@1039 | 298 | """ |
paul@1039 | 299 | |
paul@1338 | 300 | if not handler.get_journal(): |
paul@1338 | 301 | return standard_responses(handler, "DECLINED") |
paul@1338 | 302 | |
paul@1039 | 303 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1039 | 304 | |
paul@1166 | 305 | # Check the event periods against the quota's consolidated record of the |
paul@1166 | 306 | # organiser's reservations. |
paul@1039 | 307 | |
paul@1238 | 308 | periods = handler.get_periods(handler.obj, future_only=True) |
paul@1192 | 309 | freebusy = handler.get_journal().get_entries(quota, organiser) |
paul@1039 | 310 | scheduled = handler.can_schedule(freebusy, periods) |
paul@1039 | 311 | |
paul@1185 | 312 | return standard_responses(handler, scheduled and "ACCEPTED" or "DECLINED") |
paul@1039 | 313 | |
paul@1039 | 314 | def add_to_quota_freebusy(handler, args): |
paul@1039 | 315 | |
paul@1039 | 316 | """ |
paul@1039 | 317 | Record details of the current object of the 'handler' in the applicable |
paul@1039 | 318 | free/busy resource. |
paul@1039 | 319 | """ |
paul@1039 | 320 | |
paul@1338 | 321 | if not handler.get_journal(): |
paul@1338 | 322 | return |
paul@1338 | 323 | |
paul@1039 | 324 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1199 | 325 | _add_to_quota(handler, quota, organiser, organiser, True) |
paul@1039 | 326 | |
paul@1039 | 327 | def remove_from_quota_freebusy(handler, args): |
paul@1039 | 328 | |
paul@1039 | 329 | """ |
paul@1039 | 330 | Remove details of the current object of the 'handler' from the applicable |
paul@1039 | 331 | free/busy resource. |
paul@1039 | 332 | """ |
paul@1039 | 333 | |
paul@1338 | 334 | if not handler.get_journal(): |
paul@1338 | 335 | return |
paul@1338 | 336 | |
paul@1039 | 337 | quota, organiser = _get_quota_and_identity(handler, args) |
paul@1199 | 338 | _remove_from_quota(handler, quota, organiser, organiser) |
paul@1039 | 339 | |
paul@1039 | 340 | def _get_quota_and_identity(handler, args): |
paul@1039 | 341 | |
paul@1039 | 342 | """ |
paul@1039 | 343 | Combine information about the current object from the 'handler' with the |
paul@1039 | 344 | given 'args' to return a tuple containing the quota group and the user |
paul@1039 | 345 | identity involved. |
paul@1039 | 346 | """ |
paul@1039 | 347 | |
paul@1338 | 348 | assert handler.get_journal() |
paul@1338 | 349 | |
paul@1039 | 350 | quota = args and args[0] or handler.user |
paul@1039 | 351 | |
paul@1039 | 352 | # Obtain the identity for whom the scheduling will apply. |
paul@1039 | 353 | |
paul@1336 | 354 | organiser = handler.obj.get_uri("ORGANIZER") |
paul@1039 | 355 | |
paul@1039 | 356 | return quota, organiser |
paul@1039 | 357 | |
paul@1176 | 358 | # Delegation of reservations. |
paul@1176 | 359 | |
paul@1176 | 360 | def schedule_for_delegate(handler, args): |
paul@1176 | 361 | |
paul@1176 | 362 | """ |
paul@1176 | 363 | Check the current object of the given 'handler' against the schedules |
paul@1176 | 364 | managed by the quota, delegating to a specific recipient according to the |
paul@1185 | 365 | given policies. |
paul@1176 | 366 | """ |
paul@1176 | 367 | |
paul@1181 | 368 | # First check the quota and decline any request that would exceed the quota. |
paul@1181 | 369 | |
paul@1181 | 370 | scheduled = check_quota(handler, args) |
paul@1181 | 371 | response, description = scheduled or ("DECLINED", None) |
paul@1181 | 372 | |
paul@1181 | 373 | if response == "DECLINED": |
paul@1181 | 374 | return response, description |
paul@1181 | 375 | |
paul@1338 | 376 | # Obtain the journal. Any absent journal should have caused a DECLINED |
paul@1338 | 377 | # response, so this is merely to document the dependency. |
paul@1338 | 378 | |
paul@1338 | 379 | journal = handler.get_journal() |
paul@1338 | 380 | assert journal |
paul@1338 | 381 | |
paul@1181 | 382 | # Obtain the quota and organiser group details to evaluate delegation. |
paul@1181 | 383 | |
paul@1176 | 384 | quota, group = _get_quota_and_group(handler, args) |
paul@1185 | 385 | policies = args and args[1:] or ["available"] |
paul@1176 | 386 | |
paul@1176 | 387 | # Determine the status of the recipient. |
paul@1176 | 388 | |
paul@1336 | 389 | attendee_map = handler.obj.get_uri_map("ATTENDEE") |
paul@1176 | 390 | attendee_attr = attendee_map[handler.user] |
paul@1176 | 391 | |
paul@1176 | 392 | # Prevent delegation by a delegate. |
paul@1176 | 393 | |
paul@1176 | 394 | if attendee_attr.get("DELEGATED-FROM"): |
paul@1176 | 395 | delegates = set([handler.user]) |
paul@1176 | 396 | |
paul@1176 | 397 | # Obtain the delegate pool for the quota. |
paul@1176 | 398 | |
paul@1176 | 399 | else: |
paul@1338 | 400 | delegates = journal.get_delegates(quota) |
paul@1176 | 401 | |
paul@1176 | 402 | # Obtain the remaining delegates not already involved in the event. |
paul@1176 | 403 | |
paul@1176 | 404 | delegates = set(delegates).difference(attendee_map) |
paul@1176 | 405 | delegates.add(handler.user) |
paul@1176 | 406 | |
paul@1176 | 407 | # Get the quota's schedule for the requested periods and identify |
paul@1176 | 408 | # unavailable delegates. |
paul@1176 | 409 | |
paul@1338 | 410 | entries = journal.get_entries(quota, group) |
paul@1185 | 411 | conflicts = get_scheduling_conflicts(handler, entries, delegates, attendee=True) |
paul@1176 | 412 | |
paul@1185 | 413 | # Get the delegates in order of increasing unavailability (or decreasing |
paul@1185 | 414 | # availability). |
paul@1176 | 415 | |
paul@1185 | 416 | unavailability = conflicts.items() |
paul@1185 | 417 | |
paul@1185 | 418 | # Apply the policies to choose a suitable delegate. |
paul@1176 | 419 | |
paul@1185 | 420 | if "most-available" in policies: |
paul@1185 | 421 | unavailability.sort(key=lambda t: t[1]) |
paul@1185 | 422 | available = [delegate for (delegate, commitments) in unavailability] |
paul@1185 | 423 | delegate = available and available[0] |
paul@1176 | 424 | |
paul@1185 | 425 | # The default is to select completely available delegates. |
paul@1176 | 426 | |
paul@1185 | 427 | else: |
paul@1185 | 428 | available = [delegate for (delegate, commitments) in unavailability if not commitments] |
paul@1185 | 429 | delegate = available and (handler.user in available and handler.user or available[0]) |
paul@1176 | 430 | |
paul@1185 | 431 | # Only accept or delegate if a suitably available delegate is found. |
paul@1176 | 432 | |
paul@1185 | 433 | if delegate: |
paul@1176 | 434 | |
paul@1176 | 435 | # Add attendee for delegate, obtaining the original attendee dictionary. |
paul@1176 | 436 | # Modify this user's status to refer to the delegate. |
paul@1176 | 437 | |
paul@1176 | 438 | if delegate != handler.user: |
paul@1176 | 439 | attendee_map = handler.obj.get_value_map("ATTENDEE") |
paul@1176 | 440 | attendee_map[delegate] = {"DELEGATED-FROM" : [handler.user]} |
paul@1176 | 441 | attendee_attr["DELEGATED-TO"] = [delegate] |
paul@1176 | 442 | handler.obj["ATTENDEE"] = attendee_map.items() |
paul@1176 | 443 | |
paul@1185 | 444 | response = "DELEGATED" |
paul@1176 | 445 | else: |
paul@1185 | 446 | response = "ACCEPTED" |
paul@1176 | 447 | else: |
paul@1185 | 448 | response = "DECLINED" |
paul@1185 | 449 | |
paul@1185 | 450 | return standard_responses(handler, response) |
paul@1176 | 451 | |
paul@1040 | 452 | # Locking and unlocking. |
paul@1040 | 453 | |
paul@1040 | 454 | def lock_journal(handler, args): |
paul@1040 | 455 | |
paul@1040 | 456 | "Using the 'handler' and 'args', lock the journal for the quota." |
paul@1040 | 457 | |
paul@1338 | 458 | journal = handler.get_journal() |
paul@1338 | 459 | if journal: |
paul@1338 | 460 | journal.acquire_lock(_get_quota(handler, args)) |
paul@1040 | 461 | |
paul@1040 | 462 | def unlock_journal(handler, args): |
paul@1040 | 463 | |
paul@1040 | 464 | "Using the 'handler' and 'args', unlock the journal for the quota." |
paul@1040 | 465 | |
paul@1338 | 466 | journal = handler.get_journal() |
paul@1338 | 467 | if journal: |
paul@1338 | 468 | journal.release_lock(_get_quota(handler, args)) |
paul@1040 | 469 | |
paul@1040 | 470 | def _get_quota(handler, args): |
paul@1040 | 471 | |
paul@1040 | 472 | "Return the quota using the 'handler' and 'args'." |
paul@1040 | 473 | |
paul@1040 | 474 | return args and args[0] or handler.user |
paul@1040 | 475 | |
paul@1039 | 476 | # Registry of scheduling functions. |
paul@1039 | 477 | |
paul@1039 | 478 | scheduling_functions = { |
paul@1199 | 479 | "check_quota" : [check_quota], |
paul@1199 | 480 | "schedule_across_quota" : [schedule_across_quota], |
paul@1199 | 481 | "schedule_for_delegate" : [schedule_for_delegate], |
paul@1039 | 482 | } |
paul@1039 | 483 | |
paul@1040 | 484 | # Registries of locking and unlocking functions. |
paul@1040 | 485 | |
paul@1040 | 486 | locking_functions = { |
paul@1199 | 487 | "check_quota" : [lock_journal], |
paul@1199 | 488 | "schedule_across_quota" : [lock_journal], |
paul@1199 | 489 | "schedule_for_delegate" : [lock_journal], |
paul@1040 | 490 | } |
paul@1040 | 491 | |
paul@1040 | 492 | unlocking_functions = { |
paul@1199 | 493 | "check_quota" : [unlock_journal], |
paul@1199 | 494 | "schedule_across_quota" : [unlock_journal], |
paul@1199 | 495 | "schedule_for_delegate" : [unlock_journal], |
paul@1040 | 496 | } |
paul@1040 | 497 | |
paul@1039 | 498 | # Registries of listener functions. |
paul@1039 | 499 | |
paul@1039 | 500 | confirmation_functions = { |
paul@1199 | 501 | "check_quota" : [add_to_quota, update_event], |
paul@1199 | 502 | "schedule_across_quota" : [add_to_quota_freebusy, update_event], |
paul@1199 | 503 | "schedule_for_delegate" : [add_to_quota, update_event], |
paul@1039 | 504 | } |
paul@1039 | 505 | |
paul@1039 | 506 | retraction_functions = { |
paul@1199 | 507 | "check_quota" : [remove_from_quota, remove_event], |
paul@1199 | 508 | "schedule_across_quota" : [remove_from_quota_freebusy, remove_event], |
paul@1199 | 509 | "schedule_for_delegate" : [remove_from_quota, remove_event], |
paul@1039 | 510 | } |
paul@1039 | 511 | |
paul@1039 | 512 | # vim: tabstop=4 expandtab shiftwidth=4 |