1.1 --- a/imiptools/handlers/scheduling/quota.py Thu May 12 23:05:48 2016 +0200
1.2 +++ b/imiptools/handlers/scheduling/quota.py Thu May 12 23:15:18 2016 +0200
1.3 @@ -20,7 +20,7 @@
1.4 """
1.5
1.6 from imiptools.dates import get_duration, to_utc_datetime
1.7 -from imiptools.data import get_uri
1.8 +from imiptools.data import get_uri, uri_dict
1.9 from imiptools.period import Endless
1.10 from datetime import timedelta
1.11
1.12 @@ -250,6 +250,81 @@
1.13
1.14 return quota, organiser
1.15
1.16 +# Delegation of reservations.
1.17 +
1.18 +def schedule_for_delegate(handler, args):
1.19 +
1.20 + """
1.21 + Check the current object of the given 'handler' against the schedules
1.22 + managed by the quota, delegating to a specific recipient according to the
1.23 + given policy.
1.24 + """
1.25 +
1.26 + _ = handler.get_translator()
1.27 +
1.28 + quota, group = _get_quota_and_group(handler, args)
1.29 + policy = args and (args[1:] or ["arbitrary"])[0]
1.30 +
1.31 + # Determine the status of the recipient.
1.32 +
1.33 + attendee_map = uri_dict(handler.obj.get_value_map("ATTENDEE"))
1.34 + attendee_attr = attendee_map[handler.user]
1.35 +
1.36 + # Prevent delegation by a delegate.
1.37 +
1.38 + if attendee_attr.get("DELEGATED-FROM"):
1.39 + delegates = set([handler.user])
1.40 +
1.41 + # Obtain the delegate pool for the quota.
1.42 +
1.43 + else:
1.44 + delegates = handler.get_journal().get_delegates(quota)
1.45 +
1.46 + # Obtain the remaining delegates not already involved in the event.
1.47 +
1.48 + delegates = set(delegates).difference(attendee_map)
1.49 + delegates.add(handler.user)
1.50 +
1.51 + # Get the quota's schedule for the requested periods and identify
1.52 + # unavailable delegates.
1.53 +
1.54 + entries = handler.get_journal().get_entries(quota, group)
1.55 + unavailable = set()
1.56 +
1.57 + for period in handler.get_periods(handler.obj):
1.58 + overlapping = entries.get_overlapping(period)
1.59 +
1.60 + # Where scheduling cannot occur, find the busy potential delegates.
1.61 +
1.62 + if overlapping:
1.63 + for p in overlapping:
1.64 + unavailable.add(p.attendee)
1.65 +
1.66 + # Get the remaining, available delegates.
1.67 +
1.68 + available = delegates.difference(unavailable)
1.69 +
1.70 + # Apply the policy to choose an available delegate.
1.71 + # NOTE: Currently an arbitrary delegate is chosen if not the recipient.
1.72 +
1.73 + if available:
1.74 + delegate = handler.user in available and handler.user or list(available)[0]
1.75 +
1.76 + # Add attendee for delegate, obtaining the original attendee dictionary.
1.77 + # Modify this user's status to refer to the delegate.
1.78 +
1.79 + if delegate != handler.user:
1.80 + attendee_map = handler.obj.get_value_map("ATTENDEE")
1.81 + attendee_map[delegate] = {"DELEGATED-FROM" : [handler.user]}
1.82 + attendee_attr["DELEGATED-TO"] = [delegate]
1.83 + handler.obj["ATTENDEE"] = attendee_map.items()
1.84 +
1.85 + return "DELEGATED", _("The recipient has delegated the requested period.")
1.86 + else:
1.87 + return "ACCEPTED", _("The recipient has scheduled the requested period.")
1.88 + else:
1.89 + return "DECLINED", _("The requested period cannot be scheduled.")
1.90 +
1.91 # Locking and unlocking.
1.92
1.93 def lock_journal(handler, args):
1.94 @@ -275,6 +350,7 @@
1.95 scheduling_functions = {
1.96 "check_quota" : check_quota,
1.97 "schedule_across_quota" : schedule_across_quota,
1.98 + "schedule_for_delegate" : schedule_for_delegate,
1.99 }
1.100
1.101 # Registries of locking and unlocking functions.
1.102 @@ -282,11 +358,13 @@
1.103 locking_functions = {
1.104 "check_quota" : lock_journal,
1.105 "schedule_across_quota" : lock_journal,
1.106 + "schedule_for_delegate" : lock_journal,
1.107 }
1.108
1.109 unlocking_functions = {
1.110 "check_quota" : unlock_journal,
1.111 "schedule_across_quota" : unlock_journal,
1.112 + "schedule_for_delegate" : unlock_journal,
1.113 }
1.114
1.115 # Registries of listener functions.