1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/imiptools/handlers/scheduling.py Mon Oct 26 19:07:51 2015 +0100
1.3 @@ -0,0 +1,82 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Common scheduling functionality.
1.8 +
1.9 +Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from imiptools.dates import ValidityError
1.26 +
1.27 +def schedule_in_freebusy(handler):
1.28 +
1.29 + """
1.30 + Attempt to schedule the current object of the given 'handler' in the
1.31 + free/busy schedule of a resource, returning an indication of the kind of
1.32 + response to be returned.
1.33 + """
1.34 +
1.35 + # If newer than any old version, discard old details from the
1.36 + # free/busy record and check for suitability.
1.37 +
1.38 + periods = handler.get_periods(handler.obj)
1.39 +
1.40 + freebusy = handler.store.get_freebusy(handler.user)
1.41 + offers = handler.store.get_freebusy_offers(handler.user)
1.42 +
1.43 + # Check the periods against any scheduled events and against
1.44 + # any outstanding offers.
1.45 +
1.46 + scheduled = handler.can_schedule(freebusy, periods)
1.47 + scheduled = scheduled and handler.can_schedule(offers, periods)
1.48 +
1.49 + return scheduled and "ACCEPTED" or "DECLINED"
1.50 +
1.51 +def schedule_corrected_in_freebusy(handler):
1.52 +
1.53 + """
1.54 + Attempt to schedule the current object of the given 'handler', correcting
1.55 + specified datetimes according to the configuration of a resource,
1.56 + returning an indication of the kind of response to be returned.
1.57 + """
1.58 +
1.59 + # Check any constraints on the request.
1.60 +
1.61 + try:
1.62 + corrected = handler.correct_object()
1.63 +
1.64 + # Refuse to schedule obviously invalid requests.
1.65 +
1.66 + except ValidityError:
1.67 + return None
1.68 +
1.69 + # With a valid request, determine whether the event can be scheduled.
1.70 +
1.71 + scheduled = schedule_in_freebusy(handler)
1.72 +
1.73 + # Where the corrected object can be scheduled, issue a counter
1.74 + # request.
1.75 +
1.76 + return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED"
1.77 +
1.78 +# Registry of scheduling functions.
1.79 +
1.80 +scheduling_functions = {
1.81 + "schedule_in_freebusy" : schedule_in_freebusy,
1.82 + "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy,
1.83 + }
1.84 +
1.85 +# vim: tabstop=4 expandtab shiftwidth=4