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