# HG changeset patch # User Paul Boddie # Date 1445882871 -3600 # Node ID 4f736b142589a6793121a03d20dde000c2f71dcd # Parent 82234f952d833fe0e2a7957ea098ca534ad66d31 Moved scheduling logic into a separate module, adding support for explicit scheduling functions. diff -r 82234f952d83 -r 4f736b142589 docs/preferences.txt --- a/docs/preferences.txt Mon Oct 26 17:19:39 2015 +0100 +++ b/docs/preferences.txt Mon Oct 26 19:07:51 2015 +0100 @@ -195,3 +195,27 @@ represented in UTC may have apparently inappropriate hour (and for some zones) minute values that correspond to permitted values in this participant's own time zone. + +scheduling_function +------------------- + +Default: schedule_in_freebusy +Alternatives: (see below) + +Indicates the scheduling function used by resources to find an appropriate +period for an event. The imiptools.handlers.scheduling module contains the +built-in scheduling functions which include the following: + + schedule_in_freebusy accept an invitation if the event periods + are free according to the free/busy + records for the resource; decline + otherwise + + schedule_corrected_in_freebusy correct periods in an event according to + the permitted_times setting (see above), + then attempt to schedule the event + according to the free/busy records for the + resource + +The scheduling mechanism can be extended by implementing additional scheduling +functions or by extending the handler framework directly. diff -r 82234f952d83 -r 4f736b142589 imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Mon Oct 26 17:19:39 2015 +0100 +++ b/imiptools/handlers/resource.py Mon Oct 26 19:07:51 2015 +0100 @@ -20,9 +20,9 @@ """ from imiptools.data import get_address, to_part, uri_dict -from imiptools.dates import ValidityError from imiptools.handlers import Handler from imiptools.handlers.common import CommonFreebusy, CommonEvent +from imiptools.handlers.scheduling import scheduling_functions class ResourceHandler(CommonEvent, Handler): @@ -162,36 +162,15 @@ invalid requests. """ - # Check any constraints on the request. - - try: - corrected = self.correct_object() - - # Refuse to schedule obviously invalid requests. + scheduling_function = self.get_preferences().get("scheduling_function", "schedule_in_freebusy") + fn = scheduling_functions.get(scheduling_function) - except ValidityError: - return None - - # With a valid request, determine whether the event can be scheduled. - - # If newer than any old version, discard old details from the - # free/busy record and check for suitability. + # NOTE: Should signal an error for incorrectly configured resources. - periods = self.get_periods(self.obj) - - freebusy = self.store.get_freebusy(self.user) - offers = self.store.get_freebusy_offers(self.user) - - # Check the periods against any scheduled events and against - # any outstanding offers. - - scheduled = self.can_schedule(freebusy, periods) - scheduled = scheduled and self.can_schedule(offers, periods) - - # Where the corrected object can be scheduled, issue a counter - # request. - - return scheduled and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" + if not fn: + return "DECLINED" + else: + return fn(self) class Event(ResourceHandler): diff -r 82234f952d83 -r 4f736b142589 imiptools/handlers/scheduling.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imiptools/handlers/scheduling.py Mon Oct 26 19:07:51 2015 +0100 @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +""" +Common scheduling functionality. + +Copyright (C) 2015 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from imiptools.dates import ValidityError + +def schedule_in_freebusy(handler): + + """ + Attempt to schedule the current object of the given 'handler' in the + free/busy schedule of a resource, returning an indication of the kind of + response to be returned. + """ + + # If newer than any old version, discard old details from the + # free/busy record and check for suitability. + + periods = handler.get_periods(handler.obj) + + freebusy = handler.store.get_freebusy(handler.user) + offers = handler.store.get_freebusy_offers(handler.user) + + # Check the periods against any scheduled events and against + # any outstanding offers. + + scheduled = handler.can_schedule(freebusy, periods) + scheduled = scheduled and handler.can_schedule(offers, periods) + + return scheduled and "ACCEPTED" or "DECLINED" + +def schedule_corrected_in_freebusy(handler): + + """ + Attempt to schedule the current object of the given 'handler', correcting + specified datetimes according to the configuration of a resource, + returning an indication of the kind of response to be returned. + """ + + # Check any constraints on the request. + + try: + corrected = handler.correct_object() + + # Refuse to schedule obviously invalid requests. + + except ValidityError: + return None + + # With a valid request, determine whether the event can be scheduled. + + scheduled = schedule_in_freebusy(handler) + + # Where the corrected object can be scheduled, issue a counter + # request. + + return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" + +# Registry of scheduling functions. + +scheduling_functions = { + "schedule_in_freebusy" : schedule_in_freebusy, + "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, + } + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 82234f952d83 -r 4f736b142589 tests/test_resource_invitation_constraints.sh --- a/tests/test_resource_invitation_constraints.sh Mon Oct 26 17:19:39 2015 +0100 +++ b/tests/test_resource_invitation_constraints.sh Mon Oct 26 19:07:51 2015 +0100 @@ -34,6 +34,7 @@ mkdir -p "$PREFS/$USER" echo 'Europe/Oslo' > "$PREFS/$USER/TZID" echo 'share' > "$PREFS/$USER/freebusy_sharing" +echo 'schedule_corrected_in_freebusy' > "$PREFS/$USER/scheduling_function" echo '10,12,14,16,18:0,15,30,45' > "$PREFS/$USER/permitted_times" echo 'PT60S' > "$PREFS/$USER/freebusy_offers" diff -r 82234f952d83 -r 4f736b142589 tests/test_resource_invitation_constraints_alternative.sh --- a/tests/test_resource_invitation_constraints_alternative.sh Mon Oct 26 17:19:39 2015 +0100 +++ b/tests/test_resource_invitation_constraints_alternative.sh Mon Oct 26 19:07:51 2015 +0100 @@ -34,6 +34,7 @@ mkdir -p "$PREFS/$USER" echo 'Europe/Oslo' > "$PREFS/$USER/TZID" echo 'share' > "$PREFS/$USER/freebusy_sharing" +echo 'schedule_corrected_in_freebusy' > "$PREFS/$USER/scheduling_function" echo '10,12,14,16,18:0,15,30,45' > "$PREFS/$USER/permitted_times" echo 'PT60S' > "$PREFS/$USER/freebusy_offers"