# HG changeset patch # User Paul Boddie # Date 1454083326 -3600 # Node ID 39efcf72d853346eaa2489768b729265323652b5 # Parent 48b37822d2a06ed7c8a297c0f3d06c3bae2c9e73 Support multiple functions in the scheduling_function preference. diff -r 48b37822d2a0 -r 39efcf72d853 docs/preferences.txt --- a/docs/preferences.txt Fri Jan 29 16:44:32 2016 +0100 +++ b/docs/preferences.txt Fri Jan 29 17:02:06 2016 +0100 @@ -202,9 +202,12 @@ 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: +Indicates the scheduling functions used by resources to find an appropriate +period for an event, with each function to be applied to a scheduling request +appearing on a separate line. + +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; diff -r 48b37822d2a0 -r 39efcf72d853 docs/wiki/FuturePlans --- a/docs/wiki/FuturePlans Fri Jan 29 16:44:32 2016 +0100 +++ b/docs/wiki/FuturePlans Fri Jan 29 17:02:06 2016 +0100 @@ -67,24 +67,6 @@ accounting for resources, although this is beyond the scope of imip-agent itself. -=== Combining Scheduling Functions === - -Extend the resource scheduling support to provide chains of scheduling -functions, as opposed to a single function. Thus, different activities could -be combined. For example, to suggest alternative periods whilst enforcing -booking quotas, there would be two functions indicated in the -`scheduling_function` setting for a resource: - -{{{ -schedule_next_available_in_freebusy -enforce_quota -}}} - -Each function would consider the current state of the event, and the chain -would be terminated if a function decided to decline the participation of -the resource. Otherwise, each function in turn would be able to refine the -event and decide upon the resource's participation. - == Other Ideas == Some more ideas that would be worth pursuing... diff -r 48b37822d2a0 -r 39efcf72d853 docs/wiki/Preferences --- a/docs/wiki/Preferences Fri Jan 29 16:44:32 2016 +0100 +++ b/docs/wiki/Preferences Fri Jan 29 17:02:06 2016 +0100 @@ -229,9 +229,12 @@ 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: +Indicates the scheduling functions used by resources to find an appropriate +period for an event, with each function to be applied to a scheduling request +appearing on a separate line. + +The imiptools.handlers.scheduling module contains the built-in scheduling +functions which include the following: {{{#!table `schedule_in_freebusy` || accept an invitation if the event periods are free diff -r 48b37822d2a0 -r 39efcf72d853 imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Fri Jan 29 16:44:32 2016 +0100 +++ b/imiptools/handlers/resource.py Fri Jan 29 17:02:06 2016 +0100 @@ -3,7 +3,7 @@ """ Handlers for a resource. -Copyright (C) 2014, 2015 Paul Boddie +Copyright (C) 2014, 2015, 2016 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 @@ -22,7 +22,8 @@ from imiptools.data import get_address, to_part, uri_dict from imiptools.handlers import Handler from imiptools.handlers.common import CommonFreebusy, CommonEvent -from imiptools.handlers.scheduling import scheduling_functions +from imiptools.handlers.scheduling import apply_scheduling_functions, \ + scheduling_functions class ResourceHandler(CommonEvent, Handler): @@ -162,15 +163,16 @@ invalid requests. """ - scheduling_function = self.get_preferences().get("scheduling_function", "schedule_in_freebusy") - fn = scheduling_functions.get(scheduling_function) + # Obtain a list of scheduling functions. - # NOTE: Should signal an error for incorrectly configured resources. + functions = self.get_preferences().get("scheduling_function", + "schedule_in_freebusy").split("\n") - if not fn: - return "DECLINED" - else: - return fn(self) + # Obtain the actual scheduling functions. + + functions = map(scheduling_functions.get, functions) + + return apply_scheduling_functions(functions, self) class Event(ResourceHandler): diff -r 48b37822d2a0 -r 39efcf72d853 imiptools/handlers/scheduling.py --- a/imiptools/handlers/scheduling.py Fri Jan 29 16:44:32 2016 +0100 +++ b/imiptools/handlers/scheduling.py Fri Jan 29 17:02:06 2016 +0100 @@ -3,7 +3,7 @@ """ Common scheduling functionality. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 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 @@ -25,6 +25,41 @@ periods_from, remove_event_periods, \ remove_periods +def apply_scheduling_functions(functions, handler): + + """ + Apply the given scheduling 'functions' in the current object of the given + 'handler'. + """ + + response = "ACCEPTED" + + for fn in functions: + + # NOTE: Should signal an error for incorrectly configured resources. + + if not fn: + return "DECLINED" + + # Keep evaluating scheduling functions, stopping only if one + # declines or gives a null response. + + else: + result = fn(handler) + + # Return a negative result immediately. + + if not result or result == "DECLINED": + return result + + # Modify the eventual response from acceptance if a countering + # result is obtained. + + elif response == "ACCEPTED": + response = result + + return response + def schedule_in_freebusy(handler, freebusy=None): """