paul@938 | 1 | #!/usr/bin/env python |
paul@938 | 2 | |
paul@938 | 3 | """ |
paul@938 | 4 | Common scheduling functionality. |
paul@938 | 5 | |
paul@938 | 6 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@938 | 7 | |
paul@938 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@938 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@938 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@938 | 11 | version. |
paul@938 | 12 | |
paul@938 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@938 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@938 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@938 | 16 | details. |
paul@938 | 17 | |
paul@938 | 18 | You should have received a copy of the GNU General Public License along with |
paul@938 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@938 | 20 | """ |
paul@938 | 21 | |
paul@938 | 22 | from imiptools.dates import ValidityError |
paul@938 | 23 | |
paul@938 | 24 | def schedule_in_freebusy(handler): |
paul@938 | 25 | |
paul@938 | 26 | """ |
paul@938 | 27 | Attempt to schedule the current object of the given 'handler' in the |
paul@938 | 28 | free/busy schedule of a resource, returning an indication of the kind of |
paul@938 | 29 | response to be returned. |
paul@938 | 30 | """ |
paul@938 | 31 | |
paul@938 | 32 | # If newer than any old version, discard old details from the |
paul@938 | 33 | # free/busy record and check for suitability. |
paul@938 | 34 | |
paul@938 | 35 | periods = handler.get_periods(handler.obj) |
paul@938 | 36 | |
paul@938 | 37 | freebusy = handler.store.get_freebusy(handler.user) |
paul@938 | 38 | offers = handler.store.get_freebusy_offers(handler.user) |
paul@938 | 39 | |
paul@938 | 40 | # Check the periods against any scheduled events and against |
paul@938 | 41 | # any outstanding offers. |
paul@938 | 42 | |
paul@938 | 43 | scheduled = handler.can_schedule(freebusy, periods) |
paul@938 | 44 | scheduled = scheduled and handler.can_schedule(offers, periods) |
paul@938 | 45 | |
paul@938 | 46 | return scheduled and "ACCEPTED" or "DECLINED" |
paul@938 | 47 | |
paul@938 | 48 | def schedule_corrected_in_freebusy(handler): |
paul@938 | 49 | |
paul@938 | 50 | """ |
paul@938 | 51 | Attempt to schedule the current object of the given 'handler', correcting |
paul@938 | 52 | specified datetimes according to the configuration of a resource, |
paul@938 | 53 | returning an indication of the kind of response to be returned. |
paul@938 | 54 | """ |
paul@938 | 55 | |
paul@938 | 56 | # Check any constraints on the request. |
paul@938 | 57 | |
paul@938 | 58 | try: |
paul@938 | 59 | corrected = handler.correct_object() |
paul@938 | 60 | |
paul@938 | 61 | # Refuse to schedule obviously invalid requests. |
paul@938 | 62 | |
paul@938 | 63 | except ValidityError: |
paul@938 | 64 | return None |
paul@938 | 65 | |
paul@938 | 66 | # With a valid request, determine whether the event can be scheduled. |
paul@938 | 67 | |
paul@938 | 68 | scheduled = schedule_in_freebusy(handler) |
paul@938 | 69 | |
paul@938 | 70 | # Where the corrected object can be scheduled, issue a counter |
paul@938 | 71 | # request. |
paul@938 | 72 | |
paul@938 | 73 | return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@938 | 74 | |
paul@938 | 75 | # Registry of scheduling functions. |
paul@938 | 76 | |
paul@938 | 77 | scheduling_functions = { |
paul@938 | 78 | "schedule_in_freebusy" : schedule_in_freebusy, |
paul@938 | 79 | "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, |
paul@938 | 80 | } |
paul@938 | 81 | |
paul@938 | 82 | # vim: tabstop=4 expandtab shiftwidth=4 |