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 # Check any constraints on the request. 57 58 try: 59 corrected = handler.correct_object() 60 61 # Refuse to schedule obviously invalid requests. 62 63 except ValidityError: 64 return None 65 66 # With a valid request, determine whether the event can be scheduled. 67 68 scheduled = schedule_in_freebusy(handler) 69 70 # Where the corrected object can be scheduled, issue a counter 71 # request. 72 73 return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" 74 75 # Registry of scheduling functions. 76 77 scheduling_functions = { 78 "schedule_in_freebusy" : schedule_in_freebusy, 79 "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, 80 } 81 82 # vim: tabstop=4 expandtab shiftwidth=4